// Adapted to G6 Networks

// CBE Cascading Drop-down Menu
// copyright (c) 2002 Mike Foster
// get CBE at cross-browser.com
// CBE and cbeDropdownMenu are licensed under the LGPL

// v1.5 30Oct02 - bug fix for when label with no box is the last label
// v1.4 29Oct02 - added support for main labels with no boxes
// v1.3 01Oct02 - utilizes the update to CBE v4.15 which allows object methods to be used as event listeners
// v1.2 27Aug02 - now licensed under LGPL
// v1.1 20Aug02 - added optional parameters to the paint() method, for re-painting on win resize
// v1.0 17Aug02 - initial release

var
    cbeMenu,
    mnuMarker,
    downgrade = true, Main,
    ua = navigator.userAgent.toLowerCase (),
    StdStateHandler, 
    ActiveMenu,
    ActiveTimeOut,
    MenuActive,
    DefaultMenu;

// IE4 up
// Gecko
// Konquerer
// Opera

if (
    ((ua.indexOf ('msie') != -1) && (parseInt(navigator.appVersion) >= 4))
    || (ua.indexOf ('gecko') != -1)                                     
    || (ua.indexOf ('konqueror') != -1)                                 
    || window.opera                                                   
) 
{  
    document.write("<script type='text/javascript' src='js/cbe_core.js'></script>");
    document.write("<script type='text/javascript' src='js/cbe_event.js'></script>");
    downgrade = false;
}

function windowOnload () 
{
    Main = cbeGetElementById('Main').cbe;
    //alert (Main.pageX () + " " + Main.width ());
    StdStateHandler = new StateHandler_Prolexys ();
    ActiveMenu = new cbeDropdownMenu 
    (
        0, 0,                                   // coord of first label
        -1, -1,                                 // label width and height
        120,                                    // box width
        18,                                     // item height
        0,                                      // item left padding
        100, Main.pageX () + Main.width (), 
        StdStateHandler
    );
    window.cbe.addEventListener('resize', winResizeListener, false);
}

  
//alert ("x: " + mnuMarker.pageX() + " y:" + mnuMarker.pageY());


function winResizeListener () 
{
    cbeMenu.paint(0, 0);
}

// begin class cbeDropdownMenu
// Opera
function StateHandler_Prolexys ()
{
	this.bgColor = function (T)
	{
		//T.Level = T.Level ? T.Level : 0;
		switch (T.Level)
		{
			case 1:
				return '#7166A6';
			break;

			
			case 2:
				return '#E08600';
				return '#ADA4DB';
			break;
			
			default:
				return '#4E4773';
				
			break;
		}
	}
	
	this.txtColor = function (T)
	{
		//T.Level = T.Level ? T.Level : 0;
		switch (T.Level)
		{
			case 1:
				return '#FFFFFF';
			break;

			case 2:
				return '#000';
				return '#FFFFFF';
			break;
			default:
				return '#FFFFFF';
			break;
		}
	}

	this.hvrBColor = function (T)
	{
		//T.Level = T.Level ? T.Level : 0;
		switch (T.Level)
		{
			case 1:
				return '#E08600';
				return '#ADA4DB';
			break;

			case 2:
				return '#AE9DFF';
				return '#8C82CC';
			break;
			default:
				// return '#FFFF00';
                // Menu label
				return '#4E4773';
			break;
			
		}
	}

	this.hvrTColor = function (T)
	{
		//T.Level = T.Level ? T.Level : 0;
		switch (T.Level)
		{
			case 1:
				//return '#4E4773';
				return '#000000';
			break;

			case 2:
				//return '#4E4773';
				return '#000000';
			break;
			default:
				return '#AE9DFF';
			break;
			
		}
	}
}

/*
   bgColor, 
   txtColor, 
   hvrBColor, 
   hvrTColor
 */


function cbeDropdownMenu
(
	mnuX, 
	mnuY, 
	lblW, 
	lblH, 
	boxW, 
	itmH, 
	itmPad,
	MaxLeft,		// 0 for leftmost menulabel
	MaxRight,		// 0 for rightmost menulabel + width
	StateHandler
	
) 
{

    // Properties

    this.mnuX = mnuX;
    this.mnuY = mnuY;
    this.lblW = lblW;
    this.lblH = lblH;
    this.boxW = boxW;
    this.itmH = itmH;
    this.itmPad = itmPad;
  
    this.MaxRight = MaxRight != 0 ? MaxRight : 10000;
    this.MaxLeft = MaxLeft != 0 ? MaxLeft : -10000;
    this.StateHandler = StateHandler;
    //this.bgColor = bgColor;  
  
    /*
    this.\txtColor = txtColor; 
    this.\hvrBColor = hvrBColor;
    this.\hvrTColor = hvrTColor;
    */
    this.lblCount = 0;
    this.lblActive = null;
  
    // Methods

    this.paint = function (mnuX, mnuY) // this is the only public method
    { 
        if (arguments.length > 0) this.mnuX = mnuX;
        if (arguments.length > 1) this.mnuY = mnuY;
        var lbl = null; // of type Element
        var box = null; // of type CBE
        var mX = this.mnuX;
        var W;
        var H;
        var X;
        var Y;
        this.lblCount = 0;
        do 
        {
            ++this.lblCount;
            lbl = cbeGetElementById ('label' + this.lblCount)
            
            if (lbl) 
            {
                lbl.cbe.IsMenu = 1;
                lbl.cbe.Level = 0;
                with (lbl.cbe) 
                {
                    color(this.StateHandler.txtColor (lbl.cbe));    
                    background(this.StateHandler.bgColor (lbl.cbe));
                    zIndex(2002);
                    //resizeTo((this.lblW > 0) ? this.lblW : lbl.cbe.width (), (this.lblH > 0) ? this.lblH : lbl.cbe.height ());
                    //moveTo(mX, this.mnuY);
                    show();
                }
                
                W = lbl.cbe.width ();
                H = lbl.cbe.height ();
                X = lbl.cbe.pageX ();
                Y = lbl.cbe.pageY ();
            
                //if (lbl.cbe.nextSibling && lbl.cbe.nextSibling.id.indexOf('label')==-1) box = lbl.cbe.nextSibling;
                //else box = null;
                box = cbeGetElementById('menu' + this.lblCount);
                if (box)
                {
                    box = box.cbe;
                } else {
                    box = null;
                }
                lbl.cbe.childBox = box;
                lbl.cbe.parentLabel = null;
                lbl.cbe.TopLabel = lbl.cbe;
                lbl.cbe.Root = this;
                if (box) 
                {
                    this.paintBox
                    (
                        box, 
                        lbl.cbe, 
                        X + this.mnuX, 
                        Y + ((this.mnuY != 0) ? this.mnuY : H), 
                        1
                    );
                }
                mX += W;
            }
        } while (lbl);
        --this.lblCount;
    }

    this.paintBox = function (box, parent, x, y, Level) 
    {
        var mx=0, my=0, itmCount=0, W = 0;
        box.Level = Level;
        box.IsBox = 1;
        box.IsMenu = 1;
        box.Root = this;
        box.background(this.StateHandler.bgColor (box));
        //box.width(this.boxW);
        //alert (x);
        box.zIndex(2002);

	    // Uncomment the following if you want it to become the width of the parent if the content
        // is smaller.
    	// W = Math.max(box.width () + 2 * this.itmPad, (parent.Level == 0) ? parent.width () : 0);

        var itm = box.firstChild;
        // First get max width:
        while (itm) 
        {
            if (itm.id.indexOf ('i') != -1)
            {
                W = Math.max (W, itm.width ());
            }
            itm = itm.nextSibling;
        }
            
        //W = box.width () + 2 * this.itmPad;
	
        itm = box.firstChild;
        while (itm) 
        {
            itm.TopLabel = parent.TopLabel;
            itm.Root = this;
            itm.IsMenu = 1;
            itm.MyBoxLabel = parent;
            if (itm.id.indexOf ('i') != -1)
            {
                itm.Level = Level;
                itm.color (this.StateHandler.txtColor (itm));
                itm.background (this.StateHandler.bgColor (itm));
                itm.width (W + 2 * this.itmPad);
                //itm.resizeTo (W + 2 * this.itmPad, itm.height ());
                //alert (W + " " + itm.width ());
                itm.moveTo (mx + this.itmPad, my);
                itm.show ();
                my += itm.height ();
                ++itmCount;
            } else {
                itm.previousSibling.childBox = itm;
                itm.previousSibling.parentLabel = parent;
                this.paintBox
                (
                    itm, 
                    itm.previousSibling, 
                    mx + itm.parentNode.width (), 
                    my - itm.previousSibling.height (), 
                    Level + 1
                );
            }
            itm = itm.nextSibling;
        }
        //box.width (W + 2 * this.itmPad);
        box.moveTo (Math.min (x - this.itmPad, this.MaxRight - box.width () - 2 * this.itmPad), y);

        //box.height(my);
    }

    this.mousemoveListener = function (e) 
    {

        if 
        (
            !e.cbeTarget.IsMenu && 
            (this.CurrentMenuItem != this.DefaultMenu)
            && this.MenuActive
        )               
        {
            // Close menu (timed) because we're not over a menu component:
            
            if (this.MenuActive)
            {
                //window.status = "Timeout: 1";
                ActiveTimeOut = setTimeout ("CloseMenu ()", 1000);
                this.MenuActive = 0;
            }
            /*
            
            if (this.lblActive.childBox)
            {
                this.lblActive.childBox.hide();
            }

            this.lblActive.color (this.StateHandler.txtColor (this.lblActive));
            this.lblActive.background (this.StateHandler.bgColor (this.lblActive));
            this.lblActive = this.lblActive.parentLabel;
            */
        } else if (e.cbeTarget.IsMenu)
        {
            this.MenuActive = 1;
            //window.status = "Timeout: 0";
            clearTimeout (ActiveTimeOut);
            /*
            if (!e.cbeTarget.IsBox) 
            {
                
                if (this.CurrentLevel == e.cbeTarget.Level)
                {
                    // If it is the same level, unhover the old one:
                    //this.CurrentMenuItem.color (this.StateHandler.txtColor (this.CurrentMenuItem));
                    //this.CurrentMenuItem.background (this.StateHandler.bgColor (this.CurrentMenuItem));
                    //CloseMenuTreeUpToLevel (this.CurrentMenuItem, e.cbeTarget.Level + 1);
                    
                    if (this.CurrentMenuItem.childBox) 
                    {
                        // Open any child boxes the menu item may own:
                        //this.CurrentMenuItem.childBox.hide ();
                    }
                    
                    //CloseMenuTreeUpToLevel (this.CurrentMenuItem, e.cbeTarget.Level);

                } else if (this.CurrentLevel > e.cbeTarget.Level)
                {
                    if (this.CurrentMenuItem.childBox)
                    {
                        this.CurrentMenuItem.childBox.hide ();
                    }
                }
                 //alert (this.CurrentMenuItem.childBox);
                       
            }
            */
            if (!e.cbeTarget.IsBox)
            {
                
                if (this.CurrentMenuItem)
                {
                    if (e.cbeTarget.Level == this.CurrentMenuItem.Level) 
                    {
                        //this.CurrentMenuItem.color (this.StateHandler.txtColor (this.CurrentMenuItem));
                        //this.CurrentMenuItem.background (this.StateHandler.bgColor (this.CurrentMenuItem));
                        // Open any child boxes the menu item may own:
                        CloseMenuTreeUpToLevel (this.CurrentMenuItem, e.cbeTarget.Level);
                        //this.CurrentMenuItem.childBox.hide();
                    } else if (e.cbeTarget.Level < this.CurrentMenuItem.Level) 
                    {
                        if (this.CurrentMenuItem.MyBoxLabel)
                        {
                            CloseMenuTreeUpToLevel (this.CurrentMenuItem, e.cbeTarget.Level);
                        }
                        //this.CurrentMenuItem.parentLabel.childBox.hide ();

                    }
                        
                }
                this.CurrentMenuItem = e.cbeTarget;
                this.CurrentLevel = this.CurrentMenuItem.Level;
                
                this.CurrentMenuItem.color (this.StateHandler.hvrTColor (this.CurrentMenuItem));
                this.CurrentMenuItem.background (this.StateHandler.hvrBColor (this.CurrentMenuItem));
            }
            
            if (this.CurrentMenuItem.childBox) 
            {
                // Open any child boxes the menu item may own:
                this.CurrentMenuItem.childBox.show();
            }
 
        } 
        
    }

    this.CloseMenu = function ()
    {
        if (this.CurrentMenuItem)
        {
            CloseMenuTreeUpToLevel (this.CurrentMenuItem, 0);
        }
        if (DefaultMenu)
        {
            this.DefaultMenu = cbeGetElementById (DefaultMenu).cbe;
            this.CurrentMenuItem = this.DefaultMenu;
            this.CurrentMenuItem.Level = this.DefaultMenu.Level;
            OpenMenuTree (this.CurrentMenuItem);
            this.MenuActive = 1;
        } else {
            this.CurrentMenuItem = null;
            this.DefaultMenu = null;
        }
    }
  
    // Constructor Code

    this.paint();
    document.cbe.addEventListener ('mousemove', this.mousemoveListener, false, this);
    this.CloseMenu ();

} // end class cbeDropdownMenu

function CloseMenuTree (Target)
{
    if (Target.IsMenu)
    {
        if (Target.childBox)
        {
            Target.childBox.hide ();
            Target.color (Target.Root.StateHandler.txtColor (Target));
            Target.background (Target.Root.StateHandler.bgColor (Target));
        }
        
        if ((Target.Level > 0) && Target.MyBoxLabel)
        {
            CloseMenuTree (Target.MyBoxLabel);
        }
    }
}    

function OpenMenuTree (Target)
{
    if (Target.IsMenu)
    {
        Target.color (Target.Root.StateHandler.hvrTColor (Target));
        Target.background (Target.Root.StateHandler.hvrBColor (Target));
        if (Target.childBox)
        {
             Target.childBox.show ();
        }
        if (Target.Level > 0)
        {
            OpenMenuTree (Target.MyBoxLabel);
        }
    }
}    


function CloseMenuTreeUpToLevel (Target, Level)
{
    if (Target.IsMenu)
    {
        Target.color (Target.Root.StateHandler.txtColor (Target));
        Target.background (Target.Root.StateHandler.bgColor (Target));
        if (Target.childBox)
        {
            Target.childBox.hide ();
        }
        
        if ((Target.Level > Level) && Target.MyBoxLabel)
        {
            CloseMenuTreeUpToLevel (Target.MyBoxLabel, Level);
        }
    }
}

function CloseMenu ()
{
    ActiveMenu.CloseMenu ();
}

//DefaultMenu = "i2624";
