﻿namespace('nnet');

nnet.FooterToolBar = $j.klass({
    initialize: function() {
        this.ToolBar = null;
        this.CartButton = null;
        this.ProfileButton = null;
        this.AdminButton = null;
    },

    // event handler so not in context.
    initFooterToolBar: function(sender) {
        try {
            NNet.FooterToolBar.ToolBar = sender;
            sender.add_buttonClicked(NNet.FooterToolBar.buttonClick);
            NNet.FooterToolBar.CartButton = sender.findItemByValue('Cart');
            NNet.FooterToolBar.ProfileButton = sender.findItemByValue('Profile');
            NNet.FooterToolBar.AdminButton = sender.findItemByValue('Admin');

            NNet.FooterToolBar.CartButton.hide();
            NNet.FooterToolBar.ProfileButton.hide();
            NNet.FooterToolBar.AdminButton.hide();

            //RefreshUserStatus();
        } catch (e) { logE("FooterToolBar.initFooterToolBar", e); }
    },

    // event handler so not in context.
    buttonClick: function(sender, e) {
        try {
            var button = e.get_item().get_value();
            if (button == "Admin") {
                window.open("http://admin.neighbournet.com/", 'NNetAdmin');
                return false;
            } else if (button == "Print") {
                return true;
            } else if (button == "Cart") {
                NNet.CurrentUser.ShoppingCart.show();
                return false;
            } else if (button == "Profile") {
                NNet.CurrentUser.show();
                return false;
            }
        } catch (e) { logE("FooterToolBar.buttonClick", e); }
    }

});

FooterToolBar = $j.klass({
    initialize: function(option) {
        //$j('.button', this.element).attach(FooterButton);

        this.CartButton = $j('.footer-toolbar #Cart');
        this.ProfileButton = $j('.footer-toolbar #Profile');
        this.AdminButton = $j('.footer-toolbar #Admin');


        if (option.IsUserLoggedIn) {
            this.ProfileButton.show();
            if (option.IsUserAdmin) this.AdminButton.show();
            if (option.UserHasShoppingCart) this.CartButton.show();
        }

        onLogIn.subscribe(
            function(type, args) {
                try {
                    if (!args[0].IsStartUp) {
                        var tb = FooterToolBar.instances[0];   // get reference to it.
                        tb.ProfileButton.show();
                        if (args[0].IsAdmin) tb.AdminButton.show();
                    }
                } catch (e) { logE("FooterToolBar.onLogIn handler", e); }
            }
        );

        onLogOut.subscribe(
            function(type, args) {
                try {
                    if (!args[0].IsStartUp) {
                        var tb = FooterToolBar.instances[0];   // get reference to it.
                        tb.ProfileButton.hide();
                        tb.AdminButton.hide();
                        tb.CartButton.hide();
                    }
                } catch (e) { logE("FooterToolBar.onLogOut handler", e); }
            }
        );

        onShoppingCart.subscribe(
            function(type, args) {
                try {
                    var tb = FooterToolBar.instances[0];   // get reference to it.
                    if (args[0].DisplyShoppingCart) tb.CartButton.show();
                    else tb.CartButton.hide();
                } catch (e) { logE("FooterToolBar.onShoppingCart handler", e); }
            }
        );

    },

    onclick: function(e) {
        var target = $j(e.target);
        if (target.is('#Admin')) window.open("http://admin.neighbournet.com/", 'NNetAdmin');
        else if (target.is('#Profile')) NNet.CurrentUser.show();
        else if (target.is('#Cart')) NNet.CurrentUser.ShoppingCart.show();
    }

});


FooterButton = $j.klass({
    initialize: function() {
    },
    onmouseover: function() {
        this.element.addClass('button-over');
    },
    onmouseout: function() {
        this.element.removeClass('button-over');
        this.element.removeClass('button-down');
        $j('img.icon', this).removeClass('icon-over');
    },
    onmousedown: function() {
        this.element.addClass('button-down');
        $j('img.icon', this).addClass('icon-over');
    },
    onmouseup: function() {
        this.element.removeClass('.button-down');
        $j('img.icon', this).removeClass('icon-over');
    },
    hide: function() {
        this.element.hide();
    },
    show: function() {
        this.element.show();
    }

});

