/*
scripts
macharron@arteca.ca
Last edit: 2010-02-24
*/

// !js target http://www.456bereastreet.com/archive/200610/opening_new_windows_with_javascript_version_12/
var JSTarget={init:function(att,val,warning){if(document.getElementById&&document.createElement&&document.appendChild){var strAtt=((typeof att==='undefined')||(att===null))?'class':att;var strVal=((typeof val==='undefined')||(val===null))?'non-html':val;var strWarning=((typeof warning==='undefined')||(warning===null))?' (opens in a new window)':warning;var oWarning;var arrLinks=document.getElementsByTagName('a');var oLink;var oRegExp=new RegExp("(^|\\s)"+strVal+"(\\s|jQuery)");for(var i=0;i<arrLinks.length;i++){oLink=arrLinks[i];if((strAtt=='class')&&(oRegExp.test(oLink.className))||(oRegExp.test(oLink.getAttribute(strAtt)))){oWarning=document.createElement("em");oWarning.appendChild(document.createTextNode(strWarning));oLink.appendChild(oWarning);oLink.onclick=JSTarget.openWin;}oWarning=null;}}},openWin:function(e){var event=(!e)?window.event:e;if(event.shiftKey||event.altKey||event.ctrlKey||event.metaKey){return true;}else{var oWin=window.open(this.getAttribute('href'),'_blank');if(oWin){if(oWin.focus){oWin.focus();}return false;}oWin=null;return true;}},addEvent:function(obj,type,fn){if(obj.addEventListener){obj.addEventListener(type,fn,false);}else if(obj.attachEvent){obj["e"+type+fn]=fn;obj[type+fn]=function(){obj["e"+type+fn](window.event);};obj.attachEvent("on"+type,obj[type+fn]);}}};JSTarget.addEvent(window,'load',function(){JSTarget.init("rel","external","");});
 
/* ########################################################### */
/* !browsers */
var isIe = false;
var isOp = false;
var isSaf = false;
 
/* variable to know if IE */
if(jQuery.browser.msie) { 
    isIe = true;
}
/* variable to know if Opera */
if(jQuery.browser.opera) { 
    isOp = true;
}
/* variable to know if Safari */
if(jQuery.browser.safari) { 
    isSaf = true;
} 

/* ########################################################### */
/* !equalHeight */
var _equalHeight = {
	init: function(group){
		tallest = 0;
		group.each(function() {
			thisHeight = jQuery(this).height();
			if(thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		group.height(tallest);
	}
};

/* ########################################################### */
/* !home slider */
var _homeSlider = {
	init: function(){
		if(jQuery("#slider")[0]){
			setInterval(_homeSlider.change, 8000);
			_homeSlider.count = jQuery("#slider ul li").length;
			jQuery("#slider ul li:not(:eq(0))").hide();
		}
	},
	count : '',
	currentIndex: 1,
	nextIndex: 2,
	change: function(){
		
		var current = jQuery("#slider ul li:eq("+ (_homeSlider.currentIndex-1) +")");
		var next = jQuery("#slider ul li:eq("+ (_homeSlider.nextIndex-1) +")");
		
		current.fadeOut(2000);
		next.fadeIn(2000);
		
		if(_homeSlider.nextIndex+1 > _homeSlider.count){ _homeSlider.nextIndex = 1; }else{ _homeSlider.nextIndex += 1;}
		if(_homeSlider.currentIndex+1 > _homeSlider.count){ _homeSlider.currentIndex = 1; }else{ _homeSlider.currentIndex += 1;}
		
	}
};

/* ########################################################### */
/* !coll slider */
var _collSlider = {
	init: function(){
		if(jQuery("#coll-slider")[0]){
			setInterval(_collSlider.change, 6000);
			_collSlider.count = jQuery("#coll-slider ul li").length;
			jQuery("#coll-slider ul li:not(:eq(0))").hide();
		}
	},
	count : '',
	currentIndex: 1,
	nextIndex: 2,
	change: function(){
		
		var current = jQuery("#coll-slider ul li:eq("+ (_collSlider.currentIndex-1) +")");
		var next = jQuery("#coll-slider ul li:eq("+ (_collSlider.nextIndex-1) +")");
		
		current.fadeOut(2000);
		next.fadeIn(2000);
		
		if(_collSlider.nextIndex+1 > _collSlider.count) { _collSlider.nextIndex = 1; }else{ _collSlider.nextIndex += 1;}
		if(_collSlider.currentIndex+1 > _collSlider.count) { _collSlider.currentIndex = 1; }else{ _collSlider.currentIndex += 1;}
		
	}
};

/* ########################################################### */
/* !coll list */
var _collList = {
	init: function(){
		if(jQuery("#ls-coll-home")[0]){
			jQuery("#ls-coll-home li a").hover(this.hover, this.hoverOut);
		}
	},
	hoverEl: '<img src="/skin/frontend/anfibio/default/images/img-flocon2.png" alt="" class="flocon" style="display: none;" />',
	hoverElRyn: '<img src="/skin/frontend/anfibio/default/images/img-stars.png" alt="" class="flocon" style="display: none;" />',
	hover: function(){
	
		jQuery("#ls-coll-home li .flocon").stop(true, true);
		
		if(!jQuery(this).closest("li").find(".flocon")[0]){
			if(jQuery(this).closest("li").hasClass("coll-7")){
				jQuery(this).closest("li").prepend(_collList.hoverElRyn);
			}else{ 
				jQuery(this).closest("li").prepend(_collList.hoverEl);
			}
		}
		jQuery(this).closest("li").find(".flocon").fadeIn(1000);
	},
	hoverOut: function(){
		jQuery("#ls-coll-home li .flocon").stop(true, true);
		jQuery(this).closest("li").find(".flocon").fadeOut(1000);
	}
};

/* ########################################################### */
/* !code postal */
var _zip = {
	init: function(){		
		jQuery("#bt-search").click(function(){
			jQuery("#retailer-search").submit();
		});
		_zip.initVal = jQuery("#i-zip").val();
		
		jQuery("#i-zip").focus(this.change).blur(this.change);
	},
	change: function(){
		if(_zip.initVal == jQuery("#i-zip").attr("value")){ // remove label value
			jQuery("#i-zip").attr("value", "");
		}else if(jQuery("#i-zip").attr("value") === ""){ // put label value back
			jQuery("#i-zip").attr("value", _zip.initVal);
		}
	}
};

/* ########################################################### */
/* !fancybox */
var _fancy = {
	init: function(){
	
		function formatTitle(title, currentArray, currentIndex, currentOpts) {
				return '<span id="fancybox-title-over" class="clearfix"><span id="le-title">'+title +'</span><span id="numeros">Image ' +  (currentIndex + 1) + ' de ' + currentArray.length + '</span></span>';
			}
			
			function formatTitle2(title, currentArray, currentIndex, currentOpts) {
				jQuery(".bt-buy-pop").live("click",function(){
					jQuery(this).attr("href", jQuery("#ls-collection li:eq("+currentIndex+") a.bt-buy").attr("href"));
				});
				var la_class = "clearfix";
				if(jQuery("html").attr("lang") == 'en'){
					la_class = "clearfix en";
				}
				return '<span id="fancybox-title-over" class="'+la_class+'"><span id="le-title">'+title +' <a href="#" class="bt-buy-pop">'+ '' +'</a></span><span id="numeros">Image ' +  (currentIndex + 1) + ' de ' + currentArray.length + '</span></span>';
			}
			
		// collection
		if(jQuery("#ls-collection li.fancybox")[0] || jQuery("#ls-collection li.productDetails")[0]){
			
			jQuery('<span class="zoom"></span>').appendTo("#ls-collection li.fancybox a.product-image");
			jQuery("#ls-collection li.zoom a.product-image").attr("rel", "collection").fancybox({
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'overlayOpacity'	: '0.75',
				'overlayColor'		: '#000',
				'titlePosition'		: 'over',
				'titleFormat'		: formatTitle
			});
			
			jQuery("#ls-collection li.buy a.product-image").attr("rel", "collection").fancybox({
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'overlayOpacity'	: '0.75',
				'overlayColor'		: '#000',
				'titlePosition'		: 'over',
				'titleFormat'		: formatTitle2
			});
		}
		
		// boutique
		if(jQuery("#ls-miniatures.fancybox")[0]){
			
			jQuery("#bt-zoom a").click(function(){
				var newHref = jQuery("#ls-miniatures.fancybox li.selected a").attr("href");
				var newTitle = jQuery("#ls-miniatures.fancybox li.selected a").attr("title");
				
				jQuery(this).attr("href", newHref);
				jQuery(this).attr("title", newTitle);
			}).fancybox({
					'transitionIn'		: 'none',
					'transitionOut'		: 'none',
					'overlayOpacity'	: '0.75',
					'overlayColor'		: '#000',
					'titlePosition'		: 'over',
					'titleFormat'		: 'formatTitle'
				});
		}
	}
};

/* ########################################################### */
/* !fabrication */
var _fabrication = {
	init: function(){
		if(jQuery("#fab-flash")[0]){
			
			if(jQuery("#fab-flash").hasClass("en")){
				jQuery('#fab-flash').flash({
					swf: '/swf/fabrication-en.swf',
					width: '100%',
					height: '100%',
					flashvars: {}
				});
			}else{
				jQuery('#fab-flash').flash({
					swf: '/swf/fabrication.swf',
					width: '100%',
					height: '100%',
					flashvars: {}
				});
			}
			
		}
	}
};

/* ########################################################### */
/* !360 preview */
var _preview360 = {
	init: function(){
		if(jQuery("#panoramique")[0]){
			
			jQuery("#bt-360").click(_preview360.open);
			jQuery("#pano-close").click(_preview360.close);
			
			if(jQuery("#panoramique").attr("class") == "anf5545"){
				jQuery("#bt-360").hide();
			}
			
			var cacheBuster = "?t=" + Date.parse(new Date());
			var stageW = 540;
			var stageH = 360;
			
			jQuery('#preview').flash({
				swf: '/swf/preview.swf'+cacheBuster,
				width: '540',
				height: '360',
				bgcolor: '#ffffff',
				flashvars: {
					pathToFiles : "/swf/viewer/",
					xmlPath : "xml/"+ jQuery("#panoramique").attr("class") +".xml",
					componentWidth : stageW,
					componentHeight : stageH
				}
			});
		}
	},
	open: function(){
		//hack ie6
		if(isIe === true && jQuery.browser.version < 7){
			var margin = (jQuery(window).width() - jQuery("#wrapper").width())/2;
			jQuery("#backdrop").width(jQuery(window).width() + 'px').css("marginLeft", -margin);
		}
		
		jQuery("#backdrop").show().live('click', _preview360.close);
		jQuery("#panoramique").show();
		return false;

	},
	close: function(){
		jQuery("#backdrop, #panoramique").hide();
		return false;
	}
};

/* ########################################################### */
/* !mini */
var _mini = {
	init: function(){
		jQuery("#ls-miniatures li a").click(_mini.change);
		_mini.width = jQuery("#img-produit").width();
	},
	change: function(){
		if(!jQuery(this).closest("li").hasClass("selected")){
			jQuery("#ls-miniatures li").removeClass("selected");
			jQuery(this).closest("li").addClass("selected");
			
			var navSelected = jQuery("#ls-miniatures li.selected");
			var navIndex = jQuery("#ls-miniatures li").index(navSelected) + 1;
			
			var mouvement = (navIndex * _mini.width) - _mini.width;
			
			jQuery("#img-produit ul li").fadeOut("normal", function(){
				jQuery("#img-produit ul").animate({
					marginLeft: -mouvement
				}, 1, function(){
					jQuery("#img-produit ul li").fadeIn("normal");
				});
			});
		}
		return false;
	}
};

/* ########################################################### */
/* !color */
var _color = {
	init: function(){
		jQuery(".details-select select:eq(0).product-custom-option").change(_color.change);
	},
	change: function(){
		if(jQuery("option:selected", this).text() == "Black" || jQuery(this).text() == "Noir"){
			jQuery("#ls-miniatures img").each(function(){
				if(jQuery(this).attr("alt") == "bl" && !jQuery(this).closest("li").hasClass("selected")){
					jQuery(this).closest("a").trigger("click");
					return false;
				}else{
					if(!jQuery("#ls-miniatures li:eq(2)").hasClass("selected")){
						jQuery("#ls-miniatures li:eq(2) a").trigger("click");
					}
				}
			});
		}
		
		if(jQuery("option:selected", this).text() == "Brown" || jQuery(this).text() == "Brun"){
			jQuery("#ls-miniatures img").each(function(){
				if(jQuery(this).attr("alt") == "br" && !jQuery(this).closest("li").hasClass("selected")){
					jQuery(this).closest("a").trigger("click");
					return false;
				}else{
					if(!jQuery("#ls-miniatures li:eq(0)").hasClass("selected")){
						jQuery("#ls-miniatures li:eq(0) a").trigger("click");
					}
				}
			});
		}
	}
};

/* ########################################################### */
/* !widths */
var _widths = {
	init: function(){
		jQuery(".details-select select:eq(1).product-custom-option").change(_widths.change);
	},
	change: function(){
		if(jQuery("option:selected", this).text() == "19"){
			jQuery(".details-select select:eq(2).product-custom-option option:last").attr("disabled", "disabled");
		}else{
			jQuery(".details-select select:eq(2).product-custom-option option:last").attr("disabled", "");
		}
	}
};

/* ########################################################### */
/* !form submit */
var _frSubmit = {
	check: function(formId){
		
		var leForm = jQuery(formId);
		var fPrenom = jQuery("#prenom", leForm);
		var fNom = jQuery("#nom", leForm);
		var fEmail = jQuery("#email", leForm);
		var fMessage = jQuery("#message", leForm);
		var error = jQuery('<ul class="error"></ul>');
		
		if(jQuery(".error", leForm)[0]){
			jQuery(".error", leForm).remove();
		}
		
		var msgPrenom = "Please enter your first name";
		var msgNom = "Please enter your name";
		var msgEmail = "Please enter an email address";
		var msgEmail2 = "Please enter a valid email address";
		var msgMessage = "Please enter your message";
		var msgSuccess = "Thank you. The form was sent";
		var msgError = "An error occured. Please try again";
		
		if(jQuery("html").attr("lang") == 'fr'){
		
			msgPrenom = Translator.translate(msgPrenom);
			msgNom = Translator.translate(msgNom);
			msgEmail = Translator.translate(msgEmail);
			msgEmail2 = Translator.translate(msgEmail2);
			msgMessage = Translator.translate(msgMessage);
			msgSuccess = Translator.translate(msgSuccess);
			msgError = Translator.translate(msgError);
		}
		
		if(fPrenom.val() == "") {
			fPrenom.focus();
			fPrenom.select();
			error.append("<li>"+msgPrenom+".</li>");
		}
		if(fNom.val() == "") {
			fNom.focus();
			fNom.select();
			error.append("<li>"+msgNom+".</li>");
		}
		if(fEmail.val() == "") {
			fEmail.focus();
			fEmail.select();
			error.append("<li>"+msgEmail+".</li>");
		}
		if(!fEmail.val().match(/.+@.+\..+/)) {
			fEmail.focus();
			fEmail.select();
			error.append("<li>"+msgEmail2+".</li>");
		}
		if(fMessage.val() == "") {
			fMessage.focus();
			fMessage.select();
			error.append("<li>"+msgMessage+".</li>");
		}
		
		if(error.children("li")[0]){
			error.insertBefore("fieldset", leForm);
		}else {
			leForm.prev().show();
			leForm.hide();
			
			jQuery.ajax({	 
				type: "POST",	 
				url: "http://www.bottesanfibio.com/HiQFM/HiQFM.php",
				data: leForm.serialize(),	 
				success: function() {
					leForm.prev().hide();
					jQuery('<div class="success"><p>'+msgSuccess+'.</p><div>').insertBefore(leForm);
					leForm.remove();
				},
				error: function(){
					leForm.prev().hide();
					jQuery('<div class="error"><p>'+msgError+'.</p><div>').insertBefore(leForm);
				}
			}); 
		}
		
		return false;
	}
};

/* ########################################################### */
/* !ready fonction */
jQuery(document).ready(function(){

	_zip.init();
	_fancy.init();
	_fabrication.init();
	_preview360.init();
	_mini.init();
	_color.init();
	_widths.init();
	//_homeSlider.init();
	_collSlider.init();
	_collList.init();
	
	jQuery(window).load(function(){
		if(jQuery("#promo-boxes")[0]){
			_equalHeight.init(jQuery("#promo-boxes li p"));
		}
	});
});
