$(document).ready(function() {
	
	salad_html = '';
	
	$.ajax({
	  type: "GET",
	  url: "cart_items.xml",
	  dataType: "xml",
	  success: parseXml
	});

});

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function parseXml(xml)
{
	$(xml).find("Salad").each(function() {
		salad_html = salad_html + "<option value='" + $(this).text() + "'>" + $(this).text() + "</option>";
	});
	
	$(xml).find("Item").each(function() {

		var id = $(this).find("Id").text();
		var title = $(this).find("Title").text();
		var brief_description = $(this).find("Brief_Description").text();
		var long_description = $(this).find("Long_Description").text();
		var img = $(this).find("Image").text();
		
		id = trim(id);
		title = trim(title);
		brief_description = trim(brief_description);
		long_description = trim(long_description);
		img = trim(img);
		
		var html = " \
			<tr class='item' id='" + id + "'> \
				<td class='a'> \
					<img src='" + img + "' /> \
				</td> \
				<td class='b'> \
					<span class='title'>" + title + "</span>";
					
		if ( brief_description != '' ) {
			html = html + "<p>" + brief_description + "</p>";
		}
		
		if ( long_description != '' ) {
			html = html + "<div class='long_description'>" + long_description + "</div><p><a href='#' class='viewFull'>Full Description</a></p>";
		}
					
		var html = html + " \
				</td> \
				<td class='c'> \
					Choose a Salad:<br /> \
					<select>" + salad_html + "</select><br /> \
					<a href='#' class='addLink'>Add to Shopping Cart</a> \
				</td> \
			</tr> \
		";
		
		$("#cartTable").append(html);

	});
	
	$(xml).find("Single").each(function() {

		var id = $(this).find("Id").text();
		var title = $(this).find("Title").text();
		var brief_description = $(this).find("Brief_Description").text();
		var img = $(this).find("Image").text();
		
		var option_html = '';
		
		$(this).find("Option").each(function() {
			var price = $(this).attr("price");
			option_html = option_html + "<option value='" + price + "'>" + $(this).text() + " - $" + price + "</option>";
		});
		
		id = trim(id);
		title = trim(title);
		brief_description = trim(brief_description);
		img = trim(img);
		
		var html = " \
			<tr class='item' id='" + id + "'> \
				<td class='a'> \
					<img src='" + img + "' /> \
				</td> \
				<td class='b'> \
					<span class='title'>" + title + "</span>";
					
		if ( brief_description != '' ) {
			html = html + "<p>" + brief_description + "</p>";
		}
					
		var html = html + " \
				</td> \
				<td class='c'> \
					Options:<br /> \
					<select>" + option_html + "</select><br /> \
					<a href='#' class='addLink2'>Add to Shopping Cart</a> \
				</td> \
			</tr> \
		";
		
		$("#cartTable2").append(html);

	});
	
	$("#ajax_loading").remove();
	
	$("#cartTable").show();
	
	$("#cartTable .viewFull").click(function() {
		$(".long_description").hide();
		$(".viewFull").show();
		var item = $(this).closest(".item");
		$(".long_description", item).show();
		$(".viewFull", item).hide();
	});
	
	$(".addLink").click(function() {
		var id = $(this).closest("tr").attr("id");
		
		if ( $("#" + id).hasClass("chosen") ) {
			
			$("#" + id).removeClass("chosen");
			$(this).html("Add to Shopping Cart");
			
		} else {
			
			var total_chosen = $("#cartTable .chosen").size();
			if ( total_chosen == 10 ) {
				
				alert("We apologize, but you can only add up to 10 meal bundles to your shopping cart.");
				
			} else {
			
				$("#" + id).addClass("chosen");
				$(this).html("Remove from Shopping Cart");
				
			}
			
		}
		
		var total_chosen = $("#cartTable .chosen").size();
		
		$("#num1").html(total_chosen);
		
		switch ( total_chosen ) {
			
			case 1 :
			var cost = "$45.00";
			break;
			
			case 2 :
			var cost = "$90.00";
			break;
			
			case 3 :
			var cost = "$135.00";
			break;
			
			case 4 :
			var cost = "$176.00";
			break;

			case 5 :
			var cost = "$190.00";
			break;
			
			case 6 :
			var cost = "$225.00";
			break;
			
			case 7 :
			var cost = "$260.00";
			break;
			
			case 8 :
			var cost = "$300.00";
			break;
			
			case 9 :
			var cost = "$350.00";
			break;
			
			case 10 :
			var cost = "$400.00";
			break;
			
		}

		$("#num2").html(cost);
		
		return false;
	});
	
	$(".addLink2").click(function() {
		var id = $(this).closest("tr").attr("id");
		
		var price = $("#" + id + " select").val();
		price = price * 1;
		
		var existing_price = $("#num2").html();
		existing_price = existing_price.replace(/[^0-9.]/g, '');
		existing_price = existing_price * 1;

		if ( $("#" + id).hasClass("chosen") ) {
			
			$("#" + id).removeClass("chosen");
			$(this).html("Add to Shopping Cart");
			
			existing_price = existing_price - price;
			
		} else {
			
			$("#" + id).addClass("chosen");
			$(this).html("Remove from Shopping Cart");
			
			existing_price = existing_price + price;
			
		}
		
		$("#num2").html('$' + existing_price);
		
		var total_chosen = $("#cartTable2 .chosen").size();
		
		$("#num3").html(total_chosen);
		
		return false;
	});
	
}

function checkout() {
	
	var total = $("#num2").html();
	var html = "";
	
	$("#cartTable .item.chosen, #cartTable2 .item.chosen").each(function() {

		var item_id = $(this).attr("id");
		var item_title = $(".title", this).html();
		var item_select_text = $("select :selected", this).text();
		
		html += " \
		<b>" + item_title + "</b><br /> \
		Option Selected: " + item_select_text + " \
		<br /><br /> \
		";

	});
	
	html += " \
	<br /><br /> \
	<b>Total Checkout Price: " + total + " \
	";
	
	$("#form_body").val(html);
	$("#form_total").val(total);
	$("#checkout_form").submit();

}