﻿// Register the XICenter.LeftNavMenu namespace
Type.registerNamespace('XICenter.LeftNavMenu');

/*
    Builds and diplays left side navigation menu
*/
XICenter.LeftNavMenu.Display = function(element, displayElement, elementsToClear)
{
    XICenter.LeftNavMenu.Display.initializeBase(this, [element]);

    //CSS Data Members
    this._cssTextLevel_0 = null;
    this._cssTextLevel_0_Over = null;    
    this._cssTextLevel_1 = null;
    this._cssTextLevel_1_Over = null;

    //Delegate Data Members
    this._itemOverHandler = null;
    this._itemOutHandler = null;
    this._itemClickHandler = null;
    this._propertyChanged = null;

    //Web Service Data Members
    this._LeftSideMenuURLTextPath = null;
    this._LeftSideMenuURLTextMethod = null;
    this._LeftSideMenuURLTextTimeOut = null;

    //Array Data Members
    this._items = [];

    //Data Members
    this._itemCount = 0;
    this._imgBulletPlain = null;
    this._imgBulletOver = null;
    this._imgPlusePlain = null;
    this._imgPluseOver = null;
    this._imgMinusPlain = null;
    this._imgMinusOver = null;
    this._imgMenuTitle = null;
    this._displayElement = displayElement;
    this._elementsToClear = elementsToClear;
    
    this.initialize();
}

// Create the prototype for the behavior.
XICenter.LeftNavMenu.Display.prototype =
{
    initialize : function()
    {
        XICenter.LeftNavMenu.Display.callBaseMethod(this, 'initialize');
        this._itemOverHandler = Function.createDelegate(this, this._onMouseOver); 
        this._itemOutHandler = Function.createDelegate(this, this._onMouseOut); 
        this._itemClickHandler = Function.createDelegate(this, this._onClick); 
        this._propertyChanged = Function.createDelegate(this, this._onPropertyChanged);
        this.add_propertyChanged(this._propertyChanged);
    },

    _sortItems : function()
    {
        for (var iCount = 0; iCount < this._itemCount; iCount++)
        {
            this._items[iCount]._show = (this._items[iCount]._level === 0)
        }    
    },
    
    ConstructMenu : function()
    {
        this._sortItems();
        this._buildMenu();   
        this._loadOnStartCheck(); 
    },
    
    _clearDisplay : function()
    {
        var e = this.get_element();
        this.ClearHandlers(e);
        e.innerHTML = "";    
    },
    
    _buildMenu : function()
    {
        //clean up old menu
        this._clearDisplay();
                
        var oContainer = this.get_element();       
        var oTable = document.createElement("TABLE");
        var oTBody = document.createElement("TBODY");        

        var oHeadRow = document.createElement("TR");
        oTBody.appendChild(oHeadRow);
        
        var oHeadCell = document.createElement("TD");
        oHeadRow.appendChild(oHeadCell);
        oHeadCell.colSpan = 2;
        
        if (this._imgMenuTitle !== null)
        {
            var oHeadImg = document.createElement("IMG");
            oHeadImg.src = this._imgMenuTitle;
            oHeadImg.atl = "";
            oHeadImg.width = "130";
            oHeadCell.appendChild(oHeadImg);
        }

        for (var iCount = 0; iCount < this._itemCount; iCount++)
        {
            if (this._items[iCount]._show)
            {
                var oImg = null;
                var oRow = document.createElement("TR");
                oTBody.appendChild(oRow);
                
                var oCellImg = document.createElement("TD");
                oRow.appendChild(oCellImg);
                if (this._items[iCount]._level === 0)
                {
                    oImg = document.createElement("IMG");
                    oImg.id = "IMG_" + iCount;
                    if (this._items[iCount]._itemType === XICenter.LeftNavMenu.ItemType.Expandable)
                    {
                        if (this._items[iCount]._expanded)
                            oImg.src = this._imgMinusPlain;                        
                        else
                            oImg.src = this._imgPlusePlain;
                    }
                    else
                        oImg.src = this._imgBulletPlain;
                    oCellImg.appendChild(oImg);
                    this._items[iCount]._iconImageObj = oImg;
                }
                
                var oCellText = document.createElement("TD");
                var oSpanText = document.createElement("SPAN");
                oSpanText.id = "SPAN_" + iCount;
                if (this._items[iCount]._level === 0) oSpanText.className = this._cssTextLevel_0;
                if (this._items[iCount]._level === 1) oSpanText.className = this._cssTextLevel_1;            
                if (this._items[iCount]._level === 0) oSpanText.innerHTML = this._items[iCount]._text;
                if (this._items[iCount]._level === 1) oSpanText.innerHTML = "... " + this._items[iCount]._text;
                oCellText.appendChild(oSpanText);
                oRow.appendChild(oCellText);
                       
                $addHandlers(oSpanText, {"mouseover" : this._itemOverHandler,
                                         "mouseout" : this._itemOutHandler,
                                         "click" : this._itemClickHandler },
                             this); 
                if (oImg !== null)
                {
                    $addHandlers(oImg, {"mouseover" : this._itemOverHandler,
                                        "mouseout" : this._itemOutHandler,
                                        "click" : this._itemClickHandler },
                                 this); 
                }
            }            
        }
                
        oTable.appendChild(oTBody);
        oContainer.appendChild(oTable);
    },
    
    _loadOnStartCheck : function()
    {       
        for (var iCount = 0; iCount < this._itemCount; iCount++)
        {
            if (this._items[iCount]._itemType === XICenter.LeftNavMenu.ItemType.LoadOnStart)
            {
                this._getContent(iCount)
            }
        }
    },
    
    AddMenuItem : function(Text, Level, URL, ItemType)
    {
        var oMenuItem = new XICenter.LeftNavMenu.MenuItem(Text, Level, URL, Text+Level, false, ItemType);
        this._items[this._itemCount] = oMenuItem;
        this._itemCount++;        
    },
    
    _onPropertyChanged : function(sender, args)
    {
    },

    //Delegates
    _onMouseOver : function(EventArg)
    {
        if (EventArg.target.id !== "")
        {
            var TargetEle = EventArg.target.id.split("_");
            var rowIndex = parseInt(TargetEle[1]);
            if (this._items[rowIndex]._iconImageObj === null)
                EventArg.target.className = this.get_TextLevel_1_Over();    
            else
            {
                if (this._items[rowIndex]._itemType === XICenter.LeftNavMenu.ItemType.Expandable)
                {
                    if (this._items[rowIndex]._expanded)
                        this._items[rowIndex]._iconImageObj.src = this._imgMinusOver;                        
                    else
                        this._items[rowIndex]._iconImageObj.src = this._imgPluseOver;
                }
                else
                    this._items[rowIndex]._iconImageObj.src = this._imgBulletOver;
                EventArg.target.className = this.get_TextLevel_0_Over();  
            }
        }
    }, 
    
    _onMouseOut : function(EventArg)
    {
        if (EventArg.target.id !== "")
        {
            var TargetEle = EventArg.target.id.split("_");
            var rowIndex = parseInt(TargetEle[1]);
            if (this._items[rowIndex]._iconImageObj === null)
                EventArg.target.className = this.get_TextLevel_1();            
            else        
            {
                if (this._items[rowIndex]._itemType === XICenter.LeftNavMenu.ItemType.Expandable)
                {
                    if (this._items[rowIndex]._expanded)
                        this._items[rowIndex]._iconImageObj.src = this._imgMinusPlain;                        
                    else
                        this._items[rowIndex]._iconImageObj.src = this._imgPlusePlain;
                }
                else
                    this._items[rowIndex]._iconImageObj.src = this._imgBulletPlain;
                EventArg.target.className = this.get_TextLevel_0();            
            }
        }        
    },  
    
    _onClick : function(EventArg)
    {
        var TargetEle = EventArg.target.id.split("_");
        var rowIndex = parseInt(TargetEle[1]);
        if ((this._items[rowIndex]._level === 0) && (this._items[rowIndex]._itemType === XICenter.LeftNavMenu.ItemType.Expandable))
        {
            this._items[rowIndex]._expanded = !this._items[rowIndex]._expanded;
            for (var iCount = (rowIndex + 1); iCount < this._itemCount; iCount++)
            {
                if (this._items[iCount]._level === 1)
                    this._items[iCount]._show = !this._items[iCount]._show; 
                else
                    iCount = this._itemCount;   
            }   
            this._buildMenu(); 
        }
        else
        {
            if (this._items[rowIndex]._itemType === XICenter.LeftNavMenu.ItemType.Navigation)
            {
                window.location.href = this._items[rowIndex]._url;
            }
            else
            {
                this._getContent(rowIndex)
            }
        }
    }, 
    
    _getContent : function(DisplayOrder)
    {
        Sys.Net.WebServiceProxy.invoke(this._LeftSideMenuURLTextPath, 
                                       this._LeftSideMenuURLTextMethod,
                                       false,
                                       {"pageTitle" : document.title, "displayOrder" : DisplayOrder},
                                       Function.createDelegate(this, this._onMethodComplete),
                                       Function.createDelegate(this, this._onMethodError),
                                       "Left Side Menu Selection",
                                       this._LeftSideMenuURLTextTimeOut);                                     
    },
    
    _onMethodComplete : function(result, userContext, methodName)
    {
        var etc = [];
        etc = this._elementsToClear.split("~");
        for (var ECount = 0; ECount < etc.length; ECount++)
        {
            var Ele = $get(etc[ECount]);
            if (Ele)
            {
                this.ClearHandlers(Ele)
                Ele.innerHTML = "";
            }
        }
        
        var results = [];
        results = result.split("~");
        var de = $get(this._displayElement);
        de.style.height = results[0];        
        var oDiv = document.createElement("div");
        oDiv.style.position = "relative";
        oDiv.style.top = "0px";
        oDiv.style.left = "0px";
        oDiv.innerHTML = results[1];
        de.appendChild(oDiv);
    },
    
    _onMethodError : function(webServiceError, userContext, methodName)
    {
        alert(webServiceError._message);
    }, 
    
    //Properties
    get_BulletPlain : function()
    {
        return this._imgBulletPlain;
    },
    set_BulletPlain : function(value)
    {
        if (this._imgBulletPlain !== value)
        {
            this._imgBulletPlain = value;
            this.raisePropertyChanged('BulletPlain');
        }
    },
    
    get_BulletOver : function()
    {
        return this._imgBulletOver;
    },
    set_BulletOver : function(value)
    {
        if (this._imgBulletOver !== value)
        {
            this._imgBulletOver = value;
            this.raisePropertyChanged('BulletOver');
        }
    },
    
    get_PlusePlain : function()
    {
        return this._imgPlusePlain;
    },
    set_PlusePlain : function(value)
    {
        if (this._imgPlusePlain !== value)
        {
            this._imgPlusePlain = value;
            this.raisePropertyChanged('PlusePlain');
        }
    },
    
    get_PluseOver : function()
    {
        return this._imgPluseOver;
    },
    set_PluseOver : function(value)
    {
        if (this._imgPluseOver !== value)
        {
            this._imgPluseOver = value;
            this.raisePropertyChanged('PluseOver');
        }
    },
    
    get_MinusPlain : function()
    {
        return this._imgMinusPlain;
    },
    set_MinusPlain : function(value)
    {
        if (this._imgMinusPlain !== value)
        {
            this._imgMinusPlain = value;
            this.raisePropertyChanged('MinusPlain');
        }
    },
    
    get_MinusOver : function()
    {
        return this._imgMinusOver;
    },
    set_MinusOver : function(value)
    {
        if (this._imgMinusOver !== value)
        {
            this._imgMinusOver = value;
            this.raisePropertyChanged('MinusOver');
        }
    },    

    get_TextLevel_0 : function()
    {
        return this._cssTextLevel_0;
    },
    set_TextLevel_0 : function(value)
    {
        if (this._cssTextLevel_0 !== value)
        {
            this._cssTextLevel_0 = value;
            this.raisePropertyChanged('TextLevel_0');
        }
    },  
    
    get_TextLevel_0_Over : function()
    {
        return this._cssTextLevel_0_Over;
    },
    set_TextLevel_0_Over : function(value)
    {
        if (this._cssTextLevel_0_Over !== value)
        {
            this._cssTextLevel_0_Over = value;
            this.raisePropertyChanged('TextLevel_0_Over');
        }
    },          

    get_TextLevel_1 : function()
    {
        return this._cssTextLevel_1;
    },
    set_TextLevel_1 : function(value)
    {
        if (this._cssTextLevel_1 !== value)
        {
            this._cssTextLevel_1 = value;
            this.raisePropertyChanged('TextLevel_1');
        }
    },  
 
    get_TextLevel_1_Over : function()
    {
        return this._cssTextLevel_1_Over;
    },
    set_TextLevel_1_Over : function(value)
    {
        if (this._cssTextLevel_1_Over !== value)
        {
            this._cssTextLevel_1_Over = value;
            this.raisePropertyChanged('TextLevel_1_Over');
        }
    },  
    
    get_MenuImageTitle : function()
    {
        return this._imgMenuTitle;
    },
    set_MenuImageTitle : function(value)
    {
        if (this._imgMenuTitle !== value)
        {
            this._imgMenuTitle = value;
            this.raisePropertyChanged('ImageMenuTitle');
        }
    },  
          
    //Web Services
    get_LeftSideMenuURLTextPath : function()
    {
        return this._LeftSideMenuURLTextPath;
    },
    set_LeftSideMenuURLTextPath : function(value)
    {
        if (this._LeftSideMenuURLTextPath !== value)
        {
            Sys.Debug.trace('Set LeftSideMenuURLTextPath:' + value);
            this._LeftSideMenuURLTextPath = value;
            this.raisePropertyChanged('LeftSideMenuURLTextPath');
        }
    },

    get_LeftSideMenuURLTextMethod : function()
    {
        return this.LeftSideMenuURLTextMethod;
    },
    set_LeftSideMenuURLTextMethod : function(value)
    {
        if (this._LeftSideMenuURLTextMethod !== value)
        {
            Sys.Debug.trace('Set LeftSideMenuURLTextMethod:' + value);
            this._LeftSideMenuURLTextMethod = value;
            this.raisePropertyChanged('LeftSideMenuURLTextMethod');
        }
    },

    get_LeftSideMenuURLTextTimeOut : function()
    {
        return this.LeftSideMenuURLTextTimeOut;
    },
    set_LeftSideMenuURLTextTimeOut : function(value)
    {
        if (this._LeftSideMenuURLTextTimeOut !== value)
        {
            Sys.Debug.trace('Set LeftSideMenuURLTextTimeOut:' + value);
            this._LeftSideMenuURLTextTimeOut = value;
            this.raisePropertyChanged('LeftSideMenuURLTextTimeOut');
        }
    },
        
    //Clean-Up
    dispose: function()
    {
        this.ClearHandlers(this.get_element());
        XICenter.LeftNavMenu.Display.callBaseMethod(this, 'dispose');
    },
    
    ClearHandlers: function(e)
    {
        $clearHandlers(e);
        for (var i=0; i < e.childNodes.length; i++) 
        {
            this.ClearHandlers(e.childNodes[i]);    
        }
    }    
}

// Register the class as a type that inherits from Sys.UI.Control.
XICenter.LeftNavMenu.Display.registerClass('XICenter.LeftNavMenu.Display', Sys.UI.Control);

XICenter.LeftNavMenu.MenuItem = function(Text, Level, URL, ID, Show, ItemType)
{
    this._text = Text;
    this._level = Level;
    this._url = URL;
    this._id = ID;
    this._show = Show;   
    this._iconImageObj = null;
    if (ItemType === 0)
        this._itemType = XICenter.LeftNavMenu.ItemType.Expandable;
    else
    {
        if (ItemType === 1)
            this._itemType = XICenter.LeftNavMenu.ItemType.NonExpandable;
        else
            if (ItemType === 2)
                this._itemType = XICenter.LeftNavMenu.ItemType.Navigation;
            else
                this._itemType = XICenter.LeftNavMenu.ItemType.LoadOnStart;
    }
    this._expanded = false;
}
XICenter.LeftNavMenu.MenuItem.registerClass('XICenter.LeftNavMenu.MenuItem');

//Item Type states
XICenter.LeftNavMenu.ItemType = function(){};
XICenter.LeftNavMenu.ItemType.prototype = 
{
    Expandable : 0,
    NonExpandable : 1,
    Navigation : 2,
    LoadOnStart: 3
}
XICenter.LeftNavMenu.ItemType.registerEnum("XICenter.LeftNavMenu.ItemType");

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
