jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

function recalc()
{
	var rws = $('#book')[0].getElementsByTagName('tr');
	var tot = 0.0;
	for (i = 0; i < rws.length - 1; i++)
	{
		rws[i].getElementsByTagName('span')[1].innerHTML =  (rws[i].getElementsByTagName('span')[0].innerHTML * parseInt(rws[i].getElementsByTagName('select')[0].value)).toFixed(2);
		tot += (rws[i].getElementsByTagName('span')[0].innerHTML * parseInt(rws[i].getElementsByTagName('select')[0].value));
	}
	$('#tot span').html(tot.toFixed(2));
}

$(document).ready(function()
{
	$.post("/engine.php?func=addcart", {id: 0, tid:$('body')[0].className}, function(html)
	{
		$('#mijn').html(html);
	});	

	$('h1').click(function()
	{
		document.location.href = '/';
		return false;
	});
	
	$('.item').click(function()
	{
		document.location.href = $(this)[0].getElementsByTagName('a')[0].href;
		return false;
	});
	
	$('.item').mouseover(function()
	{
		$(this).addClass('itemover');
	});
	
	$('.item').mouseout(function()
	{
		$(this).removeClass('itemover');
	});
	
	
	$('#boek').click(function()
	{
		$.post("/engine.php?func=addcart", {id: $('#boek')[0].className, tid:$('body')[0].className}, function(html)
		{
			$('#mijn').html(html);
		});
		
		return false;	
	});
	
	$('.del a').live("click", function()
	{
		if ($('#book')[0])
		{
			$('#book tr.' + this.className).remove();
			recalc();
		}

		$.post("/engine.php?func=addcart", {did: this.className, tid:$('body')[0].className}, function(html)
		{
			$('#mijn').html(html);
		});	
		return false;
	});
	
	$('#checkout').live('click', function()
	{
		$('#lister').load("/engine.php?func=checkout");
		return false;	
	});
	
	$('#payed').click(function()
	{
		return confirm('Weet je zeker dat deze boeking betaald is?');
	});
	
});
