﻿/*
Javascript control showing a footer
*/

var WebFooter = Class.create();

WebFooter.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 = 'WebFooter_' + 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 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
        {
            // Build new link node
            this._htmlContainer = Builder.node('div', { id: this._nodeId });
            this._htmlContainer.className = 'WebFooter';
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'Build', arguments, 'WEBFOOTER', this.Id, e, false)
        }
    },
    
    RequestData : function()
    {
        try
        {
            // Write Log Entry
            Logs.WriteNotification('NOTIFY_REQUESTING_DATA', 'RequestData', arguments, 'WEBFOOTER', 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, 'WEBFOOTER', 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, 'WEBFOOTER', 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, 'WEBFOOTER', this.Id)
                
                return
            }
                    
            // Call ProcessContentInformation function to create entity objects/controls
            this.ProcessContentInformation();
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'OnDataRequestResponse', arguments, 'WEBFOOTER', 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))
            {
                // Create unordered list node to hold the links and append it to the html container
                var listNode = Builder.node('ul');
                this._htmlContainer.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);
                    
                    if((i + 1) == this._entity.Content.References.length)
                    {
                        webLink._htmlContainer.style.border = 'none';
                    }
                    
                    // Set id of the list item node according to the node id of the WebLink
                    listItemNode.id = webLink._nodeId + 'holder';
                                   
                    // Show the WebLink
                    webLink.Show();
                }
            }
            
            // Check if the title value is an empty string
            if((this._entity.Content.Title != null) && (!this._entity.Content.Title.Value.blank()))
            {
                // Build title node
                var titleNode = Builder.node('h1');
                // Append the title value to the title html node
                titleNode.appendChild(document.createTextNode(this._entity.Content.Title.Value));
                // Append the title html node to the content holder node
                this._htmlContainer.appendChild(titleNode);
            }
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'ProcessContantInformation', arguments, 'WEBFOOTER', 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', 'WebLink', 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, 'WEBFOOTER', this.Id, e, false)
        }
    }
}