﻿/*
Central manager class handling all operations on the site
*/

var StateManager = Class.create();

StateManager.prototype =
{
    // Constructor
    initialize : function()
    {
        // Declare Array variables holding the page history
        this.lastPagesHistory = new Array();        
        this.nextPagesHistory = new Array();
        
        // Create the back and forward buttons
        this._lastButton = Builder.node('img', { alt: 'zurück', src: 'Template/INVENIT1/Image/backbutton_inactive.gif', style: 'position:absolute;right:25px;top:15px;' });
        this._nextButton = Builder.node('img', { alt: 'vor', src: 'Template/INVENIT1/Image/forwardbutton_inactive.gif', style: 'position:absolute;right:5px;top:15px;' });
        
        // Create event handlers
        Event.observe(this._lastButton, 'click', this.SwitchToLastPage.bindAsEventListener(this));
        Event.observe(this._nextButton, 'click', this.SwitchToNextPage.bindAsEventListener(this));
    },
    
    ShowNavigation : function()
    {
        // Append the buttons to the very top of the header area
        if($('header_area_right_top') != null)
        {
            $('header_area_right_top').appendChild(this._lastButton);
            $('header_area_right_top').appendChild(this._nextButton);
        }
    },
    
    OnLinkClick : function(piEvent)
    {
        try
        {                                  
            // Get the element being the source of the event and return if it is null
            var targetElem = Event.findElement(piEvent, 'a');
            if(!targetElem)
                return;
                            
            var pageId = targetElem.href;
            
            if(!pageId)
                return;
            
            if((pageId.toLowerCase().indexOf('imageserver.aspx') == -1) && (!pageId.endsWith('#')))
            {
                // Stop the event propagation
                Event.stop(piEvent);
            }
            else
            {
                return;
            }
            
            // Prepare to fade out the main area and show a loader icon
            StateMgr.FadeOutMainArea();
                
            
            if ( SiteGuide.player.getHtmlElem() != null && SiteGuide.player.getHtmlElem().parentNode != null )
            {
            	SiteGuide.player.getHtmlElem().parentNode.removeChild(SiteGuide.player.getHtmlElem());
            }
            
            if((SiteGuide.siteGuideLayer != null) && (SiteGuide.siteGuideLayer.parentNode != null))
            {
                SiteGuide.siteGuideLayer.innerHTML = '';
            }
            
            if( pageId.toLowerCase().indexOf('home.htm') != -1 )
            {
                SiteGuide = new SiteGuideManager();
            }
                
            // Create new nextPagesHistory Array to empty it
            StateMgr.nextPagesHistory = new Array();
            
            // Check if current Page id is available and add it to the PageHistory Array
            if((Site.Page != null) && (Site.Page.Id != null))
            {
                StateMgr.lastPagesHistory.push(Site.Page.Id);
            }
            
            var func = function(){ Site.Page = new PageManager(pageId) };
            
            // Create new instance of the PageManager passing the id of the site that is to be shown
            window.setTimeout(func, 1100);
            
            // Set the button states
            StateMgr.SetButtonState();
            
            // Notifiy the main menu
            MainMenu.NotifyPageChange(pageId);
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'OnLinkClick', arguments, 'STATEMANAGER', null, e, false)
        }
    },
    
    SwitchPage : function(piPageId)
    {
        try
        {
            if(!piPageId)
                return;
            if ( SiteGuide.player.getHtmlElem() != null && SiteGuide.player.getHtmlElem().parentNode != null )
            {
            	SiteGuide.player.getHtmlElem().parentNode.removeChild(SiteGuide.player.getHtmlElem());
            }
            
            if((SiteGuide.siteGuideLayer != null) && (SiteGuide.siteGuideLayer.parentNode != null))
            {
                SiteGuide.siteGuideLayer.innerHTML = '';
            }
            
            if( piPageId.toLowerCase() == 'home.htm' )
            {
                SiteGuide = new SiteGuideManager();
            }
            
                
            // Create new nextPagesHistory Array to empty it
            StateMgr.nextPagesHistory = new Array();
            
            // Check if current Page id is available and add it to the PageHistory Array
            if((Site.Page != null) && (Site.Page.Id != null))
            {
                this.lastPagesHistory.push(Site.Page.Id);
            }
                
            // Get the href attribute of the targetElem
            var pageId = piPageId;
            
            // Prepare to fade out the main area and show a loader icon
            StateMgr.FadeOutMainArea();
            
            var func = function(){ Site.Page = new PageManager(pageId) };
            // Create new instance of the PageManager passing the id of the site that is to be shown
            window.setTimeout(func, 1100);
            
            // Set the button states
            StateMgr.SetButtonState();
            
            // Notifiy the main menu
            MainMenu.NotifyPageChange(pageId);
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'SwitchPage', arguments, 'STATEMANAGER', pageId, e, false)
        }
    },
    
    SwitchToLastPage : function()
    {
        try
        {                
            // Check if last page id is available
            if((this.lastPagesHistory == null) || (this.lastPagesHistory.length < 1))
            {
                return;
            }
                
            // Get the id of the current page and push it into the nextPagesHistory Array
            if((Site != null) && (Site.Page != null) && (Site.Page.Id != null))
            {
                this.nextPagesHistory.push(Site.Page.Id);
            }
            
            // Get the href attribute of the targetElem and remove it from the lastPagesHistory Array
            var pageId = this.lastPagesHistory.last();
            this.lastPagesHistory.pop();
            
            // Prepare to fade out the main area and show a loader icon
            StateMgr.FadeOutMainArea();
            
            var func = function(){ Site.Page = new PageManager(pageId) };
            // Create new instance of the PageManager passing the id of the site that is to be shown
            window.setTimeout(func, 1100);
            
            // Set the button states
            StateMgr.SetButtonState();
            
            // Notifiy the main menu
            MainMenu.NotifyPageChange(pageId);
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'SwitchToLastPage', arguments, 'SITE', this.Id, e, false)
        }
    },
    
    SwitchToNextPage : function()
    {
        try
        {                
            // Check if last page id is available
            if((this.nextPagesHistory == null) || (this.nextPagesHistory.length < 1))
            {
                return;
            }
                
            // Get the id of the current page and push it into the lastPagesHistory Array
            if((Site != null) && (Site.Page != null) && (Site.Page.Id != null))
            {
                this.lastPagesHistory.push(Site.Page.Id);
            }
            
            // Get the href attribute of the targetElem and remove it from the lastPagesHistory Array
            var pageId = this.nextPagesHistory.last();
            this.nextPagesHistory.pop();
            
            // Prepare to fade out the main area and show a loader icon
            StateMgr.FadeOutMainArea();
            
            var func = function(){ Site.Page = new PageManager(pageId) };
            // Create new instance of the PageManager passing the id of the site that is to be shown
            window.setTimeout(func, 1100);
            
            // Set the button states
            StateMgr.SetButtonState();
            
            // Notifiy the main menu
            MainMenu.NotifyPageChange(pageId);
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'SwitchToNextPage', arguments, 'SITE', this.Id, e, false)
        }
    },
    
    SetButtonState : function()
    {
        // Check if the lastPagesHistory Array contains any data and set the image src attribute accordingly
        if(this.lastPagesHistory.length > 0)
        {
            this._lastButton.src = 'Template/INVENIT1/Image/backbutton_active.gif';
            this._lastButton.style.cursor = 'pointer';
        }
        else
        {
            this._lastButton.src = 'Template/INVENIT1/Image/backbutton_inactive.gif';
            this._lastButton.style.cursor = 'normal';
        }
        
        // Check if the nextPagesHistory Array contains any data and set the image src attribute accordingly
        if(this.nextPagesHistory.length > 0)
        {
            this._nextButton.src = 'Template/INVENIT1/Image/forwardbutton_active.gif';
            this._nextButton.style.cursor = 'pointer';
        }
        else
        {
            this._nextButton.src = 'Template/INVENIT1/Image/forwardbutton_inactive.gif';
            this._nextButton.style.cursor = 'normal';
        }
    },
    
    HideMainArea: function()
    {
        // Prepare to hide the main area and show a loader icon
        if($('loadIconHolder') == null)
        {
            var loadIconHolder = Builder.node('div', { id: 'loadIconHolder', style: 'z-index:-1;top:200px;left:50%;width:92px;padding:4px;background-color:#CBCBCB;position:absolute;text-align:center' });
            var loadIcon = Builder.node('img', { src: 'Template/INVENIT1/Image/loadingImage.gif' });
            var loadText = Builder.node('p', { style: 'color:#FFFFFF;' });
            loadText.appendChild(document.createTextNode(Lingo.GetLingoItem('LOADING')));
            loadIconHolder.appendChild(loadIcon);
            loadIconHolder.appendChild(loadText);
            document.body.appendChild(loadIconHolder);
        }
        
        $('main_area').hide();
    },
    
    FadeOutMainArea: function()
    {        
        var mainArea = $('main_area');
        
        // Prepare to fade out the main area and show a loader icon
        if($('loadIconHolder') == null)
        {
            var loadIconHolder = Builder.node('div', { id: 'loadIconHolder', style: 'z-index:0;top:200px;left:50%;width:92px;height:72px;padding:4px;margin:0px 0px 0px -50px;background-color:#CBCBCB;position:absolute;text-align:center' });
            var loadIcon = Builder.node('img', { src: 'Template/INVENIT1/Image/loadingImage.gif' });
            var loadText = Builder.node('p', { style: 'color:#FFFFFF;' });
            loadText.appendChild(document.createTextNode(Lingo.GetLingoItem('LOADING')));
            loadIconHolder.appendChild(loadIcon);
            loadIconHolder.appendChild(loadText);
            document.body.appendChild(loadIconHolder);
        }
        
        if($('main_area').style.display != 'none')
        {
            new Effect.Fade('main_area',{duration:1.0, from:1.0, to:0.0});
        }
    },
    
    FadeInMainArea: function()
    {
        var loadIcon = $('loadIconHolder');
        var mainArea = $('main_area');
        
        var mainAreaHeight = mainArea.clientHeight;
        if(mainAreaHeight == 0)
        {
            try
            {
                mainAreaHeight = mainArea.getHeight();
            }
            catch(e)
            {
                mainAreaHeight = 600;
            }
        }
        
        mainArea.style.overflow = 'hidden';
        mainArea.style.top = '10px';
        mainArea.style.left = '50%';
        mainArea.style.width = '800px';
        mainArea.style.height = '80px';
        mainArea.style.margin = '0px 0px 0px -400px';
        
        if(loadIcon != null)
        {
            new Effect.Morph(loadIcon, { style: 'top:10px;left:50%;width:800px;margin:0px 0px 0px -400px;background-color:#FFFFFF' });
            
            var loadImage = loadIcon.getElementsByTagName('img')[0];
            if(loadImage != null)
            {
                new Effect.Fade(loadImage);
            }
            
            var loadIconDisappearFunc = function()
            {
                new Effect.Appear(mainArea, { duration: 0.2 });
            }
            
            var loadIconRemoveFunc = function()
            {
                loadIcon.parentNode.removeChild(loadIcon);
                mainArea.morph('height:' + mainAreaHeight + 'px');
            }
            
            window.setTimeout(loadIconDisappearFunc, 1000);
            window.setTimeout(loadIconRemoveFunc, 1200);
            window.setTimeout(function(){ mainArea.style.overflow = 'visible'; }.bind(this), 3200);
        }
    },
    
    /*OLD_FadeOutMainArea: function()
    {        
        // Prepare to fade out the main area and show a loader icon
        if($('loadIconHolder') == null)
        {
            var loadIconHolder = Builder.node('div', { id: 'loadIconHolder', style: 'z-index:0;top:200px;left:50%;width:92px;padding:4px;background-color:#CBCBCB;position:absolute;text-align:center' });
            var loadIcon = Builder.node('img', { src: 'Template/INVENIT1/Image/loadingImage.gif' });
            var loadText = Builder.node('p', { style: 'color:#FFFFFF;' });
            loadText.appendChild(document.createTextNode(Lingo.GetLingoItem('LOADING')));
            loadIconHolder.appendChild(loadIcon);
            loadIconHolder.appendChild(loadText);
            document.body.appendChild(loadIconHolder);
        }
        
        if($('main_area').style.display != 'none')
        {
            new Effect.Fade('main_area',{duration:1.0, from:1.0, to:0.0});
        }
    },
    
    OLD_FadeInMainArea: function()
    {        
        // Prepare to fade in the main area and remove loader icon
        var func = function()
        {
            Effect.Appear('main_area');
            
            if($('loadIconHolder') != null)
            {
                $('loadIconHolder').parentNode.removeChild($('loadIconHolder'));
            }
        }
        
        window.setTimeout(func, 1000);
    },*/
    
    RegisterLinks : function()
    {
        // Get all links on the site and add event handler
        var links = $$('a');
        for(var x = 0; x < links.length; x++)
        {
            if(links[x].href.toLowerCase().startsWith('http://' + Site.HostHeader))
            {
                Event.observe(links[x], 'click', StateMgr.OnLinkClick);
            }
        }
    }
}