﻿/*
Central manager class handling all operations on the site
*/

var SiteManager = Class.create();

SiteManager.prototype =
{
    // Constructor
    initialize : function(piUser)
    {
        // Check if the input parameters are filled ans throw exception if not
        if(piUser == null)
        {
            throw 'Error: The constrcutor of the SiteManager class requires 1 input parameter.';
        }
        
        // Get the current host header value
        this.HostHeader = document.domain;
        
        // Declare variable holding the EntityObject
        this._entity;
        
        // Declare variable holding the link element for the current page's stylesheet
        this._currentPageStyleElem;

        // Declare variable holding the page object
        this.Page;

        // Declare variable holding the user object
        this.User = piUser;
        
        // Call RequestData function
        this.RequestData();
    },
    
    RequestData : function()
    {
        try
        {
            // Write Log Entry
            Logs.WriteNotification('NOTIFY_REQUESTING_DATA', 'RequestData', arguments, 'SITE', this.Id)
            // Create input parameters for the webservice method
            var params=$H( {piHostHeader : this.HostHeader} ).toQueryString();
            // Create new Ajax request
            new Ajax.Request("ISDataService.asmx/GetSiteData", { 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, 'SITE', this.HostHeader, 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, 'SITE', this.Id)
            // Create new EntityObject passing the xml data returned from the web service
            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, 'SITE', this.Id)
                return
            }
            
            // Create instance of the StateManager
            StateMgr = new StateManager();
            
            // Call ProcessTemplateInformation function to build the site template
            // >> This feature is part of the server side code in the new version
            //this.ProcessTemplateInformation();
            
            StateMgr.ShowNavigation();
            
            // Create the main menu
            MainMenu = new WebMenu('47c611b4-aba9-4c6c-8cd7-249a8444badd', 'header_area_right_bottom');
            MainMenu.Show();
            
            // Create the footer
            // >> This feature is part of the server side code in the new version
            /*var footer = new WebFooter('4a2cb477-09c9-4093-8ded-7f94db6066db', 'footer_area');
            footer.Show();*/
            
            // Check if a start page is provided
            // >> This feature is part of the server side code in the new version
            /*if((this._entity.Configuration.Site.Startpage != null) && (!this._entity.Configuration.Site.Startpage.Value.blank()))
            {
                StateMgr.SwitchPage(this._entity.Configuration.Site.Startpage.Value);
            }
            else
            {
                // Write Error to the Log
                Logs.WriteError('ERROR_MISSING_START_PAGE', 'OnDataRequestResponse', arguments, 'SITE', this.Id)
                // Create new message box
                var msg = new WebMessage(Lingo.GetLingoItem('Error'), Lingo.GetLingoItem('MSG_NO_START_PAGE'));
                // Show message box
                msg.Show();
            }*/
            
            StateMgr.RegisterLinks();
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'OnDataRequestResponse', arguments, 'SITE', this.Id, e, false)
        }
    },
    
    ProcessTemplateInformation : function()
    {
        try
        {
            // Check if entities markup property is null and write error to the log if so
            if((this._entity.Structure.Site_markup == null) || (this._entity.Structure.Site_markup.Value.blank()))
            {
                // Write Error to the Log
                Logs.WriteError('ERROR_MISSING_MARKUP', 'ProcessTemplateInformation', arguments, 'SITE', this.Id)
                return;
            }
            
            // Insert the markup into the document's body
            document.body.innerHTML = this._entity.Structure.Site_markup.Value;
            StateMgr.HideMainArea();
            
            // Check if main area node is null and write error to the log if so
            if((this._entity.Structure.Main_content == null) || (this._entity.Structure.Main_content.Value.blank()))
            {
                // Write Error to the Log
                Logs.WriteError('ERROR_MISSING_MAIN_CONTENT_AREA', 'ProcessTemplateInformation', arguments, 'SITE', this.Id)
                return;
            }
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'ProcessTemplateInformation', arguments, 'SITE', this.Id, e, false)
        }
    }
}