$(document).ready(function() {

    /* The Cart  ----------------------------- */
    $("#show-cart").toggle(function() {
        if ($.browser.msie) {
            $("#cart").show();
        }
        else {
            $("#cart").fadeIn("fast");
        }
        
        return false;
    }, function() {
        if ($.browser.msie) {
            $("#cart").hide();
        }
        else {
            $("#cart").fadeOut("fast");
        }
        
        
        });


    $("#cart a.close").click(function() {
        if ($.browser.msie) {
            $("#cart").hide();
        }
        else {
            $("#cart").fadeOut("fast");
        }
        return false;
    });



    $("#cart .item-delete").live('click', function() {
        var btn = $(this);

        var index = $(this).closest("tr").find("input[name='cartrows.index']").val();
        var id = $(this).closest("tr").find("input[name='cartrows[" + index + "].ProductID']").val();
        var descID = $(this).closest("tr").find("input[name='cartrows[" + index + "].DescriptionID']").val();

        $.ajax({
            type: "POST",
            url: "/Shop/RemoveProductFromCart",
            dataType: "text",
            data: { id: id, descriptionID: descID },
            success: function(data) {
                if (data === null || data === "") data = "0";
                $("span#cart_total").text(data + ":-");
                btn.parents('tr').fadeOut('fast', function() {
                    $(this).remove();
                });
            }
        });

        return false;
    });


    //$("#tellfriend_submit").click(function() {
    //alert($("#tellfriend_form").validate());
    //alert($("#tellfriend_form").Validate());
    /*
    $.ajax({
    type: "POST",
    url: "/Home/SendTellFriend",
    dataType: "json",
    data: $("#tellfriend_form :input"),
    success: function(data) {
    alert("data");
    }
    });
    */
    //    return false;
    //});

    $("#cart-update").live('click', function() {
        //var bla = [{"guid":"f021b312-1d6f-43f9-9d6a-3d19dbc1e062","itemPrice":5},{"guid":"6311b82e-cf6c-4c04-8244-a5721ea93083","itemPrice":0},{"guid":"646f4114-993f-481a-9a6d-8e130792288d","itemPrice":7495}];
        $.ajax({
            type: "POST",
            url: "/Shop/UpdateCartQuantity",
            dataType: "json",
            data: $("#cart :input"),
            success: function(data) {
                if (data === null || data === "") return;
                var sum = 0;
                for (var i = 0; i < data.length; i++) {
                    var el = $("#cart input[name='cartrows[" + data[i].guid + "].Quantity']");
                    if (data[i].itemPrice === 0) { // delete row
                        el.parents("tr").fadeOut('fast', function() {
                            $(this).remove();
                        });
                    } else {
                        sum += data[i].itemPrice;
                        el.parents().find("td.item-price").text(data[i].itemPrice + ":-");
                    }
                }
                $("span#cart_total").text(sum + ":-");
            }
        });
        return false;
    });





    $("#cart #cart-empty").click(function() {
        $.ajax({
            type: "POST",
            url: "/Shop/ClearCart",
            dataType: "text",
            success: function(data) {
                $("#cart table").slideUp(function() {
                    $("span#cart_total").text("0:-");
                    $(this).remove();
                    $("#cart .empty").animate({ height: 30 }, 300);
                });
            }
        });
        return false;
    });

    $("#tipsa").toggle(function() {
        if ($.browser.msie) {
            $("#tipsa_box").show();
        }
        else {
            $("#tipsa_box").fadeIn("fast");
        }
        
        return false;
    }, function() { 
        if ($.browser.msie) {
            $("#tipsa_box").hide();
        }
        else {
            $("#tipsa_box").fadeOut("fast");
            }
        
        });

    $("#tipsa_box a.close").click(function() {
        if ($.browser.msie) {
            $("#tipsa_box").hide();
        }
        else {
            $("#tipsa_box").fadeOut("fast");
        }
        return false;
    });

    $('#new_products_switcher').cycle();


    $("#betalningsvilkor").click(function() {

        $.get('/Content/betalningsvilkor.htm', function(data) {
        $('#overlay_box .padding').html(data);
        
            if ($.browser.msie) {
                $('#overlay_box').show();
            }
            else {
                $('#overlay_box').fadeIn();
            }
           
        });
        return false;

    });


    $("#leverans").click(function() {

        $.get('/Content/leverans.htm', function(data) {
            $('#overlay_box .padding').html(data);
            if ($.browser.msie) {
                $('#overlay_box').show();
            }
            else {
                $('#overlay_box').fadeIn();
            }
        });
        return false;

    });






    $("#overlay_box a.close").click(function() {
        if ($.browser.msie) {
            $('#overlay_box').hide();
        }
        else {
            $('#overlay_box').fadeOut();
        }
        return false;
    });

});


function blinkCart() {
    $("#cartinfo").animate({ backgroundColor: "#3399FD" }, 50,
    function() {
    $("#cartinfo").animate({ backgroundColor: "white" }, 500)
    });
}