/*----------------------------------------------------------------------------------*/

function hideSubmitBtn() {
	jQuery("#wait").css({ display: "block", opacity: 0}).animate({opacity: 1});
}

function videoDsp(videoSrc) {

	jQuery('#video').html("");
	jQuery('#video').flash({
		src: BBVars.pagesURL + 'ximg/flash/player.swf',
		width: 180,
		height: 320,
		wmode: 'transparent',
		allowScriptAccess: 'always',
		salign: 'tl',
		align: 'top',
		flashvars: {
			flvwidth: 180,
			flvheight: 320,
			flvname: videoSrc
		}
	});
}

function ajaxPageLoaded( nextPage ) {

	jQuery("#prodListLayer").stop().animate({opacity: 1});

	if( nextPage ) {
	
		win = jQuery(window);
	
		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();
		
		jQuery(".nextPageLayer .BBTTip").unbind();
		BBTool_toolTip();
		
		jQuery(".nextPageLayer").removeClass().css("height", "auto").append( jQuery("<div class='nextPageLayer'></div>") );
		BBVars.pageLoading = false;
	
	}

}

/*-------------------- TRI PAR ATTRIBUT (UNIVERS) --------------------*/
function filterOver(menuID) {

	var menu = document.getElementById(menuID + '_layer').getElementsByTagName("ul")[0];
	if(!menu.style.display || menu.style.display == "none") menu.style.display = "block";
	else menu.style.display = "none";

}

function sortList(type, value){
	if(type =='price') {
		document.product_form.price_origine.value = value;
	} else if(type == 'number') {
		document.product_form.maxrows.value = (value<0) ? 12 : parseInt(document.product_form.maxrows.value) + 12*value;
		var form = jQuery("#product_form"),
			action = jQuery("#product_form").attr("action");
		if(value<0) {
			document.product_form.isAjax.value = "false";
			if(action.indexOf("getPage")!=-1) {
				action = action.slice( 0, action.indexOf("getPage") - 1);
				form.attr("action", action );
			}
		} else {
			jQuery("#product_form").attr("action", action + ((action.indexOf("?")==-1) ? "?" : "&") + "isAjax=true" );
			document.product_form.isAjax.value = "true";
		}
	}
	else if(type == 'tri') {
		document.product_form.tri.value = value; 
	}
	document.product_form.submit();
}
/*---------------------------------------------------*/

function QBCallback() {
	
	var attribsLayer = jQuery("#BBTool_msgBoxBody .prodAttribsLayer"),
		cartBtn = jQuery(".toCart", attribsLayer),
		thumbs = jQuery("#BBTool_msgBox .prodImgLayer ul li.thumb");
	
	cartBtn.bind("click", function(e) {
		e.preventDefault();
		BBCart("addOption", jQuery("ul.sizesList:visible li.selected", attribsLayer).attr("id") );
	});
	
	MagicZoom.refresh();
	
	attribsLayer.each( function() {
	
		var prodAttribs = jQuery(this),
			colorList = jQuery(".colorsList", prodAttribs),
			colors = jQuery("a", colorList),
			sizeList = jQuery(".sizesList", prodAttribs),
			sizes = jQuery("li", sizeList);

		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() {
				
				colors.not(color).removeClass("selected");
				color.addClass("selected");
				
				thumbs.each( function() {
					var thumb = jQuery(this);
						thumbInf = eval( "(" + thumb.attr("data-thumbInf") + ")" );
					if(thumbInf.colorID==color.attr("rel")&&thumbInf.prodID==prodAttribs.attr("data-prodID")) 
						jQuery("img", thumb).trigger("click");
				});
				
				sizeList.css("display", "block");
				sizeList.not("[data-colorID="+colorID+"]").css("display", "none");
				
				BBCart("colorChange", colorID);
				
				if( jQuery("ul[data-colorID="+colorID+"] li[data-stock!=0]", prodAttribs).length>0 )
					BBCart("setAttrib", jQuery("ul[data-colorID="+colorID+"] li[data-stock!=0]", prodAttribs).eq(0).attr("id") );
					
			});
			
		});
		
		BBCart("getOptions", prodAttribs.attr("data-prodID") );
			
	});
	
}

/*-------------------- ONGLETS FICHE PRODUIT --------------------*/
function tab_dyn_fiche (nb) {
	jQuery("#tab_list_ul li").each(function(i) {
		if(nb==i) {
			jQuery(this).removeClass();
			jQuery(this).addClass("li_actif");
			jQuery(this).fadeTo("fast",1,function(){
				jQuery("#tab_layer_"+jQuery(this).attr("tabId")).css("display","block").fadeTo("fast",1,function(){if(BBVars.isMsIE) this.style.removeAttribute("filter");}).css("opcaity", "1");
			});
		} else {
			jQuery(this).removeClass();
			jQuery(this).addClass("li_none");
			jQuery(this).fadeTo("fast",0.33,function(){
				jQuery("#tab_layer_"+jQuery(this).attr("tabId")).css("display","none").fadeTo("fast",0,function(){if(BBVars.isMsIE) this.style.removeAttribute("filter");});
			});
		}
	});
	
}
/*----------------------------------------------------------------*/

function pictoHover(linkObj) {

	var label = jQuery('pictos_label');
	var label_layer = jQuery('pictos_label_layer');
	
	if(linkObj) {
		label.innerHTML = linkObj.title;
		label_layer.style.visibility = "visible";
	} else label_layer.style.visibility = "hidden";

}

var tmp_colorLabel = "";

function colorHover(label,DetID,attrib_id,sel_coul_id) {
	//alert(attrib_id);
	//alert(DetID);
	var label_layer = jQuery('color_label2');
	/*for(var i=0; i<Details.length; i++){
		var Detail = Details[i];
		if(DetID != Detail.DetailID)
			jQuery('OAttribut_a' +attrib_id + '_d' + Detail.DetailID ).style.display='none';
		else jQuery('OAttribut_a' +attrib_id + '_d' + Detail.DetailID ).style.display='block';
	}*/
	if(attrib_id){
		if(tab_coul){
		for(var i=0; i<tab_coul.length; i++){
			(tab_coul[i] == DetID)?	jQuery('OAttribut_a' +attrib_id + '_d' + tab_coul[i] ).style.display='block':jQuery('OAttribut_a' +attrib_id + '_d' + tab_coul[i] ).style.display='none';
			}
		}
	}
	if(label && label != "") {
		tmp_colorLabel = label_layer.innerHTML;
		label_layer.innerHTML = label;
	} else label_layer.innerHTML = tmp_colorLabel;

}

function setShippingMode() {
	var deliveryId = jQuery("#deliveryChoices_content .selected").attr("index");
	
	/*jQuery("#frmModeLivraison .fdpContent").each(function() {
		var fdpContent = jQuery(this).html();
		var fdpID = jQuery(this).attr("id");
		jQuery(".fdpContent" + fdpID).html("<strong>" + BBLabels.label_frais + ":</strong>" + fdpContent);
	});*/

	jQuery("#postLivraison").attr("href","javascript: pageTracker._linkByPost(document.forms['frmLivraison" + deliveryId + "']); BBTool_formPost('frmLivraison" + deliveryId + "',false);");
	
	jQuery("#deliveryChoices_content .title").bind("click", function( e ) {
			//e.preventDefault();
			var ModLivr = jQuery(this).attr("index");
			var TrackerLivr = "pageTracker._linkByPost(document.forms['frmLivraison" + ModLivr + "']);";
			jQuery("#postLivraison").attr("href","");
			jQuery("#postLivraison").attr("href","javascript: " + TrackerLivr + "BBTool_formPost('frmLivraison" + ModLivr + "',false);");
		});
	jQuery("#deliveryChoices_content .title").eq(deliveryId).trigger("click");
}


/*--------------------------------- CALLBACKS ---------------------------------*/
function keyLogin_callback() {
jQuery("#frmValider :input").bind("keypress", function( event ) {
		if(event.keyCode == 13) {
			event.preventDefault();
			BBTool_formPost('frmValider',false);
		}
	});
}


function keyBlockLogin_callback() {
jQuery("#loginBlock_form :input").bind("keypress", function( event ) {
		if(event.keyCode == 13) {
			event.preventDefault();
			BBTool_formPost('loginBlock_form',true);
		}
	});
}

function keypswd_callback() {
jQuery("#forgotten_mdp_error :input").bind("keypress", function( event ) {
		if(event.keyCode == 13) {
			event.preventDefault();
			BBTool_formPost('forgotten_mdp_error',true);
		}
	});
}

function checkClient_callback(data) {}
function checkClient() {
	jQuery.get(
		((BBVars.currentPageName=="paiement"||BBVars.currentPageName=="confirmation")?BBVars.pageURL:BBVars.pagesURL) + "ajax/login.cfm?formAction=checkClient",
		function(data) {BBTool_openMsgBox(data, true)}
	);
}

function sendPwd_callback(data) {setTimeout("BBTool_closeMsgBox()",2000);}

function login_callback(type,data) {
	setTimeout("BBTool_closeMsgBox()",500);
	BBVars.isClient = true;
	
	switch(type) {
	
		case "cardNumber":
			if (data != "")
				jQuery.get(
					BBVars.pageURL + "carte_fidelite/checkFidelity.cfm?cardNumber=" + data,
					function(dataapres) { 
						jQuery("#card_error").html(dataapres.replace(/^\s+/g,'').replace(/\s+$/g,''));
						jQuery("#card_error").stop().css({display: "block", opacity: 0}).animate({opacity: 1});
						setTimeout("jQuery('#card_error').stop().animate({opacity: 0}, function() {jQuery(this).css('display','none')})", 4000);
					}			
				);
			break;
		
		case "consultPoints":
			jQuery(location).attr('href',BBVars.pageURL + "carte_fidelite/consulter_vos_points.cfm");
			break;
			
	}
	
	
}

function BBLogin(type,data) {
	
	switch(type) {
	
		case "login":
				$.get(
					((BBVars.currentPageName!="paiement")?BBVars.pagesURL:BBVars.pageURL) + "ajax/login.cfm?formAction=prompt",
					function(data) {BBTool_openMsgBox(data, true)}
				);
			break;
			
		case "fidelityCard":
				$.get(
					BBVars.pageURL + "ajax/login.cfm?formAction=prompt&cardNumber=" + data,
					function(data) {BBTool_openMsgBox(data, true)}
				);
			break;
		
		case "consultPoints":
				$.get(
					BBVars.pageURL + "ajax/login.cfm?formAction=prompt&consultPoints=true",
					function(data) {BBTool_openMsgBox(data, true)}
				);
			break;
	}
}


function sendPwd() {
	jQuery.get(
		((BBVars.currentPageName!="paiement")?BBVars.pagesURL:BBVars.pageURL) + "ajax/login.cfm?formAction=promptPwd",
		function(data) {BBTool_openMsgBox(data, true)}
	);
}

function getSizeGuide( ring ) {
	BBSettings.BBPopup.elementsSettings.width = "800px"; 
	if(ring) {
		jQuery.get(
			BBVars.pagesURL + "ajax/guide_tailles_bagues.cfm",
			function(data) {BBTool_openMsgBox(data, true)}
		);
	} else {
		jQuery.get(
			BBVars.pagesURL + "ajax/guide_tailles.cfm",
			function(data) {BBTool_openMsgBox(data, true);}
		);
	}
}

function getVille(code_postal) {
	jQuery.get(
		BBVars.pagesURL + "ajax/getVille.cfm?code_postal=" + code_postal,
		function(data) {
			var ville = data.split('|');
			document.getElementById('ville').value = ville[0].replace(/^\s+/g,'').replace(/\s+$/g,'');}
	);
}

function initCheckoutPathbar() {
	jQuery("#Process li a").each( function(i,el) {
		var link = jQuery(this).attr("href");
		jQuery(this).parent().bind("click", function() {
			window.location = link;
		});
	});
}


/*-------------------- ONGLETS CARTE MORGAN STAR --------------------*/
function tab_dyn_carte (nb) {
	jQuery("#tab_list_ul li").each(function(i) {
		if(nb==i) {
			jQuery(this).removeClass();
			jQuery(this).addClass("li_actif");
			jQuery(this).fadeTo("fast",1,function(){
				jQuery("#card_layer_"+jQuery(this).attr("tabId")).css("display","block").fadeTo("fast",1,function(){if(BBVars.isMsIE) this.style.removeAttribute("filter");}).css("opacity","1");
				//jQuery("#card_layer_"+jQuery(this).attr("tabId")).style.removeAttribute('filter');
			});
		} else {
			jQuery(this).removeClass();
			jQuery(this).addClass("li_none");
			jQuery(this).fadeTo("fast",0.33,function(){
				jQuery("#card_layer_"+jQuery(this).attr("tabId")).css("display","none").fadeTo("fast",0,function(){if(BBVars.isMsIE) this.style.removeAttribute("filter");});
			});
		}
	});
	
}
/*--------------------------------------------------------------------*/

function BBOpenMondial() {
	BBSettings.BBPopup.elementsSettings.width = "795px";
	jQuery.get(
		BBVars.pagesURL + "VersionB/ajax/mondial_relay.cfm?pays=" + document.frmLivraison1.pays.value + "&ville_mondial_relay=" + document.frmLivraison1.ville.value + 
			"&cp_mondial_relay=" + document.frmLivraison1.cp.value+ "&adresse_mondial_relay=" + document.frmLivraison1.adresse1.value + "&kiala_point_id=" + document.frmLivraison5.point_relais.value,
		function(data) {BBTool_openMsgBox(data, true);
		BBSettings.BBPopup.elementsSettings.width = "795px";
		}
	);
}

function initOpHours() {
		
		//jQuery("#BBTool_msgBoxContent").addClass("mondial_relay");
		
		jQuery("#BBTool_msgBoxContent .resultats span.hl").tooltip(
				function(el) { return jQuery(".OPHours", jQuery(el).parent()).html(); },
				{pointer: true, xOffset: 20, tTipDspPos: "hrzt"}
			);
	
}


/*-------------------- POPUP CADEAU PANIER --------------------*/
function cadeauPopup() {
	BBSettings.BBPopup.elementsSettings.width = "935px";
	jQuery.get(
		BBVars.pagesURL + "ajax/cadeau.cfm",
		function(data) {
			BBTool_openMsgBox(data, true);
			BBSettings.BBPopup.elementsSettings.width = "610px";
		}
	);
}

function cadeauPopupCallback() {
	BBcountChr();
	BBTool_flash();
}

function cadeauPopupCloseCallback(data) {
	BBTool_closeMsgBox();
}

function BBcountChr() {
	var maxChr = 480;
	jQuery("#nbChr").html(maxChr);
	
	document.getElementById('txt_message').onkeyup=function( event ) {
		var chrNow = document.forms['cadeau_form'].elements['txt_message'].value.length;
		var maxLine = 6;
		
		if(event.keyCode == 8) {
			document.getElementById('txt_message').removeAttribute('readonly', false);
			var chrCount = maxChr - chrNow;
			jQuery("#nbChr").html(chrCount);
		} else {
			var chrCount = maxChr - chrNow;
			jQuery("#nbChr").html(chrCount);
			if(chrCount == 0) {
				document.getElementById('txt_message').setAttribute('readonly', true);
			} else {
				document.getElementById('txt_message').removeAttribute('readonly', false);
			}
		}

	}
}

function SizeGuidPopupCallback() {
	jQuery("#BBTool_msgBoxContent").addClass("sizeGuid");
}

function deleteCadeau() {
	jQuery("#basketForm input[name=action]").val('deleteCadeau');
	BBTool_formPost("basketForm",false);
	//window.location = BBVars.pagesURL + "panier.cfm?deleteCadeau";
}

function Trim(orgString){
  return LTrim(RTrim(orgString))
}

function LTrim(orgString){
  return orgString.replace(/^\s+/,'')
}

function RTrim(orgString){
  return orgString.replace(/\s+$/,'')
}

function initMenu() {
	
	jQuery(".mainCat .mainCatLink").each( function(i,el) {
		if(BBVars.currentPageName == "index" ) {
			var currentLink = jQuery(this),
				currentCatList = currentLink.parent().next("ul").eq(0);
		} else {
			var currentLink = jQuery(this),
				currentCatList = currentLink.next("ul").eq(0);
		}
		currentLink.bind("click", function(e) {
			e.preventDefault();
			if(!jQuery(this).hasClass("selected")) {
				currentCatList.slideToggle();
				currentLink.addClass("selected");
			} else if(jQuery(this).hasClass("selected")) {
				currentCatList.slideToggle();
				currentLink.removeClass("selected");
			}
		});
	});

	if(BBVars.currentPageName == "silhouettes")
		jQuery(".mainCatLink.silhouettes").eq(0).trigger("click");
	else {
		jQuery("#leftMenuList li.select .sCatList").slideToggle();
		jQuery(".mainCatLink.currentCat").eq(0).trigger("click");
	}

}

function smoothScroll() {
	jQuery(".navBtn").bind("click", function( e ) {
		e.preventDefault();
		jQuery('html,body').stop().animate({scrollTop: jQuery("#"+jQuery(this).attr("target")).offset().top-10}, "slow");
	});
}


function initBBRadio() {

	if(jQuery(".BBRadio").length>0) {

		jQuery("form").each( function( i ) {
		
			var currentForm = jQuery(this);
		
			jQuery(".BBRadio", this).each( function( j ) {
			
				var inputName = jQuery(this).attr("name");
				var inputValue = jQuery(this).attr("value");
				
				if(jQuery("fieldset input[name='"+jQuery(this).attr("name")+"']", currentForm).length==0)
					jQuery("fieldset", currentForm).append("<input type='hidden' name='"+inputName+"' id='"+inputName+"' value='"+inputValue+"'>");
					
				jQuery(this).replaceWith(
					jQuery("<div class='"+((j==0)?"BBRadio checked":"BBRadio")+"' group='"+inputName+"'>&nbsp;</div>").bind("click", function(e) {
						jQuery(".BBRadio[group="+inputName+"]",currentForm).removeClass("checked");
						jQuery(this).addClass("checked");
						jQuery("#"+inputName).val(inputValue);
					})
				)
				
			});
			
		
		});

	}

}


function change_adresse(formulaire){
	if(document.forms[formulaire].elements['infos_adresse_livraison'].value != '') {
		window.location = 'livraison.cfm?adresse='+document.forms[formulaire].elements['infos_adresse_livraison'].value;
	}
}

function changeAdressLivr() {
	jQuery.get(
		BBVars.pagesURL + "VersionB/ajax/adresse.cfm",
		function(data) {BBTool_openMsgBox(data, true);}
	);
}

function checkLivraison() {
	var erreur = "";

	if(jQuery("#nom_a_changer").val() == "")
		erreur+="<li>- Nom</li>";
	
	if(jQuery("#prenom_a_changer").val() == "")
		erreur+="<li>- Prenom</li>";
	
	if(jQuery("#adresse1_a_changer").val() == "")
		erreur+="<li>- Adresse</li>";
		
	if(jQuery("#cp_a_changer").val() == "")
		erreur+="<li>- Code Postal</li>";
		
	if(jQuery("#ville_a_changer").val() == "")
		erreur+="<li>- Ville</li>";
	
	if (erreur != "")
		{	jQuery("#erreur").html("Il manque des information pour pouvoir valider votre panier : <br><ul>" + erreur + "</ul>");
			jQuery("#erreur").css('display','block');}
}

function setNouvelleAdresse() {

	jQuery("#change_nom").val(jQuery("#nom_a_changer").val());
	jQuery("#change_prenom").val(jQuery("#prenom_a_changer").val());
	jQuery("#change_adresse1").val(jQuery("#adresse1_a_changer").val());
	jQuery("#change_adresse2").val(jQuery("#adresse2_a_changer").val());
	jQuery("#change_cp").val(jQuery("#cp_a_changer").val());
	jQuery("#change_ville").val(jQuery("#ville_a_changer").val());
	//jQuery("#change_pays").val(jQuery("#pays_a_changer").val());
	
	if (jQuery("#cbFact").attr("checked"))
		jQuery("#utilise_adresse_fact").val(1);
	else
		jQuery("#utilise_adresse_fact").val(0);
		
	if (jQuery("#cbDefaut").attr("checked"))
		jQuery("#change_adresse_client").val(1);
	else
		jQuery("#change_adresse_client").val(0);
		
	jQuery("#adrLivraison").html("<strong>" + BBLabels.label_adresseLivraison + "</strong><ul><li><span class='name'>" + jQuery("#nom_a_changer").val() + " " + jQuery("#prenom_a_changer").val() + "</span></li><li>" + jQuery("#adresse1_a_changer").val() + "</li><li>" + jQuery("#adresse2_a_changer").val() + "</li><li>" + jQuery("#cp_a_changer").val() + " " + jQuery("#ville_a_changer").val() + "</li><li class='changeAdress_link'><a href='javascript: changeAdressLivr();'>" + BBLabels.label_autreAdresse + "</a></li></ul>");

	setTimeout("BBTool_closeMsgBox()", 1500);

}

/* CARTE FIDELITE */
function initAvantages() {
	jQuery("##avantages_choix li").each( function(i) {
		var item = jQuery(this);
		var container = jQuery("##avantages_txt p");
		var arrow = jQuery("##avantages_arrow img");
		
		jQuery(this).bind("mouseenter", function(e) {
			if(!jQuery(this).hasClass("selected")) {
				jQuery("##avantages_choix li").removeClass("selected");
				jQuery(this).addClass("selected");
				container.stop().animate({
				opacity : 0
					}, 200, function() {
						jQuery(this).html(item.html());
					});
				arrow.stop().animate({
					left: (27+(-70-115*(3-i)))+"px"
					}, 400, function() {
						container.stop().animate({opacity: 1});
					});
		 	}
	 	});
	 	
	 });
}

function popupCarte() {
	jQuery.get(
		BBVars.pageURL + "carte_fidelite/popup_carte.cfm",
		function(data) {BBTool_openMsgBox(data, true)}
	);
}

function initHelpCard() {
	jQuery("#help").bind("mouseenter", function(e) {
		jQuery("#CardHelp").stop().css({display: "block", opacity: 0}).animate({opacity: 1});
	}).bind("mouseleave", function(e) { 
		jQuery("#CardHelp").stop().animate({opacity: 0}, function() {jQuery(this).css("display","none");});
	});
}

function checkFidelity() {
	var cardNumber = jQuery("#fidelityCard").val();
	var cardLength = cardNumber.length;
	
	if(cardLength < 13) { 
		
		jQuery("#card_error").html(BBLabels.label_codeIncorrect);
		jQuery("#card_error").css({display: "block", opacity: 0}).animate({opacity: 1});
		setTimeout("jQuery('#card_error').stop().animate({opacity: 0}, function() {jQuery(this).css('display','none')})", 4000);
		
	} else {
	
		if(BBVars.isClient) {

			jQuery.get(
				BBVars.pageURL + "carte_fidelite/checkFidelity.cfm?cardNumber=" + cardNumber,
				function(data) { 
					jQuery("#card_error").html(data.replace(/^\s+/g,'').replace(/\s+$/g,''));
					jQuery("#card_error").stop().css({display: "block", opacity: 0}).animate({opacity: 1});
					setTimeout("jQuery('#card_error').stop().animate({opacity: 0}, function() {jQuery(this).css('display','none')})", 4000);
				}			
			);
			
		} else {
		
			BBLogin('fidelityCard',cardNumber);
			
		}
		
	}
}

function ReloadWindow() {
	jQuery("#BBTool_msgBoxCloseBtn").bind("click", function(e) { 
		location.reload();
	});
}

function callCardFrame(url) {
	var destination = jQuery("#account_content");
	var frame = "<iframe src='"+ BBVars.pageURL + url +"?frame' id='frame' frameborder='0' height='auto'></iframe>"
	destination.html(frame);
}
