﻿/*
Javascript control showing a single WebSubMenu entity
*/

var WebLink = Class.create();

WebLink.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 = 'WebLink_' + 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('a', { id: this._nodeId });
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'Build', arguments, 'WEBLINK', this.Id, e, false)
        }
    },
    
    RequestData : function()
    {
        try
        {
            // Write Log Entry
            Logs.WriteNotification('NOTIFY_REQUESTING_DATA', 'RequestData', arguments, 'WEBLINK', 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, 'WEBLINK', 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, 'WEBLINK', 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, 'WEBLINK', this.Id)
                
                return
            }
                    
            // Call ProcessContentInformation function to create entity objects/controls
            this.ProcessContentInformation();
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'OnDataRequestResponse', arguments, 'WEBLINK', this.Id, e, false)
        }
    },
    
    ProcessContentInformation : function()
    {   
        try
        {     
            // Check if the title value or link value are emtpy strings and return if so
            if((this._entity.Content.Title.Value.blank()) || (this._entity.Content.Link.Value.blank()))
            {
                return;
            }
            
            // Declare linkNode variable for the html link element
            var linkNode = this._htmlContainer;
            
            // Check if the link is an internal or external link r a javascript function call
            if(this._entity.Content.Link.Value.toLowerCase().startsWith('http'))
            {
                // Add external link attributes to the html link
                linkNode.href = this._entity.Content.Link.Value;
                linkNode.target = '_blank';
            }
            else if(this._entity.Content.Link.Value.toLowerCase().startsWith('javascript'))
            {
                linkNode.href = this._entity.Content.Link.Value;
            }
            else
            {
                // Add internal link attributes to the html link
                linkNode.href = this._entity.Content.Link.Value;
                // Set an event handler function for the click event
                Event.observe(linkNode, 'click', StateMgr.OnLinkClick);
            }
            
            // Append the title text to the link node
            linkNode.appendChild(document.createTextNode(this._entity.Content.Title.Value));
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'ProcessContentInformation', arguments, 'WEBLINK', 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, 'WEBLINK', this.Id, e, false)
        }
    },
    
    SetImage : function(piImageUrl)
    {
        try
        {
            // Set padding
            this._htmlContainer.style.paddingLeft = '20px';
            // Set the background image
            this._htmlContainer.style.backgroundRepeat = 'no-repeat';
            this._htmlContainer.style.backgroundPosition = 'left center';
            this._htmlContainer.style.backgroundImage = 'url(' + piImageUrl + ')';
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'SetImage', arguments, 'WEBLINK', this.Id, e, false)
        }
    }
}