$(document).ready(function () {

    $("#basketItemsWrap li:first").hide();
    $("#slidingTopContent").hide();

    $("#slidingTopTrigger").live("click", function (event) {
        $("#slidingTopContent").slideToggle("slow", function () {
            if ($("#slidingTopContent").is(":visible")) {
                $("#slidingTopFooterLeft").html('<img src="/Design2010/images/bullet-up.gif" alt="Hide Basket" /> <a href="/nojsredirect.aspx" onclick="return false;" id="slidingTopTrigger">Hide Basket</a>');
            } else {
                $("#slidingTopFooterLeft").html('<img src="/Design2010/images/bullet-down.gif" alt="Show Basket" /> <a href="/nojsredirect.aspx" onclick="return false;" id="slidingTopTrigger">Show Basket</a>');
            }
        });
    });


    $(".productPriceWrapRight a img").click(function () {
        var productIDValSplitter = (this.id).split("_");
        var productIDVal = productIDValSplitter[1];
        var productVariantIDVal = productIDValSplitter[2];
        var productQuantityVal = 1;
        if ($("#txtQuantity_" + productVariantIDVal).length) {
            productQuantityVal = $("#txtQuantity_" + productVariantIDVal).val();
        }


        if ($("#slidingTopContent").is(":visible")) {

            var productX = $("#productImageWrapID_" + productIDVal).offset().left;
            var productY = $("#productImageWrapID_" + productIDVal).offset().top;

            if ($("#productID_" + productIDVal).length > 0) {
                var basketX = $("#productID_" + productIDVal).offset().left;
                var basketY = $("#productID_" + productIDVal).offset().top;
            } else {
                var basketX = $("#basketTitleWrap").offset().left;
                var basketY = $("#basketTitleWrap").offset().top;
            }

            var gotoX = basketX - productX;
            var gotoY = basketY - productY;

            var newImageWidth = $("#productImageWrapID_" + productIDVal).width() / 3;
            var newImageHeight = $("#productImageWrapID_" + productIDVal).height() / 3;

            $("#productImageWrapID_" + productIDVal + " img")
		.clone()
		.prependTo("#productImageWrapID_" + productIDVal)
		.css({ 'position': 'absolute' })
		.animate({ opacity: 0.4 }, 100)
		.animate({ opacity: 0.1, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight }, 1200, function () {
		    $(this).remove();

		    $("#notificationsLoader").html('<img src="/Design2010/images/loader.gif">');

		    $.ajax({
		        type: "POST",
		        url: "/addtocart.aspx",
		        data: { productID: productIDVal, productVariantID: productVariantIDVal, productQuantity: productQuantityVal, action: "addToBasket" },
		        success: function (theResponse) {

		            if ($("#productID_" + productIDVal).length > 0) {
		                $("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
		                $("#productID_" + productIDVal).before(theResponse).remove();
		                $("#productID_" + productIDVal).fadeOut();
		                $("#productID_" + productIDVal).fadeIn();
		                $("#notificationsLoader").empty();
		                var num = $('#basketItems > li').length;
		                if (num = 3) {
		                    $("#emptybasket").html('<a href="/ShoppingCart.aspx"><img alt="Checkout now" src="/Design2010/images/checkout.gif" style="border:none;" /></a>');
		                }

		            } else {
		                $("#basketItemsWrap li:first").before(theResponse);
		                $("#basketItemsWrap li:first").hide();
		                $("#basketItemsWrap li:first").show("slow");
		                $("#notificationsLoader").empty();
		                var num = $('#basketItems > li').length;
		                if (num = 3) {
		                    $("#emptybasket").html('<a href="/ShoppingCart.aspx"><img alt="Checkout now" src="/Design2010/images/checkout.gif" style="border:none;" /></a>');
		                }
		            }

//		            var totalPrice = 0;
//		            $('#basketItems > li').each(function (index) {
//		                if ($(this).find('span').text() != '') {
//		                    totalPrice = totalPrice + parseFloat($(this).find('span').text().substring(1));
//		                }
//		            });
//		            $('#total').empty();
//		            $('#total').html('&pound;' + totalPrice.toFixed(2));
//		            $('#total').fadeOut();
		            //		            $('#total').fadeIn();

		            $.ajax({
		                type: "POST",
		                url: "/addtocart.aspx",
		                data: { productID: productIDVal, productQuantity: 1, action: "gettotal" },
		                success: function (theResponse) {
		                    $('#total').empty();
		                    $('#total').html(theResponse);
		                    $('#total').fadeOut();
		                    $('#total').fadeIn();
		                },
		                error: function (request, status, error) {
		                    alert(request.responseText);
		                }
		            });

		        }
		    });
		});

        } else {

            $("#slidingTopContent").slideToggle("slow", function () {

                $("#slidingTopFooterLeft").html('<img src="/Design2010/images/bullet-up.gif" alt="Hide Basket" /> <a href="/nojsredirect.aspx" onclick="return false;" id="slidingTopTrigger">Hide Basket</a>');
                $("#notificationsLoader").html('<img src="/Design2010/images/loader.gif">');


                var productX = $("#productImageWrapID_" + productIDVal).offset().left;
                var productY = $("#productImageWrapID_" + productIDVal).offset().top;

                if ($("#productID_" + productIDVal).length > 0) {
                    var basketX = $("#productID_" + productIDVal).offset().left;
                    var basketY = $("#productID_" + productIDVal).offset().top;
                } else {
                    var basketX = $("#basketTitleWrap").offset().left;
                    var basketY = $("#basketTitleWrap").offset().top;
                }

                var gotoX = basketX - productX;
                var gotoY = basketY - productY;

                var newImageWidth = $("#productImageWrapID_" + productIDVal).width() / 3;
                var newImageHeight = $("#productImageWrapID_" + productIDVal).height() / 3;

                $("#productImageWrapID_" + productIDVal + " img")
		.clone()
		.prependTo("#productImageWrapID_" + productIDVal)
		.css({ 'position': 'absolute' })
		.animate({ opacity: 0.4 }, 100)
		.animate({ opacity: 0.1, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight }, 1200, function () {
		    $(this).remove();
		    $.ajax({
		        type: "POST",
		        url: "/addtocart.aspx",
		        data: { productID: productIDVal, productVariantID: productVariantIDVal, productQuantity: productQuantityVal, action: "addToBasket" },
		        success: function (theResponse) {

		            if ($("#productID_" + productIDVal).length > 0) {
		                $("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
		                $("#productID_" + productIDVal).before(theResponse).remove();
		                $("#productID_" + productIDVal).fadeOut();
		                $("#productID_" + productIDVal).fadeIn();
		                $("#notificationsLoader").empty();
		                var num = $('#basketItems > li').length;
		                if (num = 3) {
		                    $("#emptybasket").html('<a href="/ShoppingCart.aspx"><img alt="Checkout now" src="/Design2010/images/checkout.gif" style="border:none;" /></a>');
		                }

		            } else {
		                $("#basketItemsWrap li:first").before(theResponse);
		                $("#basketItemsWrap li:first").hide();
		                $("#basketItemsWrap li:first").show("slow");
		                $("#notificationsLoader").empty();
		                var num = $('#basketItems > li').length;
		                if (num = 3) {
		                    $("#emptybasket").html('<a href="/ShoppingCart.aspx"><img alt="Checkout now" src="/Design2010/images/checkout.gif" style="border:none;" /></a>');
		                }
		            }

//		            var totalPrice = 0;
//		            $('#basketItems > li').each(function (index) {
//		                if ($(this).find('span').text() != '') {
//		                    totalPrice = totalPrice + parseFloat($(this).find('span').text().substring(1));
//		                }
//		            });
//		            $('#total').empty();
//		            $('#total').html('&pound;' + totalPrice.toFixed(2));
//		            $('#total').fadeOut();
		            //		            $('#total').fadeIn();
		            $.ajax({
		                type: "POST",
		                url: "/addtocart.aspx",
		                data: { productID: productIDVal, productQuantity: 1, action: "gettotal" },
		                success: function (theResponse) {
		                    $('#total').empty();
		                    $('#total').html(theResponse);
		                    $('#total').fadeOut();
		                    $('#total').fadeIn();
		                },
		                error: function (request, status, error) {
		                    alert(request.responseText);
		                }
		            });
		        }
		    });

		});

                $("#slidingTopTrigger").fadeTo(4000, 1, function () {
                    $("#slidingTopContent").slideToggle("slow", function () {
                        $("#slidingTopFooterLeft").html('<img src="/Design2010/images/bullet-down.gif" alt="Show Basket" /> <a href="/nojsredirect.aspx" onclick="return false;" id="slidingTopTrigger">Show Basket</a>');
                    });

                });

            });
        }

    });



    $("#basketItemsWrap li img").live("click", function (event) {
        var productIDValSplitter = (this.id).split("_");
        var productIDVal = productIDValSplitter[1];
        var productVariantIDVal = productIDValSplitter[2];

        $("#notificationsLoader").html('<img src="/Design2010/images/loader.gif">');

        $.ajax({
            type: "POST",
            url: "/addtocart.aspx",
            data: { productID: productIDVal, productVariantID: productVariantIDVal, productQuantity: 1, action: "deleteFromBasket" },
            success: function (theResponse) {

                $("#productID_" + productIDVal).hide("slow", function () {
                    $(this).remove();
                });
                $("#notificationsLoader").empty();
                var num = $('#basketItems > li').length;
                if (num == 2) {
                    $("#emptybasket").html('Empty basket.');
                }

                //                var Itemprice = $("#productID_" + productIDVal).find('span').text();
                //                var totalPrice = $('#total').text();
                //                var grandTotal = parseFloat(totalPrice.substring(1)) - parseFloat(Itemprice.substring(1));
//                var totalPrice = 0;
//                $('#basketItems > li').each(function (index) {
//                    if ($(this).find('span').text() != '') {
//                        if ($("#productID_" + productIDVal).find('span').text() != $(this).find('span').text()) {
//                            totalPrice = totalPrice + parseFloat($(this).find('span').text().substring(1));
//                            //alert($(this).find('span').text());
//                        }
//                    }
//                });
//                $('#total').empty();
//                $('#total').html('&pound;' + totalPrice.toFixed(2));
//                $('#total').fadeOut();
                //                $('#total').fadeIn();
                $.ajax({
                    type: "POST",
                    url: "/addtocart.aspx",
                    data: { productID: productIDVal, productQuantity: 1, action: "gettotal" },
                    success: function (theResponse) {
                        $('#total').empty();
                        $('#total').html(theResponse);
                        $('#total').fadeOut();
                        $('#total').fadeIn();
                    },
                    error: function (request, status, error) {
                        alert(request.responseText);
                    }
                });

            }
        });
    });
});
