function mixOverzicht(pagina) {
	$('#laadmixoverzicht').html('<div style="width: 220px; height: 30px; float: left; margin-top: 20px; text-align: center;"><img src="../img/laden.gif" /></div>');
	$("#laadmixoverzicht").load(pagina, function(){
		window.setTimeout(function() {
			$("#laadmixoverzicht").load("mix_inhoud.php?ms=" + new Date().getTime());
		}, 600);
	});
}

function mixProducten(tab) {
	$('#scrollwrapper').html('<div id="laden" style="width: 630px; float: left;	height: 20px; margin-top: 80px;	text-align: center;"><img src="../img/laden.gif" /></div>');
	$("#scrollwrapper").load(tab);
}

function mixInfo(info) {
	$('#productinfo').html('<div class="margin10"><img src="../img/laden.gif" /></div>');
	$("#productinfo").load(info);
}

function updatePrijs(){
	//$('#totaalbedrag').html('<img src="../img/laden.gif" />');
	//$("#totaalbedrag").load("mix_totaalbedrag.php?ms=" + new Date().getTime());
}

$(document).ready(function() {	

	var options = { path: '/', expires: 1 };


	mixOverzicht("mix_inhoud.php?ms=" + new Date().getTime());
	//updatePrijs();
	
	//mixInfo("mix_info.php");
	
	$("#melding, #kortingerror").hide();
	$("#melding, #kortingerror").fadeIn(300, function(){
		$("#melding, #kortingerror").fadeOut(300, function(){
			$("#melding, #kortingerror").fadeIn(300, function(){
				$("#melding, #kortingerror").fadeOut(300, function(){
					$("#melding, #kortingerror").fadeIn(300);
				});
			});
		});
	});
	
	var kortinggeklikt = 0;
	$("#inputkorting").click(function() {
		if(kortinggeklikt == 0){
			$(this).val("");
			kortinggeklikt = 1;
		} 
	});
	
	$("#ikhebeenkortingscode").click(function() {
		$("#kortingblok1").hide();
		$("#kortingblok2").fadeIn(400);
	});
	
	/* tabs =================================== */
	$(".tab.product").click(function() {
		$('#scrollwrapper').trigger( 'goto', [ 0 ] );
		$('#scrollwrapper').html('<div id="laden" style="width: 630px; float: left;	height: 20px; margin-top: 80px;	text-align: center;"><img src="../img/laden.gif" /></div>');
		var productcategorie = $(this).attr('name');
		$("#scrollwrapper").load("mix_producten.php?cat="+productcategorie);
	});
	
	$(".pr").livequery('click', function() { 
		$(".pr.aan").removeClass("aan");
		$(this).addClass("aan");
		var gekozenproduct = $(this).attr('id');		
		mixInfo("mix_info.php?p="+gekozenproduct);
	});
	
	$(".pr_voegtoe, .voegtoe").livequery('click', function() { 
		var toevoegen = $(this).attr('id');
		var gewicht = $(this).attr('alt');
		var vol = 0;
		
		var toevoegenid = toevoegen.split("_");
		var basis = $(this).attr('name');
		if((basis == 1)){
			if($.cookie("mmm_mix1")){
				if($.cookie("mmm_mix1").length > 9){
					if(confirm("Let op! Door je basis nu te vervangen wordt de rest van je mix verwijderd.")){
						$.cookie("mmm_mix1", toevoegenid[1]+"_", options);
						mixOverzicht("mix_inhoud.php?ms=" + new Date().getTime());
					}
				} else {
					alert("Je hebt al een basis gekozen. Je basis zal worden vervangen.");
					$.cookie("mmm_mix1", toevoegenid[1]+"_", options);
					mixOverzicht("mix_inhoud.php?ms=" + new Date().getTime()); 
				}
			} else {
				$.cookie("mmm_mix1", $.cookie("mmm_mix1")+"_"+toevoegenid[1]+"_", options);
				mixOverzicht("mix_inhoud.php?ms=" + new Date().getTime()); // schattige nooit-hetzelfde url
			}
		} else {
			// gewicht updaten
			var nieuwgewicht = Number($.cookie("mmm_gewicht"))+Number(gewicht);
			
			var waarschuwing = "Sorry, dit past er niet meer bij.\nVerwijder een of meer producten en probeer het opnieuw.";
			if($.cookie("mmm_huidigemix") == 1 && $.cookie("mmm_huidigezak") == 1){
				if(nieuwgewicht > 400){
					alert(waarschuwing);
					vol = 1;
				}
			}
			if($.cookie("mmm_huidigemix") == 1 && $.cookie("mmm_huidigezak") == 2){
				if(nieuwgewicht > 650){
					alert(waarschuwing);
					vol = 1;
				}
			}
			if($.cookie("mmm_huidigemix") == 2 && $.cookie("mmm_huidigezak") == 1){
				if(nieuwgewicht > 450){
					alert(waarschuwing);
					vol = 1;
				}
			}
			if($.cookie("mmm_huidigemix") == 2 && $.cookie("mmm_huidigezak") == 2){
				if(nieuwgewicht > 750){
					alert(waarschuwing);
					vol = 1;
				}
			}
			if($.cookie("mmm_huidigemix") == 3 && $.cookie("mmm_huidigezak") == 1){
				if(nieuwgewicht > 450){
					alert(waarschuwing);
					vol = 1;
				}
			}
			if($.cookie("mmm_huidigemix") == 3 && $.cookie("mmm_huidigezak") == 2){
				if(nieuwgewicht > 1000){
					alert(waarschuwing);
					vol = 1;
				}
			}
			// cookie met producten
			if(vol == 0){
				$.cookie("mmm_mix1", $.cookie("mmm_mix1")+"_"+toevoegenid[1]+"_", options);
				$.cookie("mmm_gewicht", nieuwgewicht, options);
				mixOverzicht("mix_inhoud.php?ms=" + new Date().getTime());
			}
			
		}
	});
	
	$(".lekkermet_pr").livequery('click', function() { 
		$(".pr.aan").removeClass("aan");
		var gekozenlekkermet = $(this).attr('id');
		mixInfo("mix_info.php?p="+gekozenlekkermet);
	});

	$(".inhoudverwijder").livequery('click', function() { 
		var verwijder = $(this).attr('id');
		if(confirm("Weet je zeker dat je dit ingredi\u00EBnt wilt verwijderen?\nDit kan niet ongedaan worden gemaakt.")){
			mixOverzicht("mix_inhoud.php?verwijder="+verwijder);
		}
	});
	
	$("#volgendestap.plaatsen").click(function() {
		if(confirm("Je kunt je mix niet meer aanpassen nadat je de bestelling hebt geplaatst.\nKlik op OK om verder te gaan of op Annuleren om je mix nog aan te passen.")){
			
		} else {
			return false;
		}
	});
	
	$("#volgendestap.betaling").click(function() {
		if($.cookie("mmm_acceptatie") == 1){
			
		} else {
			alert("Je heb de voorwaarden van MixMyMix niet geaccepteerd.");
			return false;
		};
	});
	
	$('.pr').livequery(function(){ 
        $(this).hover(function() { 
			$(this).children().show();
		}, function() { 
			$(this).children().hide();
		}); 
    }, function() { 
        $(this).unbind('mouseover').unbind('mouseout'); 
    }); 

	$(".knop_meerinfo").livequery('click', function() { 
		$(".productinfo_tekst").toggle();
		$(".knop_meerinfo").toggle();
	});
	
	$('#voorwaardenpopup').click(function(){
		var link=$(this).attr('href');
		window.open(link,'CSV bestand bekijken','width=660,height=450,scrollbars=yes,toolbar=no,location=no,resize=no'); return false
		return false;
	});
	
	// extra opties opslaan
	$("#geefeennaamopslaan").click(function () {
        var opslaan_naam = $("#inputmixnaam").val();
		opslaan_naam = opslaan_naam.replace(/['"]/g,'');
		$("#inputmixnaam").val(opslaan_naam);
		$.cookie('mmm_naam', opslaan_naam, options);
		if(opslaan_naam == "" || opslaan_naam == " "){
			$("#aanwijzingnaam").html("<strong class='bevestiging'>Er wordt nu de standaardnaam geprint.</strong>");
		} else {
			$("#aanwijzingnaam").html("<strong class='bevestiging'>De naam <em style='color: #726602'>"+opslaan_naam+"</em> is opgeslagen!</strong>");
			mixOverzicht("mix_inhoud.php?naam="+opslaan_naam+"&ms=" + new Date().getTime());
		};
    });
	
	$("#geefeentekstopslaan").click(function () {
        var opslaan_tekst = $("#inputmixtekst").val();
		opslaan_tekst = opslaan_tekst.replace(/['"]/g,'');
		$("#inputmixtekst").val(opslaan_tekst);
		$.cookie('mmm_tekst', opslaan_tekst, options);
		if(opslaan_tekst == "" || opslaan_tekst == " "){
			$("#aanwijzingtekst").html("<strong class='bevestiging'>Er wordt nu geen tekst geprint.</strong>");
		} else {
			$("#aanwijzingtekst").html("<strong class='bevestiging'>De tekst <em style='color: #726602'>"+opslaan_tekst+"</em> is opgeslagen!</strong>");
			mixOverzicht("mix_inhoud.php?tekst="+opslaan_tekst+"&ms=" + new Date().getTime());
		};
    });

	$(".keuzeverpakking").click(function () {
		var kleurverpakking = $(this).attr('id');
		$.cookie('mmm_kleur', kleurverpakking, options);
		$(".keuzeverpakking.aan").removeClass("aan");
		$(this).addClass("aan");
		$("#kleurbevestiging").html("<strong class='bevestiging'>Je verpakking wordt nu "+kleurverpakking+"!</strong>");
		mixOverzicht("mix_inhoud.php?verpakking="+kleurverpakking+"&ms=" + new Date().getTime());
    });
	
	
	
	
	// overige
	
	$("#acceptatie").click(function () {	
		if ($(this).is(':checked')) {
			$("#acceptatiedik").css({fontWeight:"bold"});
			$.cookie('mmm_acceptatie', '1', options);
		} else {
			$("#acceptatiedik").css({fontWeight:"normal"});
			$.cookie('mmm_acceptatie', '0', options);
		}
    });
	
	$("#knopafleveradres").click(function () {
		$("#afleveradres1").hide();
		$("#afleveradres2").show();
		$("#aflnaam").focus();
    });
	
	$("#annulerenadres").click(function () {
		$("#afleveradres2").hide();
		$("#afleveradres1").show();
    });
	
	$("#select_aantal1, #select_aantal2, #select_aantal3").change(function(){
		var aantal = $(this).val();
		var idx = $(this).attr('alt');
		$(this).hide();
		$(this).parent().html("<img src='../img/laden.gif' alt='...' title='' style='float: right;' />");
		var wijzigaantal = "_wijzigaantal.php?wijzigaantal="+aantal+"&x="+idx;
		document.location = wijzigaantal;		
	});
	
	
	$("#forminloggen").each( function(){

		$(this).validate({
			rules: {
				inlognaam: {
					required: true,
					minlength: 4,
					maxlength: 20
				},
				ww3: {
					required: true,
					minlength: 5,
					maxlength: 15
				}
			},
			messages: {
				inlognaam: {
					required: "Vul je gebruikersnaam in.",
					minlength: "De gebruikersnaam moet bestaan uit minimaal 4 tekens.",
					maxlength: "De gebruikersnaam mag bestaan uit maximaal 20 tekens."
				},
				ww3:  {
					required: "Vul een geldig wachtwoord in.",
					minlength: "Het wachtwoord moet bestaan uit minimaal 5 tekens.",
					maxlength: "Het wachtwoord mag bestaan uit maximaal 15 tekens."
				}
			},
			errorPlacement: function(error, element) {
				element.parent().next().next().html(error);
			}
		});
	});
	
	
	
	$(".voedingswaarden_onzemixen").click(function () {
		$(this).hide();
		$(this).next().show();
		$(this).parent().parent().parent().parent().find("p.inhoud").hide();
		$(this).parent().parent().parent().parent().find("p.inhoud2").fadeIn(800);
    });
	
	
	$(".mixinhoud_onzemixen").click(function () {
		$(this).hide();
		$(this).prev().show();
		$(this).parent().parent().parent().parent().find("p.inhoud").fadeIn(800);
		$(this).parent().parent().parent().parent().find("p.inhoud2").hide();
    });
	
	
	
	
	
	
});
