﻿/// <reference name="MicrosoftAjax.js" />
/// <reference path="jquery.timers.js" />
/// <reference path="ITG.js" />

ITG.Client.ShoppingCart = {};

ITG.Client.ShoppingCart.Manager = function(url, cartLinkID, checkoutLinkID)
{
    this._cartLinkID = cartLinkID;
    this._checkoutLinkID = checkoutLinkID;
    this._checkoutPostBack = null;
    this._timeOut = 0;
    this._url = url;
}

ITG.Client.ShoppingCart.Manager.prototype =
{
    get_cartLinkID: function()
    {
        return this._cartLinkID;
    },

    set_cartLinkID: function(value)
    {
        this._cartLinkID = value;
    },

    get_checkoutLinkID: function()
    {
        return this._checkoutLinkID;
    },

    set_checkoutLinkID: function(value)
    {
        this._checkoutLinkID = value;
    },

    get_checkoutPostBack: function()
    {
        return this._checkoutPostBack;
    },

    set_checkoutPostBack: function(value)
    {
        this._checkoutPostBack = value;
    },

    get_timeOut: function()
    {
        return this._timeOut;
    },

    set_timeOut: function(value)
    {
        this._timeOut = value;
    },

    get_url: function()
    {
        return this._url;
    },

    set_url: function(value)
    {
        this._url = value;
    },

    sessionEnded: function()
    {
        var message;
        var window;

        message = "We apologize but due to {0} minutes of inactivity, your cart has been cleared. Please try again.";
        message = String.format(message, this.get_timeOut());

        radWindow = radalert(message, 300, 150, "Inside the Grape");
        radWindow.add_close(function()
        {
            if (ITG.Client.eCommerce) ITG.Client.eCommerce.deleteCart(function()
            {
                document.location.replace(document.location.href);
            });
        });
    },

    startTimer: function()
    {
        var me = this;
        var timeout = this.get_timeOut() * 60 * 1000;

        $(document).stopTime("sessionTimer");
        $(document).everyTime(timeout, "sessionTimer", function()
        {
            me.sessionEnded();
        });
    },

    update: function(sessionId, onlineRetailerId)
    {
        var me = this;
        var parameters = "{ sessionId: '" + sessionId + "', onlineRetailerId: " + onlineRetailerId + " }";
        var webservice = this.get_url() + "/GetShoppingCartTotals";

        try
        {
            $.ajax(
            {
                type: "POST",
                url: webservice,
                data: parameters,
                contentType: "application/json",
                dataType: "json",

                error: function(result)
                {
                    radalert("An error occurred attempting to retrieve the shopping cart totals.", 300, 150, "Inside the Grape");
                },

                success: function(result)
                {
                    var cartLink = $("#" + me.get_cartLinkID());
                    var checkoutLink = $("#" + me.get_checkoutLinkID());
                    var count = 0;
                    var total = 0;

                    if (result)
                    {
                        count = result.Count;
                        total = result.FormattedTotal;
                    }

                    if (count > 0)
                    {
                        cartLink.html(String.format("{0} bottles ({1})", count, total));
                        checkoutLink.attr("href", "javascript:" + me.get_checkoutPostBack());
                        checkoutLink.removeAttr("disabled");
                    }
                    else
                    {
                        cartLink.html("0 bottles ($0.00)");
                        checkoutLink.attr("disabled", "disabled");
                        checkoutLink.removeAttr("href");
                    }
                }
            });
        }
        catch (e)
        {
        }

        return false;
    }
}
