var isIE = (navigator.appVersion.indexOf("MSIE")==-1);
var isIE6 = (navigator.appVersion.indexOf("MSIE 6")!=-1);

NavLang = Class.create();
NavLang.prototype = {

	initialize: function () {
		this.langNodes = $$("#languages a");
		for (var i=0;i<this.langNodes.length;i++) {
			//Event.observe(this.langNodes[i],"click",this.change.bindAsEventListener(this,this.langNodes[i].parentNode.className));
		}
	},
	
	change: function (e,lang) {
		var newAJAX = new Ajax.Request(
			urlSite+"/includes/php/language.php?language="+lang, {
				method: "GET",
				postBody:$H(lang).toQueryString(),
				onComplete: function(response) {
					document.location = urlSite+"/"+response.responseText;
				}.bind(this)
			}
		);
	}
}

Panel = Class.create();
Panel.prototype = {

	initialize: function () {
		this.open = false;
		this.interval = null;
		this.speed = 20;
		this.target = $("productInfosNew");
		this.target.style.left = "0px";
		Event.observe($("moreInfos"),"click",this.startOpenClose.bind(this));
	},
	
	startOpenClose: function () {
		var to = (this.open) ?-1 :1;
		
		if (this.open) {
			if ($("productInfosMask")) {
				$("productInfosMask").style.zIndex = 6;				
			}
			if ($("productNav")) {
				$$("#productNav ul")[0].style.zIndex = 5;
			}
		}
		
		$('moreInfosArrow').className = (this.open) ?'close' :'open';
		if (this.interval == null) { this.interval = new PeriodicalExecuter(this.move.bind(this,to),10/1000); }
		else {
			this.speed = 20;
			this.interval.stop();
			this.interval.callback = this.move.bind(this,to);
			this.interval.registerCallback();
		}
		this.open =! this.open;
	},
	
	move: function (to) {
		var myLeft = parseInt(this.target.style.left) + this.speed*to;
		this.speed = (this.speed>1) ?this.speed - 1 :1;
		if (to == 1 && parseInt(this.target.style.left) + this.speed*to > 132) { 

			if ($("productInfosMask")) {
				$("productInfosMask").style.zIndex = 5;				
			}
			if ($("productNav")) {
				$$("#productNav ul")[0].style.zIndex = 6;
			}
		
			myLeft = 132; this.interval.stop();
		}
		else if (to == -1 && parseInt(this.target.style.left) + this.speed*to < 0) { 
			myLeft = 0;
			this.interval.stop();
		}
			
		this.target.style.left = myLeft + "px";
	}
	
}


ProductNavOld = Class.create();
ProductNavOld.prototype = {
	
	initialize: function () {
		if ($("productNav")) {
			this.nodes = $("productNav").getElementsByTagName("li");
			for (var i=0;i<this.nodes.length;i++) {
				this.nodes[i].id = i + "productNav";
				Event.observe(this.nodes[i].getElementsByTagName("a")[0],"click",this.setNav.bindAsEventListener(this));
			}
			this.currentId = 1;
			this.starting = true;
		}
	},
	
	setNav: function (e) {
		if ($("openPanel")) {
			if (this.starting&&!myPanelOldProduct.open) {				
				myPanelOldProduct.startOpenClose(null);
			}
		}
	}
	
}

PanelOldProduct = Class.create();
PanelOldProduct.prototype = {

	initialize: function () {
		this.open = false;
		this.interval = null;
		this.speed = 20;
		this.target = $("midTextMove");
		this.target.style.left = "0px";
		Event.observe($("openPanel"),"click",this.startOpenClose.bindAsEventListener(this));
		var myProductNavOld = new ProductNavOld();
	},
	
	startOpenClose: function (e) {
		var to = (this.open) ?-1 :1;
		var panelButton = (e!=null) ?Event.element(e) :$("openPanel").getElementsByTagName("img")[0];
		if (!this.open) { 
			$("scrollTools").style.display = "none"; 
			panelButton.src = panelButton.src.replace("open","close");
		}
		else { panelButton.src = panelButton.src.replace("close","open"); }
		if (this.interval == null) { this.interval = new PeriodicalExecuter(this.move.bind(this,to),10/1000); }
		else {
			this.speed = 20;
			this.interval.stop();
			this.interval.callback = this.move.bind(this,to);
			this.interval.registerCallback();
		}
		this.open =! this.open;
	},
	
	move: function (to) {
		var myLeft = parseInt(this.target.style.left) + this.speed*to;
		this.speed = (this.speed>1) ?this.speed - 1 :1;
		if (to == 1 && parseInt(this.target.style.left) + this.speed*to > 226) { 
			if ($("productNav")) { $("productNav").style.zIndex = 3; }
			myLeft = 226; this.interval.stop();
		}
		else if (to == -1 && parseInt(this.target.style.left) + this.speed*to < 0) { 
			myLeft = 0; this.interval.stop(); 
			$("scrollTools").style.display = "block";
		}
		if ($("productNav")) { if (to == -1 && $("productNav").style.zIndex!=1) { $("productNav").style.zIndex = 1; } }
		this.target.style.left = myLeft + "px";
	}
	
}

ProductNav = Class.create();
ProductNav.prototype = {

	initialize: function () {
		/*$("productNav").innerHTML += '<div id="productNavMask"></div>';
		this.target = $("productNavMask");*/
		this.nodes = $("productNav").getElementsByTagName("li");
		for (var i=0;i<this.nodes.length;i++) {
			this.nodes[i].id = i + "productNav";
			Event.observe(this.nodes[i].getElementsByTagName("a")[0],"click",this.setNav.bindAsEventListener(this));
		}
		this.currentId = 1;
		this.starting = true;
	},
	
	setNav: function (e) {
		var src = $("myImg").src;
		var id = (e==null) ?0 :parseInt(Event.element(e).parentNode.parentNode.id);
		$("myImg").src = src.replace(this.currentId+".jpg",(id+1)+".jpg");
		this.currentId = id+1;
		if ($("moreInfos")) {
			if (!this.starting&&!myPanel.open) { myPanel.startOpenClose(); }
		}
		this.starting = false;
	}

}


MidTopNav = Class.create();
MidTopNav.prototype = {
	
	initialize: function () {
		
		var realWidth = 0;
		this.nodes = $("midTopNav").getElementsByTagName("li");
		var itemOn = "";
		for (var i=0;i<this.nodes.length;i++) { 
			if (this.nodes[i].className=="on") {
				this.nodes[i].id = "midTopNavOn"+i;
				itemOn = this.nodes[i].id;
			}
			realWidth += this.nodes[i].offsetWidth;
			/*
			if (this.nodes[i].className!="on") {
				Event.observe(this.nodes[i],"mouseover",this.roll.bindAsEventListener(this,1));
				Event.observe(this.nodes[i],"mouseout",this.roll.bindAsEventListener(this,-1));
			}
			*/
			this.nodes[i].innerHTML = '<div class="bg"></div>' + this.nodes[i].innerHTML;
		}
		
		var add = (!isIE) ?0 :0;		
		$("midTopNav").style.width = realWidth + add + "px";
		if ($("midTopNavMask")) {
			if ($("midTopNav").offsetWidth<$("midTopNavMask").offsetWidth) {
				$("midTopNavMask").style.width = realWidth + 15 + "px";
			}
		}
		
		var myScroll =  new Scroller ("horizontal",{
			container:$("midTopNav"),
			mask:$("midTopNavMask"),
			scLeft:$("midTopNavScLeft"),
			scRight:$("midTopNavScRight"),
			cursorH:$("midTopNavCursor"),
			scrollbar:$('midTopNavBg'),
			scrollbarBgH:$("midTopNavBg")
			},10,2);
		myScroll.init();	
		if (itemOn) {
			myScroll.moveToAnchor(itemOn);
		}
		
	}
	
	/*
	roll: function (e,type) {
		var o = (Event.element(e).tagName=="LI") ?Event.element(e) :Event.element(e).parentNode.parentNode;
		if (type==1) { Element.addClassName(o,"on"); }
		else if (type==-1) { Element.removeClassName(o,"on"); }
	}
	*/
	
}

EquipmentRoll = Class.create();
EquipmentRoll.prototype = {
	
	initialize: function () {
		this.boxNodes = $$("#mid .boxEquipment2 .boxImg");
		for (var i=0;i<this.boxNodes.length;i++) {
			this.linkNodes = this.boxNodes[i].getElementsByTagName("a");
			for (var j=0;j<this.linkNodes.length;j++) {
				Event.observe(this.linkNodes[j],'mouseover',this.roll.bindAsEventListener(this,(j+2)));
				Event.observe(this.linkNodes[j],'mouseout',this.roll.bindAsEventListener(this,1));
			}
		}
		this.oldId = 1;
	},
	
	roll: function (e,id) {
		var target = Event.element(e).parentNode.parentNode.parentNode.parentNode.getElementsByTagName("img")[0];
		target.src = target.src.replace('-'+this.oldId,'-'+id);
		this.oldId = id;
	}

}
	
function viewVideo () {
	
}

var openPanelStartY = 0;
function onScrollMove (move) {
	$("openPanel").style.top = openPanelStartY + $("midTextMask").scrollTop + "px";
}

function sendToFriend (subject) {
	document.location = 'mailto:?subject='+subject+'&body='+document.title+' : '+window.location;
}

var myPanel;
var myPanelOldProduct;
var myIE6Hack;
var myMidTopNav;

Event.observe(window, "load", function () {  
	
	myIE6Hack = new IE6Hack();
	//myIE6Hack.pngFix(urlSite+'/includes/images/pix.gif');
	
	$('nav').setStyle({
		left:(($('container').offsetWidth/2) - ($('nav').offsetWidth/2)) + 'px',
		visibility:'visible'
	});
	
	var nav1 = new Nav(
            {
                element: 'nav',
                config: {
                     navItemSelector: 'li'
                    ,navSubItemSelector: 'ol'
                    ,setNavItem:true
                    //,domHandle:true
                    ,zindex: 1000
                    ,relative:true
                    ,visibleMode:true
                    ,timeOpen:100
                    ,timeClose:300
                    ,timeCloseLevel:20
                    ,left:true
                    ,top:false
                    ,classname:'l{depth}'
                    ,1: { left:false, top:true }
                    //,2: { left:false, top:true }
                    //,3: { left:false, top:true }
                }
            }
        );
	
	
	var searchForm = new FormCheck();
	searchForm.registerInputText(document.globalForm.mySearch);
	searchForm.registerInputTextSubmitOnKeyEnter(document.globalForm,document.globalForm.mySearch,urlSite+"/"+searchUrl);
	Event.observe($("searchOk"), "click", function (e) { 
		with ( document.globalForm) { action = urlSite+"/"+searchUrl; submit(); }
	});
	
	if ($("languages")) { var myNavLang = new NavLang(); }
	
	if ($("moreInfos")) { myPanel = new Panel(); }
	if ($("productNav")) { var myProductNav = new ProductNav();  }
	if ($("openPanel")) { myPanelOldProduct = new PanelOldProduct(); }

	if ($("midTextMask")) {
		$("midTextBg").style.height = $("midText").offsetHeight + "px";
		openPanelStartY = $("openPanel").offsetTop;
		var myScroll =  new Scroller ("vertical",{
			container:$("midText"),
			mask:$("midTextMask"),
			scUp:$("scUp"),
			scDn:$("scDn"),
			cursorV:$("cursor"),
			scrollbar:$('midTopNavBg'),
			scrollbarBgV:$("midTopNavBg")
		},10,2,onScrollMove);
		myScroll.init();
	}
	
	if ($("presScrollMask")) {
		var myPresScroll =  new Scroller ("vertical",{
			container:$("presScroll"),
			mask:$("presScrollMask"),
			scUp:$("scUp"),
			scDn:$("scDn"),
			cursorV:$("cursor"),
			scrollbar:$('scrollTools'),
			scrollbarBgV:$("scrollBg")
		},10,2);
		myPresScroll.init();
	}
	
	if ($("rightTextMask")) {
		var myRightTextScroll =  new Scroller ("vertical",{
			container:$("rightTextCont"),
			mask:$("rightTextMask"),
			scUp:$("scUpProducts"),
			scDn:$("scDnProducts"),
			cursorV:$("cursorProducts"),
			scrollbar:$('scrollToolsProducts'),
			scrollbarBgV:$("scrollBgProducts")
		},10,2);
		myRightTextScroll.init();
		
	}
	
	if ($("midTopNav")) {
		myMidTopNav = new MidTopNav();
	}
	
	if ($("pdvSelect")) {
		Event.observe($("pdvSelect"),"change",function () { 
			if ($F("pdvSelect")!="") {
				if ($('country')) {
					$('country').value = '';
				}
				document.pdvForm.action = urlSite + "/" + pdv;
				document.pdvForm.submit();
			}
		});
	}
	
	Event.observe($("searchOk"),"click",function () { 
		with (document.globalForm) { action = urlSite + "/" + searchUrl; submit(); } 
	});

});