function do_option_costs() {

	$('.option_costs').remove();
	$(".opt_select").each(
	
	function(){
		 
		 var id = $(this).attr('id');
		 var cost = $("#"+id+' :selected').attr('rel');
		 
		// $('#option_cost').append('<p>'+id+' cost is&pound;'+cost+' </p>');
		 $('#product_form').append('<input type="hidden" value="'+cost+'" id="'+id+'" class="option_costs" />');
		 
	});
	
}


function update_costs(){
	
	var id = $(this).attr('id');
	var cost = $("#"+id+' :selected').attr('rel');
	var price = $("#price").html();
	
	do_option_costs();

	var ops = $(".option_costs").sum();
	
	
	$("#option_cost").html(parseFloat(ops).toFixed(2));
	
//		do_print_costs();
	var print = $(".print_costs").sum();
//		alert($(".print_costs").length());

	
	var totalcost = parseFloat(price) + parseFloat(ops) + parseFloat(print);
	$("#total_price").html(totalcost.toFixed(2));

}


function do_print_costs() {
	
	$('.print_costs').remove();
	
	$opts = '';
// alert (	$(".print_row:visible").length);	
	$(".print_row:visible").each(
			
		function(){
			var printCat = $(this).find('.print_cols').attr('rel');
			if (printCat === undefined) {
				printCat = $(this).find('.print_panels').attr('rel');
			}
			var numCols = $(this).find('.print_cols').val();
			var numPanels = $(this).find('.print_panels').val();
			if (!(numCols == 0 && numPanels == 0)) {
				if ($opts != '') {
					$opts += '-';
				}
				$opts += printCat + ':' + numCols + ':' + numPanels;
			}
		});
	
	if ($opts != '') {
//		alert($opts);
		var qty = $('#qty').val();
		var prodId = $('input[name=product_id]').val();
		$.getJSON('/shop/get_print_costs/' + $opts + '/' + prodId + '/' + qty, function(data) {
//				alert(data.printCost);
			$('#product_form').append('<input type="hidden" value="'+data.printCost+'" id="printCost" class="print_costs" />');
			update_costs();
		});
	} else {
		update_costs();
	}
}




$(document).ready(function() { 
	/*place jQuery actions here*/ 
	var link = "/index.php/";
	get_price();
	
	$("ul.products form").submit(function() {
		// Get the product ID and the quantity 
		var id = $(this).find('input[name=product_id]').val();
		var qty = $(this).find('input[name=quantity]').val();
		
		 $.post(link + "cart/add_cart_item", { product_id: id, quantity: qty, ajax: '1' },
  			function(data){
  			
  			if(data == 'true'){
    			
    			$.get(link + "cart/show_cart", function(cart) {
  					$("#cart_content").html(cart);
				});

    		}else{
    			alert("Product does not exist");
    		}	
    		
 		 }); 

		return false;
	});
	
	$(".empty").live("click", function(){
    	$.get(link + "cart/empty_cart", function() {
    		
    		window.location = window.location;
  				//$(".cart_list").html(cart);
			
		});
		
		return false;
    });

	do_option_costs();
	
	do_print_costs();

	$(".opt_select").live("change", function() {
    	
		update_costs();
		
	});
	
	$(".print_cols,.print_panels").live("change", function() {
    	
		do_print_costs();
		
	});
	
	
	$("#qty, #print_colours").live("change", function(){
		
		get_price();
	
	});

	function get_price(){	
		
		var qty = $('#qty').val();
		var id = $('[name=product_id]').val();
		var cols = $('#print_colours :selected').val();
	
		if(qty < 1){
		
			$(this).val(1);
			return false;
		}
		
		$.get("/shop/get_cost/"+id+"/"+qty+"/1", function(price) {
				
				var price = parseFloat(price);
				
				//$('input[name=product_price]').html(price);
				$('#price').html(price.toFixed(2));
				update_costs();
				
		});
	
	}	
			
	
});
