$(document).ready(function() {
	// Verberg javascript warning
	$("#jswarning").hide();
	// Verberg submenu's
	$("#subnav").show();
	$("li > ul").hide();
	$(".currentSubMenu").parent().show().parent().show().parent().show();
	$(".currentSubMenu > a").css("color","#1a7884");

	$('ul > li.sub > a:has(span)').click(function() {
		$(this).parent().children(".sub").slideDown("fast");
		$(this).parent().siblings(".sub").children(".sub").slideUp("fast");
		return false;
	});

	//verberg alle loading beeldjes
	$('img.jq_loading').hide();

	$.get("../layout/code/contactformtoken.php", function(txt) {
		$("#contactForm > fieldset > ol").append("<li><input type='hidden' name='ts' value='" + txt + "' /></li>");
	});

	$('input[@class=aantal]').livequery('blur', function(event) {
		var id = $(event.target).attr("id").substr($(event.target).attr("id").lastIndexOf('_')+1);
		updatePrice(id);
	});

	updateTotal();
	checkEmpty();

	$('a.jq_add').click( function() {
		var id = this.id.substr(this.id.lastIndexOf('_')+1);
		var action = "add";
		var tabel = $('#tabel').text();
		var ref = $('#jq_ref_' + id).text();
		if($('#ap_' +id).size()) {
			$('#ap_' +id+ ' > td').animate( { backgroundColor: 'rgb(245,233,165)' }, 300).animate( { backgroundColor: 'white' }, 1000);
		} else {
			$.ajax({
				type: "GET",
				url: "index.php",
				data: "ID=" + id + "&action=" + action + "&tabel=" +tabel+ "&ajax=true",
				dataType: "json",
				beforeSend: function(){showLoading(id);},
				success: function(json){hideLoading(id),addCartRow(json),updatePrice(id);}
			});
		}
		return false;
		function showLoading(id) {
			$('#jq_add_loading_' + id).show();
			$('#jq_add_loading_' + id).css({ margin: "0 18px 0 20px" });
			$('#jq_add_' + id + ' span').hide();
		}
		function hideLoading(id) {
			$('#jq_add_loading_' + id).hide();
			$('#jq_add_' + id + ' span').show();
		}
		function addCartRow(json) {
			$('#mandje tr:last').before("<tr id='ap_" + json.ID + "' class='inhoud'><td colspan='2'>" + json.artikel + " (" + json.ref + ")</td><td><input class='aantal' type='text' id='aantal_" + json.ID + "' name='aantal_" + json.ID + "' value='1' /></td><td style='width: 40px' id='prijs_" + json.ID + "' class='prijs_" + json.prijs + "'><span class='optellen'>" + json.prijs + "</span></td><td><a href='#' class='jq_del' id='jq_del_" + json.ID + "'>Verwijder</a></td></tr>");
			$('img.jq_loading').hide();
			checkEmpty();
		}


	});


	$('a.jq_del').livequery('click', function(event) {
		var id = event.target.id.substr(event.target.id.lastIndexOf('_')+1);
		var action = "del";
		$.ajax({
			type: "GET",
			url: "index.php",
			data: "ID=" + id + "&action=" + action + "&ajax=true",
			beforeSend: function(){showLoading(id);},
			success: function(){hideLoading(id),hideCartRow(id);}
		});
		return false;

		function showLoading(id) {
			$('#jq_del_' + id).parent().empty().append("Bezig...");
		}
		function hideLoading(id) {
			$('#ap_' + id +' td:last').empty().append("OK!");
		}
		function hideCartRow(id) {
			//$('#prijs_' +id+ ' > span').empty().append(0);
			$('#ap_' + id).fadeOut("fast",function() {
				$('#ap_' + id).remove();
				checkEmpty();
				updateTotal();
			});

		}
	})



	function updatePrice(id) {
		var aantal = +$('#aantal_'+ id).val();
		var prijs = +$('#prijs_'+id).attr("class").substr($('#prijs_'+id).attr("class").lastIndexOf('_')+1);
		var subtotaal = aantal * prijs;
		var action = "updateAantal";
		$.ajax({
			type: "GET",
			url: "index.php",
			data: "ID=" + id + "&action=" + action + "&aantal=" + aantal + "&ajax=true",
			beforeSend: function(){showLoading(id);},
			success: function(){hideLoading(id);}
		});
		$('#prijs_' + id + ' > span').empty().append(subtotaal);
		updateTotal();

		function showLoading(id) {
			$('#jq_prijs_loading_' + id).show();
			$('#jq_prijs_loading_' + id).css({ margin: "0 2px 0 2px" });
			$('#prijs_' + id + ' > span').hide();
		}
		function hideLoading(id) {
			$('#jq_prijs_loading_' + id).hide();
			$('#prijs_' + id + ' > span').show();
		}
	}

	function updateTotal() {
		var k = 0;
		$(".optellen").each(function() {
			k = +k + ( +$(this).text() );
			k=Math.round(k*1000)/1000;
		});
		$('#prijs > strong').empty().append(k);
		$('#footer').prepend("<br class='clear ie6' />");
	};

	function checkEmpty() {
		if($('.inhoud').size()) {
			$('#leeg').hide();
			$('.nietleeg').show();
		} else {
			$('#leeg').show();
			$('.nietleeg').hide();
		}
	};
	
	$("#footer").prepend("<br class='clear ie6' />");

})

function externalLinks() {
	if (!document.getElementsByTagName) {
		return;
	}
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") &&
		anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}
	}
}
window.onload = externalLinks;