

var Panier = new Class({
	options : {},
	initialize : function(options) {
	
		this.setOptions(options);
		this.panier = $('panier');
		this.panier.productLines = this.panier.getElements('tr.productLines');
		this.panier.productLines.each(
			function(line) {
				
				this.putRequested(line);
		
			}.bind(this)
		);
		
	},
	putRequested : function(line) {
		
		if(!line) {
			return;
		}
		var commandeInput = line.getElements('.commande input');
		if(commandeInput[0].checked) {
			line.addClass('requested');
		}
		
	},
	onCommand : function(elm) {
	
		var parent = getParent($(elm), {nodeName:"div", className:"radio"});
		if ($(parent).hasClass('active')) {
			return; 
		}
		var cellule = getParent(elm, {nodeName:"td"});
		getParent($(elm), {nodeName:"tr", className:"productLines"}).removeClass('requested');
		var radios = $(cellule).getElements('.radio');
		radios.each(
			function(radio){
			
				if(radio.hasClass('active')) {
					radio.removeClass('active');
					radio.getElement('input').setAttribute('checked','');
					radio.removeClass('checked');
				}
				else {
					radio.addClass('active');
					radio.getElement('input').setAttribute('checked','checked');
					radio.addClass('checked');
				}
				if(radio.hasClass('commande') && radio.hasClass('checked')) {
					getParent(radio, {nodeName:"tr", className:"productLines"}).addClass('requested');
				}
			
			}
		);
		
	},
	removeLine: function(elm) {
	
		var line = getParent(elm, {nodeName:"tr", className:"productLines"});
		line.remove();
		this.noLine();
	},
	noLine: function(lines) {
		this.panier.productLines = this.panier.getElements('tr.productLines');
		if (this.panier.productLines.length == 0){
			this.panier.getElement('.hiddenLine').removeClass('hiddenLine');
		}
		
	}
})

Panier.implement(new Options);

var ChangeVisu = new Class({
	initialize : function() {
	
		this.familles = $('familles');
		this.image = $('mediaFamily');
		 if(!this.familles) {
			return;
		 }
		this.imageUrl;
		this.familles.liste = $('listFamily');
		this.familles.categories = this.familles.liste.getElements('li');
		this.familles.categories.each(
			function(categorie) {
			
				if(this.image && categorie.getElement('input').checked) {
					this.imageUrl = this.image.src = $(categorie).getAttribute('rel');
				}
				categorie.addEvent(
					'mouseenter',
					function() {
						
						this.rollover(categorie);
					
					}.bind(this)
				);
				categorie.addEvent(
					'mouseleave',
					function() {

						this.rollout();
			
					}.bind(this)
				);
				categorie.addEvent(
					'click',
					function() {

						this.checkThis(categorie);
					
					}.bind(this)
				);
				
			}.bind(this)
		);
		
	},
	rollover: function(elm) {
	
		this.image.src = $(elm).getAttribute('rel');
	
	},
	rollout: function(categorie){
	
		this.image.src = this.imageUrl;		
	
	},
	checkThis : function(categorie) {
	
		if(!categorie) {
			return;
		}
		this.imageUrl = $(categorie).getAttribute('rel');
		if(!this.imageUrl) {
			return;
		}
		this.familles.categories.each(
			function(cat) {
			
				cat.getElement('input').setAttribute('checked','');
		
			}
		);
		categorie.getElement('input').setAttribute('checked','checked');
		submit_category_selection();
		
	}
});

/*
@author : Florian Harmel
@params : options - object
			. width - number // Sert à ...
@methods : 
	- doUrl - void //
	- createElms
	- setMaskDimensions
	- 
*/
var PopIn = new Class({
	options : {
		width : 650
	},
	initialize : function(options){
		
		this.setOptions(options);
		this.page = $E('body');
		this.layer = $('layer');
		if(!(this.page && this.layer)) {
			return;
		}
		this.createElms();
		
		
		//gestion Envoyer a un ami
		this.sendTo();
		
		//check url pour ouvrir le layer eventuellement parametre
		this.doUrl();
	},
	doUrl : function(){
		var qString = document.location.search;
		var toUse = qString.substr(1,qString.length);
		var duos = toUse.split('&');
		var number = 0;
		var param = false;
		var sendToAlreadyOpened = false;
		
		duos.each(function(duo){
			var array = duo.split('=');
			if (array[0]=='selectedAxs'){
				param = true;
				identifiant = array[1];
			}
			if (array[0]=='sendToOpen'){
				sendToAlreadyOpened = array[1];
			}
		});
		//ouverture layer identifie dans url
		var identifiant = $(identifiant);
		if (!identifiant) return;
		this.show(identifiant, sendToAlreadyOpened);
		update_form_query_string();
	},
	createElms : function(){
		//on met le layer dans body
		this.page.adopt(this.layer);
		
		this.mask = new Element('div',{'class':'popMask'}).setOpacity(0);
		this.page.adopt(this.mask);
		if(IS_IE){
			this.mask.iframe = new Element('iframe')
			this.mask.iframe.src = 'javascript:void(0)';
			this.mask.adopt(this.mask.iframe);
		}
	},
	setMaskDimensions : function(){
		var coord = {};
		coord.width = window.getScrollWidth();
		coord.height = window.getScrollHeight();
		this.mask.setStyles(coord);
		if (IS_IE){
			this.mask.iframe.setStyles(coord)
		};
	},
	updateLayerPosition : function(options){
		this.posX = window.getScrollLeft()+window.getWidth()/2 - this.options.width/2;
		this.posY = window.getScrollTop()+window.getHeight()/2 - this.layer.getSize().size.y/2;
		this.layer.setStyles({'left':this.posX,'top':this.posY,'width':this.options.width});
	},
	populate : function(ref){
		this.contenuLayer = $(ref).getElement('.detailsContent').clone();
		this.contenuLayer.setStyle('display','block');
		this.contenuLayer.injectBefore(this.switchSentToAFriendLink);
	},
	unPopulate : function(){
		this.contenuLayer.remove();
	},
	show : function(ref, bool) {
		this.populate(ref);
		this.updateLayerPosition();
		this.setMaskDimensions();
		if (bool == 'true') this.switchSendTo();
		this.layer.effect('opacity').start(1);
		this.mask.effect('opacity').start(0.7);
	},
	hide : function() {
		
		this.mask.effect('opacity',{duration:200,
			onStart : function(){
				if (this.switchSendTo.open == true) this.switchSendTo();
			}.bind(this),
			onComplete : function(){
				this.layer.effect('opacity').start(0);
				this.unPopulate();
			}.bind(this)
		}).start(0);
	},
	
	zoom : function(img){
		var _self = this;
		var imgSrc = img.src
		var imgUrl = imgSrc.split('/')[imgSrc.split('/').length - 1];
		var zoomedUrl = _zoomedImagesPrefix + imgUrl;
		var zoomedUrl = imgSrc.replace(new RegExp("\\b"+imgUrl+"\\b","g"), zoomedUrl);
		//creation masque
		//_self.contenuLayer.effect('').start(0);
		//recuperation de l'image
		this.secLayer = new Element('div');
		this.secLayer.className = 'cloneLayerInside';
		this.secLayer.innerHTML = '<a class="closeBox" onclick="return false;" href="#">Fermer</a>';
		this.layer.adopt(_self.secLayer);
		this.secLayer.setStyles({
			'height' : _self.contenuLayer.scrollHeight + 30,
			'width' : _self.contenuLayer.scrollWidth,
			'opacity': 0
		})
		this.secLayer.effect('opacity', {
			duration : 300,
			/*onStart : function(){
				if (IS_IE){
					_self.formElms.each(function(formElm){
						formElm.addClass('invisible');
					})
				}
			},*/
			onComplete : function(){
				new Asset.image(zoomedUrl, {
					onload : function(){
						var image = this;
						//formattage et positionnement image
						image.setOpacity(0);
						image.className += 'zoomedImage';
						
						var heighToUse = window.ie6 ? _self.secLayer.offsetHeight : _self.secLayer.scrollHeight;
						image.setAttribute('height', heighToUse);
						//insertion image + effet;
						_self.secLayer.adopt(this);
						image.style.width = 'auto';
						_self.secLayer.on = true;
						this.effect('opacity').start(1);
					}
				})
			}
		}).start(1)
		this.secLayer.addEvent('click', function(e){
			new Event(e).stop();
			_self.closeZoom();
		});
	},
	
	closeZoom : function(){
		var _self = this;
		this.secLayer.effect('opacity', {
			onComplete : function(){
				_self.secLayer.remove();
				_self.secLayer.on = false;
				/*if (IS_IE){
					_self.formElms.each(function(formElm){
						formElm.removeClass('invisible');
					})
				}*/
			}
		}).start(0)
	},
	sendTo : function(){
		this.switchSentToAFriendLink = this.layer.getElement('.switchSentToAFriendLink');
		this.liens =this.switchSentToAFriendLink.getElements('a');
		this.sendToAFriend = this.layer.getElement('.sendToAFriend');
		this.sendToAFriendContent = this.sendToAFriend.getElement('.sendContent');
		//size it
		this.sendToAFriend.sizeY = this.sendToAFriend.getSize().size.y;
		this.sendToAFriendContent.setStyle('margin-top', -this.sendToAFriend.sizeY);
	},
	switchSendTo : function(){
		if (this.switchSendTo.open == true){
			this.sendToAFriendContent.effect('margin-top', {onComplete : function(){
				this.layer.effect('top').start(this.posY);
				this.liens[1].addClass('hidden');
				this.liens[0].removeClass('hidden');
			}.bind(this)}).start(-this.sendToAFriend.sizeY);
			this.switchSendTo.open = false;
		}else{
			this.layer.effect('top', {onComplete : function(){
				this.sendToAFriendContent.effect('margin-top').start(0);
				this.liens[0].addClass('hidden');
				this.liens[1].removeClass('hidden');
			}.bind(this)}).start(this.posY-this.sendToAFriend.sizeY/2);
			
			//on flag louverture
			this.switchSendTo.open = true;
		}
	}
});
PopIn.implement(new Options)


var Landing = new Class({
	initialize : function(){
		this.oDom  = $('landing');
	},
	zoom : function(img){
		var _self = this;
		var imgSrc = img.src
		var imgUrl = imgSrc.split('/')[imgSrc.split('/').length - 1];
		var zoomedUrl = _zoomedImagesPrefix + imgUrl;
		var zoomedUrl = imgSrc.replace(new RegExp("\\b"+imgUrl+"\\b","g"), zoomedUrl);
		//creation masque
		//_self.contenuLayer.effect('').start(0);
		//recuperation de l'image
		this.secLayer = new Element('div');
		this.secLayer.className = 'cloneLayerInside';
		this.secLayer.innerHTML = '<a class="closeBox" onclick="return false;" href="#">Fermer</a>';
		this.oDom.adopt(_self.secLayer);
		this.secLayer.setStyles({
			'height' : _self.oDom.scrollHeight,
			'width' : _self.oDom.scrollWidth,
			'opacity': 0
		})
		
		this.secLayer.effect('opacity', {
			duration : 300,
			onComplete : function(){
				new Asset.image(zoomedUrl, {
					onload : function(){
						var image = this;
						//formattage et positionnement image
						image.setOpacity(0);
						image.className += 'zoomedImage';
						
						var heighToUse = window.ie6 ? _self.secLayer.offsetHeight : _self.secLayer.scrollHeight;
						image.setAttribute('height', heighToUse);
						//insertion image + effet;
						_self.secLayer.adopt(this);
						image.style.width = 'auto';
						_self.secLayer.on = true;
						this.effect('opacity').start(1);
					}
				})
			}
		}).start(1)
		this.secLayer.addEvent('click', function(e){
			new Event(e).stop();
			_self.closeZoom();
		});
	},
	closeZoom : function(){
		var _self = this;
		this.secLayer.effect('opacity', {
			onComplete : function(){
				_self.secLayer.remove();
				_self.secLayer.on = false;
			}
		}).start(0)
	}
})




var TipsAxs = new Class({
	initialize : function(){
		this.tds = $$('.tipIt');
		this.bloc = $('panier');
		
		this.tds.each(function(td){
			td.addEvent('mouseenter',function(){
				this.show(td);
			}.bind(this))
			td.addEvent('mouseleave',function(){
				this.hide(td);
			}.bind(this))
		}.bind(this))
		
		this.div = new Element('div').injectInside($('page'))
		.addClass('clonedTipBoxLayer')
		.setStyles({
			opacity:0,
			display:'block'
		});
		
		ifrlayer.make(this.div);
		
		this.opac = new Fx.Styles(this.div, {duration: 100, transition: Fx.Transitions.linear});
		this.opac2 = new Fx.Styles(this.div, {duration: 100, transition: Fx.Transitions.linear, onComplete:function(e){
			ifrlayer.hide(e);
		}});
	},
	show : function(td){
		this.div.empty();
		this.doClone(td);
		this.posIt(td);
		this.clone.setStyle('display','block');
		ifrlayer.make(this.div);
		this.opac.start({
		    'opacity': 1
		});
		$clear(this.time);
	},
	hide : function(td){
		//this.div.setStyle('opacity',0);
		//this.delClone();
		this.time = (function(){
			this.opac2.start({
			    'opacity': 0
			});
		}).delay(500,this)
	},
	posIt : function(td){
		if (this.div.getCoordinates().bottom >= document.documentElement.clientHeight){
			this.div.setStyle('top', td.getCoordinates().bottom - this.div.offsetHeight)
		}
		if((td.getCoordinates().right - this.bloc.getLeft()) + this.div.offsetWidth > this.bloc.offsetWidth){
			this.div.setStyle('left', td.getLeft() - $('page').getLeft() - this.div.offsetWidth)
		}else{
			this.div.setStyle('left', td.getCoordinates().right - $('page').getLeft())
		}
	},
	doClone : function(td){
		this.layer = td.getElement('.tipBoxLayer');
		this.clone = this.layer.clone();
		this.clone.injectInside(this.div);
		this.div.setStyle('top', td.getTop());
	},
	delClone :function(){
		this.clone.remove();
	}
	
})

var AjaxJspRefresh = new Class({
    initialize : function(urlJSP, id){
		this.idZone = id;
        this.oRefreshedPlace = $(id);
        this.url = urlJSP;
        this.locate = document.location;
    },
    refresh : function(params){
        var _self = this;
        this.locate = document.location;
        this.oParams = params ? params : this.getParams();
        this.oParams['q'] = getSearchString();
        this.oParams['ps'] = current_page_size;
        this.oParams['model'] = current_model;
        this.oParams['p'] = getPage();
        this.oParams['popId'] = current_axs_id;
        var myXHR = new Ajax(this.url, {
            method: 'get',
            onRequest : function()
            {
                _self.setLoader();
            },
            onSuccess : function(response){
                _self.onSuccess(response);
            },
            onComplete : function(){
                nedstat_trigger(search_action, getCategoryId(), getCleanedSearchString());
                if (_self.oParams['navClick'])
                    goToElement('zoneRecherche');
                _self.unsetLoader();
                popup.doUrl();
            }
        
        }).request(_self.oParams);
        
    },
    onSuccess : function(response){
        var _self = this;
        _self.oRefreshedPlace.setHTML(response);
		generateElements();
    },
    getParams : function(){
        var hash = this.locate.hash;
        hash = hash.split('#')[1];
        return hash;
    },
    setLoader : function(){
        //alert('setloader');
        //$('loaderZone').addClass('loader');
    },
    unsetLoader : function(){
        //$('loaderZone').removeClass('loader');
        //alert('unsetLoader');
    },
    urlInscription : function(oParams){
        var query;
        if (typeof oParams == 'string'){
            
        }else if(typeof oParams == 'object'){
            query =  Object.toQueryString(oParams);
        }
        this.locate.hash = query;
    }
});
window.addEvent('domready', function(){
	zoomer = new Landing()
})
/*
var goToElement = function(sId){
    var scroll = new Fx.Scroll(window, {
        wait: true,
        duration: 1500
    });
    scroll.toElement($(sId))
}
*/
