$(document).ready(function(){
	
	// search for products
	$("#searchProducts select").change(function () {
		productsSearch();
	});
	// show hide div by select value
	$(".JQshowDivBySelect").change(function () {
		showDivBySelectValue($(this).attr("value"));
	});
	// hide div by select value
	$("#JQactiveProductEditDetailsBySelect").change(function () {
		if ($(this).attr("value")==1){
			$('#productEditdetails').addClass('deactive');
		}
		else{
			$('#productEditdetails').removeClass('deactive');
		}
	});
	
	// show hide div bij radio value
	$(".JQshowDivByRadio").click(function () {
		showDivBySelectValue($(this).attr("value"));
	});
	// destroy search session after nav click
	$("#navigation ul li a").click(function () {
		searchDelete();
	});
	
	// plaats bestelling button disabled till accept conditions
	toggleOrderButton();
	$('#order_conditions, #order_age').change(function() {
		toggleOrderButton();
	});
	
	// link in new window. Doesnt work within content
	$("a[rel='external']").click(function(){
    	window.open($(this).attr('href'));
    	return false;
    });
    
   	// payment styles
    $("#payment_boxes div.box").hover(
		function () {
			$(this).addClass('hover');
		},
		function () {
			$(this).removeClass('hover');
		}
	);
	$("#payment_boxes div.box").click(function (e) {
		togglePaymentItem($(this).attr('id'));
	});
	
	cartActions();
	
});

/* Groeped functions */

function cartActions() {
	// edit cart
	$(".JQcartEdit").change(function () {
		cartEdit($(this).attr("id"));
	});
	// delete cart item
	$(".JQcartDelete").click(function () {
		cartDelete($(this).attr("id"));
		return false;
	});
}


/* Functions */

function togglePaymentItem(payment_id){
	$('#payment_boxes div.box#'+payment_id+' div.top input').attr('checked', true);
	$('#payment_boxes div.box').removeClass('active');
	$('#payment_boxes #'+payment_id).addClass('active');
	return false;
}

function toggleOrderButton() {
	if ($('#order_conditions').is(':checked') && $('#order_age').is(':checked')) {
		$('input#plaats_bestelling').attr('disabled', '');
		$('input#plaats_bestelling').removeClass('disabled');
	}
	else {
		$('input#plaats_bestelling').attr('disabled', 'disabled');
		$('input#plaats_bestelling').addClass('disabled');
	}
}


function productsSearch(){
	var values = "";
	var div = "#products";
	var action = "products/searchAjax";
	
	$(div).html("<div id=\"ajax_loading\"><img src=\""+webroot+"img/ajax-loader.gif\" /></div>");
	
	$("#searchProducts select").each(function () {
		values += $(this).attr("name") +"="+$("#searchProducts #"+$(this).attr("id")+" option:selected").attr("value")+"&";
	});
	
	$.ajax({
        type: "POST",
        url: webroot+action,
        data: values,
        error: function(msg){
           	$(div).html("Het is niet gelukt om de actie uit te voeren: "+msg);
        },
        success: function(msg){
            $(div).html(msg);
        }
    });
}

function searchDelete(){
	var action = "products/searchDelete";
	$.ajax({
        type: "POST",
        url: webroot+action
    });
}

function cartEdit(id) {
	var action = "cart/edit/"+id;
	var values = ""; // post values
	
	$('#JQcartBoxTotal').html("<img src=\""+webroot+"img/ajax-loader.gif\" />");
	
	$(".JQcartEdit").each(function () {
		values += 'Cart['+$(this).attr("id")+']' +"="+$("#"+$(this).attr("id")+" option:selected").attr("value")+"&";
	});
	
	$.ajax({
        type: "POST",
        url: webroot+action,
        data: values,
        error: function(msg){
           	$('#JQcartBoxTotal').html("Het is niet gelukt om de actie uit te voeren: "+msg);
        },
        success: function(msg){
        	var array = eval(msg);
            $('#JQcartBoxTotal').html(array[0]); // subtotaal
            $('#JQcartBoxdelivery').html(array[2]); // bezorgkosten
            $('#JQcartRowTotal_'+id).html(array[1]); // product totaal
        }
    });
}

function showDivBySelectValue(id) {
	$("div.JQhidden").hide();
	if (id!=''){
		$("div.JQhidden#"+id).fadeIn('fast');
	}
}

function cartDelete(id){
	var action = "cart/delete/";
	var values = ""; // post values
	var hideItem = id;
	
	$("#JQcartRow_"+hideItem+" td").animate(
		{"opacity": 0.0},'slow',"linear", function(){
			$("#JQcartRow_"+hideItem).hide();
			$("#JQcartRow_"+hideItem).html('');
		}
	);
	
	
	$.ajax({
        type: "POST",
        url: webroot+action+id,
        data: values,
        error: function(msg){
           	alert("Het is niet gelukt om de actie uit te voeren: "+msg);
        },
        success: function(msg){
        	var array = eval(msg);
            if (array[0] != 0) {
	            $('#JQcartBoxTotal').html(array[0]); // subtotaal
	            $('#JQcartBoxdelivery').html(array[1]); // bezorgkosten
    		}
    		else{
    			$("#JQcartBoxTotal").html("&euro 0,00");
    			$("#cartBoxContainer table").fadeOut("fast");
    			$("#cartBoxContainer").html("<p>Uw winkelwagen is leeg.</p>");
    		}
        }
    });
    
}

