$(document).ready(function() {
var cart_url="http://www.marymulari.com/includes/marymulari/shopping_cart_ajax.php";
	function refresh_cart()
	{
		$.post(cart_url, {ajax:"shopping_cart",get:"cart_summary"}, function(data){update_summary(data);}, "html");
	}
	function add_to_cart(buttonId,itemQty)
	{
		itemId=get_item_id(buttonId);
		if(typeof itemQty == "undefined")itemQty=1;
		$.post(cart_url, {ajax:"shopping_cart",update:"add",item_id:itemId,qty:itemQty}, function(data){
			update_summary(data);
			$.post(cart_url, {ajax:"shopping_cart",update:"cart_button",item_id:itemId,button_id:buttonId}, function(data){update_button(buttonId,data);}, "html");
		});
	}
	function set_to_cart(inputQtyId,itemQty)
	{
		itemId=get_item_id(inputQtyId);
		if(typeof itemQty == "undefined")itemQty=1;
		$.post(cart_url, {ajax:"shopping_cart",update:"set",item_id:itemId,qty:itemQty}, function(data){
			update_summary(data);
		//$.post(cart_url, {ajax:"shopping_cart",update:"cart_button",item_id:itemId,button_id:buttonId}, function(data){update_button(buttonId,data);}, "html");
		});
	}
	function set_ship_city(val)
	{
		$.post(cart_url, {ajax:"contact_info",update:"city",city:val}, function(data){
			update_shipping_location(data);
		});
	}
	function set_ship_state(val)
	{
		$.post(cart_url, {ajax:"contact_info",update:"state",state:val}, function(data){
			update_shipping_location(data);
		});
	}
	function verify_empty_cart()
	{
		$.post(cart_url, {ajax:"shopping_cart",update:"verify_empty_cart"}, function(data){update_summary(data);}, "html");
	}
	function empty_cart()
	{
		$.post(cart_url, {ajax:"shopping_cart",update:"clear_cart"}, function(data){
			update_summary(data);			
			$("div.shopping_cart").each(function(){//only reloads the page if it is the shopping cart page itself
				window.location.reload(true);
			});
			//always re-write the links so they are active and uptodate
			replace_links();
		}, "html");
	}
	function remove_line()
	{
		$.post(cart_url, {ajax:"shopping_cart",update:"clear_cart"}, function(data){update_summary(data);}, "html");
	}
	
	function update_summary(html)
	{
		$("#cart_summary").replaceWith(html);
		register_events("#cart_summary ");//the trailing space means we register events only on items contained by #cart_summary
	}
	
	function get_item_id(full_id)
	{
		return full_id.slice( full_id.indexOf("_item_id_")+9 , full_id.lastIndexOf("__") );
	}
	/*Unused
	function cart_button(item_id)
	{
		return "cart_button_item_id_" + item_id + "__";
	}
	*/
/*update quantity displays on product pages?*/
	
	function update_button(button_id, button_html)
	{
		$("#"+button_id).replaceWith(button_html);
		register_events("#"+button_id);
	}	
	
	function update_shipping_location(form_html)
	{
		$("div.shipping_location").replaceWith(form_html);
		register_shipping_location_events();
					
			$("div.shipping_location").each(function(){//only reloads the page if it is the shopping cart page itself
				window.location.reload(true);
			});
	}
	
	/*Replace links*/
	function replace_links(){
		$("a.add_to_cart").each(function()
		{
			var the_button = $(this).attr("id");
			var the_item = get_item_id(the_button);
			$.post(cart_url, {ajax:"shopping_cart",update:"cart_button",item_id:the_item,button_id:the_button}, function(data){	update_button(the_button,data);}, "html");
		});
		/*Unused
		$(".item_line .item_qty form input").each(function()
		{
		if (  ($(this).attr("type")=="text")  &&  ($(this).attr("id")).substr(0,4)=="qty_"  )
			{
				var the_qty = $(this).attr("id");
				var the_item = get_item_id(the_qty);
				$.post(cart_url, {ajax:"shopping_cart",update:"cart_qty",item_id:the_item,qty_id:the_qty}, function(data){update_qty(the_qty,data);}, "html");
			}
		});*/
		
		register_events();
	}
	/*Register events*/
	function register_events(obj)
	{
		//if obj is omitted, all A tags of the appropriate class are registered
		//if obj is a CSS selector, all selected elements of the appropriate class are registered
		//if obj is a CSS selector followed by a space, only elements contained in the seleted elements are registered
		if (obj==null || obj=="")
		{
			obj="a";
		}
		
		$(obj+".add_to_cart").each(function()
		{
			$(this).unbind("click");
			$(this).click(function(e){
				e.preventDefault();
				add_to_cart($(this).attr("id"));//qty=1
			});
		});
		$(obj+".empty_cart").each(function()
		{
			$(this).unbind("click");
			$(this).click(function(e){
				e.preventDefault();
				verify_empty_cart();
			});
		});
		$(obj+".confirm_empty_cart").each(function()
		{
			$(this).unbind("click");
			$(this).click(function(e){
				e.preventDefault();
				empty_cart();
			});
		});
		$(obj+".refresh_cart").each(function()
		{
			$(this).unbind("click");
			$(this).click(function(e){
				e.preventDefault();
				refresh_cart();
			});
		});
		$(".item_qty input").each(function()
		{
			$(this).unbind("change");
			$(this).bind("change",function(e){
				e.preventDefault();
				set_to_cart($(this).attr("id"),$(this).val());
			});
		});
	}
	
	function register_shipping_location_events() {
	
		$(".shipping_location input#ship_city").each(function()
		{
			$(this).unbind("change");
			$(this).bind("change",function(e){
				e.preventDefault();
				set_ship_city($(this).val());
			});
		});
		
		$(".shipping_location input#ship_state").each(function()
		{
			$(this).unbind("change");
			$(this).bind("change",function(e){
				e.preventDefault();
				set_ship_state($(this).val());
			});
		});
		
	}
	
	
	//hide FCK if the hidden hide_fck_editors div is present
	$("#hide_fck_editors").each(function(){
		$(".fck_editor").hide();
	});
	replace_links();//internally calls 	register_events();
	register_shipping_location_events();
	refresh_cart();
});