﻿//---------------------------------------------
// CartInfoBasic.js 
// Copyright (C) Jetshop AB 2009
//---------------------------------------------
Type.registerNamespace('JetShop.StoreControls');

JetShop.StoreControls.CartInfoBasic = function(element) {
    JetShop.StoreControls.CartInfoBasic.initializeBase(this, [element]);

    this._succeededHandler = null;
    this._failedHandler = null;
    this._divWrapper = null;

    this._divViewCartButton = null;
    this._cartDetailsPage = null;
    this._viewCartButtonImageUrl = null;
    this._viewCartButtonAltText = null;
    this._viewCartButtonEnabledCssClass = null;
    this._viewCartButtonCssClass = null;

    this._divCheckoutButton = null;
    this._checkOutPage = null;
    this._checkoutButtonImageUrl = null;
    this._checkoutButtonAltText = null;
    this._checkOutButtonCssClass = null;
    this._checkoutButtonEnabledCssClass = null;
    this._myPagesButtonImageUrl = null;
    this._myPagesButtonAltText = null;
    this._changeUserInfoPage = null;

    this._logoutButtonImageUrl = null;
    this._signOutPage = null;

    this._logInOutButtonCssClass = null;
    this._logInButtonCssClass = null;
    this._logOutButtonCssClass = null;
    this._logInButtonAltText = null;
    this._logOutButtonAltText = null;
}

JetShop.StoreControls.CartInfoBasic.prototype =
{
    initialize: function() {
        // initialize the base
        JetShop.StoreControls.CartInfoBasic.callBaseMethod(this, 'initialize');
        this._succeededHandler = Function.createDelegate(this, this._onSucceeded);
        this._failedHandler = Function.createDelegate(this, this._onFailed);

        this._divWrapper = $get(this.get_id() + "_wrapper");

        CI_Update(this.get_id(), "basic");
    },

    get_CartDetailsPage: function() {
        return this._cartDetailsPage;
    },

    set_CartDetailsPage: function(value) {
        this._cartDetailsPage = value;
    },

    get_ViewCartButtonImageUrl: function() {
        return this._viewCartButtonImageUrl;
    },

    set_ViewCartButtonImageUrl: function(value) {
        this._viewCartButtonImageUrl = value;
    },

    get_ViewCartButtonAltText: function() {
        return this._viewCartButtonAltText;
    },

    set_ViewCartButtonAltText: function(value) {
        this._viewCartButtonAltText = value;
    },

    get_ViewCartButtonEnabledCssClass: function() {
        return this._viewCartButtonEnabledCssClass;
    },

    set_ViewCartButtonEnabledCssClass: function(value) {
        this._viewCartButtonEnabledCssClass = value;
    },

    get_ViewCartButtonCssClass: function() {
        return this._viewCartButtonCssClass;
    },

    set_ViewCartButtonCssClass: function(value) {
        this._viewCartButtonCssClass = value;
    },

    get_CheckOutPage: function() {
        return this._checkOutPage;
    },

    set_CheckOutPage: function(value) {
        this._checkOutPage = value;
    },

    get_CheckoutButtonImageUrl: function() {
        return this._checkoutButtonImageUrl;
    },

    set_CheckoutButtonImageUrl: function(value) {
        this._checkoutButtonImageUrl = value;
    },

    get_CheckoutButtonAltText: function() {
        return this._checkoutButtonAltText;
    },

    set_CheckoutButtonAltText: function(value) {
        this._checkoutButtonAltText = value;
    },

    get_CheckOutButtonCssClass: function() {
        return this._checkOutButtonCssClass;
    },

    set_CheckOutButtonCssClass: function(value) {
        this._checkOutButtonCssClass = value;
    },

    get_CheckoutButtonEnabledCssClass: function() {
        return this._checkoutButtonEnabledCssClass;
    },

    set_CheckoutButtonEnabledCssClass: function(value) {
        this._checkoutButtonEnabledCssClass = value;
    },

    set_MyPagesButtonImageUrl: function(value) {
        this._myPagesButtonImageUrl = value;
    },

    get_MyPagesButtonImageUrl: function() {
        return this._myPagesButtonImageUrl;
    },

    set_MyPagesButtonAltText: function(value) {
        this._myPagesButtonAltText = value;
    },

    get_MyPagesButtonAltText: function() {
        return this._myPagesButtonAltText;
    },

    set_ChangeUserInfoPage: function(value) {
        this._changeUserInfoPage = value;
    },

    get_ChangeUserInfoPage: function() {
        return this._changeUserInfoPage;
    },

    set_LogoutButtonImageUrl: function(value) {
        this._logoutButtonImageUrl = value;
    },

    get_LogoutButtonImageUrl: function() {
        return this._logoutButtonImageUrl;
    },

    set_SignOutPage: function(value) {
        this._signOutPage = value;
    },

    get_SignOutPage: function() {
        return this._signOutPage;
    },

    set_LogInOutButtonCssClass: function(value) {
        this._logInOutButtonCssClass = value;
    },

    get_LogInOutButtonCssClass: function() {
        return this._logInOutButtonCssClass;
    },

    set_LogInButtonCssClass: function(value) {
        this._logInButtonCssClass = value;
    },

    get_LogInButtonCssClass: function() {
        return this._logInButtonCssClass;
    },

    set_LogInButtonAltText: function(value) {
        this._logInButtonAltText = value;
    },

    get_LogInButtonAltText: function() {
        return this._logInButtonAltText;
    },

    set_LogOutButtonCssClass: function(value) {
        this._logOutButtonCssClass = value;
    },

    get_LogOutButtonCssClass: function() {
        return this._logOutButtonCssClass;
    },

    set_LogOutButtonAltText: function(value) {
        this._logOutButtonAltText = value;
    },

    get_LogOutButtonAltText: function() {
        return this._logOutButtonAltText;
    },

    loadCartInfo: function(displayType) {
        JetShop.StoreControls.Services.CartInfoService.LoadCartInfo(this._succeededHandler, this._failedHandler, this.get_id());
    },

    _onSucceeded: function(result) {
        var objInfo = $get(this.get_id() + "_wrapper");

        if (result.TotalItems > 0) {
            if (objInfo != null && objInfo != "undefined") {
                this._createLayout(false);
            }
        }
        else {
            if (objInfo == null || objInfo == "undefined") {
                this._createLayout(true);
            }
        }

        var id = this.get_id();

        if (result.TotalItems > 0) {
            var sum = $get(id + "_sum");
            if (sum != null && sum != "undefined") {
                if (result.GlobalShowSumInclVAT) {
                    $get(id + "_sum").innerHTML = result.DynamicTotalSumWithDiscount;
                }
                else {
                    $get(id + "_sum").innerHTML = result.TotalSum;
                }
            }

            var cartItems = $get(id + "_cartItems");
            if (cartItems != null && cartItems != "undefined") {
                $get(id + "_cartItems").innerHTML = result.TotalItems;
            }
        }

        if (result.IsAuthenticated == true) {

            var hlChangeUserInfoPage = $get(id + "_hlMyPagesButton");
            if (hlChangeUserInfoPage != "undefined" && hlChangeUserInfoPage != null) {
                hlChangeUserInfoPage.href = this._changeUserInfoPage;
            }

            var imgMyPagesButtonImageUrl = $get(id + "_imgMyPagesButton");
            if (imgMyPagesButtonImageUrl != "undefined" && imgMyPagesButtonImageUrl != null) {
                imgMyPagesButtonImageUrl.src = this._myPagesButtonImageUrl;
                imgMyPagesButtonImageUrl.alt = this._myPagesButtonAltText;
                imgMyPagesButtonImageUrl.title = this._myPagesButtonAltText;
            }

            var imgLogInOutButton = $get(id + "_imgLogInOutButton");
            if (imgLogInOutButton != "undefined" && imgLogInOutButton != null) {
                imgLogInOutButton.src = this._logoutButtonImageUrl;
                imgLogInOutButton.alt = this._logOutButtonAltText;
                imgLogInOutButton.title = this._logOutButtonAltText;
            }

            var hlLogInOutButton = $get(id + "_hlLogInOutButton");
            if (hlLogInOutButton != "undefined" && hlLogInOutButton != null) {
                hlLogInOutButton.href = this._signOutPage;

                hlLogInOutButton.parentNode.className = this._logInOutButtonCssClass + " " + this._logOutButtonCssClass;
            }

            // This checks if the page must be reloaded to render cart from scratch
            if (result.DisplayShoppingCart == true && (hlChangeUserInfoPage == "undefined" || hlChangeUserInfoPage == null)) {
                href = window.location.href;
                window.location.href = href;
            }
        }
    },

    _onFailed: function(result) {
        var test = 100;
    },

    _createLayout: function(emptyCart) {
        // Empty nodes

        var controlID = this.get_id();

        if (emptyCart == true) {
            // TODO
        }
        else {
            var divViewCartButton = $get(controlID + "_divViewCartButton");
            if (divViewCartButton != "undefined" && divViewCartButton != null) {
                divViewCartButton.className = this._viewCartButtonCssClass + ' ' + this._viewCartButtonEnabledCssClass;
            }

            var hlViewCartButton = $get(controlID + "_hlViewCartButton");
            if (hlViewCartButton != "undefined" && hlViewCartButton != null) {
                hlViewCartButton.href = this._cartDetailsPage;
                hlViewCartButton.alt = this._viewCartButtonAltText;
                hlViewCartButton.title = this._viewCartButtonAltText;
            }

            var imgViewCartButton = $get(controlID + "_imgViewCartButton");
            if (imgViewCartButton != "undefined" && imgViewCartButton != null) {
                imgViewCartButton.src = this._viewCartButtonImageUrl;
            }

            var lblViewCartButton = $get(controlID + "_lblViewCartButton");
            if (lblViewCartButton != "undefined" && lblViewCartButton != null) {
                hlViewCartButton.innerHTML = this._viewCartButtonAltText;
            }

            var divCheckoutButton = $get(controlID + "_divCheckoutButton");
            if (divCheckoutButton != "undefined" && divCheckoutButton != null) {
                divCheckoutButton.className = this._checkOutButtonCssClass + ' ' + this._checkoutButtonEnabledCssClass;
            }

            var hlCheckoutButton = $get(controlID + "_hlCheckoutButton");
            if (hlCheckoutButton != "undefined" && hlCheckoutButton != null) {
                hlCheckoutButton.href = this._checkOutPage;
                hlCheckoutButton.alt = this._checkoutButtonAltText;
                hlCheckoutButton.title = this._checkoutButtonAltText;
            }

            var imgCheckoutButton = $get(controlID + "_imgCheckoutButton");
            if (imgCheckoutButton != "undefined" && imgCheckoutButton != null) {
                imgCheckoutButton.src = this._checkoutButtonImageUrl;
            }

            var lblCheckoutButton = $get(controlID + "_lblCheckoutButton");
            if (lblCheckoutButton != "undefined" && lblCheckoutButton != null) {
                lblCheckoutButton.innerHTML = this._checkoutButtonAltText;
            }

            var imgLogoutButton = $get(controlID + "_imgLogoutButton");
            if (imgLogoutButton != "undefined" && imgLogoutButton != null) {
                imgLogoutButton.src = this._logoutButtonImageUrl;
            }
        }
    },

    dispose: function() {
        // call to the base to do its dispose
        JetShop.StoreControls.CartInfoBasic.callBaseMethod(this, 'dispose');
    }
}

JetShop.StoreControls.CartInfoBasic.registerClass('JetShop.StoreControls.CartInfoBasic', Sys.UI.Control);

function CI_Update(controlID, displayType)
{     
	$find(controlID).loadCartInfo(displayType);
}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();