// JavaScript Document
$(document).ready(function(){

	// Create an array with all the current variant ids in it.
	var cart_arr = new Array();

	function createCartArray () {
		// Empty any existing array
		cart_arr = [];
		// load items into the array
		$(".order_item").each(function(){
			var title = $.trim($("h3.cart_item_food_title", this).text());
			var variant = $.trim($("p.cart_item_variant_title", this).text());
			var both = title + ': ' + variant;
			cart_arr.push(both);
		});
	}
	
	function doesProductExist(product) {
		var in_cart = $.inArray(product, cart_arr)
		if (in_cart >= 0) {
			return true;
		} else {
			return false;
		}
	}
	
	/* attach a submit handler to the form */
	$(".order_form").submit(function(event) {
		/* stop form from submitting normally */
		event.preventDefault();
			
		/* get some values from elements on the page: */
		var $form = $( this ),
			term_food_id = $form.find( 'input[name="food_id"]' ).val(),
			term_var_id = $form.find( 'input[name="var_id"]' ).val(),
			term_me = $form.find( 'input[name="me"]' ).val(),
			term_options = $form.find( 'input[name="options"]' ).val(),
			term_quantity = $form.find( 'input[name="quantity"]' ).val(),
			term_var_min = $form.find( 'input[name="var_min"]' ).val(),
			
			url = $form.attr( 'action' );
			id = $form.attr('id');
			
		// CHECK TO SEE IF A PRESET MENU AND IF SO, STRIP THE SPAN
		var preset_test = $.trim($(this).closest('div.food_item_entry').children('h3.food_item').html());
		var span_ind = preset_test.indexOf('<span class="food_item">');
		
		if (span_ind > 0) {
			var food_title = $.trim($(this).closest('div.food_item_entry').children('h3.food_item').html());
			var food_title = food_title.substring(0, span_ind);
		} else {
			var food_title = $.trim($(this).closest('div.food_item_entry').children('h3.food_item').text());
		}
		
		var variant_title = $.trim($(this).closest('tr').children('td.title').text());
		var both = food_title + ': ' + variant_title;
		
		if (doesProductExist(both)) {
			var answer = confirm ("A similar item already exists in your cart. Are you sure you want more than one?\n\nPlease note, if you want more than one item you can increase the quantity on the next page.");
			if (answer) {
				/* Send the data using post and put the results in a div */
				$.post( url, { food_id: term_food_id, var_id : term_var_id, me : term_me, options : term_options, quantity : term_quantity,  var_min : term_var_min},
					function() {
						$('#sub_content_sidebar').load('/wp-content/themes/swansons/catering-cart-ajax.php', function() {
							removeBorder($('#basket_contents'));
							Cufon.refresh();
							createCartArray();
						});
					}
				);
			} // END IF
		} else {
			/* Send the data using post and put the results in a div */
			$.post( url, { food_id: term_food_id, var_id : term_var_id, me : term_me, options : term_options, quantity : term_quantity,  var_min : term_var_min},
				function() {
					$('#sub_content_sidebar').load('/wp-content/themes/swansons/catering-cart-ajax.php', function() {
						removeBorder($('#basket_contents'));
						Cufon.refresh();
						createCartArray();
					});
				}
			);
		}
	});
	
	$(".delete_me").live('click', function(event) {
		/* stop form from submitting normally */
		event.preventDefault(); 
		
		// get some values from elements on the page:
		var $form = $( this ),
			term_id = $form.find( 'input[name="id"]' ).val(),
			
			url = $form.attr( 'action' );
			id = $form.attr('id');
	
		// Send the data using post and put the results in a div
		$.post( url, { id: term_id},
			function() {
				$('#sub_content_sidebar').load('/wp-content/themes/swansons/catering-cart-ajax.php', function() {
					removeBorder($('#basket_contents'));
					Cufon.refresh();
					createCartArray();
				});
			}
		);
	});
	
	createCartArray();
});
