var $j = jQuery.noConflict();

$j(document).ready(function(){

	setEventNav();

	/***/
	//setEventOnglet();

	$j('.colors .btVariant').bind('click',setVariantImgs);

	/*Diaporama !*/
	setDiaporama();

	/* !*/
	initPopins();
	initLegend();
});

function setDiaporama(){
	$j('#outilsDiapo li a').bind('click',function(){
		if(!$j(this).hasClass('linkPopin')){
			$j('#outilsDiapo li.on').removeClass('on');
			$j(this).parent().addClass('on'); // active l'Item

			var pathImg = $j(this).attr('rel');
			$j('#visuelDiapo img').attr('src',pathImg);
			src=pathImg;
			src_me=src;
			src_la=src.replace('_me','_la');
			//update magiczoom
			$j('#zoom').attr('href',src_la);
			MagicZoom.update(document.getElementById('zoom'), src_la, src_me);
		}
	})
}

function setVariantImgs(){
	var title = $j(this).attr('title');
	var value = $j(this).attr('rel');
	var arr = value.split(",");

	if(arr[0]!="")
	{
		$j('.colorsPdt #colorName').html(title); // Nom de couleur choisi

		var valueResult = value.substring(0,value.length-3); // on supprime les 3 derniers caracteres de la valeur de boutton
		//$j('.pdtTitle .refPdt').html(valueResult); // affecte la valeur result au Référence
		$j('#outilsDiapo li a').each(function(i,obj)
		{
			if(!$j(this).hasClass('linkPopin'))
			{
				var rel = $j(this).attr('rel');
				var path = rel.substr(0,rel.lastIndexOf('/')+1);// on récupère le chemin
				$j(this).attr('rel',path+''+arr[i]);// affect l image au rel du

				var imgMini = $j('img',$j(this)).attr('src');
				var pathMini = imgMini.substr(0,imgMini.lastIndexOf('/')+1);// on récupère le chemin des Mini images

				//$j($j('#outilsDiapo li a')[i]).html('<img src="'+pathMini+''+value+'_'+i+'.jpg" onerror="alert(\"---\");" alt="" />'); // on remplace les Mini images par celles correspondant au variants

				//$j('#outilsDiapo li a img').show();
				if(arr[i])
				{
					$j($j('#outilsDiapo li a img')[i]).attr("src",pathMini+''+arr[i]); // on remplace les Mini images par celles correspondant au variants
					$j($j('#outilsDiapo li a img')[i]).show();
				}
				else
				{
					$j($j('#outilsDiapo li a img')[i]).hide();
				}

			}
		})

		$j('#errordiv').hide();
		$j('#visuelDiapo img').show();
		var firstRel = $j($j('#outilsDiapo li a')[0]).attr('rel');
		$j('#visuelDiapo img').attr('src',firstRel);
		src=firstRel;
		src_me=src;
		src_la=src.replace('_me','_la');
		//update magiczoom
		$j('#zoom').attr('href',src_la);
		MagicZoom.update(document.getElementById('zoom'), src_la, src_me);
	}
	else
	{
		$j('#errordiv').show();
	}

	return false;
}

function errorImgs(objImg){
	$j(objImg).hide();
}

function errorBigImg(obj){
	$j('#visuelDiapo img').hide();
	$j('#outilsDiapo li a img').hide();
	$j('#noBigImg').show();
}

// Click sur les onglets , on affiche le bloc coresspond à l'item
function setEventOnglet(){
	if($j('.detailFiche .itemDetail').length){
		$j('.detailFiche .itemDetail').hide();
		$j($j('.detailFiche .itemDetail')[0]).show();

		$j('.ficheDetPdt .onglets li a').bind('click',function(e){
			$j('.onglets li.active').removeClass('active');
			$j('.detailFiche .itemDetail').hide();

			$j(this).parent().addClass('active');
			var index = $j('.ficheDetPdt .onglets li').index($j(this).parent());
			$j($j('.detailFiche .itemDetail')[index]).show();
			e.preventDefault();
		})
	}
}

function setEventNav(){
	$j('.listItem li ul').hide();
	$j('.listItem > li.selected > ul').show();
	$j('.listItem > li.selected > ul > li.selected > ul').show();
	//$j('.listItem li.selected').removeClass('selected');

	// Roll over / Roll out
	$j('ul.listItem li a.navItem').bind('mouseenter',function(){
		$j(this).addClass('hover');
	})

	$j('ul.listItem li a.navItem').bind('mouseleave',function(){
		if($j(this).hasClass('hover')) $j(this).removeClass('hover');
	})

	//display list collection
	$j('ul.listItem li a.navItem').bind('click',function(){
		if($j(this).hasClass('hover'))$j(this).removeClass('hover');
		if($j(this).parent().hasClass('selected')){
			$j(this).parent().removeClass('selected');
			$j(this).next().hide();
		}else{
			$j('.listItem li ul').hide();
			$j('.listItem li.selected').removeClass('selected');
			$j(this).parent().addClass('selected');
			$j('.listItem li.selected ul').show();
			//on masque les Ã©ventuels sous-menus par dÃ©faut
			$j('.listItem li.selected ul li ul').hide();
		}
	});
	//manage sublevels that have other sublevels
	$j('ul.listItem li ul li a').bind('click',function(){
		if ($j(this).parent("li").children("ul").length > 0) {
			if ($j(this).hasClass('hover'))$j(this).removeClass('hover');
			if ($j(this).parent().hasClass('selected')) {
				$j(this).parent().removeClass('selected');
				$j(this).next().hide();
			}
			else {
				$j('.listItem li ul li ul').hide();
				$j('.listItem li ul li.selected').removeClass('selected');
				$j(this).parent().addClass('selected');
				$j('.listItem li ul li.selected ul').show();
			}
			//deactivate link
			return false;
		}
	});
}

/********************************************************/
// Init des popins
/********************************************************/
function initPopins(){
	var dim = getPageSize();
	$j('.linkPopin').bind('click',function(){
		var _this = this;
		loadPopin($j(_this).attr('rel'))
		$j.ajax({
			type: "GET",
			url: $j(_this).attr('href'),
			dataType: "html",
			success: function(data){
				try{
					showPopin(data,$j(_this).attr('rel'));
				}catch(e){}
			}
		});
		return false;
	});
}

/********************************************************/
// Affichage des popins
/********************************************************/
function showPopin(data,id){
	$j('#popinTemp').html(data);
	$j('#popinTemp').removeAttr('style');
	$j('#popinTemp').show();
	setPopinPosition(id);

	$j('#outilsDiapo li a').each(function(i,obj){
		if($j(this).parent().hasClass('on')){
			var img = $j(this).attr('rel');
			img = img.substr(img.lastIndexOf('/')+1,img.length);
			$j('#img_zoom').attr('src','/images/article_la/'+img);
		}
	})
	//var img = $j($j('#outilsDiapo li a')[0]).attr('rel');
	if($j("#popinOverlay").length){
		$j("#popinOverlay").unbind();
		$j("#popinOverlay").bind('click',function(){
			$j("#popinOverlay").fadeOut();
			$j("#popinTemp").fadeOut();
		});
	}
}

function getHeight(){
	if(window.innerHeight || window.innerWidth)
	return window.innerHeight ;
	return document.documentElement.clientHeight ;
}

function refreshPositionPopin(popin){
	$j(window).bind('resize',function(){
		var dim = getPageSize();
		popin.css({
			left: (dim[2]-popin.width())/2
		})
	});

}

function setPopinPosition(id){

	var dim = getPageSize();
	var scroll = getPageScroll();
	var overlay = $j('#popinOverlay');
	var popin = $j('#'+id);
	resizaOverlay(1);
	popin.css({
		top: scroll[1] + (dim[3] / 10) - 50,
		left: (dim[2]-$j(popin).width())/2,
		opacity: 1,
		display: 'block'
	}).fadeIn();
	refreshPositionPopin(popin);

	popin.find('#MB_close').bind('click', function(){
		overlay.fadeOut();
		popin.fadeOut();
		return false;
	});
}

function resizeOverlayForPopin(){
	$j(window).bind('resize',function(){
		resizaOverlay();
	});

	$j(window).bind('scroll',function(){
		resizaOverlay();
	});
}

function resizaOverlay(cond){
	var overlay = $j('#popinOverlay');
	var scroll = getPageScroll();
	//alert(hi);
	if(cond){
		overlay.css({
			opacity: 0.7,
			display: 'block'
		}).fadeIn('slow',function(){
			var hi = getHeight() + scroll[1];
			overlay.height(hi);
		});
	}else{
		var hi = getHeight() + scroll[1];
		overlay.height(hi);
	}
}

function loadPopin(id){
	if(!$j('#popinOverlay').length)
	$j('body').append('<div id="popinOverlay"></div>');

	if(!$j('#popinTemp').length)
	$j('body').append('<div id="popinTemp"></div>');
	//$j('#popinTemp').html("<div id='"+id+"' >Loading..</div>");
	setPopinPosition("popinTemp");
}

function setWidthForLayers(){
	$j('#onglets > li').each(function(){
		var resultshearch = $j('.resultSearch > div',$j(this))
		nbrItems = $j('ul',resultshearch).length;
		var wiFinale = Math.ceil((nbrItems * 135.3) + ( nbrItems * 14.85));
		resultshearch.width(wiFinale);
	});
}

// Renvoie les dimensions de la page
function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth;
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = xScroll;
	} else {
		pageWidth = windowWidth;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};

function getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
}

/********************************************************/
// Catalogue // Affichage des legende
/********************************************************/
function initLegend(){
	$j("#listCatalogue > li").addClass('hideLegend');

	$j("#listCatalogue li").mouseover(function(){
		$j(this).addClass('activeLegend');
	}).mouseout(function(){
		$j(this).removeClass('activeLegend');
	});
}
