﻿/*
Javascript control showing a single WebTeaser entity
*/

var WebTeaser = Class.create();

WebTeaser.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 = 'WebTeaser_' + 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 content holder node
            this._htmlContainer = Builder.node('div', {id: this._nodeId});
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'Build', arguments, 'WEBTEASER', this.Id, e, false)
        }
    },
    
    RequestData : function()
    {
        try
        {
            // Write Log Entry
            Logs.WriteNotification('NOTIFY_REQUESTING_DATA', 'RequestData', arguments, 'WEBTEASER', 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, 'WEBTEASER', 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, 'WEBTEASER', 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, 'WEBTEASER', this.Id)
                
                return
            }
                    
            // Call ProcessContentInformation function to create entity objects/controls
            this.ProcessContentInformation();
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'OnDataRequestResponse', arguments, 'WEBTEASER', this.Id, e, false)
        }
    },
    
    ProcessContentInformation : function()
    { 
        try
        {               
            // Set the class according to the border configuration
            if(this._entity.Configuration.Enable_border.Value.toLowerCase() == 'true')
            {
                this._htmlContainer.className = 'WebTeaserBorder';
            }
            else
            {
                this._htmlContainer.className = 'WebTeaser';
            }
            
            // Check if the title image value is an empty string
            if((!this._entity.Content.Titleimage.Value.blank()) && (!this._entity.Content.Link.Value.blank()))
            {
                // Try to get the title text to use in the title image's alt attribute
                var titleText = '';
                if((this._entity.Content.Title != null) && (!this._entity.Content.Title.Value.blank()))
                {
                    titleText = this._entity.Content.Title.Value;
                }
                
                // Build link node
                var linkNode = Builder.node('a', { href: this._entity.Content.Link.Value, page: this._entity.Content.Link.Value });
                // Build title node
                var titleNode = Builder.node('h1');
                // Build image node
                var titleImageNode = Builder.node('img',{ src: 'Data/Image/' + this._entity.Content.Titleimage.Value, alt: titleText });
                // Append the image node to the title node
                titleNode.appendChild(titleImageNode);
                // Append the title node to the linknode
                linkNode.appendChild(titleNode);
                // Set an event handler function for the click event
                Event.observe(linkNode, 'click', StateMgr.OnLinkClick);
                // Append the title html node to the content holder node
                this._htmlContainer.appendChild(linkNode);
            }
            else if(!this._entity.Content.Titleimage.Value.blank())
            {
                // Build title node
                var titleNode = Builder.node('h1');
                // Build image node
                var titleImageNode = Builder.node('img',{ src: 'Data/Image/' + this._entity.Content.Titleimage.Value });
                // Append the image node to the title node
                titleNode.appendChild(titleImageNode);
                // Append the title html node to the content holder node
                this._htmlContainer.appendChild(titleNode);
            }
            
            // Check if the text value is an empty string
            if(!this._entity.Content.Text.Value.blank())
            {
                // Build text node
                var textNode = Builder.node('div');
                // Get the background color configuration and set background color
                var bgColor = this._entity.Configuration.Background_color.Value;
                textNode.style.backgroundColor = bgColor;
                // Append the title value to the title html node
                textNode.innerHTML = this._entity.Content.Text.Value;
                // Append the text html node to the content holder node
                this._htmlContainer.appendChild(textNode);
            }
            
            // Check if the link value is an empty string
            if(!this._entity.Content.Link.Value.blank())
            {
                // Build link node
                var linkNode = Builder.node('a', { href: this._entity.Content.Link.Value, page: this._entity.Content.Link.Value });
                // Set link text
                linkNode.appendChild(document.createTextNode(Lingo.GetLingoItem('LEARNMORE')));
                // Set an event handler function for the click event
                Event.observe(linkNode, 'click', StateMgr.OnLinkClick);
                // Append the link html node to the text node
                textNode.innerHTML += '<br><br>';
                textNode.appendChild(linkNode);
            }
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'ProcessContentInformation', arguments, 'WEBTEASER', 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', 'WebTeaser', 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, 'WEBTEASER', this.Id, e, false)
        }
    }
}