	$(document).ready(function() {
	
		
		$('a.facebox').facebox();
		
		
		$('input.add-button').click(function(){
		
			var this_id =  this.id.split('-');
			var id = parseInt(this_id[1]);
			var print_option_id = parseInt(this_id[2]);
			var print_option_item_id = $('#PrintOptionItemId' + id + ' option:selected').val();
			
			if(print_option_item_id == 0){
				alert('Please select an option');
				return false;
			}
			
			/* get the highest k value */
			
			var k = $('#option-holder-' + id + ' input.print_option_count:last').val();
			
			if(k === undefined){
				k = 0;
			} else {
				k++;
			}		
			
			var str = '';
			str = str + '<div class="print_option_list" id="print_option_item_list-' + id + '-' + print_option_item_id + '">';
			str = str + '<a class="remove-link" href="#" id="remove-' + id + '-' + print_option_item_id + '">remove</a>';
			str = str + '<span>';
			str = str + '<strong>' + p[print_option_id][print_option_item_id]['title'] + '</strong>';
			str = str + '</span>';
			str = str + '<span  class="quantity-holder">';
			str = str + '<label for="quantity-' + id + '-' + print_option_item_id + '">Qty</label>';
			str = str + '<input type="text" class="mini-input quantity" value="1" name="data[OrderItem][k' + k + '][' + print_option_item_id + '][qty]" id="quantity-' + id + '-' + print_option_item_id + '" />';
			
			
			/*
			 
			if(p[print_option_id][print_option_item_id]['allow_borders'] == 1){
				str = str + '<label for="borders-' + id + '-' + print_option_item_id + '">Borders</label>';
				str = str + '<input class="mini-input print_options" type="checkbox"  value="1" name="data[OrderItem][k' + k + '][' + print_option_item_id + '][is_border]" id="borders-' + id + '-' + print_option_item_id + '" />';
			}
			if(p[print_option_id][print_option_item_id]['allow_matt'] == 1){
				str = str + '<label for="matt-' + id + '-' + print_option_item_id + '">Matt</label>';
				str = str + '<input class="mini-input print_options" type="checkbox"  value="1" name="data[OrderItem][k' + k + '][' + print_option_item_id + '][is_matt]" id="matt-' + id + '-' + print_option_item_id + '" />';
			}

			*/ 
			
			
			str = str + '</span>';
			str = str + '<input type="hidden" name="cost-' + id + '-' + print_option_item_id + '" id="cost-' + id + '-' + print_option_item_id + '" value="' + p[print_option_id][print_option_item_id]['cost'] + '" />';
			str = str + '<input type="hidden" class="print_option_count" id="print_option_count-' + id + '-' + k + '[]" name="print_option_count-' + id + '[]" value="' + k + '" />';
			str = str + '<div class="clear-options"></div>';
			str = str + '</div>';
			
			$('#option-holder-' + id).append(str);
			
			
			
			get_total(id);
			
			
			
			submit_form('OrderItem' + id);
			
			return false;
		
		});
	
	});
	
	
	
	$('a.remove-from-lightbox').click(function(){
				var h = $(this).parent().parent().attr('id');
				var href = $(this).attr('href');
				$.post(href); // make ajax call;
				$('#' + h + ' ~ div').remove(); // select the sibling
				$('#' + h).attr('class', 'show25');
				$(this).parent().remove();
				update_summary_basket();
		return false;
	});
	
	$('a.remove-link').livequery('click', function(){
				$(this).parent().remove();			
				var this_id =  this.id.split('-');
				var id = parseInt(this_id[1]);
				var print_option_item_id = this_id[2];
				var href = '/order_items/' + REMOVE_ACTION + '/' + id + '/' + print_option_item_id;
				$.post(href); // make ajax call;
				get_total(id);
				update_summary_basket();
				return false;
	});
	
			
	/**
	* Force an update when the user chnages the quantity
	*/
	$('input.quantity').livequery('change', function(){
				var val = $(this).val();
				var id = get_id(this.id);
				if(isNaN(val)){
					$(this).val(1);
				} else if (val <= 0){
					$(this).parent().parent().remove();
				}
				get_total(id);
	});
	
	
	/*
	       
	       
	
	
	// Force an update when the user chnages the print options
	// DISABLED Because we don't currently have any print options
	$('input.print_options').livequery('click', function(){
				var id = get_id(this.id);
				get_total(id);
	});
	
	*/ 
	
	
	$('input.add-to-cart-button').livequery('click', function(){
		
		var id = get_id($(this).attr('id'));
		
		
		submit_form('OrderItem' + id);
		
		return false;
		
		
		
	});
	
	function submit_form(form_id){
			var this_id =  form_id.split('OrderItem');
				var id = parseInt(this_id[1]);
				
				var vars = $('#' + form_id).serialize();
			
				$.post('/order_items/' + ADD_ACTION, vars, function(data){
					
								
					if(data == 'good'){
						
						$('#submit-' + id).attr('class','update-cart-button-disabled');
						$('#lightbox-message-' + id).attr('class','lightbox-message');
						$('#lightbox-message-' + id).html('The changes have been saved to your cart');
						
						update_summary_basket();
						
						
					} else {
					
					
						alert('Sorry the system was unable to update your cart');
					}
					
				});
				
				return false;
		
	
	}
	
	function update_summary_basket(){
		$.get('/order_items/ajax_basket', function(data){
			$('#basket-summary-holder').html(data);
		});
		
	}
	
	
	function get_id(id){
	
		var this_id =  id.split('-');
		id = parseInt(this_id[1]);
		
		return id;
	
	}
	
	
	function get_total(photo_id){
	
		var total = 0;
	
		$('#option-holder-' + photo_id + ' .print_option_list').each(function(){
		
			var this_id = this.id;
			
			var qty = this_id.replace('print_option_item_list-', 'quantity-');
			var cost = this_id.replace('print_option_item_list-', 'cost-');
			
			qty = $('#' + qty).val();
			cost = $('#' + cost).val();
			
			
			
			total = total + (qty * cost);
			
			
			
		});
		
		if(total == 0){
			total = '';
			$('#submit-' + photo_id ).remove();
			
		} else {
			total = '<span>Total: &#163;' + total.toFixed(2) + '</span>';
			
			update_submit_button(photo_id);
			
			
		}
		
		$('#total-' + photo_id).html(total);
	
	}
	
	function update_submit_button(id){
	
		var but = '<input class="add-to-cart-button" type="submit" name="submit-' + id + '" id="submit-' + id + '" value="add to basket" />';
			
		
		
		if($('#submit-' + id).length == 0){
			$('#total-' + id).after(but);
		} else {
			$('#submit-' + id).attr('class','add-to-cart-button');
		}
		$('#submit-' + id).attr('class','add-to-cart-button');
		$('#lightbox-message-' + id).attr('class', 'lightbox-message-red');
		$('#lightbox-message-' + id).html('Click \'Update Cart\' to save your changes');
	
	}