﻿/*
Javascript control showing a horizontal Menu
*/

var WebMenu = Class.create();

WebMenu.prototype =
{
    // Constructor
    initialize : function(piEntityId, piTarget)
    {
        // Declare variable holding the id of the content entity
        this.Id = piEntityId;
        // Declare variable holding the id of the entity's html node
        this._nodeId = 'WebMenu_' + piEntityId;
        // Declare variable holding the id of the html node which serves as content holder for this entity
        this._target = piTarget;
        
        // Declare variable holding the entity object
        this._entity;
        
        // Declare Hash variable holding the WebMenuItem objects
        this._menuItems = new Hash();
        
        // Declare variable holding the html node which serves as content container
        this._htmlContainer;
        
        // Call Build function to build the content holder
        this.Build();
        // Call RequestData function to get the xml data from the web service
        this.RequestData();
    },
    
    Build : function()
    {
        try
        {
            // Check if the html container is already part of the document body
            if($(this._nodeId) != null)
            {
                // Fill this._htmlContainer and return
                $(this._nodeId).parentNode.removeChild($(this._nodeId));
            }
            
            // Build new content holder node
            this._htmlContainer = Builder.node('div', {id: this._nodeId, style: 'background-color:#FFFFFF'});
            this._htmlContainer.className = 'WebMenu';
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'Build', arguments, 'WEBMENU', this.Id, e, false)
        }
    },
    
    RequestData : function()
    {
        try
        {
            // Write Log Entry
            Logs.WriteNotification('NOTIFY_REQUESTING_DATA', 'RequestData', arguments, 'WEBMENU', this.Id)
            // Create input parameters for the webservice method
            var params=$H( {piHostHeader : Site.HostHeader, piEntityId : this.Id} ).toQueryString();
            // Create new Ajax request
            new Ajax.Request("ISDataService.asmx/GetEntityData", { method : "post", parameters : params, requestHeaders : ['Pragma', 'no-cache', 'Cache-Control', 'no-store, no-cache, max-age=0, must-revalidate'], onSuccess : this.OnDataRequestResponse.bind(this), onFailure: this.OnRequestFail.bind(this) });
        }
        catch(e)
        {
            Logs.WriteException('ERROR_STARTING_REQUEST', 'RequestData', arguments, 'WEBMENU', this.Id, e, false)
        }
    },
    
    OnRequestFail : function()
    {
        Logs.WriteError('ERROR_REQUESTING_DATA', 'RequestData', null, 'WEBTEXT', this.Id);
    },
    
    // Receives entity's xml data from the webservice
    OnDataRequestResponse : function(transport)
    {
        try
        {
            // Write Log Entry
            Logs.WriteNotification('NOTIFY_RECEIVING_DATA', 'OnDataRequestResponse', arguments, 'WEBMENU', this.Id)
            // Set local variable holding the xml data
            this._entity = new EntityObject(new XmlDocument(transport.responseXML));
            
            // Write error log if the last interaction did not succeed
            if(this._entity.Success == false)
            {
                // Write Error to the Log
                Logs.WriteError('ERROR_MISSING_XML_DATA', 'OnDataRequestResponse', arguments, 'WEBMENU', this.Id)
                
                return
            }
                    
            // Call ProcessContentInformation function to create entity objects/controls
            this.ProcessContentInformation();
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'OnDataRequestResponse', arguments, 'WEBMENU', this.Id, e, false)
        }
    },
    
    ProcessContentInformation : function()
    {                
        try
        {
            // Check if any link reference exist and iterate through the link references
            if((this._entity.Content.References != null) && (this._entity.Content.References.length > 0))
            {               
                for(var i = 0; i < this._entity.Content.References.length; i++)
                {                    
                    // Create a new WebMenuItem object
                    var menuItem = new WebMenuItem(this._entity.Content.References[i].Id, this);
                    
                    if(i == this._entity.Content.References.length -1)
                    {
                        menuItem._linkHolder.style.width = '110px';
                        menuItem._linkHolder.style.border = 'none';
                        
                        menuItem._bgHolder.style.width = '110px';
                        menuItem._bgHolder.style.border = 'none';
                    }
                    
                    // Add the WebMenuItem object to the _menuItems Hash
                    this._menuItems[this._entity.Content.References[i].Id] = menuItem;
                    
                    // Append the WebMenuItem object to the _htmlContainer
                    this._htmlContainer.appendChild(menuItem.GetHtml());
                }
            }
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'ProcessContentInformation', arguments, 'WEBMENU', this.Id, e, false)
        }
    },
    
    Show : function()
    {
        try
        {
            // Get the target html node
            var targetNode = $(this._target);
            // Check if the the target html node is null
            if(targetNode == null)
            {
                // Create new message box
                var msg = new WebMessage(Lingo.GetLingoItem('Error'), Lingo.GetLingoMessage('MSG_MISSING_MAIN_TARGET_AREA', 'WebMenu', this.Id));
                // Show message box
                msg.Show();
                return;
            }
            
            // Append content holder to target html node
            targetNode.appendChild(this._htmlContainer);
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'Show', arguments, 'WEBMENU', this.Id, e, false)
        }
    },
    
    NotifyLinkClick : function(piMenuItemId)
    {
        if(piMenuItemId == null)
        {
            return;
        }
        
        // Iterate through _menuItems Hash and set each item inactive
        for(var i = 0; i < this._menuItems.keys().length; i++)
        {
            var item = this._menuItems[this._menuItems.keys()[i]];
            item.SetInactive();
        }
        
        // Get the item that is to be set active
        var activeItem = this._menuItems[piMenuItemId];
        // Check if the item exists, set active and switch to the according page if so
        if(activeItem != null)
        {
            activeItem.SetActive();
        }
    },
    
    NotifyPageChange : function(piPageId)
    {
        if(piPageId == null)
        {
            return;
        }
        
        for(var i = 0; i < this._menuItems.keys().length; i++)
        {
            var item = this._menuItems[this._menuItems.keys()[i]];
            if((item.PageId != null) && (item.PageId == piPageId))
            {
                this.NotifyLinkClick(item.Id);
                return;
            }
        }
        
        // Iterate through _menuItems Hash and set each item inactive
        for(var i = 0; i < this._menuItems.keys().length; i++)
        {
            var item = this._menuItems[this._menuItems.keys()[i]];
            item.SetInactive();
        }
    },
    
    Disable : function()
    {
        // Iterate through _menuItems Hash and disable each item
        for(var i = 0; i < this._menuItems.keys().length; i++)
        {
            var item = this._menuItems[this._menuItems.keys()[i]];
            item.Disable();
        }
    },
    
    Enable : function()
    {
        // Iterate through _menuItems Hash and enable each item
        for(var i = 0; i < this._menuItems.keys().length; i++)
        {
            var item = this._menuItems[this._menuItems.keys()[i]];
            item.Enable();
        }
    }
}