﻿/*
Javascript control showing a single WebSubMenu entity
*/

var WebPageMenu = Class.create();

WebPageMenu.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 = 'WebPageMenu_' + 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 variable holding the html node which serves as content wrapper
        this._htmlContainer;
        // Declare variable holding the html node which serves as title container
        this._titleContainer;
        // Declare variable holding the html node which serves as content container
        this._contentContainer;
        
        // 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
        {
            // Build new content holder node
            this._htmlContainer = Builder.node('div', {id: this._nodeId});
            this._htmlContainer.className  = 'WebPageMenu';
            
            // Build title container
            this._titleContainer = Builder.node('div');
            this._titleContainer.className = 'WebPageMenuTitle';
            
            // Build content container
            this._contentContainer = Builder.node('div');
            this._contentContainer.className = 'WebPageMenuContent';
            
            this._htmlContainer.appendChild(this._titleContainer);
            this._htmlContainer.appendChild(this._contentContainer);
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'Build', arguments, 'WEBPAGEMENU', this.Id, e, false)
        }
    },
    
    RequestData : function()
    {
        try
        {
            // Write Log Entry
            Logs.WriteNotification('NOTIFY_REQUESTING_DATA', 'RequestData', arguments, 'WEBPAGEMENU', 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, 'WEBPAGEMENU', this.Id, e, false)
        }
    },
    
    OnRequestFail : function()
    {
        Logs.WriteError('ERROR_REQUESTING_DATA', 'RequestData', null, 'WEBPAGEMENU', this.Id);
    },
    
    // Receives entity's xml data from the webservice
    OnDataRequestResponse : function(transport)
    {
        try
        {
            // Write Log Entry
            Logs.WriteNotification('NOTIFY_RECEIVING_DATA', 'OnDataRequestResponse', arguments, 'WEBPAGEMENU', 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, 'WEBPAGEMENU', this.Id)
                
                return
            }
                    
            // Call ProcessContentInformation function to create entity objects/controls
            this.ProcessContentInformation();
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'OnDataRequestResponse', arguments, 'WEBPAGEMENU', this.Id, e, false)
        }
    },
    
    ProcessContentInformation : function()
    {        
        try
        {
            // Check if the title value is an empty string
            if(!this._entity.Content.Title.Value.blank())
            {
                // Build title node
                var titleNode = Builder.node('h1');
                // Append the text node to the title node
                titleNode.appendChild(document.createTextNode(this._entity.Content.Title.Value));
                // Append the title html node to the content holder node
                this._titleContainer.appendChild(titleNode);
            }
            
            // Create and append title image
            var titleImage = Builder.node('img', { src: 'Template/INVENIT1/Image/titleImage.gif' });
            this._titleContainer.appendChild(titleImage);
            
            // Check if any link reference exist and iterate through the link references
            if((this._entity.Content.References != null) && (this._entity.Content.References.length > 0))
            {
                // Create unordered list node to hold the links and append it to the html container
                var listNode = Builder.node('ul');
                this._contentContainer.appendChild(listNode);
                
                for(var i = 0; i < this._entity.Content.References.length; i++)
                {
                    // Create list item node to hold the current link and add it to the unordered list node
                    var listItemNode = Builder.node('li');
                    listNode.appendChild(listItemNode);
                    // Create new WebLink object
                    var webLink = new WebLink(this._entity.Content.References[i].Id, listItemNode);
                    
                    // Check if a link image is configured
                    if((this._entity.Configuration.Linkimage != null) && (!this._entity.Configuration.Linkimage.Value.blank()))
                    {
                        webLink.SetImage(this._entity.Configuration.Linkimage.Value);
                    }
                    
                    // Show the WebLink
                    webLink.Show();
                }
            }
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'ProcessCOntentInformation', arguments, 'WEBPAGEMENU', 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', 'WEBPAGEMENU', 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, 'WEBPAGEMENU', this.Id, e, false)
        }
    }
}