﻿
Type.registerNamespace("WebTemplate");

// Constructor
WebTemplate.Menu = function(element) {
    WebTemplate.Menu.initializeBase(this, [element]);
}

WebTemplate.Menu.prototype = {

    // Release resources before control is disposed.
    dispose: function() {

        var element = this.get_element();
        WebTemplate.Menu.callBaseMethod(this, 'dispose');
    },

    initialize: function() {
        var me = this;  // we need this to pass to the handling closures
        var element = this.get_element();
        
        // fetch our menu from the server
        this._service().GetMenu(
            function(menu) {
                var myself = me;
                me.buildMenu(menu);

            }
        );
        
        WebTemplate.Menu.callBaseMethod(this, 'initialize');
    },
    
    update: function() {
        var me = this;  // we need this to pass to the handling closures
        var mode = this.get_mode();

        // fetch our menu from the server
        this._service().GetMenu(
            function(menu) {
                var myself = me;
                me.buildMenu(menu);

            }
        );
        
        WebTemplate.Menu.callBaseMethod(this, 'update');
    },
    
    buildMenu: function(menuDef) {
    
        // find our menu div and clear it out
        var menuContainer = $(this.get_element());
        menuContainer.empty();
        
        var menuUL = $('<ul>').attr('id', 'navmenu').appendTo(menuContainer);
    
        for(var j = 0; j < menuDef.Items.length; j++)
        {
            this.createMenuItem(menuUL, menuDef.Items[j]);
        }
        
        menuUL.clickMenu();
    },
    
    createMenuItem: function(parentItem, menuOption) {
        
        var li = $('<li>').attr('id', menuOption.Id);
        
        // building html string instead of building up elements to avoid IE7 issue
        if(menuOption.Target == undefined || menuOption.Target == null || menuOption.Target == "") menuOption.Target = "#";
        li.html('<a href=\'' + menuOption.Target + '\' id=\'' + 'a_' + menuOption.Id + '\' class=\'banner\' title=\'' + menuOption.Description + '\'>' + menuOption.Title + '</a>');
        li.appendTo(parentItem);
        
        var subList = null;
        if( menuOption.Children.length > 0)
        {
            var subList = $('<ul>').appendTo(li);
        }
        
        for(var i = 0; i < menuOption.Children.length; i++)
        {
            this.createMenuItem(subList, menuOption.Children[i]);
        }
    
    }

}
// register the class and indicate we inherit from SYS.UI.Control
WebTemplate.Menu.registerClass('WebTemplate.Menu', WebTemplate.WebTemplateControl);

// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager 
// that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
