﻿/*
Javascript control showing a single WebText entity
*/

var WebText = Class.create();

WebText.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 = 'WebText_' + 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
        {
            this._htmlContainer = Builder.node('div', {id: this._nodeId});
            this._htmlContainer.className  = 'WebText';
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'Build', arguments, 'WEBTEXT', this.Id, e, false)
        }
    },
    
    RequestData : function()
    {
        try
        {
            // Write Log Entry
            Logs.WriteNotification('NOTIFY_REQUESTING_DATA', 'RequestData', arguments, 'WEBTEXT', 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, 'WEBTEXT', 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, 'WEBTEXT', 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, 'WEBTEXT', this.Id)
                
                return
            }
                    
            // Call ProcessContentInformation function to create entity objects/controls
            this.ProcessContentInformation();
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'OnDataRequestResponse', arguments, 'WEBTEXT', this.Id, e, false)
        }
    },
    
    ProcessContentInformation : function()
    {
        try
        {
            // Create variable to hold the followup link
            var followup = ''
            // Create variable indicating if the followup link is enabled
            var enableFollowup = false;
            // Check if followup link is enabled
            if((this._entity.Configuration.Use_followup_link != null) && (this._entity.Configuration.Use_followup_link.Value != null) && (!this._entity.Configuration.Use_followup_link.Value.blank()))
            {
                if(this._entity.Configuration.Use_followup_link.Value == 'USE_GLOBAL_CONFIG')
                {
                    enableFollowup = Helpers.ParseBoolean(Site._entity.Configuration.Webtext.Use_followup_link.Value);
                }
                else
                {
                    enableFollowup = Helpers.ParseBoolean(this._entity.Configuration.Use_followup_link.Value);
                }
            }
            
            // Check if followup link is available
            if((this._entity.Content.Followup_link != null) && (this._entity.Content.Followup_link.Value != null) && (!this._entity.Content.Followup_link.Value.blank()))
            {
                followup = this._entity.Content.Followup_link.Value;
            }
            
            // Create the followup link for the image element if followup is present and enabled
            if((enableFollowup == true) && (!followup.blank()))
            {
                // Check if the link is an internal or external link
                if(followup.toLowerCase().startsWith('http'))
                {
                    // Create external link
                    var imageFollowupNode = Builder.node('a', { href: followup, target: '_blank', style: 'cursor:pointer' });
                }
                else
                {
                    // Create internal link
                    var imageFollowupNode = Builder.node('a', { href: followup, page: followup, style: 'cursor:pointer'});
                    // Set an event handler function for the click event
                    Event.observe(imageFollowupNode, 'click', StateMgr.OnLinkClick);
                }
            }
            
            // Create variable to hold the WebImage object
            var image;       
            // Check if image reference is filled
            if((this._entity.Content.References != null) && (this._entity.Content.References[0] != null) && (!this._entity.Content.References[0].Id.blank()))
            {
                // Create new WebImage object
                if(imageFollowupNode != null)
                {
                    image = new WebImage(this._entity.Content.References[0].Id, imageFollowupNode, 'LEFT');
                }
                else
                {
                    image = new WebImage(this._entity.Content.References[0].Id, this._htmlContainer, 'LEFT');
                }
            }
            
            // Check if an image should be used as title
            var useTitleImage = false;
            if(this._entity.Configuration.Use_title_image.Value == 'USE_GLOBAL_CONFIG')
            {
                if(Site._entity.Configuration.Webtext.Use_title_image.Value.toLowerCase() == 'true')
                {
                    useTitleImage = true;
                }
                else
                {
                    useTitleImage = false;
                }
            }
            else if(this._entity.Configuration.Use_title_image.Value.toLowerCase() == 'true')
            {
                useTitleImage = true;
            }
            
            // Add the title
            if(useTitleImage == true)
            {
                // Check if the title image value is an empty string
                if((this._entity.Content.Titleimage != null) && (!this._entity.Content.Titleimage.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 title node
                    var titleNode = Builder.node('h1');
                    // Build image node
                    var titleImg = Builder.node('img', { src: 'Data/Image/' + this._entity.Content.Titleimage.Value, style: 'border:0', alt: titleText });
                    // Append the title image to the title html node
                    titleNode.appendChild(titleImg);
                    
                    // Add the followup link to the title element if followup is present and enabled
                    if((enableFollowup == true) && (!followup.blank()))
                    {
                        // Check if the link is an internal or external link
                        if(followup.toLowerCase().startsWith('http'))
                        {
                            // Create external link
                            var followupNode = Builder.node('a', { href: followup, target: '_blank' });
                        }
                        else
                        {
                            // Create internal link
                            var followupNode = Builder.node('a', { href: followup, page: followup});
                            // Set an event handler function for the click event
                            Event.observe(followupNode, 'click', StateMgr.OnLinkClick);
                        }
                        
                        // Append the title html node to the followupNode
                        followupNode.appendChild(titleNode);
                        // Append the followupNode to the content holder node
                        this._htmlContainer.appendChild(followupNode);                        
                    }
                    else
                    {                    
                        // Append the title html node to the content holder node
                        this._htmlContainer.appendChild(titleNode);
                    }
                }
            }
            else
            {            
                // 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));
                    
                    // Add the followup link to the title element if followup is present and enabled
                    if((enableFollowup == true) && (!followup.blank()))
                    {
                        // Check if the link is an internal or external link
                        if(followup.toLowerCase().startsWith('http'))
                        {
                            // Create external link
                            var followupNode = Builder.node('a', { href: followup, target: '_blank' });
                        }
                        else
                        {
                            // Create internal link
                            var followupNode = Builder.node('a', { href: followup, page: followup});
                            // Set an event handler function for the click event
                            Event.observe(followupNode, 'click', StateMgr.OnLinkClick);
                        }
                        
                        // Append the title html node to the followupNode
                        followupNode.appendChild(titleNode);
                        // Append the followupNode to the content holder node
                        this._htmlContainer.appendChild(followupNode);                        
                    }
                    else
                    {                    
                        // Append the title html node to the content holder node
                        this._htmlContainer.appendChild(titleNode);
                    }
                }
            }
            
            // Check if the image object is filled
            if(image != null)
            {
                // Get the image alignment configuration
                var imageAlignment = this._entity.Configuration.Image_alignment;
                // Check if imageAlignment is filled and if global config is to be used
                if((imageAlignment != null) && (!imageAlignment.Value.blank()))
                {
                    if(imageAlignment.Value == 'USE_GLOBAL_CONFIG')
                    {
                        imageAlignment = Site._entity.Configuration.Webtext.Image_alignment;
                    }
                    // Set the Alignment property of the WebImage
                    image.Alignment = imageAlignment.Value.toLowerCase();
                }
                
                // Get the large image size configuration
                var largeImageSize = this._entity.Configuration.Large_image_size;
                // Check if largeImageSize is filled and if global config is to be used
                if((largeImageSize != null) && (!largeImageSize.Value.blank()))
                {
                    if(largeImageSize.Value == 'USE_GLOBAL_CONFIG')
                    {
                        largeImageSize = Site._entity.Configuration.Webtext.Large_image_size;
                    }
                    // Set the LargeImageSize property of the WebImage
                    image.LargeImageSize = largeImageSize.Value;
                }
                
                if(imageFollowupNode != null)
                {
                    this._htmlContainer.appendChild(imageFollowupNode);
                }
                
                // Call Show function to insert the image into the provided target element
                image.Show();
            }
            
            // Check if the text value is an empty string
            if((this._entity.Content.Text != null) && (!this._entity.Content.Text.Value.blank()))
            {
                // Build text node
                var textNode = Builder.node('div');
                // 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);
                
                // Build empty html div layer to clear the float style of the embedded image
                if((image) && (image != null)&& (image.Alignment != 'none'))
                {
                    var clearElem = Builder.node('div', { style : 'clear:' + image.Alignment});
                    textNode.appendChild(clearElem);
                }
            }
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'ProcessContentInformation', arguments, 'WEBTEXT', 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', 'WebText', 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, 'WEBTEXT', this.Id, e, false)
        }
    }
}