function addToBasketAjax(objButton, bRelated)
{
	// Get the form that is related to the basket.
	var objForm = $(objButton).parents('form:first');

	var iQuantity = $(objForm).children('[name=quantity]').val();
	var iProductId = $(objForm).children('[name=product_id]').val();
	var iCategoryId = $(objForm).children('[name=category_id]').val();
	var iParentProduct = $(objForm).children('[name=parent_product]').val();

	$.post(
        '/basket/addtobasketajax',
        {
           product_id : iProductId,
           quantity : iQuantity,
	   category_id : iCategoryId,
	   parent_id : iParentProduct
        },
        function(strData)
        {
		strData = strData.trim();
		updateQuickBasket();

		if (strData != "" && !bRelated)
		{
			relatedPopup(strData);
		}
		else
		{
			var arrOffset = $(objButton).offset();
			var iWidth = $(objButton).outerWidth();
			var iHeight = $(objButton).outerHeight();

			var iPromptHeight = $('#basketPrompt').outerHeight();
			var iPromptWidth = $('#basketPrompt').outerWidth();

			var iNewTop = arrOffset.top;
			var iNewLeft =  arrOffset.left + iWidth;

			$('#basketPrompt').css({top: iNewTop, left: iNewLeft});
			$('#basketPrompt').fadeIn(1000, function()
			{
				$('#basketPrompt').delay(1000).fadeOut(1000);
			});
		}
        }
    );
}

function addToBasketAjaxMore(objButton)
{
	// Get the form that is related to the basket.
	var objForm = $(objButton).parents('form:first');
	var arrProducts = new Array();
	var iCategoryId = $(objForm).children('[name=category_id]').val();

	$(".quantity").each(function ()
	{
		if ($(this).val() > 0)
		{
			var strId = $(this).attr("id");

			strId = strId.substring(9);

			arrProducts.push({"category_id" : iCategoryId, "id" : strId, "quantity" : $("#quantity_" + strId).val()});
		}
	});

	if (arrProducts.length <= 0)
	{
		//$.prompt('Please select some products first');
		return false;
	}


	$.post(
		'/basket/addtobasketajaxmore',
		{
		   products : arrProducts
		},
		function(strData)
		{
			strData = strData.trim();
			updateQuickBasket();
			if (strData)
			{
				relatedPopup(strData);
			}
			else
			{
				var arrOffset = $(objButton).offset();
				var iWidth = $(objButton).outerWidth();
				var iHeight = $(objButton).outerHeight();

				var iPromptHeight = $('#basketPrompt').outerHeight();
				var iPromptWidth = $('#basketPrompt').outerWidth();

				var iNewTop = arrOffset.top;
				var iNewLeft =  arrOffset.left + iWidth;

				$('#basketPrompt').css({top: iNewTop, left: iNewLeft});
				$('#basketPrompt').fadeIn(1000, function()
				{
					$('#basketPrompt').delay(1000).fadeOut(1000);
				});
			}
		}
	    );


}

function addToWishlistAjax(objButton)
{
	// Get the form that is related to the basket.
	var objForm = $(objButton).parents('form:first');

	var iQuantity = $(objForm).children('[name=quantity]').val();
	var iProductId = $(objForm).children('[name=product_id]').val();

	$.post(
        '/wishlist/addtowishlistajax',
        {
           product_id : iProductId,
           quantity : iQuantity
        },
        function(strData)
        {
        	var arrOffset = $(objButton).position();
            var iWidth = $(objButton).outerWidth();
            var iHeight = $(objButton).outerHeight();

            var iPromptHeight = $('#wishlistPrompt').outerHeight();
            var iPromptWidth = $('#wishlistPrompt').outerWidth();

            var iNewTop = arrOffset.top;
            var iNewLeft =  arrOffset.left + iWidth;

            $('#wishlistPrompt').css({top: iNewTop, left: iNewLeft});
            $('#wishlistPrompt').fadeIn(1000, function()
            {
            	$('#wishlistPrompt').delay(1000).fadeOut(1000);
            });
        }
    );
}

function updateQuickBasket()
{
	$.ajax(
	{
		url: "/basket/quick",
		context: document.body,
		success: function(strData)
		{
			$("#quickBasket").html(strData);
		}
	});
}

function updateDeliveryCost()
{
	$("#basketConfirm").submit();
}

function relatedPopup(strData)
{
	$('#mycarousel').jcarousel();

	Shadowbox.open({
			content:    strData,
			player:     "html",
			height:     560,
			width:      600,
			options: {
				modal: true,
				onFinish: function()
				{
					$('#mycarousel').jcarousel();
					$('.relatedPopup').css("visibility", "visible");
				}
			}
		    });


}


$(document).ready(function ()
{
	$("#checkoutButton").click(function ()
	{
		$("[name=mode]").val("buy");

	});
	
	$("#checkoutButtonDelivery").click(function ()
    {
		$("#noDelivery").addClass("check");
		$("#checkDelivery").effect("shake", { times:3, distance: 4 }, 120);
		return false;
	});

	$("#continueShopping").click(function ()
	{
		document.location.href = "/";
		return false;
	});
	
	$(".voucherButton").click(function ()
	{
		$("#basketForm").submit();
		return false;
	});


	$(".categoryRowSpan").each(function ()
	{
		var strCategory = $(this).attr("category");

		var iCount = $("tr[category=" + strCategory + "]").length;

		$(this).attr("rowspan", iCount);

		dDeliveryCharge = 0;

		$("tr[category=" + strCategory + "]").each(function ()
		{
			if ($(this).attr("delivery") > 0)
			{
				dDeliveryCharge += parseFloat($(this).attr("delivery"));
			}
		});

		$("[delivery_category=" + strCategory + "]").html(currencyFormatted(dDeliveryCharge));
	});

	$(".checkDeliveryButton").click(function ()
	{
		if ($("#postcode").val() != "")
		{
			$("[name=mode]").val("checkdelivery");
			$("#basketForm").submit();
		}
		else
		{
			$("#postcodeError").html("Please enter a postcode first");
		}

		return false;
	})

	$("#metricMeasure").click(function ()
	{
		$("[metric != '']").each(function ()
		{
			$(this).html($(this).attr("metric"));
		});

		return false;
	});

	$("#imperialMeasure").click(function ()
	{
		$("[imperial != '']").each(function ()
		{
			$(this).html($(this).attr("imperial"));
		});

		return false;
	});
	
	$("#calculate").click(function ()
	{
		var dRoomWidth = $("#roomWidth").val();
		var dRoomLength = $("#roomLength").val();
		
		var dPackSize = $("#packSize").val();
		
		if (!isNaN(dRoomWidth) && !isNaN(dRoomLength))
		{
			var dTotal = Math.ceil((dRoomWidth * dRoomLength / dPackSize ));
			
			$("#roomTotal").val(dTotal);
		}
	});
});

