﻿var isCartLoaded = false;
$(document).ready(function() {

$("#anchItems").unbind();
$("#anchItems").click(function() {
     
        if ($("#cartSumm").css("display") == "none")
            ShowCart();
        else
            HideCart();
    });
});
function ShowCart() {
    // if (!isCartLoaded)
    UpdateCart();
    $("#cartSumm").show('slow');
}
function HideCart() {
    $("#cartSumm").hide('slow');
}
var clickdelay = null;
var thistextBox = null;
var cartitemID = "";
function UpdateCart() {
    $.getJSON("/Services/CartCalls.asmx/GetCartTable", null, function(data) {
        if (data.length == 0) {
            $("#cartSumm").setTemplate("<div class='inner'><div class='content'><span class='emptycart'> {$T} </span></div></div> ", null, { filter_data: false });
            $("#cartSumm").processTemplate("Your Cart is Empty ");
            return;
        }
        // attach the template
        $("#cartSumm").setTemplateURL('/js/Templates/ShoppingCart.htm', null, { disallow_functions: false, filter_data: false, filter_params: false })
        // process the template
        $("#cartSumm").processTemplate(data);
        setTimeout(function() {
            $(".__cartQuantity").filter("[tag='" + cartitemID + "']").focus();
        }, 500);

        $(".__cartQuantity").keydown(function(event, e) {

            if (parseInt(event.which) > 57 && parseInt(event.which) < 96) {
                return false;

            }
        });
        $(".__cartQuantity").keyup(function(event) {

            //            if (parseInt(event.which) < 48 || parseInt(event.which) > 57) {
            //                return false;

            //            }
            thistextBox = this;

            if (!(parseInt($(this).val()) >= 1)) {
                alert("Enter a value greater than zero");
                return;
            }

            if (clickdelay != null)
                clearTimeout(clickdelay);

            clickdelay = setTimeout(function() {

                var value = $(thistextBox).attr("tag") + "," + $(thistextBox).val();
                $.post("/Services/CartCalls.asmx/ChangeQuantity", { val: value },
                function(data) {
                    cartitemID = $(thistextBox).attr("tag");

                    UpdateCart();



                });

            }, 1000);


        });





        //changeQuantity








        //delete product from cart
        $(".__anchDelete").click(function() {
            var itemid = $(this).attr("tag");
            $.post("/Services/CartCalls.asmx/DeleteCartItem", { itemID: itemid }, function(data) {
                if (data.ok == 1) {
                    $("#item_" + itemid).remove();
                    $('#anchItems').html('Items: ' + data.count + '/ $' + data.total);
                }
            }, "json");
        });
        $("#hlCloseSummary").click(function() {
            HideCart();
        });

    });
}