﻿//---------------------------------------------------------------------------------
//  (c) 2007 Guardian Networks, Inc
//---------------------------------------------------------------------------------
GNgui.GNguiHExpandMenu = Class.create ( );

GNgui.GNguiHExpandMenu.prototype = 
{
	initialize : function ( obj )
	{
		this.ObjNameBase = obj.ObjNameBase;
		this.ParentObj = obj.Parent;
		this.Direction = obj.Direction;
		this.ImageWidth = 13;
		this.MaxWidth = obj.Width;
		this.Top = obj.Top;
		this.Closed = true;
		this.ImageID = this.ObjNameBase + "Image";
		this.Height = this.ParentObj.clientHeight - ( this.Top + 50 );
		this.Tabs = new Array ();

		this.ContainerDiv = $ce ( 'div' );
		this.ContainerDiv.style.zIndex = 20;
		this.ContainerDiv.id = this.ObjNameBase;
		this.ContainerDiv.ParentObj = this;
		this.ContainerDiv.Persistent = true;
		this.ContainerDiv.style.position = "absolute";
		this.ContainerDiv.style.top = this.Top;
		this.ContainerDiv.Height = this.Height;
		this.ContainerDiv.style.height = this.Height;
		this.ContainerDiv.style.overflow = 'hidden';

		this.ImageDiv = $ce ( 'div' );
		this.ImageDiv.style.zIndex = 20;
		this.ImageDiv.id = this.ObjNameBase + "ImageDiv";
		this.ImageDiv.Width = 16;
		this.ImageDiv.style.position = "absolute";
		this.ImageDiv.style.top = 0;
		this.ImageDiv.Height = this.Height;
		this.ImageDiv.style.height = this.ImageDiv.Height;

		this.TabTable = ce$ ( 'table', this.ImageDiv.id + 'Table', '', this.ImageDiv );
		this.TabTable.border = "0";
		this.ImageDiv.appendChild ( this.TabTable );

		this.ContentDiv = $ce ( 'div' );
		this.ContentDiv.style.zIndex = 20;
		this.ContentDiv.id = this.ObjNameBase + "ContentDiv";
		this.ContentDiv.style.width = this.MaxWidth - this.ImageWidth;
		this.ContentDiv.className = 'HExpandMenuInner';
		this.ContentDiv.style.backgroundColor = "FFFFFF";
		this.ContentDiv.style.position = "absolute";
		this.ContentDiv.style.visibility = 'hidden';
		this.ContentDiv.Width = this.MaxWidth - this.ImageDiv.Width;
		this.ContentDiv.style.width = this.ContentDiv.Width;
		this.ContentDiv.style.top = 0;
		this.ContentDiv.Height = this.Height - 50;
		this.ContentDiv.style.height = this.ContentDiv.Height;

		this.ParentObj.appendChild ( this.ContainerDiv );

		if ( this.Direction == "right" )
		{
			this.Left = 0;
			this.Right = this.MaxWidth;

			this.ContainerDiv.Left = this.Left;

			this.ImageDiv.Left = 0;
			this.ImageDiv.style.left = this.ImageDiv.Left;

			this.ContentDiv.Left = 1;
			this.ContentDiv.style.left = this.ContentDiv.Left;
		}
		else
		{
			this.Right = this.ParentObj.clientWidth;
			this.Left = this.Right - this.MaxWidth;

			this.ContainerDiv.Left = this.Left;

			this.ImageDiv.Left = 0;
			this.ImageDiv.style.left = this.ImageDiv.Left;

			this.ContentDiv.Left = this.ImageDiv.Width + 1;
			this.ContentDiv.style.left = this.ContentDiv.Left;
		}
		this.ContainerDiv.style.left = this.ContainerDiv.Left;

		this.ContainerDiv.Width = this.ImageWidth;
		this.ContainerDiv.style.width = this.ContainerDiv.Width;

		this.Image = $e( this.ImageID );

		this.ContainerDiv.appendChild ( this.ImageDiv );
		this.ContainerDiv.appendChild ( this.ContentDiv );

		this.CloseDiv ();

    Gutenberg.Subscribe 
    ( 
			"ContentWidth", 
			this.ResizeOccurred.bindAsEventListener ( this ) 
    );
		Gutenberg.Subscribe 
		( 
			'GNguiHExpandMenuOpened', 
			this.MenuOpened.bindAsEventListener ( this ) 
		);
		Gutenberg.Subscribe 
		( 
			'GNguiHExpandMenuItemOpen', 
			this.MenuItemOpened.bindAsEventListener ( this ) 
		);
		Gutenberg.Subscribe 
		( 
			'GNguiHExpandMenuItemClosed', 
			this.MenuItemClosed.bindAsEventListener ( this ) 
		);
	},
	
	ResizeOccurred : function ( event )
	{
		if ( this.Direction == "left" )
		{
			this.Right = this.ParentObj.clientWidth;

			if ( this.Closed == true )
			{
				this.AlignClosed ();
			}
			else
			{
				this.AlignOpened ();
			}
		}
	},
	
	MenuOpened : function ( DivOpened )
	{
		if ( DivOpened.ObjNameBase != this.ObjNameBase )
		{
			this.CloseDiv();
		}
	},
	
	MenuItemOpened : function ( event )
	{
		Event.stop ( event );
		this.OpenDiv ();
		return false;
	},
	
	MenuItemClosed : function ( event )
	{
		Event.stop ( event );
		var AllClosed = true;
		l=this.Tabs.length;
		for ( var i = 0, len = l; i < len; i++ )
		{
			if ( this.Tabs[ i ].Closed == false )
			{
				AllClosed = false;
			}
		}
		if ( AllClosed )
		{
			this.CloseDiv ();
		}
		return false;
	},
	
	ExtendCell : function ( obj )
	{
		Cell = obj.Cell;

		Cell.id = obj.Data.Title + "TabCell";
		Cell.Title = obj.Data.Title;
		Cell.Container = obj.Container;
		Cell.HasContent = false;

		Cell.ImageInactive = './FD/Images/ExpandingMenu/' + obj.Data.ImageInactive;
		Cell.ImageActive = './FD/Images/ExpandingMenu/' + obj.Data.ImageActive;

		var CellImage = $ce ( 'img' );
		CellImage.id = obj.Data.Title + "TabCellImage";
		Cell.CellImageID = CellImage.id;
		Cell.appendChild ( CellImage )

		var ContentDiv = $ce ( 'div' );

		ContentDiv.id = obj.Cell.Title + "AccordianDiv";
		ContentDiv.style.position = "absolute";
		ContentDiv.style.top = 0;
		ContentDiv.style.visibility = "hidden";
		this.ContentDiv.appendChild ( ContentDiv );

		Cell.ContentDivID = ContentDiv.id;

		Cell.Closed = true;

		Cell.SetEvents = function ()
		{
			Event.observe 
			( 
				this.CellImageID, 
				'click', 
				this.OpenOrClose.bindAsEventListener ( this ) 
			);
			Gutenberg.Subscribe 
			( 
				'GNguiHExpandMenuItemOpen', 
				this.MenuItemOpened.bindAsEventListener ( this ) 
			);
		};

		Cell.MenuItemOpened = function ( ItemOpened )
		{
			if ( ItemOpened.ContentDivID != this.ContentDivID )
			{
				this.Closed = true;
				this.CheckStatus ( false );
			}
		};

		Cell.BuildAccordion = function ()
		{
			if ( this.HasContent )
			{
				if (this.ContentDivID == 'WaterAccordianDiv')
				{
					this.Accordian = new Rico.Accordion
					( 
						$e( this.ContentDivID ), 
						{
							panelHeight : this.Container.Height - 50,
							expandedBg : '#25BBFC',
							hoverBg : '#038FCC',
							collapsedBg : '#038FCC',
							expandedTextColor : '#000000',
							hoverTextColor : '#000000',
							collapsedTextColor : '#000000',
							borderColor : '#C2C2C2'
						} 
					);
				}
				else if (this.ContentDivID == 'Parking MeterAccordianDiv')
				{
					this.Accordian = new Rico.Accordion
					( 
						$e( this.ContentDivID ), 
						{
							panelHeight : this.Container.Height - 50,
							expandedBg : '#C1D72D',
							hoverBg : '#A8BB24',
							collapsedBg : '#A8BB24',
							expandedTextColor : '#000000',
							hoverTextColor : '#000000',
							collapsedTextColor : '#000000',
							borderColor : '#C2C2C2'
						} 
					);				
				}
				else
				{
					this.Accordian = new Rico.Accordion
					( 
						$e( this.ContentDivID ), 
						{
								panelHeight : this.Container.Height - 50,
								expandedBg : '#C2C2C2',
								hoverBg : '#A6A6A6',
								collapsedBg : '#A6A6A6',
								expandedTextColor : '#000000',
								hoverTextColor : '#000000',
								collapsedTextColor : '#000000',
								borderColor : '#C2C2C2'
						} 
					);				
				}
				this.Container.appendChild ( $e( this.ContentDivID ) );
			}
		};

		Cell.CheckStatus = function ( publish )
		{
			if ( this.Closed )
			{
				$e( this.CellImageID ).src = this.ImageInactive;
				$e( this.ContentDivID ).style.visibility = "hidden";
				$e( this.CellImageID ).alt = "Click to open " + this.Title;

				if ( publish == true )
				{
					Gutenberg.PublishNow ( "GNguiHExpandMenuItemClosed", this );
				}
			}
			else
			{
				$e( this.CellImageID ).src = this.ImageActive;
				$e( this.ContentDivID ).style.visibility = "inherit";
				$e( this.CellImageID ).alt = "Click to close";

				if ( publish == true )
				{
					Gutenberg.PublishNow ( "GNguiHExpandMenuItemOpen", this );
				}
			}
		};

		Cell.OpenOrClose = function ( event )
		{
			if (Permissions.CheckMenuPermissions("Monitoring Maps"))
			{
				MapSearchMenuOptions.Hide();
			}
			CancelContextMenu();
			Event.stop ( event );

			if ( this.Closed )
			{
				this.Open();
			}
			else
			{
				this.Close();
			}
			return false;
		};
   
		Cell.ShowStack = function ()
		{
			var Report = Gutenberg.PrintMessages ();
			var fso = new ActiveXObject("Scripting.FileSystemObject");
			var s = fso.CreateTextFile("C:\\test.txt", true);
			s.WriteLine(Report);
			s.Close();
		};

		Cell.Close = function ()
		{
			this.Closed = true;
			this.CheckStatus ( true );
		};

		Cell.Open = function ()
		{
			this.Closed = false;
			this.CheckStatus ( true );
		};

		Cell.AddContent = function ( obj )
		{
			var InnerAccordionContainer = $ce ( 'div' );
			var InnerAccordionTitle = $ce ( 'div' );
			var Content = $e( obj.Content );

			Content.style.visibility = "hidden";

			InnerAccordionContainer.id = this.Title + "AccordionContainer";
			InnerAccordionContainer.className = "AccordionContainer"

			InnerAccordionTitle.id = this.Title + "AccordionTitle";
			InnerAccordionTitle.className = "OuterAccordionTitle";
			InnerAccordionTitle.style.textAlign = "Center";
			InnerAccordionTitle.innerHTML = obj.Title;

			Content.className = "AccordionContent";

			InnerAccordionContainer.appendChild ( InnerAccordionTitle );
			InnerAccordionContainer.appendChild ( Content );
			Content.style.visibility = "inherit";
			$e( this.ContentDivID ).appendChild ( InnerAccordionContainer );
			this.HasContent = true;
		};
	},
	
	AddTab : function ( obj )
	{
		var row = this.TabTable.insertRow ();
		var cell = row.insertCell ();
		//Element.extend ( cell );
		this.ExtendCell 
		( 
			{
				Data : obj, 
				Cell : cell,
				Container : this.ContentDiv
			}
		);
		cell.SetEvents ();
		this.Tabs.push ( cell );
		return cell;
	},
	
	GetTab : function ( title )
	{
		return $e( title + "TabCell" )
	},
	
	CloseDiv : function ()
	{
		this.AlignClosed ();        
		this.ContentDiv.style.visibility = 'hidden';
		this.Closed = true;
	},
	
	Hide : function ()
	{
		this.ContainerDiv.style.visibility = "hidden";
	},
	
	Show : function ()
	{
		this.ContainerDiv.style.visibility = "inherit";
	},
	
	AlignClosed : function ()
	{
		this.ContainerDiv.Width = this.ImageDiv.Width;
		this.ContainerDiv.style.width = this.ContainerDiv.Width;
		    
		if ( this.Direction == "left" )
		{
			this.Left = this.Right - this.ImageDiv.Width;
			this.ContainerDiv.Left = this.Left;
			this.ContainerDiv.style.left = this.Left;
		}
		else
		{
			this.ImageDiv.Left = 0;
			this.ImageDiv.style.left = this.ImageDiv.Left;
		}
	},
	
	AlignOpened : function ()
	{
    this.Left = this.Right - this.MaxWidth;

    this.ContainerDiv.Width = this.MaxWidth;
    this.ContainerDiv.style.width = this.MaxWidth;

    this.ContainerDiv.Left = this.Left;
    this.ContainerDiv.style.left = this.Left;

    this.ContentDiv.style.visibility = 'inherit';
    
    this.Closed = false;

    if ( this.Direction == "right" )
    {
      this.ImageDiv.Left = this.ContentDiv.Width;
      this.ImageDiv.style.left = this.ImageDiv.Left;
    }
	},
	
	OpenDiv : function ()
	{
    if ( this.Closed == true )
    {
      this.AlignOpened ();            
      Gutenberg.PublishNow 
      ( 
        "GNguiHExpandMenuOpened" , 
        { 
            ObjNameBase : this.ObjNameBase, 
            Direction : this.Direction 
        } 
      );
    }
	},
	
	OpenOrClose : function ( event )
	{
    Event.stop ( event );
    
    if ( this.Closed )
    {
      this.OpenDiv();
    }
    else
    {
      this.CloseDiv();
    }
    return false;
	}
}




