var Nav = Class.create();
Nav.prototype = {
	initialize: function() {
		this.tabPart = $$('#navLeft li');
		this.tabIdSelect =  new Array();
		this.tID = '';
		this.tempo = 3000;
		this.auto = false;
		this.tabId = new Array(); 
	    this.subMenuChoisi = null;
		this.initVariables();

		for(var i=0;i<this.tabPart.length;i++){
			this.a = this.tabPart[i].getElementsByTagName('a')[0];
			
			Event.observe(this.a, 'click',this.viewHide.bind(this,this.a));
		}
		//Event.observe($$('#snav a')[0], 'click',this.close.bind(this));
		Event.observe($("snav"), 'mouseover',this.show.bind(this));
		Event.observe($("snav"), 'mouseout',this.hideEvent.bind(this));
		
		//this.set("p3-p120-p121");
		
		this._doc = document.documentElement;
		
	},
	initVariables: function() {
		this.tabPart = $$('#navLeft li');
		this.tabIdSelect =  new Array();
		this.tID = '';
		this.tempo = 3000;
		this.auto = false;
		this.tabId = new Array();
	},

	_setNavPositionTop: function () {
		var maxY = this._doc.scrollTop+this._doc.clientHeight;
		var navY = Position.cumulativeOffset($("snav"))[1]+$("snav").offsetHeight;
		if (navY>maxY) {
			$("snav").setStyle({ top:($("snav").offsetTop-(navY-maxY))+"px" });
		}		
	},
	
	viewHide:function(e){
		clearTimeout(this.tID);	
		
		$$('#navLeft a').each(function(item) {
			item.removeClassName('PI_on');
		});
		
		$("snav").style.top = e.positionedOffset()[1]+13+"px";
		
		if(this.tabIdSelect[0] == e.rel && !this.auto){
			// on affiche ou masque les sous-rayons de niveau 3 à 5
			if($("snav").style.visibility == "visible" ){
				$("snav").style.visibility = "hidden";
			}
			else{
				$("snav").style.visibility = "visible";
			 	e.addClassName('PI_on');
			}
		}
		else{		
			// on a choisi un rayon de niveau 2
			$("snav").style.top = e.positionedOffset()[1]+13+"px";
		
			$$('#navLeft a').each(function(item) {
				item.removeClassName('PI_on');
			});
			this.subMenuChoisi=e;
			e.addClassName('PI_on');
			this.load(e,1,false);
		}
		
		this._setNavPositionTop();
		
		this.tabIdSelect[0] = e.rel;
	},
	
	close:function(e){
		$("snav").style.visibility = "hidden";
		$$('#navLeft a').each(function(item) {
			item.removeClassName('PI_on');
		});
	},
	
	load:function(e,idNav,goToRayon){
		this.updateSnav(idNav);
		if (goToRayon){
		PI_loadLay('loadingLay','loading');
//			var url='/magasin/produitList.jsp';
//			var listeParameters = 'rayonId='+e.rel+'&selListeProduits=rayons&optionPrix=Aucun&optionMarques=Toutes&numPage=1&devDurable=0&filtreRayonId=&selectionId=';
//			window.location.href=url+'?'+listeParameters;
      var chaine = e.rel;
      var tab = chaine.split('&'); 
      var rayonId = tab[0];
      var nomRayon4Url = tab[1];
      window.location.href='/magasin-en-ligne/achat-acheter-'+nomRayon4Url+'-'+rayonId+',rayons,Aucun,Toutes,1,0,.html';

			//var url = '/magasin/componentMagasin/produitListContent.jsp';
			//var target = 'contentMid';
			//var listeParameters = 'rayonId='+e.rel+'&selListeProduits=rayons&optionPrix=Aucun&optionMarques=Toutes&numPage=1&devDurable=0&filtreRayonId=&selectionId=';
			// masquer le menu
			//$("snav").style.visibility="hidden";

			// layer d'attente
			//PI_loadLay('loadingLay','loading');

			//var myAjax = new Ajax.Request(url, {
			//onComplete:this.callBackRayon.bindAsEventListener(this,idNav), parameters: listeParameters});
		}
		else
		{
			var url = '/magasin/componentMagasin/navGaucheSubRayon.jsp';
			var target = 'snav'+idNav;
			var listeParameters = 'rayonId='+e.rel;

			var myAjax = new Ajax.Request(url, {
			onComplete:this.callBack.bindAsEventListener(this,idNav), parameters: listeParameters});
		}
	},


	
	callBack:function(e,idNav){
		$('snav'+idNav).update(e.responseText);
		$('snav'+idNav).style.display = "block";
		PI_rescueFrame($('snav'+idNav))
		
		if(idNav == 3){
			this.setSnav(idNav);
			this.auto = false;
		}
		
		if(idNav == 1){
		$("snav").style.visibility = "visible";
			if(parseInt($("snav").style.top)+$("snav").getHeight() > $("content").getHeight()){
				$("snav").style.top = $("content").getHeight()-$("snav").getHeight()+"px";
			}
		}
		
		this.navSelect = "#snav"+idNav+" a";
		for(var i=0;i<$$(this.navSelect).length;i++){
			Event.observe($$(this.navSelect)[i], 'click',this.loadSnav.bind(this,(idNav+1),$$(this.navSelect)[i]));
		}
		if(this.auto){
			this.setSnav(idNav);
		}
		
		this._setNavPositionTop();
		
	},

		callBackRayon:function(e,idNav){

		// mise à jour du contenu
		$('contentMid').update(e.responseText);

		// mise à jour de l'identifiant du contenu
		if ($('home')!=null) $('home').id='listProduct';

		// drag n drop
		PI_ContentScroll();
		$$('div.PI_boxProduct').each(function(e){
			if(e.id) this["d"+e.id] = new SubsDraggable(e.id, {dragelement:PI_getDragElement});
		});
		$$('div.PI_boxProductList').each(function(e){
			if(e.id){
				this["d"+e.id] = new SubsDraggable(e.id, {dragelement:PI_getDragElement});
			}
		});
		$$('div.PI_boxProductDetail').each(function(e){
			if(e.id) this["d"+e.id] = new SubsDraggable(e.id, {dragelement:PI_getDragElement});
		});
		Event.observe(document, "mousemove", PI_eventMouseMove);	

		// initialisation pour la page liste produit
		AWL_init();

		// raz du dernier rayon cliqué
		this.initVariables();
		
		PI_removeLay();
	},
	
	loadSnav:function(idNav,e){
		this.navSelect = "#snav"+(idNav-1)+" a";
	
		$$(this.navSelect).each(function(item) {
			item.removeClassName('PI_on');
		});

		e.addClassName('PI_on');

		this.tabIdSelect[idNav] = e.rel;
		var goToRayon=false;
		var uri = e.href;
		if (uri.substring(uri.lastIndexOf('#') + 1, uri.length) == 'container_true') goToRayon=true;
		this.load(e,idNav,goToRayon);
	},
	
	updateSnav:function(id){
		for(var i=id;i<=3;i++){
			$("snav"+i).update();
			$('snav'+i).style.display = "none";
		}
	},
	
	set:function(sId){
		this.auto = true;
		
		this.tabId = sId.split("-");

		for(var i=0;i<this.tabPart.length;i++){
			this.a = this.tabPart[i].getElementsByTagName('a')[0];
			if(this.a.rel == this.tabId[0]){
				this.viewHide(this.a,true);
			}
		}
		this.hideEvent(3000);
	},
	
	setSnav:function(idNav){
		if(idNav > this.tabId.length){
			 this.auto = false;
			 return;
		}
		this.navSelect = "#snav"+idNav+" a";

		for(var i=0;i<$$(this.navSelect).length;i++){
			this.a = $$(this.navSelect)[i];

			if(this.a.rel.replace("&","") == this.tabId[idNav]){
				this.loadSnav((idNav+1),this.a);
			}
		}
	},
	
	show: function(o,id){
		$("snav").style.visibility="visible";
		clearTimeout(this.tID);
	},
	
	hideEvent: function(t){
		clearTimeout(this.tID);
		// var myTemp = 3000;
		// if(t) this.tID = setTimeout(this.hide.bind(this),t);
		// else  this.tID = setTimeout(this.hide.bind(this),this.tempo);
		this.tID =  setTimeout(this.hide.bind(this),this.tempo);

	},
	
	hide: function(){
		$("snav").style.visibility="hidden";
		$$('#navLeft a').each(function(item) {
			item.removeClassName('PI_on');
		});
		// marquer le sous menu choisi
		if (this.subMenuChoisi!=null) {
			this.subMenuChoisi.addClassName('PI_on');
		}
	}
}