/************************/
/** jquery code 4 CoL   */
/** Stephane Orluc      */
/** 06/11/09            */
/** Version:1.0         */
/************************/

	/** jQuery **/

	/** Variables **/
	var accueilSliderCount = 2;
	var slideAuto = false;
	var headerMenuIconTabFr = ['ui-icon-person','ui-icon-mail-closed','ui-icon-comment','ui-icon-note','ui-icon-cart','ui-icon-locked','ui-icon-close'];
	var headerMenuLinkTabFr = ['/fr/qui','/fr/contacter','/fr/temoignages','/fr/partenaires','/cart','/user','/logout'];
	var headerMenuIconTabEn = ['ui-icon-person','ui-icon-mail-closed','ui-icon-cart','ui-icon-locked','ui-icon-close'];
	var headerMenuLinkTabEn = ['/en/who_are_we','/en/contact','/cart','/user','/logout'];
	var fourSpaces = "&nbsp;&nbsp;&nbsp;&nbsp;";
	var noneImage = "/sites/all/themes/couetteonline/images/none.png";
	var parse_button = /([A-Za-z]*)(\d*)/;
	var sliderTimeout = null;
	
	/** Functions **/
	function autoSlideshowAccueil(){
		slideAuto = true;
		$('#buttonContainerRight').click();
		slideAuto = false;
		if (accueilSliderCount > 0 ){
			accueilSliderCount--;
			sliderTimeout = setTimeout("autoSlideshowAccueil()",10000);
		}
	}

	function slideShowAccueil(index){
		$(".homeBanner.active").removeClass('active');
		$("#banner"+(index)).addClass('active');
		$('#spriteBanner').animate({left: "-"+(((index-1)*620)%1860)+"px"},1250,'easeInOutBack');
	}
	function setHeaderMenuIcons(selector, index, iconTab, linkTab){
		selector.addClass("menu"+index);
		if (!$.browser.msie)
			selector.prepend("<a class='ui-icon "+iconTab[index]+"' style='border:1px' href='"+linkTab[index]+"'>"+fourSpaces+"</a>");
		else
			selector.prepend("<img class='ui-icon "+iconTab[index]+"' style='border:1px' src='"+noneImage+"'></img>");
	}
	/** Main **/	
	sliderTimeout = setTimeout("autoSlideshowAccueil()",10000);		
	$(function(){
		/*$('.catalogNavDetail').mouseenter(function(){alert('coucou2')});*
		$('.catalogNavDetaiList').mouseenter(function(){alert('coucou3')});*
		$('nav#navleft ul li>nav').mouseenter(function(){alert('enter')});
		$('nav#navleft ul li>nav').mouseleave(function(){alert('leave')});*/
		/*$('nav#navleft ul li>nav').hover(function(){alert('enter')},function(){alert('leave')});*/
		/*$('.catalogNavDetaiList').hover(function(){alert('enter')},function(){alert('leave')});*/
		$('#buttonContainerRight').click(
			function(){
				var result = parse_button.exec($('.homeBanner.active').attr('id'));
				var index = (result[2]%3)+1;
				if (!slideAuto){
					accueilSliderCount = 0;
					clearTimeout(sliderTimeout);
				}
				slideShowAccueil(index);
			},
			function(){
		});
		$('#buttonContainerLeft').click(
			function(){ 
				var result = parse_button.exec($('.homeBanner.active').attr('id'));
				var index = ((result[2]+1)%3)+1;
				if (!slideAuto){
					accueilSliderCount = 0;
					clearTimeout(sliderTimeout);
				}
				slideShowAccueil(index);
			},
			function(){
		});
		$('.imgButton').hover(
			function(){$(this).attr( 'src' , $(this).attr('src').replace( /.png/ ,'_over.png' ) ) 
			},
			function(){$(this).attr( 'src' , $(this).attr('src').replace( /_over.png/ ,'.png' ) )  
		});
		if ($.browser.msie){
			$('.tradImg').hide();
			$('.tradButton').hover(function(e){$('.tradImg').show();},function(e){$('.tradImg').hide();})
		}else{
			$('.tradImg').slideUp(3000);
			$('.tradButton').hover(function(e){$('.tradImg').slideDown(250);},function(e){$('.tradImg').fadeOut(1000);})
		}
		$('.secNavInnerMenu').hover(function(){
			$(this).css('border-bottom','3px #FFCD00 solid');},
			function(){
				if($(this).hasClass('secNavActive'))
					$(this).css('border-bottom','3px #FFA200 solid');
				else
					$(this).css('border-bottom','3px transparent solid');}
		);
		$(".first").hover(function(){$(this).css('z-index','100');},function(){$(this).css('z-index','10');});
		$(".second").hover(function(){$(this).css('z-index','100');},function(){$(this).css('z-index','9');});
		$(".third").hover(function(){$(this).css('z-index','100');},function(){$(this).css('z-index','8');});
		$(".four").hover(function(){$(this).css('z-index','100');},function(){$(this).css('z-index','7');});
		$('#cart-pane legend').css('margin-top','30px');
		$('.jqTextHomeButton').each(function(i){
			$(this).click(
				function(e){
					var parse_button = /([A-Za-z]*)(\d*)/;
					var result = parse_button.exec(this.id);
					slideshowAccueil(result[2]);
				}
			)
		});
		$('.jqImgClass').hover(
			function(){$(this).attr( 'src' , $(this).attr('src').replace( /.png/ ,'_over.png' ) ).css('z-index','1000000') 
			},
			function(){$(this).attr( 'src' , $(this).attr('src').replace( /_over.png/ ,'.png' ) ).css('z-index','')  
		});
		$(".jqDivClass").each(function (i) {  if (!$.browser.msie) $(this).fadeIn(2000+i*1000); else  $(this).css("display","block");})
		$("#header-region")
		.find("#block-menu-menu-menufr, #block-menu-menu-menuen, #block-menu-menu-menu")
			.find(".content>.menu>.leaf>a").css("position","relative").css("padding-left","5px").end().end()
		.find("#block-menu-menu-menuen>.content>.menu>.leaf").each(function(i){
			setHeaderMenuIcons($(this),i,headerMenuIconTabEn,headerMenuLinkTabEn);
		}).end()
		.find("#block-menu-menu-menufr, #block-menu-menu-menu").find(".content>.menu>.leaf").each(function(i){
			setHeaderMenuIcons($(this),i,headerMenuIconTabFr,headerMenuLinkTabFr);
		});
		$(".ui-icon").hover(function(){$(this).addClass("ui-state-hover")},function(){$(this).removeClass("ui-state-hover")});
		$(".jqImgAccueil").hide();
		$("#jqSlideAccueil .node h2").css("display","none");
		$(".productImageBloc").each(function(i){
			var imgSelector = $(this).children().filter(".blockIcon");
			var imgSelectorLen = imgSelector.length;
			imgSelector.each(function(j){
				$(this).click(
					function(){
						var imgNum=j+1;
						var imgId=$(this).attr('id');
						var prodId=$(this).parent().attr('id');
						$("#"+prodId+"blockParent").animate({marginLeft: ((j%imgSelectorLen)*(-350))+"px"}, 500);
						$("."+prodId+"blockIconShad").css('z-index','100');
						$("#"+prodId+"block"+imgNum+"IconShad").css('z-index','0');					
					}
				)
			});
		});
		
		//apply this to all the productTab (active/inactive)
		$('.productTab').hover(
			function(e){
				// when over if the over one is currently inactive then apply effects
				// The filter part is evaluated during the runtime
				$(this).filter('.inactive-productTab')
					.css('background','url("/sites/all/themes/couetteonline/images/sprite_catalogue.png") -4590px 0')
					.css('margin-top','-3px')
					.css('height','38px');
			},
			function(e){
				// when over if the over one is currently inactive then apply effects
				// The filter part is evaluated during the runtime
				$(this).filter('.inactive-productTab')
					.css('height','35px')
					.css('margin-top','0px')
					.css('background','url("/sites/all/themes/couetteonline/images/sprite_catalogue.png") -4370px 0');
			})
			.click(
				function(e){
					// retrieves the tab number with a regexp
					// this regesp is composed by two packets one composed only with alphabetic 
					// characteres and the second one is a numeric one representing the position
					var parse_button = /([A-Za-z]*)(\d*)/;
					var result = parse_button.exec($(this).attr("id"));
					var index = result[2];
					// removes the active-productTab and make it inactive
					// modifies the background of the former active tab
					// in order to display the inactive image
					$('.active-productTab')
						.removeClass('active-productTab')
						.addClass('inactive-productTab')
						.css('background','url("/sites/all/themes/couetteonline/images/sprite_catalogue.png") -4370px 0');
					// make the clicked tab active and remove the former inactive class
					// make this tab size and position normal
					// change the background with the active image
					$(this)
						.removeClass('inactive-productTab')
						.addClass('active-productTab')
						.css('height','35px')
						.css('margin-top','0px')
						.css('background','url("/sites/all/themes/couetteonline/images/sprite_catalogue.png") -4150px 0');
					// change the top head of the display box in order to fit with the new tab
					// the head to display is based on the tab number
					$("#productsHead").css('background','url("/sites/all/themes/couetteonline/images/sprite_catalogue.png") -'+((index-1)*830)+'px 0');
					// hide all the products and then display the products corresponding to the clicked tab
					$(".productsPres").hide().filter("#products"+index).fadeIn();
		});
		$(".productsPres").hide().filter("#products1").fadeIn();

		$('.catalogBox').hover(
			function(e){
				$(this).css('z-index','100000').css('margin','3px 5px 5px 3px').css('-moz-box-shadow','2px 2px 8px 1px #555').css('-webkit-box-shadow','2px 2px 8px 1px #555').css("filter","progid:DXImageTransform.Microsoft.Shadow(color='gray',Direction=135, Strength=4)")},
			function(e){
				$(this).css('z-index','10').css('margin','4px').css('-moz-box-shadow','0 0 8px #555').css('-webkit-box-shadow','0 0 8px #555').css("filter","progid:DXImageTransform.Microsoft.Shadow(color='gray', Direction=135, Strength=2)")});
		$('.itemListCatalog').not('.active').hide().addClass('up');
		/*$('.catalogCategory > a').hover(
			function(){
				var selectedList = $(this).parent().find('>.itemListCatalog');
				if (selectedList.hasClass('up')){
					selectedList.slideDown('fast').removeClass('up').addClass('down');
				}
			},
			function(){});*/
});

