$(document).ready(function () {
    
        
    $("#basketItemsWrap li:first").hide();
    $("#addtocartbtns a span").click(IdiUBasket);
    $("#addtocartbtns a img").click(IdiUBasket);

    

    function IdiUBasket() {

        var productIDVal = this.id;
        var quantity = 1;
        if ($("input[name*='txtQuantity']") != null) {
            quantity = $("input[name*='txtQuantity']").val();
        }

        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="../images/UpdateProgress.gif">');

		        $.ajax({
		            type: "POST",
		            url: "../addtocart.aspx",
		            data: { productID: productIDVal, productQuantity: quantity, 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();
		                } else {
		                    $("#basketItemsWrap li:first").before(theResponse);
		                    $("#basketItemsWrap li:first").hide();
		                    $("#basketItemsWrap li:first").show("slow");
		                    $("#notificationsLoader").empty();
		                }


		                var naslov = $("#href_" + productIDVal + " img").attr("title");

		                var Niz = naslov.split('Price');

		                $("#dialog").html(Niz[0] + "</br>" + "Price" + Niz[1] + "</br>" + "</br>" + "has been added to cart!");
		                $("#dialog").dialog();




		                //Gets number of items text
		                $.ajax({
		                    type: "POST",
		                    url: "../addtocart.aspx",
		                    data: { productID: productIDVal, productQuantity: quantity, action: "getemptybaskettext" },
		                    success: function (theResponse) {
		                        $('#emptybasket').empty();
		                        $('#emptybasket').html(theResponse);
		                    },
		                    error: function (request, status, error) {
		                        $("#dialog").html('Item is already on shopping cart! Error 101');
		                        $("#dialog").dialog();
		                        //alert(request.responseText);
		                    }
		                });



		                //Gets sub total for mini cart
		                $.ajax({
		                    type: "POST",
		                    url: "../addtocart.aspx",
		                    data: { productID: productIDVal, productQuantity: quantity, action: "gettotal" },
		                    success: function (theResponse) {
		                        $('#subtotalMiniCart').empty();
		                        $('#subtotalMiniCart').html(theResponse);
		                        $('#subtotalMiniCart').fadeOut();
		                        $('#subtotalMiniCart').fadeIn();
		                    },
		                    error: function (request, status, error) {
		                        $("#dialog").html('Item is already on shopping cart! Error 102');
		                        $("#dialog").dialog();
		                        //alert(request.responseText);
		                    }
		                });



		                $("#productImageWrapID_" + productIDVal).css("background-image", "url('../images/chair_in_cart.png')");

		                //$("#href_" + productIDVal + " img").css({ opacity: 0 });

		                $("#href_" + productIDVal + " img").attr("src", "../images/prazan.png");
		                $("#href_" + productIDVal + " img").css("height", "0px");
		                $("#href_" + productIDVal + " img").css("width", "0px");

		                //$("#href_" + productIDVal + " img").hide(4000);



		                //Gets checkout text, this can be avoided if you don't care about languages and want the text in same language
		                $.ajax({
		                    type: "POST",
		                    url: "../addtocart.aspx",
		                    data: { productID: productIDVal, productQuantity: quantity, action: "getcheckoutbutton" },
		                    success: function (theResponse) {
		                        $('#buttonsMiniCart').empty();
		                        $('#buttonsMiniCart').html(theResponse);
		                        $('#buttonsMiniCart').fadeOut();
		                        $('#buttonsMiniCart').fadeIn();
		                    },
		                    error: function (request, status, error) {
		                        $("#dialog").html('Item is already on shopping cart! Error 103');
		                        $("#dialog").dialog();
		                        //alert(request.responseText);
		                    }
		                });


		            },
		            error: function (request, status, error) {
		                $('#emptybasket').empty();
		                $("#dialog").html('Item is already on shopping cart! Error 104');
		                $("#dialog").dialog();
		                //alert(request.responseText);
		            }
		        });
		    });

    };



    $("#basketItemsWrap li img").live("click", function (event) {
        var productIDValSplitter = (this.id).split("_");
        var productIDVal = productIDValSplitter[1];






        //$("#notificationsLoader").html('<img src="~/images/UpdateProgress.gif">');
        $("#notificationsLoader").html('<img src="../images/UpdateProgress.gif">');
        $.ajax({
            type: "POST",
            url: "../addtocart.aspx",
            data: { productID: productIDVal, productQuantity: 1, action: "deleteFromBasket" },
            success: function (theResponse) {

                $("#productID_" + productIDVal).hide("slow", function () {
                    $(this).remove();
                });
                $("#notificationsLoader").empty();



                //Gets sub total for mini cart
                $.ajax({
                    type: "POST",
                    url: "../addtocart.aspx",
                    data: { productID: productIDVal, productQuantity: 1, action: "gettotal" },
                    success: function (theResponse) {
                        $('#subtotalMiniCart').empty();
                        $('#subtotalMiniCart').html(theResponse);
                        $('#subtotalMiniCart').fadeOut();
                        $('#subtotalMiniCart').fadeIn();
                    },
                    error: function (request, status, error) {
                        $("#dialog").html('Item is already on shopping cart! Error 105');
                        $("#dialog").dialog();
                        //alert(request.responseText);

                    }
                });

                //Gets number of items text
                $.ajax({
                    type: "POST",
                    url: "../addtocart.aspx",
                    data: { productID: productIDVal, productQuantity: 1, action: "getemptybaskettext" },
                    success: function (theResponse) {
                        $('#emptybasket').empty();
                        $('#emptybasket').html(theResponse);
                    },
                    error: function (request, status, error) {
                        $("#dialog").html('Item is already on shopping cart! Error 107');
                        $("#dialog").dialog();
                        //alert(request.responseText);

                    }
                });

                //Gets checkout text, this can be avoided if you don't care about languages and want the text in same language
                $.ajax({
                    type: "POST",
                    url: "../addtocart.aspx",
                    data: { productID: productIDVal, productQuantity: 1, action: "getcheckoutbutton" },
                    success: function (theResponse) {
                        $('#buttonsMiniCart').empty();
                        $('#buttonsMiniCart').html(theResponse);
                        $('#buttonsMiniCart').fadeOut();
                        $('#buttonsMiniCart').fadeIn();
                    },
                    error: function (request, status, error) {
                        $("#dialog").html('Item is already on shopping cart! Error 108');
                        $("#dialog").dialog();
                        //alert(request.responseText);

                    }
                });


                $("#href_" + productIDVal + " img").css("height", "23px");
                $("#href_" + productIDVal + " img").css("width", "30px");
                $("#href_" + productIDVal + " img").css({ opacity: 0 });

                $("#href_" + productIDVal + " img").attr("src", "../images/chair_available.png");

                $("#productImageWrapID_" + productIDVal + " img")
		        .animate({ opacity: 1 }, 4000);

                //$("#productImageWrapID_" + productIDVal + " img").show(4000);

                $("#href_" + productIDVal).attr("href", "#");

            }
        });
    });
});
