$(function() {
	$("#cart tr .remove input").click(function() {
		var orderCode = $(this).val();
		$.ajax({
			type: "GET",
			url: "cart_action.php",
			data: "remove[]=" + orderCode,
			success: function() {
				$("#cart tr .remove input[value=" + orderCode + "]").parent().parent().fadeOut(500, function() {
					$(this).remove();
					calcPrice();
					window.location.reload();	
				});
			},
			error: function() {
				window.location("cart_action.php?remove[]="+orderCode);
			}
		});
	});
	
	$("#cart tr .quantity input").change(function() {
		var orderCode = $(this).attr("name").slice(9, -1);
		var quantity = $(this).val();
		$.ajax({
			type: "GET",
			url: "cart_action.php",
			data: "quantity[" + orderCode + "]=" + quantity,
			success: function() {
				var startColor = $("#cart tr .quantity input[name*=" + orderCode + "]").parent().parent().hasClass("odd") ? "#eee" : "#fff";
				$("#cart tr .quantity input[name*=" + orderCode + "]").parent().parent().find("td").animate({ backgroundColor: "#ff8" }, 100).animate({ backgroundColor: startColor }, 800);
				calcPrice();
			},
			error: function() {
				window.location("cart_action.php?quantity[" + orderCode + "]=" + quantity);
			}
		});
	});
});

function calcPrice() {
	var totalPrice = 0;
	$("#cart tr .quantity").parent().each(function() {
		var quantity = $(".quantity input", this).val();
		var unitPrice = $(".unit_price", this).text().slice(1);
		var extendedPrice = quantity*unitPrice;
		totalPrice += extendedPrice;
		
		$(".extended_price", this).html("$" + extendedPrice);
		$("#total_price").html("$"+totalPrice);
	});
	if ( totalPrice == 0 ) {
		$("#cart").parent().replaceWith("<p class='center'>You have no items in your cart.</p>");
	}
}
/*******************************************************************************************************/
function initCartDialog(){
	$('form.cart_form').live('submit', function(){
		var url = '/cart_action.php';
		url += ($('input[name=order_code]', this).length > 0)?'?order_code='+$('input[name=order_code]', this).val():'';
		url += ($('input[name=quantity]', this).length > 0)?'&quantity='+$('input[name=quantity]', this).val():'';
		var iframe = '<iframe frameborder="0" name="sCartIframe" id="sCartIframe" hspace="0" src="'+url+'" style="width:100%; height:100%;" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>';
		$('#dialog').empty().append(iframe);
		$('#dialog').dialog({
			modal: true,
			width:'828',
			height:'500',
			minWidth:'780',
			minHeight:'500',
			position:'center',
			title:'Shopping Cart',
			resizable:false
		});
		return false;
	});
	return false;
}
/*******************************************************************************************************/
function sCartLoaded(w, h){
	$('#dialog').dialog({
		width:parseInt(w)+34,
		height:parseInt(h)+75
	});
	$('#dialog').dialog({
		position:'center',
		resizable:true
	});				
	return false;
}
/*******************************************************************************************************/
function dsCartLoaded(){
	window.parent.sCartLoaded($('body').width(), $('body').height());
	window.parent.getCartTtlItems();
	window.parent.getCartSubTtl();
	window.parent.getCartItems();
    return false;
}
/*******************************************************************************************************/
function closeCart(){
	$('#dialog').dialog('close');
    return false;
}
/*******************************************************************************************************/
function getCartTtlItems(){
	if($('#cartTtlItems').length <= 0){
		return false;
	}
	$.post('../cart_action.php', { mtd:'getCartTtlItems' },
	function(data){
		$('#cartTtlItems').text(data);
		return false;
	});
	return false;
}
/*******************************************************************************************************/
function getCartSubTtl(){
	if($('#cartSubTtl').length <= 0){
		return false;
	}
	$.post('../cart_action.php', { mtd:'getCartSubTtl' },
	function(data){
		$('#cartSubTtl').text(data);
		return false;
	});
	return false;
}
/*******************************************************************************************************/
function getCartItems(){
	//if($('#cartItems').length <= 0){
	if($('#ciCtnr').length <= 0){
		return false;
	}
	$.post('../cart_action.php', { mtd:'getCartItems', tid:$('#citid').val() },
	function(data){
		var footer = '';
		if($('#'+$('#ciCtnr').val()).children('.keep-me')){
			footer = $('#'+$('#ciCtnr').val()+' .keep-me');
		}
		$('#'+$('#ciCtnr').val()).empty().append(data).append(footer);
		return false;
	});
	return false;
}
/*******************************************************************************************************/
