
/*--------------------------------- INIT FUNCTIONS ---------------------------------*/

jQuery(document).ready(function() {
	
	checkClient();
	BBCart("check");
	BBTool_flash();
	initMenu();
	smoothScroll();
	initBBRadio();
	BBTool_initRTForms();
	BBTool_toolTip();
	initCheckoutPathbar();
	//initDebug();
	
	var win							= jQuery(window),
		BBTool_pageSizes 			= BBTool_getPageSize(),
		BBTool_pageScroll 			= BBTool_getPageScroll();
		BBVars.pageWidth 			= BBTool_pageSizes[0],
		BBVars.pageHeight 			= BBTool_pageSizes[1],
		BBVars.windowWidth 			= BBTool_pageSizes[2],
		BBVars.windowHeight 		= BBTool_pageSizes[3],
		BBVars.pageScrollX 			= BBTool_pageScroll[0],
		BBVars.pageScrollY 			= BBTool_pageScroll[1],
		BBVars.dspAreaWidth			= win.width(),
		BBVars.dspAreaHeight		= win.height(),
		BBVars.dataSendContentID	= (jQuery(".dataSend").length>0) ? jQuery(".dataSend").attr("id") : "";
		
	win.bind('resize', function() {
		BBTool_pageSizes		= BBTool_getPageSize(),
		BBVars.pageWidth		= BBTool_pageSizes[0],
		BBVars.pageHeight		= BBTool_pageSizes[1],
		BBVars.windowWidth		= BBTool_pageSizes[2],
		BBVars.windowHeight		= BBTool_pageSizes[3],
		BBVars.dspAreaWidth		= win.width(),
		BBVars.dspAreaHeight	= win.height();
		
	}).bind("scroll", function() {
		BBTool_pageScroll 		= BBTool_getPageScroll(),
		BBVars.pageScrollX 		= BBTool_pageScroll[0],
		BBVars.pageScrollY 		= BBTool_pageScroll[1];
		
		if(jQuery(".nextPageLayer").length>0) {
			
			if(BBVars.windowHeight + BBVars.pageScrollY > jQuery(".nextPageLayer").offset().top  && !BBVars.pageLoading && jQuery("#BBTool_overlay:visible").length==0) {
				var form = jQuery("#product_form"),
					page = jQuery("input[name=page]", form),
					action = jQuery("#product_form").attr("action");
				
				BBVars.pageLoading = true;
				page.val( eval( page.val() ) + 1 );
				
				if(action.indexOf("getPage")==-1) action = action + ( (action.indexOf("?")==-1) ? "?" : "&" ) + "getPage=" + page.val();
				else action = action.slice(0, action.length-1) + page.val()
				
				form.attr("action", action );
				form.attr("data-BBParams", "{action:'"+action+"', ajaxPost: true}");
				
				jQuery("#prodListLayer").stop().animate({opacity: 0.5}, function() {
					BBTool_formPost("product_form", true);
				});

			}
		}
	});
	
	var searchLabelInput = jQuery("#search");
	searchLabelInput.data("label", searchLabelInput.val() );
	
	searchLabelInput.bind("focus", function(e) {
		searchLabelInput.val("");
	}).bind("blur", function(e) {
		if(searchLabelInput.val()=="") searchLabelInput.val( searchLabelInput.data("label") ); 
	}).autocomplete(
		BBVars.pagesURL + "/ajax/search.cfm", {
			minChars:2,
			delay:200,
			autoFill:false,
			matchSubset:false,
			matchContains:1,
			cacheLength:10,
			selectOnly:1
	});
	
	jQuery("#custServiceLink").tooltip(
		function (el) { return "<p class='csTxt'>" + el.rel + "</p>";},
		{pointer: true, xOffset: 10, yOffset: 10, tTipDspPos: "hrzt", classN: 'cs', tTipWidth: 100, tTipHeight: 40}
	);
	
	if(BBVars.currentPageName=="index") {
		jQuery.get(
			BBVars.pageURL + "ajax/promo.cfm",
			function(data) {
				BBSettings.BBPopup.elementsSettings.width = "1163px";
				BBSettings.BBPopup.elementsSettings.backgroundColor = "transparent";
				BBTool_openMsgBox(data, true);
				BBSettings.BBPopup.elementsSettings.backgroundColor = "#fff";
			}
		);
	} else if(BBVars.currentPageName=="univers" || BBVars.currentPageName=="selection" || BBVars.currentPageName=="recherche") {
	
		// Recherche
		if(jQuery("#price_origine").length>0) {
			jQuery("#price_origine").slider({
				range: true,
				min: 0,
				max: eval(jQuery("#borne_prix_max").val()),
				values: [ jQuery("#price_min").val(), jQuery("#price_max").val() ],
				slide: function( event, ui ) {
					jQuery( "#ecart_prix" ).html( ui.values[ 0 ] + " &euro; et " + ui.values[ 1 ] + " &euro;");
					jQuery("#price_min").val(ui.values[ 0 ]);
					jQuery("#price_max").val(ui.values[ 1 ]);
				}
			});
			
			jQuery("#price_origine a").each( function( index ) {
				(index == 0) ? jQuery(this).addClass("first") : jQuery(this).addClass("last");
			});
			
			jQuery( "#ecart_prix" ).html( jQuery( "#price_origine" ).slider( "values", 0 ) +
				" &euro; et " + jQuery( "#price_origine" ).slider( "values", 1 ) + " &euro;");
		}
	
		// Silhouettes
		if(jQuery(".prodInfos_layer_encart").length==1) {
		
			var linksCont = jQuery(".prodInfos_layer_encart");
			
			/*if(jQuery(".flashLayer", linksCont).length>=1) {
				jQuery(".imgDspLayer", linksCont).css("display", "none");
				jQuery("#video", linksCont).css("display", "block");
			} else {
				jQuery("#video", linksCont).css("display", "none");
				jQuery(".imgDspLayer", linksCont).css("display", "block");
			}*/
			
			jQuery("a", linksCont).bind("click", function(e) {
				e.preventDefault();
				var link = jQuery(this),
					linkCont = link.attr("href");
				
				if (link.hasClass("dspZoomLink")&&jQuery("#imageSilhouette:visible").length==0) {
					jQuery("#video").stop().animate({opacity: 0}, function() {
						jQuery(this).css("display", "none");
						jQuery(".imgDspLayer", linksCont).css("display","block").stop().animate({opacity: 1});
					});
				} else if(jQuery("#imageSilhouette:visible").length==1) {
					jQuery(".imgDspLayer", linksCont).stop().animate({opacity: 0}, function() {
						jQuery(this).css("display", "none");
						jQuery("#video").css("display","block").stop().animate({opacity: 1});
						videoDsp(linkCont);
					});
				} else {
					jQuery(".imgDspLayer", linksCont).css("display", "none");
						jQuery("#video").css("display","block").stop().animate({opacity: 1});
						videoDsp(linkCont);
				}
			});
			
			jQuery(".dspSilLink").trigger("click");
		} else
			jQuery(".pages_layer").css("display", "block");
		
		jQuery("#products_list li").each( function() {
			var item = jQuery(this),
				prodID = item.attr("prodID"),
				colorID = item.attr("colorID"),
				btn = jQuery(".cart", item);
				
			item.bind("mouseenter", function() {
				btn.css("display", "block").stop().animate({opacity: 1});
			}).bind("mouseleave", function() {
				btn.stop().animate({opacity: 0}, function() {jQuery(this).css("display", "none");});
			});
				
			btn.css({opacity: 0}).bind("click", function(e) {
				e.preventDefault();
				jQuery.get(
					BBVars.pagesURL + "/fiche.cfm?isAjax=true"
					+ "&produit_id="		+ prodID
					+ "&coul_att_detailID="	+ colorID,
					function(data) {
						BBSettings.BBPopup.elementsSettings.width = "720px";
						BBTool_openMsgBox(data, true);
					}
				);
			});
		});
	
	/*------------------------------------------------------------------------------------*/
	/*------------------------------------ SILHOUETTES -----------------------------------*/
	/*------------------------------------------------------------------------------------*/
	
	} else if(BBVars.currentPageName=="silhouettes") {
	
		var slider = jQuery("#sliderLayer"),
			sliderDim = { width: slider.width(), height: slider.height() },
			sliderPos = { top: slider.offset().top, left: slider.offset().left },
			list = jQuery("ul", slider),
			items = jQuery("li", list),
			itemWidth = 180,//items.eq(0).width(),
			maxWidth = 245,
			minWidth = 150,
			timer = null,
			step = 0,
			dist = 0;
			
		jQuery("img", items).css({display: "block"}).bind("load", function() {
			jQuery(this).parent().css("backgroundImage", "none");
		})/*.tooltip(
			function(el) { return "<img src='"+jQuery(el).attr("src")+"'>" },
			{tTipDspPos: "free", pointer: true, xOffset: 10, yOffset: 10}
		);*/
			
		function slide() {
		
			list.css({ left: ((dist>0) ? "+=" : "-=") + step*0.8 });
			
			var pos = sliderPos.left - list.offset().left - 490,
				dspIndex = Math.floor( pos / itemWidth ) + 3,
				dspItems = jQuery("li", list).slice(dspIndex,7+dspIndex);
			
			var pt = sliderPos.left + 367.5;
			
			dspItems.each( function(i,el) {
				var item = jQuery(this),
					img = jQuery("img",item),
					itemPos = Math.abs( pt - item.offset().left);
					
				img.css({
					width: maxWidth - 95 * Math.sin(itemPos/300),
					top: (sliderDim.height - img.height())/2
				}).css({
					left: (180-img.width())/2
				});
				
				/*
					opacity: 1 - Math.sin(itemPos/490)
				*/
					
				/*item.css({
					width: maxWidth - 95 * Math.sin(itemPos/300),
				})*/
				if(i==2) {
					
					jQuery("#silLabelLayer").html( jQuery("span", item).html() );
					items = jQuery("li", list);
					
					if(dspIndex==3) {
						var firstItem = items.eq(0),
							lastItem = items.eq(items.length-1);
					
						lastItem.insertBefore( firstItem );
						list.css({left: "-=" + itemWidth});
					
					} else if (dspIndex==items.length-5) {
					
						var firstItem = items.eq(0),
							lastItem = items.eq(items.length-1);
					
						firstItem.insertAfter( lastItem );
						list.css({left: "+=" + itemWidth});
					
					}
				}
				
			});
			
			timer = setTimeout( slide, 5 - step );
		}
		
		list.css({ width: items.length * itemWidth, left: "-" + ((items.length * itemWidth)/2 - 490) + "px" });
		
		slide();
		
		slider.bind("mousemove", function(e) {
			dist = ((sliderDim.width/2) - (e.pageX - sliderPos.left))/100;
			step = Math.abs(dist);
			if(step!=0&&!timer) {
				clearTimeout(timer);
				timer = setTimeout( slide, 5 - step );
			}
		});
	
	/*------------------------------------------------------------------------------------*/
	/*--------------------------------------- FICHE --------------------------------------*/
	/*------------------------------------------------------------------------------------*/
	
	} else if(BBVars.currentPageName=="fiche") {

		var galList = jQuery(".prodImgLayer ul"), 
			galItems = jQuery("li.thumb", galList), 
			thumbs = jQuery("a", galItems); 
		
		galItems.each( function() {
			var item = jQuery(this),
				link = jQuery("a", this);
			link.bind("click", function(e) {
				galItems.removeClass("selected");
				item.addClass("selected");
			}).bind("mouseenter", function() {
				item.addClass("hover");
			}).bind("mouseleave", function() {
				item.removeClass("hover");
			});
		});
		
		var attribsLayer = jQuery(".prodAttribsLayer"),
			crossList = jQuery(".crossList"),
			crossItems = jQuery("li", crossList);
		
		attribsLayer.each( function() {
		
			var prodAttribs = jQuery(this),
				colorList = jQuery(".colorsList", prodAttribs),
				colors = jQuery("a", colorList),
				sizeList = jQuery(".sizesList", prodAttribs),
				sizes = jQuery("li", sizeList);
				
			crossItems.each( function() {
				var prod = jQuery(this),
					link = jQuery(".cart", prod),
					prodID = link.attr("rel"),
					colorID = link.attr("rev");
					
				prod.bind("mouseenter", function() {
					link.css("display", "block").stop().animate({opacity: 1});
				}).bind("mouseleave", function() {
					link.stop().animate({opacity: 0}, function() {jQuery(this).css("display", "none");});
				});
				
				link.css({opacity: 0}).bind("click", function(e) {
					e.preventDefault();
					jQuery.get(
						BBVars.pagesURL + "/fiche.cfm?isAjax=true"
						+ "&produit_id="		+ prodID
						+ "&coul_att_detailID="	+ colorID,
						function(data) {
							BBSettings.BBPopup.elementsSettings.width = "720px";
							BBTool_openMsgBox(data, true);
						}
					);
					
				});
			});
				
			sizes.bind("click", function(e) {
				e.preventDefault();
				sizes.removeClass("selected");
				jQuery(this).addClass("selected");
				BBCart('setAttrib', jQuery(this).attr("id"), setAttrib_callBack);
			})
			
			colors.each( function() {
			
				var color = jQuery(this),
					colorID = colorID = color.attr("rel");
			
				color.bind("click", function() {
					var nbImg = 0;
					
					galItems.each( function() {
						var thumb = jQuery(this),
							thumbInf = eval( "(" + thumb.attr("data-thumbInf") + ")" )
							img = jQuery("a", thumb);
						if(thumbInf.colorID==color.attr("rel")&&thumbInf.prodID==prodAttribs.attr("data-prodID")) {
							nbImg++;
							thumb.removeClass("hidden");
							if(nbImg==1) {
								jQuery("img", thumb).trigger("click");
								MagicZoom.update(document.getElementById("mainProdImgLink"+thumbInf.prodID), img.attr("href"), img.attr("rev") );
							}
						} else thumb.addClass("hidden");
					});
					
					if(nbImg<=1) galItems.addClass("hidden" );
							
					colors.not(color).removeClass("selected");
					color.addClass("selected");
					
					sizeList.css("display", "block");
					sizeList.not("[data-colorID="+colorID+"]").css("display", "none");
					
					crossItems.css("display", "block");
					crossItems.not("[data-colorID="+colorID+"]").css("display", "none");
					
					BBCart("colorChange", colorID);
					
					if( jQuery("ul[data-colorID="+colorID+"] li[data-stock!=0]").length>0 )
						BBCart("setAttrib", jQuery("ul[data-colorID="+colorID+"] li[data-stock!=0]").eq(0).attr("id") );
						
					/*if(jQuery("option", sizeList).length-jQuery("option.disabled", sizeList).length==1)
						BBCart("setAttrib", "item" + jQuery("#option_dispo"+colorID).val() );
					else
						BBCart("setAttrib", "item" + jQuery("option", sizeList).eq(1).attr("id") );*/
					
				});
				
				/*if(colorID==BBVars.currentColorID)
					color.trigger("click");*/
			
			});
			
			BBCart("getOptions", prodAttribs.attr("data-prodID") );
				
		});
			
		var TTList = jQuery(".miscInfosLayer ul"),
			TTItems = jQuery("li", TTList).not(".HP");
			
		TTItems.each( function() {
			var item = jQuery(this),
				TTContent = jQuery("p", item).html(),
				link = jQuery("a", item);
			link.css({visibility: "visible", opacity: 0});
			item.bind("mouseenter", function() {
				link.stop().animate({opacity: 1});
			}).bind("mouseleave", function() {
				link.stop().animate({opacity: 0});
			}).tooltip( TTContent, {pointer: true, pointerOffset: {x: -109,y:0}, xOffset: 0, yOffset: 5, tTipDspPos: "top", classN: "misc"} );
		});
			
	} else if(BBVars.currentPageName=="consulter_vos_points") {
	
		initHelpCard();
		
	} else if(BBVars.currentPageName=="panier") {
	
		initHelpCard();
		
	} else if(BBVars.currentPageName=="login") {
		keyLogin_callback();
		
	} else if(BBVars.currentPageName=="livraison") {
		var options = {
			header:			"div.title", 
			autoheight:		true
		}
		jQuery("#deliveryChoices_content").accordion(options);
		
		setShippingMode();
		
		//if(BBVars.adrType!=0) jQuery("#deliveryChoices_content").activate((BBVars.adrType-1));
	
	} else if(BBVars.currentPageName=="nouveau_compte") {
	
		initBBSelect("#new_account_form");
		
	} else if(BBVars.currentPageName=="paiement") {
		var options = {
			header:			'div.title', 
			autoheight:		false,
			clearStyle:		true
		}
		jQuery("#paymentChoices_content").accordion(options);
		
		initBBSelect("#formPaiement0");
		
	} else if(BBVars.currentPageName=="mailing") {
	
		initBBSelect("#frmNewsletter");
	
	} else if(BBVars.currentPageName=="contact") {
		var options = {
			header:			'strong', 
			autoheight:		false
		}
		jQuery("#slider").accordion(options);
		initBBSelect("#formContact");
		
	} else if(BBVars.currentPageName=="carte_morgan_star") {
		tab_dyn_carte(0);
	} else if (BBVars.currentPageName=="votre-mission-seduction") {
		fbInit("mission_layer", "e3d162db58429c200635e2ce5b3183c3", BBVars.pagesURL+"contenu/xd_receiver.htm");
	
	} else if(BBVars.currentPageName=="nouvelle_campagne_ete") {
		jQuery(".wallpaper li").each( function( index ) {
			
			jQuery(this).bind("mouseenter", function(e) {
				e.preventDefault();
				var content_id = jQuery(this).attr("id");
				jQuery("#" + content_id + " div").stop().css({display: "block", opacity: 0}).animate({opacity: 1});
				jQuery("#" + content_id).addClass("hover");
			}).bind("mouseleave", function(e) {
				e.preventDefault();
				var content_id = jQuery(this).attr("id");
				jQuery("#" + content_id + " div").stop().animate({opacity: 0}, function() {jQuery(this).css("display","none");});
				jQuery("#" + content_id).removeClass("hover");
			});
			
				
		});
		
	} else if(BBVars.currentPageName=="operation_pressing") {
		jQuery("#contenu_magasins .resultats span.hl").tooltip(
				function(el) { return jQuery(".OPHours", jQuery(el).parent()).html(); },
				{pointer: true, xOffset: 20, tTipDspPos: "hrzt"}
			);
			
	} else if(BBVars.currentPageName=="vos_avantages") {
		initAvantages();
	} else if( BBVars.currentPageName.indexOf("compte")!=-1 ) {

		jQuery("#account_toolBar_list").children().eq( jQuery("#account_toolBar_list").attr("data-selected") ).addClass("on");
		
		var accountSliderOptions = {
			header:			'a.title', 
			autoheight:		false,
			alwaysOpen:		false,
			active:			false,
			collapsible:	true
		}
		jQuery("#accountSlider").accordion(accountSliderOptions);
		jQuery("#accountSlider").bind("click", function() {
			jQuery("#account_toolBar_list li").removeClass("on");
			jQuery(this).addClass("on");
		});
		
		initBBSelect("#account_form");

		if (document.location.hash != '')
			popupCarte();
			
		jQuery("#WLLayer .cmdProd a").bind("click", function(e) {
			e.preventDefault();
			jQuery.get(
				BBVars.pagesURL + "/fiche.cfm?isAjax=true"
				+ "&produit_id="		+ jQuery(this).attr("rel")
				+ "&coul_att_detailID="	+ jQuery(this).attr("rev"),
				function(data) {
					BBSettings.BBPopup.elementsSettings.width = "720px";
					BBTool_openMsgBox(data, true);
				}
			);
		});

	}

	if(BBVars.pageHeight>jQuery("#siteLayer").height()) 
		jQuery("#siteLayer").css("height", BBVars.pageHeight-40);

});
