
// global variable for checkout popup
var boxy;


function boxy_it(selector, options)
{
	
	var show = function() {
		boxy = new Boxy(selector, options);
		boxy.moveTo(null, 20);
	}
	
	if(boxy && boxy.isVisible())
	{
		boxy.hide(show);
	}
	else
	{
		show();	
	}

}




$(document).ready(function() {
	
    $('#cart .decrease_quantity').live("click", function(e){
    	var product_id = $(this).attr("product_id");
		var quantity = parseInt($(this).attr("quantity"));
    	set_quantity(product_id, quantity-1);
    });
    
    $('#cart .increase_quantity').live("click", function(e){
    	var product_id = $(this).attr("product_id");
		var quantity = parseInt($(this).attr("quantity"));
		set_quantity(product_id, quantity+1);
	});

    $('#cart .delete').live("click", function(e){
    	var product_id = $(this).attr("product_id");
		set_quantity(product_id, 0);
		return false;
	});

	$('.add_to_cart_button').click(function(e){	

		var product_id = $(this).attr('product_id');
		var direct_to_checkout = $(this).attr('direct_to_checkout');

		if(direct_to_checkout == 1){
			$.post("/s/_add_to_cart.php", {user_id_cookie : USER_ID_COOKIE, product_id : product_id}, function(response){
				document.location = "/goto_checkout.php?user_id_cookie="+USER_ID_COOKIE;
			});
		} else {
			boxy_it("#cart_popup_contents", {title : "Your Cart", modal : true});
			updating_cart_spinner();
		
			$.post("/s/_add_to_cart.php", {user_id_cookie : USER_ID_COOKIE, product_id : product_id}, function(response){
				update_cart(1);
			});
		}	
	});
	
	$('#keep_shopping_button').click(function(){
		boxy.hide();	
	});
	
	$('.checkout_button').click(function(){
		if($('#cart_count').text() != "0")
			document.location = "/goto_checkout.php?user_id_cookie="+USER_ID_COOKIE;
	});

	$('#cart').bind('updated', function(e, cart_html, cart_count){
		if(cart_count == 0)
			$('.checkout_button').addClass('disabled');
		else
			$('.checkout_button').removeClass('disabled');
	});
	
	$('.add_to_cart_button').hover(
		function(){
			var product_id = $(this).attr('product_id');
			$('#smiley_'+product_id).show();
		},
		function(){
			var product_id = $(this).attr('product_id');
			$('#smiley_'+product_id).hide();
		}
	);
});

function update_cart(editable, country_code){

	if(editable == undefined)
		editable = parseInt($('#cart').attr("editable"));

	if(country_code == undefined)
		country_code = $('#cart').attr("country_code") != undefined ? $('#cart').attr("country_code") : '';
	
	var show_shipping = $('#cart').attr("shp") != undefined ? $('#cart').attr("shp") : 0;
	var order_key = ($('#cart').attr("order_key") != undefined ? $('#cart').attr("order_key") : '');
	
	$.post("/s/_update_cart.php", {user_id_cookie : USER_ID_COOKIE, editable : editable, show_shipping : show_shipping, country_code : country_code, order_key : order_key}, function(response){
		response = JSON.parse(response);
		$('#cart').html(response.cart_html).height(''); // unset height so cart can extend freely

		$('#cart').trigger('updated', [response.cart_html, response.cart_count, response.order_revision]);
		FB.XFBML.Host.parseDomElement(document.getElementById('cart'));
		//$('#cart_count').text(response.cart_count);
	});
}

function updating_cart_spinner(){
	var old_height = $("#cart").innerHeight();
	old_height = Math.max(old_height, 220);
	if(old_height)
		$("#cart").height(old_height); // set height so things don't hop around as much
	
	//$('#cart').html("<table ><tr><td  ><div >Updating Cart</div></td></tr></table>");
	$('#cart').html('<table align="center" style="width:100%;height:100%;"><tr><td valign="middle" style="border:0px solid #3E5B99;text-align:center;"><img src="/s/images/spinner_large_transparent.gif" style="margin-bottom:10px" /><div style="font-size:12px;color:#3E5B99">Updating Cart</div></td></tr></table>');
}

// creates an order if one doesn't exist
function add_to_cart(product_id){
	updating_cart_spinner();
	$.post(
		"/s/_add_to_cart.php",
		{user_id_cookie : USER_ID_COOKIE, product_id : product_id},
		function(response) {
	   		update_cart(true);
   		}
	);
}

function set_quantity(product_id, quantity){
	updating_cart_spinner();
	$.post(
		"/s/_set_quantity.php",
		{user_id_cookie : USER_ID_COOKIE, product_id : product_id, quantity: quantity},
		function(response) {
			update_cart(true);
	    }
	);
}