﻿namespace('nnet');

nnet.HeaderToolBar = $j.klass({
    initialize: function() {
        this.OptionsPanel = null;
        this.OptionsContentPanel = null;

        this.OptionsToolBar = null;
        this.OptionsDisplayedToolBar = null;

        this.LoginButton = null;
        this.LogoutButton = null;
        this.SignUpButton = null;
        this.SearchButton = null;
        this.HelpButton = null;

        this.SignUpSeparator = null;
    },


    // Called from toolbar when initialised
    // event handler so not in context.
    initOptionsToolBar: function(sender) {
        try {
            NNet.HeaderToolBar.OptionsToolBar = sender;
            sender.add_buttonClicked(NNet.HeaderToolBar.optionButtonClick);
            NNet.HeaderToolBar.LoginButton = sender.findItemByValue('Login');
            NNet.HeaderToolBar.LogoutButton = sender.findItemByValue('Logout');
            NNet.HeaderToolBar.SignUpSeparator = sender.findItemByValue('SignUpSeparator');
            NNet.HeaderToolBar.SignUpButton = sender.findItemByValue('SignUp');
            NNet.HeaderToolBar.SearchButton = sender.findItemByValue('Search');
            NNet.HeaderToolBar.HelpButton = sender.findItemByValue('Help');

            // the following should be available
            NNet.HeaderToolBar.OptionsPanel = $j(sVars.PageOptionsPanelID);
            NNet.HeaderToolBar.OptionsContentPanel = $j(sVars.PageOptionsContentID);

            onLogIn.subscribe(
                function(type, args) {
                    try {
                        if (!args[0].IsStartUp) { NNet.HeaderToolBar.showLogout(); }
                    } catch (e) { logE("HeaderToolBar.onLogIn handler", e); }
                }
            );


        } catch (e) { logE("HeaderToolBar.initOptionsToolBar", e); }
    },

    // event handler so not in context.
    initOptionsDisplayedToolBar: function(sender) {
        try {
            NNet.HeaderToolBar.OptionsDisplayedToolBar = sender;
            sender.add_buttonClicked(NNet.HeaderToolBar.optionDisplayedButtonClick);
        } catch (e) { logE("HeaderToolBar.initOptionsDisplayedToolBar", e); }
    },

    // event handler so not in context.
    initSearchDisplayedToolBar: function(sender) {
        try {
        } catch (e) { logE("HeaderToolBar.initOptionsDisplayedToolBar", e); }
    },


    // event handler so not in context.
    optionButtonClick: function(sender, e) {
        try {
            var button = e.get_item().get_value();
            if (button == "Login") {
                NNet.Security.showLogin(null);
                return false;
            } else if (button == "Logout") {
                NNet.Security.logout();
                return false;
            } else if (button == "Help") {
                NNet.Help.show({ uri: "/Help/Services/Listing/Guildlines" });
                return false;
            } else if (button == "Search") {
                //$j(sVars.PageOptionsPanelID).hide();
                //$j(sVars.SearchOptionsContentID).show();
                return false;
            } else if (button == "Feedback") { // Not in menu any more
                window.open("http://support.neighbournet.com/fogbugz/ServiceDirectory.html", "Feedback");
                return false;
            } else if (button == "SignUp") {
                NNet.CurrentUser.signUp();
                return false;
            }
        } catch (e) { logE("HeaderToolBar.optionButtonClick", e); }
    },

    // event handler so not in context.
    optionDisplayedButtonClick: function(sender, e) {
        try {
            NNet.HeaderToolBar.OptionsContentPanel.hide();
            NNet.HeaderToolBar.OptionsPanel.show();
            NNet.Security.hideLogin();
        } catch (e) { logE("HeaderToolBar.optionDisplayedButtonClick", e); }
    },

    showLogin: function() {
        try {
            this.SignUpButton.show();
            this.SignUpSeparator.show();
            this.LoginButton.show()
            this.LogoutButton.hide();
        } catch (e) { logE("HeaderToolBar.showLogin", e); }
    },

    showLogout: function() {
        try {
            this.SignUpButton.hide();
            this.SignUpSeparator.hide();
            this.LoginButton.hide();
            this.LogoutButton.show();
        } catch (e) { logE("HeaderToolBar.showLogin", e); }
    }


});



