$(function(){
	initBox();
});

/* init plugins */
function initBox(){
	
	var color;
	if ( $("a", "#submitBtn").size() > 0 ){
		color = $("a", "#submitBtn").css("color");
	} else {
		color = $(".addArt").css("color");
	}
		
	var back;
	if ( $("a", "#submitBtn").size() > 0 ){
		back = $("a", "#submitBtn").css("backgroundColor");
	} else {
		back = $(".addArt").css("backgroundColor");
	}	

	$("a", "#submitBtn").hover(
      function () {
        $(this).css('color',back);
        $(this).css('backgroundColor',color);
      }, 
      function () {
        $(this).css('color',color);
        $(this).css('backgroundColor',back);
      }
    );
    
    $(".addArt").hover(
      function () {
        $(this).css('color',back);
        $(this).css('background',color);
      }, 
      function () {
        $(this).css('color',color);
        $(this).css('background',back);
      }
    );
    
    
	$("form[name=refreshForm]").bind("submit",function(){
		
		var myData = $(this).serialize();
		
		$.ajax({
			type: "POST",
			url: SITE_URL+"plugins/create_link/include/action_ajax_front.php?action=addProductSession",
			data: myData,
			success: function(result){
				if (!result.match("error","i") ){
					window.location.reload();
				} else {
					alert(result);
				}
			}
		 });
		 
		 return false;		
	});
	
}

function addProductBox(self, mode, idproduct, price, qte, type, name, url, panier, tva){
		
	var qte;
	var isGoodLot = false;
	
	if (mode == "ajout"){
		qte = "1";
		isGoodLot = true;
	}
	
	if (mode == "suppression"){
		isGoodLot = true;
	}
	
	/* gestion de prix par lot et/ou tranche */
	if (mode == "ajoutDyn")	{
		
		qte = $(self).find("input[name=quantite]").val();
		qte = (isNaN(qte))? 1: qte;

		var qteMin = $(self).find("input[name=mini]").val();
		var qteMaxi = $(self).find("input[name=maxi]").val();
		var operateur = $(self).find("input[name=operateur]").val();
		
		var isGoodLot = false;
		switch(operateur){
		
			case '<':
				if (qte < parseInt(qteMin))
					isGoodLot = true;
				else
					isGoodLot = false;
				break;
		
			case '<>':
				if ((qte >= parseInt(qteMin)) && (qte <= parseInt(qteMaxi)))
					isGoodLot = true;
				else
					isGoodLot = false;
				
				break;
		
			case '>':
				if (qte > parseInt(qteMin))
					isGoodLot = true;
				else
					isGoodLot = false;
				break;
				
			case '!':
				isGoodLot = true;
				break;
			
			default:
				isGoodLot = true;
				break;
		}
	}
		
	if (isGoodLot == true){
		
		/* box */
		$("#dialog").dialog({
			open: function(event, ui) { $(this).find("span.quantite").text(qte); $(this).find("span.prix").text(price); },
			bgiframe: true,
			resizable: false,
			height:160,
			width: 300,
			modal: true,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			buttons: {
				'Continuer': function() {
					$(this).dialog('close');
				},
				'Voir mon panier': function() {
					$(this).dialog('close');
					window.location.href = panier;
				}
			}
		}).css('display','block');
		
		/* requete */
		$.ajax({
			type: "GET",
			url: SITE_URL+"plugins/create_link/include/action_ajax_front.php?action=addProductSession",
			data: {"mode":mode,"l":idproduct,"p":price,"q":qte,"t":type,"n":name,"u":url,"tva":tva},
			success: function(result){
				
				if (!result.match("error","i") ){
					
					$("#tab-panier").find("span:first").text("x");
					
					if (mode == "suppression"){
						window.location.reload();
					}
					
				} else {
					alert(result);
				}
			}
		 });
		 
		
	} else{
		alert("Attention les quantités ne sont pas respectées !");
	}
	
	return false; 
}

