$(document).ready(function()
{
	var confirm_message = $('#var-confirm-message').text();
	var confirm_message_empty = $('#var-confirm-message-empty').text();
	var error_message = $('#var-error-message').text();
	var base_url = $('#var-base-url').text();
	var site_lang = $('#var-lang').text();

	function update_cart()
	{
    	//update the cart totals
		var shop_url = base_url + "products/ajax_cart" + '/1337' + Math.floor(Math.random()*9999999);
		$.getJSON( shop_url, function(cart_info)
		{
			$('#var-total-price span').text(cart_info.display_total_price);
			$('#var-delivery-costs span').text(cart_info.display_delivery_costs);
			$('#var-discount span').text(cart_info.display_discount);
			
			if( $('#var-discount').html().substr(0,1) != '-' && cart_info.discount > 0)
			{
				$('#var-discount').html(cart_info.discount_sign + ' ' + $('#var-discount').html());
			}
			$('#valid-discount-code').text(cart_info.discount_code);
			$('#valid-discount-code-description span').text(cart_info.discount_percentage);
			$('#var-final-price span').text(cart_info.display_final_price);
			if(cart_info.nr_of_products == 0)
			{
				$('#shopping-cart').hide();
				$('#shopping-cart-empty').slideDown();
			}

			if(cart_info.count_products == 0)
			{
				$('.cart-headers-products').hide();
			}

			if(cart_info.count_canvas == 0)
			{
				$('.cart-headers-canvas').hide();
			}
			
			//update the cart summary in the header
			$('#header-shop-nr-of-products span').text(cart_info.nr_of_products);
			$('#header-shop-final-price span').text(cart_info.display_final_price);
		});		
	}

    $('.cart-product-delete img').click(function(event)
    {
    	event.preventDefault();
    	if(confirm(confirm_message))
    	{
    		$(this).parent().parent().hide();
	    	var loader = $(this).parent().parent().parent().find('.cart-loading');
	    	loader.show();
	    	
    		//make it official with an ajax call
    		var id = $(this).parent().parent().parent().parent().attr('id');
    		var uri = base_url + 'products/ajax_delete/' + id + '/1337' + Math.floor(Math.random()*9999999);
			$.get(uri, function(data)
			{
				if(data == 'TRUE') 
				{
			    	$(document.getElementById(id)).fadeOut('slow', function()
			    	{
				    	//update the cart totals
				    	update_cart();
			    	});
				}
				else
				{
					alert(error_message);
					loader.find('img').attr('src', base_url + "img/layout/ajax/action_failed.png");
				}
			});
		}
	    return false;
    });
	
	$('.cart-canvas-delete img').click(function(event)
    {
    	event.preventDefault();
    	if(confirm(confirm_message))
    	{
    		$(this).parent().parent().hide();
	    	var loader = $(this).parent().parent().parent().find('.cart-loading');
	    	loader.show();
	    	
    		//make it official with an ajax call
    		var id = $(this).parent().parent().parent().parent().attr('id');
    		var ids = id.split('-');
    		var photo_id = ids[0];
    		var canvas_id = ids[1];
    		var uri = base_url + 'products/ajax_delete_canvas/' + photo_id + '/' + canvas_id + '/1337' + Math.floor(Math.random()*9999999);
			$.get(uri, function(data)
			{
				if(data == 'TRUE') 
				{
			    	$(document.getElementById(id)).fadeOut('slow', function()
			    	{
				    	//update the cart totals
				    	update_cart();
			    	});
				}
				else
				{
					alert(error_message);
					loader.find('img').attr('src', base_url + "img/layout/ajax/action_failed.png");
				}
			});
		}
	    return false;
    });
    
//    $('.cart-product-amount #ProductAmount').change(function(event)
//    {
//    	event.preventDefault();
//    	
//    	var product_amount = parseInt($(this).val());
//    	var product_price = $(this).parent().parent().find('.cart-product-price span').text().replace(',','.');
//    	var product_total_price = $(this).parent().parent().find('.cart-product-total-price span');
//    	
//    	var new_total = Math.round((product_price * product_amount)*100)/100;
//    	product_total_price.text(new_total);
//    	alert(product_amount + ' * euro ' + product_price + ' = ' + new_total );
//    	
//    	
//    	return false;
//    });

    $('.change-amount').click(function(event)
    {
    	event.preventDefault();
    	
    	$(this).css('cursor', 'wait');
    	$('#shopping-cart').css('cursor', 'wait');
    	
    	var current_id = $(this).parent().parent().parent().parent().attr('id');
    	var new_amount = $('#shopping-cart').find('#product-amount-'+current_id).attr('value');

		var uri = base_url + 'products/change_cart/' + current_id + '/' + new_amount + '/1337' + Math.floor(Math.random()*9999999);
		$.get(uri, function(data)
		{
			location.reload(true);
		});


    	//.cart-product-amount #ordersAmount
//    	var product_amount = parseInt($(this).val());
//    	var product_price = $(this).parent().parent().find('.cart-product-price span').text().replace(',','.');
//    	var product_total_price = $(this).parent().parent().find('.cart-product-total-price span');
//    	
//    	var new_total = Math.round((product_price * product_amount)*100)/100;
//    	product_total_price.text(new_total);
//    	alert(product_amount + ' * euro ' + product_price + ' = ' + new_total );
    	
    	return false;
    });
    
     $('.change-amount-canvas').click(function(event)
    {
    	event.preventDefault();
    	
    	$(this).css('cursor', 'wait');
    	$('#shopping-cart').css('cursor', 'wait');
    	
    	var current_id = $(this).parent().parent().parent().parent().attr('id');
		var ids = current_id.split('-');
		var photo_id = ids[0];
		var canvas_id = ids[1];
		
    	var new_amount = $('#shopping-cart').find('#canvas-amount-' + photo_id + '-' + canvas_id).attr('value');
		var uri = base_url + 'products/change_cart_canvas/' + photo_id + '/' + canvas_id + '/' + new_amount + '/1337' + Math.floor(Math.random()*9999999);
		$.get(uri, function(data)
		{
			location.reload(true);
		});


    	//.cart-product-amount #ordersAmount
//    	var product_amount = parseInt($(this).val());
//    	var product_price = $(this).parent().parent().find('.cart-product-price span').text().replace(',','.');
//    	var product_total_price = $(this).parent().parent().find('.cart-product-total-price span');
//    	
//    	var new_total = Math.round((product_price * product_amount)*100)/100;
//    	product_total_price.text(new_total);
//    	alert(product_amount + ' * euro ' + product_price + ' = ' + new_total );
    	
    	return false;
    });
    
    
    
    $('#empty-cart a').click(function(event)
    {
    	event.preventDefault();
    	if(confirm(confirm_message_empty))
    	{
    		//make it official with an ajax call
    		var uri = base_url + 'products/empty_cart/ajax' + '/1337' + Math.floor(Math.random()*9999999);
			$.get(uri, function(data)
			{
				if(data == 'SUCCESS') 
				{
					$('#shopping-cart').hide();
					$('#shopping-cart-empty').slideDown();

					//update the cart summary in the header
					var zero_money = '0.00';
					if(site_lang == 'nl') zero_money = '0,00';
					$('#header-shop-nr-of-products span').text('0');
					$('#header-shop-final-price span').text(zero_money);
				}
				else
				{
					alert(error_message);
				}
			});
		}
	    return false;
    });

    $('#check-discount').click(function(event)
    {
    	event.preventDefault();
    	var status_failed = $('#discount-failed'); status_failed.hide();
    	var status_success = $('#discount-success'); status_success.hide();
    	
    	//show a loader
    	var loader = $('#discount-loading'); loader.show();
    	
    	var entered_code = $('#discount-code').val();
    	
    	//check the code
		var uri = base_url + 'products/check_discount/' + entered_code + '/ajax' + '/1337' + Math.floor(Math.random()*9999999);
		$.get(uri, function(data)
		{
			if(data == 'INVALID') 
			{
				status_failed.show();
			}
			else
			{
				status_success.show();
				update_cart();
				$('#discount-code').fadeOut();
				$('#valid-discount-code').show();
				$('#valid-discount-code-description').show();
				$('#check-discount').hide();
				$('#remove-discount').show();
			}
			loader.hide();
		});
    	
    	return false;
    });
    
    $('#remove-discount').click(function(event)
    {
    	event.preventDefault();
    	var status_failed = $('#discount-failed'); status_failed.hide();
    	var status_success = $('#discount-success'); status_success.hide();
    	
    	//show a loader
    	var loader = $('#discount-loading'); loader.show();
    	
		var uri = base_url + 'products/remove_discount/ajax' + '/1337' + Math.floor(Math.random()*9999999);
		$.get(uri, function(data)
		{
			if(data == 'SUCCESS') 
			{
				update_cart();
				$('#remove-discount').hide();
				$('#valid-discount-code').hide();
				$('#valid-discount-code-description').hide();
				$('#discount-code').val("");//empty input field
				$('#discount-code').show();
				$('#check-discount').show();
				
			}
			loader.hide();
		});
    	
    	return false;
    });
});
