﻿/*
Javascript control showing a horizontal Menu
*/

var WebMenuItem = Class.create();

WebMenuItem.prototype =
{
    // Constructor
    initialize : function(piEntityId, piWebMenuObject)
    {
        // Declare variable holding the WebMenu object
        this._webMenu = piWebMenuObject;
        
        // Declare variable holding the id of the link entity
        this.Id = piEntityId;
        
        // Declare variable holding the page id, the link refers to
        this.PageId;
        
        // Declare variable holding the entity object
        this._entity;
        
        // Declare variable holding the html node which serves as content container
        this._htmlContainer;
        // Declare variable holding the link
        this._linkHolder;
        // Declare variable holding the background
        this._bgHolder;
        
        // Declare variables indicatring if the control is disbaled o enabled
        this._enabled = true;
        
        // Declare variables for the rise and fall interval
        this._fallInterval;
        this._riseInterval;
        
        // Declare variable indicating if the menu item is currently active
        this._active = false;
        
        // 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()
    {
        // Create a div used as menu item holder
        this._htmlContainer = Builder.node('div');
        this._htmlContainer.className = 'MenuItem';
        
        // Create linkHolder div
        this._linkHolder = Builder.node('div');
        this._linkHolder.className = 'MenuLinkHolder';
        
        // Append the linkHolder to the _htmlContainer
        this._htmlContainer.appendChild(this._linkHolder);
        
        // Create backgroundHolder div
        this._bgHolder = Builder.node('div');
        this._bgHolder.className = 'MenuBgHolder';
        this._bgHolder.innerHTML = '&nbsp;';
        
        // Append the bgHolder to the _htmlContainer
        this._htmlContainer.appendChild(this._bgHolder);
    },
    
    RequestData : function()
    {
        try
        {
            // Write Log Entry
            Logs.WriteNotification('NOTIFY_REQUESTING_DATA', 'RequestData', arguments, 'WEBMENUITEM', 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, 'WEBMENUITEM', this.Id, e, false)
        }
    },
    
    OnRequestFail : function()
    {
        Logs.WriteError('ERROR_REQUESTING_DATA', 'RequestData', null, 'WEBMENUITEM', this.Id);
    },
    
    // Receives entity's xml data from the webservice
    OnDataRequestResponse : function(transport)
    {
        try
        {
            // Write Log Entry
            Logs.WriteNotification('NOTIFY_RECEIVING_DATA', 'OnDataRequestResponse', arguments, 'WEBMENUITEM', 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, 'WEBMENUITEM', this.Id)
                
                return
            }
                    
            // Call ProcessContentInformation function to create entity objects/controls
            this.ProcessContentInformation();
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'OnDataRequestResponse', arguments, 'WEBMENUITEM', this.Id, e, false)
        }
    },
    
    ProcessContentInformation : function()
    {        
        // 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;
        }
        
        // Add the link title to the _linkHolder
        this._linkHolder.appendChild(document.createTextNode(this._entity.Content.Title.Value));
        // Set the PageId
        this.PageId = this._entity.Content.Link.Value;
        
        // Create mouseover and mouseout event handlers for the list item
        Event.observe(this._htmlContainer, 'mouseover', this.InitFall.bindAsEventListener(this));
        Event.observe(this._htmlContainer, 'mouseout', this.InitRise.bindAsEventListener(this));
        Event.observe(this._htmlContainer, 'click', this.Click.bindAsEventListener(this));
    },
    
    Enable : function()
    {
        this._enabled = true;  
    },
    
    Disable : function()
    {
        this._enabled = false;
    },
    
    Click : function()
    {
        if((this._active == true) || (this._enabled == false))
        {
            return;
        }
        
        StateMgr.SwitchPage(this.PageId);
    },
    
    InitFall : function(piEvent)
    {
        /**/Logs.WriteNotification('Initializing MenuItem Fall Action', 'InitFall', arguments, 'WebMenuItem', null)
        if(this._enabled == false)
        {
            return;
        }
        
        clearInterval(this._fallInterval);
        clearInterval(this._riseInterval);
        this._fallInterval = window.setInterval(this.Fall.bind(this), 40);
    },
    
    InitRise : function(piEvent)
    {
        if((this._active == true) || (this._enabled == false))
        {
            return;
        }
        
        clearInterval(this._fallInterval);
        clearInterval(this._riseInterval);
        this._riseInterval = window.setInterval(this.Rise.bind(this), 40);
    },
    
    Fall : function()
    {        
        if($(this._bgHolder).getHeight() < 40)
        {
            this._bgHolder.style.height = ($(this._bgHolder).getHeight() + 5) + 'px';
        }
        else
        {
            clearInterval(this._fallInterval);
            clearInterval(this._riseInterval);
        }
        
        if($(this._bgHolder).getHeight() > 20)
        {
            this._linkHolder.style.color = '#FFFFFF';
        }
    },
    
    Rise : function()
    {        
        if($(this._bgHolder).getHeight() > 10)
        {
            this._bgHolder.style.height = ($(this._bgHolder).getHeight() - 5) + 'px';
        }
        else
        {
            clearInterval(this._fallInterval);
            clearInterval(this._riseInterval);
        }
        
        if($(this._bgHolder).getHeight() < 20)
        {
            this._linkHolder.style.color = '#5C5C5C';
        }
    },
    
    SetActive : function()
    {
        this.InitFall();
        this._active = true;
    },
    
    SetInactive : function()
    {
        this._active = false;
        this.InitRise();
    },
    
    GetHtml : function()
    {
        return this._htmlContainer;
    }
}