﻿history.navigationMode = 'compatible';
var loaded = false;
$(function () {
    populateProducts();
    popuplateTotal();
    loaded = true;
});

function popuplateTotal() {
    $.ajax({
        type: "GET",
        url: "GetTotal.aspx",
        data: "",
        success: function (result) {
            $("#total").html(result);

            if (result.length == 0)
                $("#total").css("display", "none");
            else
                $("#total").css("display", "inline");
        }
    });
}

function populateProducts() {
    $(".product").each(function (index, value) {
        $.ajax({
            type: "GET",
            url: "GetProducts.aspx?id=" + value.id,
            data: "",
            success: function (result) {
                $(value).html(result);
                $('.button').unbind('click');
                $('.button').click(onClickAddProduct);
            }
        });
    });

    if (!loaded) {
        $(".groupProduct").each(function (index, value) {
            $.ajax({
                type: "GET",
                url: "GetProducts.aspx?group=" + value.id,
                data: "",
                success: function (result) {
                    $(value).html(result);
                    $('.addGroup').unbind('click');
                    $('.addGroup').click(onAddGroup);
                }
            });
        });
    }

    $(".inlineProduct").each(function (index, value) {
        $.ajax({
            type: "GET",
            url: "GetProducts.aspx?alt=yes&id=" + value.id,
            data: "",
            success: function (result) {
                $(value).html(result);
                $('.button').unbind('click');
                $('.button').click(onClickAddProduct);
            }
        });
    }); 

    $(".inlineProductTitle").each(function (index, value) {
        $.ajax({
            type: "GET",
            url: "GetProducts.aspx?titleAlt=yes&id=" + value.id,
            data: "",
            success: function (result) {
                $(value).html(result);
                $('.button').unbind('click');
                $('.button').click(onClickAddProduct);
            }
        });
    });
}

function onAddGroup() {
    var button = this;
    var div = $(this).parent().parent();
    var checkContainer = div.children(".subGroupItems").first();
    var checkedItems = checkContainer.children(":checked");
    var select = $(div).children("p").first().children("select");
    
    var dataString = "multiAdd=yes&id=";

    for (var i = 0; i < checkedItems.length; i++)
        dataString += checkedItems[i].id + ":";    

    if (select.val() != "none")
        dataString += select.val();
    else {
        if (checkedItems.length == 0) {
            alert("Please make a selection first");
            return;
        }
    }

    if (select.length == 2) {
        var option = select.get(1);
        var val = $(option).val();

        if (val == "none" && select.val() != "none") {
            alert("Please ensure you've selected all required fields first");
            return;
        }

        dataString += "&option=" + option.id + ":" + val;
    }

    $(button).attr("disabled", "disabled");

    $.ajax({
        type: "POST",
        url: "Cart.aspx",
        data: dataString,
        success: function (result) {
            updateTotal(result);
            div.fadeOut(1000, function () {
                select.val("none");
                checkedItems.removeAttr("checked");
                $(button).removeAttr("disabled");
            });

            div.fadeIn(600);
            populateProducts();
        }
    });
}

function onClickAddProduct() {
    // validate and process form here      
    var button = this;
    var div = $(button).parent().parent().get(0);
    var dataString = 'add=' + div.id;

    $.ajax({
        type: "POST",
        url: "Cart.aspx",
        data: dataString,
        success: function (result) {
            var quantity = result.split(":")[0];
            var total = result.split(":")[1];
            $(button).parent().children("span.quantity").html(quantity);
            updateTotal(total);
        }
    });

    return false;
}

function updateTotal(amount) {
    if ($("#total").css("display") == "none")
        popuplateTotal();
    else
        popuplateTotal();

    $("#total").fadeOut("300");
    $("#total").fadeIn("300");
}

$(window).scroll(function () {
    $('#total').css('top', $(this).scrollTop() + "px");
});
