jQuery(document).ready(function() {
    jQuery("#productController").jFlow({
        slides: "#productSlides",  // the div where all your sliding divs are nested in
        controller: ".jFlowControl2", // must be class, use . sign
        slideWrapper : "#jFlowSlide2", // must be id, use # sign
        selectedWrapper: "jFlowSelected",  // just pure text, no sign
        width: "176px",  // this is the width for the content-slider
        height: "168px",  // this is the height for the content-slider
        duration: 400,  // time in miliseconds to transition one slide
        prev: ".jFlowPrev2", // must be class, use . sign
        next: ".jFlowNext2" // must be class, use . sign
    });

     var options = {
         beforeSubmit:  validateAdd,  // pre-submit callback
         success:       afterAdd,  // post-submit callback
         dataType : 'json'
    };

     // bind form using 'ajaxForm'
     jQuery('#frmProduct').ajaxForm(options);
});
//var prod_id = {$product_id};

function getSizesForProductAndColor(color_id){
	var prod_id = jQuery('#pid').val();
	var objSelect = document.getElementById("sizes");	
	objSelect.options.length = 0;
	if(parseInt(color_id) == 0){
		objSelect.options[0] = new Option("Taille",0);
		jQuery("#divDisponibilite").slideUp();
		jQuery("#actionButtons").fadeOut();
		jQuery("#product_prix").hide();
		jQuery("#price_start").show();
		return false;
	}
	jQuery.getJSON("request.php",{method:0,prod_id:prod_id,color_id:color_id},
		function(json){						
			if(typeof(json.items) != "undefined" && typeof(json)!= "undefined"){
      			jQuery.each(json.items,
	     			function(i,item){
      					objSelect.options[i] = new Option(item.size_value, item.size_id);
      				});
			}
			changeColorImage(jQuery('#colors option:selected').val());
			//getDataForProductWithColorAndStock(jQuery("#sizes").val());
			getDateheureForProductAndColorAndSizes(jQuery("#sizes").val());
		});
}
function getDateheureForProductAndColorAndSizes(size_id){
	//alert("size_id"+size_id);
	var prod_id = jQuery('#pid').val();
	var color_id = jQuery('#colors').val();
	var objSelect = document.getElementById("dateheure");	
	objSelect.options.length = 0;
	if(parseInt(color_id) == 0){
		objSelect.options[0] = new Option("Date et heure de livraison",0);
		jQuery("#divDisponibilite").slideUp();
		jQuery("#actionButtons").fadeOut();
		jQuery("#product_prix").hide();
		jQuery("#price_start").show();
		return false;
	}
	jQuery.getJSON("request.php",{method:3,prod_id:prod_id,color_id:color_id,size_id:size_id},
		function(json){						
			if(typeof(json.items) != "undefined" && typeof(json)!= "undefined"){
      			jQuery.each(json.items,
	     			function(i,item){
      					objSelect.options[i] = new Option(item.dateheure_value, item.dateheure_id);
      				});
			}
			changeColorImage(jQuery('#colors option:selected').val());
			getDataForProductWithColorAndStock(jQuery("#dateheure").val());
		});
}
function getDataForProductWithColorAndStock(dateheure_id){
	//alert("dateheure"+dateheure_id);
	var color_id = jQuery('#colors').val();
	var size_id = jQuery('#sizes').val();
	var finalPrice = 0;
	var prod_id = jQuery('#pid').val();

	jQuery.getJSON("request.php",{method:1,prod_id:prod_id,color_id:color_id,size_id:size_id,dateheure_id:dateheure_id},			
			function(json){
				jQuery("#price-new").html("");
				jQuery("#price-old").html("");
				if(json.pricepromo != "undefined" && parseFloat(json.pricepromo) > 0){
				    jQuery("#info-price-old").show();
                    // jQuery("#price").html(json.pricepromo+"&nbsp;&euro;"+"<del>"+json.price+"&euro;</del>");
                    jQuery("#price-new").html(json.pricepromo);
                    jQuery("#price-old").html("<del>"+json.price+"&nbsp;&euro;</del>");
                    finalPrice = parseFloat(json.pricepromo);
				}else{
				    jQuery("#info-price-old").hide();
					  jQuery("#price-new").html(json.price);
					  finalPrice = parseFloat(json.price);
				}
				if(finalPrice <= 0 || parseInt(json.stock) <= 0){
				   jQuery("#actionButtons").fadeOut();
				  	jQuery("#divDisponibilite").slideDown();
				}else{
				 jQuery("#actionButtons").fadeIn();
					jQuery("#divDisponibilite").slideUp();
				}
				if(finalPrice > 0){
			  		jQuery("#product_prix").show();
			  		jQuery("#price_start").hide();
			  	}
			}
	);
}
function validateAdd(){

  	var nQty = jQuery("#quantity").val();
  	var txtErr = "";
  	if(jQuery("#colors").val() == "0"){
  		txtErr += "Vous devez saisir une couleur !" + "<br/>";
  	}
  	if(jQuery("#sizes").val() == "0"){
  		txtErr += "Vous devez saisir une taille !" + "<br/>";
  	}
  	if(isNumber(nQty) == false || nQty < 1){
  		txtErr += "La quantit&eacute; doit &ecirc;tre un num&eacute;ro !" + "<br />";
  	}
    if (jQuery("#country").val() == "0"){
  		txtErr += "Vous devez saisir le lieu de livraison !" + "<br />";
    }
  	if (txtErr != ""){
     	 jError(txtErr, "Erreurs");
     	 return false;
  	}
  	return true;
}
function afterAdd(json){
	if(typeof(json) != "undefined" && json != null){
		if(typeof(json.message) != "undefined" && json.message != null){
			jAlert(json.message,json.messagetitle,function(){});
		}
		if(typeof(json.total) != "undefined" && json.total != null && parseFloat(json.total) != Number.NaN){
			jQuery("#cartTotal").html(json.total+"&nbsp;&euro;");
		}
		if(typeof(json.items) != "undefined" && json.items != null && parseInt(json.items) != Number.NaN){
			jQuery("#cartArticles").html(json.items);
		}
	}
	 //alert(json);
}
function serializeToJSON(strTxt){
    var strJSON = "{";
    arrTmp = strTxt.split("&");
    for(i =  0; i < arrTmp.length; i++){
       arrTmp2 = arrTmp[i].split("=");
       if (arrTmp2[0] == "quantity")
            continue;

       if (strJSON!="{")
           strJSON+=","
       strJSON +="'"+arrTmp2[0]+"':'"+arrTmp2[1]+"'";
    }
    strJSON+="}";
      return strJSON;
}

function changeColorImage(colorname){
    if(colorname!=""){
        jQuery("a[name='"+colorname+"']").mouseover();
    }
}


