// initing functions

jQuery(document).ready(function(){
	if(jQuery(".productCarrousel").length > 0){
		setupCarousel();
	}
	jQuery(".tabsContent [id^=tab]").hide();
	jQuery(".productTabs .tabs li:first").addClass("currentItem");
	jQuery(".productTabs #tab1").show();
	jQuery(".productTabs .tabs a").bind("click", initTab);

	// find size
	jQuery(".findSize").bind("click", findSize);

	//quantity
	jQuery("[name=quantityPdt]").bind("focus", function(){
		jQuery(this).blur();
	});
	jQuery(".quantityBt a[@class^=quantity]").bind("click", productQuantity);

	//popin
	jQuery(".closePopin").bind("click", closePopin);
	jQuery(".cartBt .alertProduct").bind("click", function(){
		if (checkSize() != false)
			openPopin("alertPdt");
	});
	jQuery("#addCart").bind("click", checkSize);
	jQuery("#wishItem .badge").bind("click", function(){
		openPopin("confirmDelete");
	});
	jQuery(".addSelected").bind("click", checkWishListAddItems);
	jQuery("#submitEmail").bind("click", checkMail);
	jQuery(".cancelDelete").bind("click", function(){
		jQuery("#popin .closePopin").trigger("click");
		return false;
	});

	if (document.getElementById('displayMessageStockReminder') != null){
		openPopin("validAlert")
	}
	if (document.getElementById('displayErrorStockReminder') != null){
		openPopin("alreadyAddedAlert")
	}
	if (document.getElementById('checkCurrentVariant') != null){
		checkVariant()
	}

});


// check current variant
function checkVariant() {
   // select current sku on load
	try{
		if (document.getElementById('sku') != null){
			var currentSku = document.getElementById('sku').value;
			if (document.getElementById(currentSku) != null){
					box = eval(document.getElementById(currentSku));
					box.checked = true;
			}
		}
	}
	catch(err){}
}

function checkVariantBySku(sku) {
	if (document.getElementById(sku) != null){
		box = eval(document.getElementById(sku));
		box.checked = true;
	}
}

// carousel setup
function setupCarousel() {
    jQuery(".productCarrousel div").jCarouselLite({
        btnNext: ".productCarrousel .bottom",
        btnPrev: ".productCarrousel .top",
		vertical: true,
		speed: 600
    });
}

// init tabs
function initTab(){
	jQuery(".productTabs .tabs li").removeClass("currentItem");
	jQuery(".tabsContent [id^=tab]").hide();
	jQuery(this).parent().addClass("currentItem");
	jQuery(this.hash).fadeIn(600);
	jQuery(this).blur();
	return false;
}

// Size
function findSize() {
	openSizeWindow(this);
	return false;
}

// stocks
function checkStocks(sku) {
	var size = document.getElementById(sku);
	for(var key in stocks){
			box = eval(document.getElementById(key));
			box.checked = false;
		}
	box = eval (size);
	box.checked = true;
	document.getElementById('sku').value = sku;
	document.getElementById('skuReminder').value = sku;
	document.getElementById('skuWishList').value = sku;

	//disable add to cart button && display loading text
	document.getElementById('displayAdd').style.display = 'none';
	document.getElementById('displayLoad').style.display = 'block';
	
	
	/*if (stocks[sku] == "true"){
		document.getElementById('displayAdd').style.display = 'block';
		document.getElementById('displayEmpty').style.display = 'none';
	}
	else{
		document.getElementById('displayAdd').style.display = 'none';
		document.getElementById('displayEmpty').style.display = 'block';
	}*/
}

function checkSize() {
	if (document.getElementById('isUniqueTaille') == null){
		var inputs = jQuery(".taille input:checked");
		if (inputs.length == 0) {
			inputs = jQuery(".pdtSize ul input:checked");
		}
		if(inputs.length < 1) {
			openPopin("showSize");
			return false;
		}
	}
}

function checkWishListAddItems() {
	var inputs = jQuery("#wishListPreferred ul input:checked");
	if(inputs.length < 1) {
		openPopin("productSelect");
		return false;
	}
}
// overriden on product page (jsp) for dynamic values
// Handling Product quantity //
function productQuantityElement(element) {	
	var quantityInput = jQuery(element).parent().siblings("input");	
	var num = 1;	
	if(jQuery(element).attr("class") == "quantityMore") {
		var moreQuantity = parseInt(quantityInput.val(), 0) + parseInt(num, 0);		
		if (moreQuantity <= 4){			
			quantityInput.attr("value", moreQuantity);	
		}
	}else if(jQuery(element).attr("class") == "quantityLess"){
		var lessQuantity = parseInt(quantityInput.val(), 0) - parseInt(num, 0);		
		if(parseInt(lessQuantity) >= 1){
			quantityInput.attr("value", lessQuantity);
			jQuery(element).blur();
			return false;
		}else{
			jQuery(element).unbind("click",function(){
				jQuery(element).blur();
				return false;
			});
		}
	}
	jQuery(element).blur();
	return false;
}

function productQuantity() {
	var quantityInput = jQuery(this).parent().siblings("input");
	var num = 1;
	if(jQuery(this).attr("class") == "quantityMore") {
		var moreQuantity = parseInt(quantityInput.val(), 0) + parseInt(num, 0);
		if (moreQuantity <= 4){
			quantityInput.attr("value", moreQuantity);
		}
	}else if(jQuery(this).attr("class") == "quantityLess"){
		var lessQuantity = parseInt(quantityInput.val(), 0) - parseInt(num, 0);
		if(parseInt(lessQuantity) >= 1){
			quantityInput.attr("value", lessQuantity);
			jQuery(this).blur();
			return false;
		}else{
			jQuery(this).unbind("click",function(){
				jQuery(this).blur();
				return false;
			});
		}
	}
	jQuery(this).blur();
	return false;
}



// param is class to add to popin to display right content
function openPopin(param) {
	jQuery("#popin").addClass(param);
	jQuery("#productFlash").css("visibility", "hidden");
	jQuery("#popin").show();
	var htmlWidth = jQuery("html")[0].scrollWidth;
	var htmlHeight = jQuery("html")[0].scrollHeight;
	var bodyHeight = jQuery("body")[0].scrollHeight;
	//alert('html:'+htmlHeight +'body:' + bodyHeight)
	
	jQuery("#popin .bgPopin").height(htmlHeight );
	jQuery("#popin").width(htmlWidth );
	
	if(htmlHeight > bodyHeight) {
		jQuery("#popin .bgPopin").height(htmlHeight );
	}else if(htmlHeight < bodyHeight) {
		jQuery("#popin .bgPopin").height(bodyHeight);
	}
}

function closePopin() {
	jQuery("#popin").hide();
	jQuery("#productFlash").css("visibility", "visible");
	jQuery("#popin").removeClass();
	jQuery(".errorEmail").hide();
	return false;
}


// checking mail
function checkMail() {
	var regexp = new RegExp(/^([a-zA-Z0-9]+(([\.\-\_]?[a-zA-Z0-9]+)+)?)\@(([a-zA-Z0-9]+[\.\-\_])+[a-zA-Z]{2,4})$/);
	if(!jQuery("#alert .email").val().match(regexp)){
		jQuery(".errorEmail").show();
		return false;
	}else{
		jQuery(".errorEmail").hide();
	}
}

// open alert popin
function openAlertPopin(){	
	if (checkSize() != false) {
		$("#grise").hide();
		$("#grise").css({'height':$("body").height()+"px",'top':'0px','left':'0px'});
		$("#pop_quickview_product").hide();
		document.getElementById('pop_quickview_stock_reminder').style.display = 'block';
		jQuery("#pop_quickview_stock_reminder").removeClass();
		jQuery("#pop_quickview").removeClass();
		jQuery("#popin").addClass("alertPdt");

		$("#grise").show();
		$("#pop_quickview_stock_reminder").show();
		$("#popin").show();

		//openPopin("alertPdt");
	}
}

// refresh page
function refresh() {
	var newLocation = window.location.href.split("#");
	window.location.href = newLocation[0];
}
