﻿var Menu = function( id )
{
	Extends( Control,this, id );
	
	this.items = new MenuItemCollection( null, this );
}

Menu.prototype = new function()
{
	this.menu            = this;
	this.items           = null;
	this.childVisible    = null;
	this.topToBottom     = false; // 0 = leftRight; 1 = topButton
	
	this.popupOffsetTop  = 0;
	this.popupOffsetLeft = 0;
	
	this.cssClassSubItem      = "menuSubItem";
	this.cssClassSubItemPadre = "menuSubItemPadre";
	
	this.focusTimer      = null;
	this.itemToExpand    = null;
	this.itemExpanded    = null;
	
	this.cancelWindowBlur = false;
	
	this.Init = init; function init()
	{
		for( var i = 0; i < this.items.GetCount(); i++ )
		{
			var menuItem = this.items.Get(i);
			this.configureElement( menuItem, menuItem.element );
		}
		
		browser.AttachEvent( document, "mousedown", new pox.Delegate( this, this.mouseDownHandler ) );
		browser.AttachEvent( window, "blur", new pox.Delegate( this, this.blurHandler ) );
	}
	
	this.mouseDownHandler = function( args )
	{
		if( ControlHandler.GetControlByElement( args.Target ) == this )
		{
			//browser.Trace("se produjo un mouse down en el menu");
			this.cancelWindowBlur = true;
		}
		else if( this.IsOnFocus() )
		{
			//browser.Trace("Blurea por mouse down");
			this.Blur();
		}
	}
	
	this.blurHandler = function( args )
	{
		if( !this.cancelWindowBlur )
		{
			this.Blur();
		}
		
		args.CancelEvent      = this.cancelWindowBlur;
		this.cancelWindowBlur = false;
	}
	
	this.onMenuItemClick = function( menuItem )
	{
		if( menuItem.onClick != null )
			menuItem.onClick();
		
		this.hide();
	}
	
	this.hide = function()
	{
		this.collapseItem( this.itemExpanded );
	}
	
	this.collapseItem = function( menuItem )
	{
		if( menuItem != null )
		{
			browser.Hide( menuItem.panel );
			menuItem.parent.itemExpanded = null;
			
			this.collapseItem( menuItem.itemExpanded );
		}
	}
	
	this.onMenuItemMouseOver = cc; function cc( td, menuItem )
	{
		//if( menuItem.idleStyle == null )
		//	menuItem.idleStyle = td.childNodes[0].currentStyle;
		
		if( this.IsOnFocus() )
		{
			// Me fijo si mi menu Superior tiene expandido un elemento.
			if( menuItem.parent.itemExpanded != null )
				this.collapseItem( menuItem.parent.itemExpanded );
			
			if( menuItem.items.GetCount() > 0 )
			{
				// Si el menuItem no posee el panel dibujado lo mando a dibujar
				if( menuItem.panel == null )
					this.renderChildPanel( menuItem, document.body );
			
				var r = browser.GetClientRectangle( menuItem.element );
				
				if( menuItem.parent == this && !this.topToBottom )
					browser.ShowFloating( menuItem.panel, r.Bottom + this.popupOffsetTop, r.Left + this.popupOffsetLeft );
				else
					browser.ShowFloating( menuItem.panel, r.Top, r.Right );
				
				menuItem.parent.itemExpanded = menuItem;
			}
		}
		else
		{
			if( this.focusTimer == null )
			{
				this.focusTimer = new Timer( 100 );
				this.focusTimer.OnTick.Suscribe( this, this.focusTimerEnd );
			}
			else
				this.focusTimer.Stop();
			
			this.focusTimer.Start();
			this.itemToExpand = menuItem;
			
			//browser.Trace( "esperar a "+ menuItem.text );
		}
	}
	
	this.focusTimerEnd = function( sender )
	{
		this.focusTimer.Stop();
		this.Focus();
		
		if( this.itemToExpand != null )
		    this.onMenuItemMouseOver( this.itemToExpand.element, this.itemToExpand );
	}
	
	this.onMenuItemMouseOut = function( td, menuItem )
	{
		if( !this.IsOnFocus() && menuItem == this.itemToExpand )
			this.focusTimer.Stop();
			
		//if( menuItem.overStyle == null )
		//	menuItem.overStyle = td.childNodes[0].currentStyle;
		
		//if( menuItem.parent.itemExpanded == menuItem )
		//	td.childNodes[0].className="Item:hover";
	}
	
	this.renderChildPanel = function( menuItem, output )
	{
		var marco = this.createElement( "div" );
		marco.className        = "menuMarco";
		marco.style.position   = "absolute";
		marco.style.visibility = "hidden";
		browser.InsertElement( output, marco );
		
		var table = this.createElement( "table" );
		table.cellSpacing      = "0";
		table.cellPadding      = "0";
		browser.InsertElement( marco, table );
		
		var tBody = document.createElement( "tbody" );
		browser.InsertElement( table, tBody );
		
		var childItem;
		var height = 0;
		var width  = 0;
		var size;
		var content;
		
		for( var i = 0; i < menuItem.items.GetCount(); i++ )
		{
			childItem = menuItem.items.Get(i);
			
			if( childItem.text == "-" )
				this.createSeparator( childItem, tBody );
			else
				this.createItem( childItem, tBody );
		}
		
		// Agrando el tama�o de los links para que queden todos iguales
		/*
		var maxWidth = 1;
		
		for( var i = 0; i < menuItem.items.GetCount(); i++ )
		{
			childItem = menuItem.items.Get(i);
			
			if( childItem.text != "-" )
				maxWidth = Math.max( maxWidth, browser.GetClientWidth( childItem.linkElement ) );
		}
		
		for( i = 0; i < menuItem.items.GetCount(); i++ )
		{
			childItem = menuItem.items.Get(i);
			
			if( childItem.text != "-" )
				childItem.linkElement.style.width = maxWidth;
		}
		*/
		
		menuItem.panel = marco;
	}
	
	this.createItem = createItem; function createItem( menuItem, output )
	{
		var tr = document.createElement( "tr" );
		var td = document.createElement( "td" );
		browser.InsertElement( tr, td );
		
		var link = this.createElement( "a" );
		link.style.display = "block";
		
		link.className = this.cssClassSubItem;
		
		if( menuItem.items.GetCount() > 0 )
			link.className += " "+ this.cssClassSubItemPadre;
		
		if( menuItem.url != null )
		{
		    link.href = menuItem.url;
		    
		    if( menuItem.target != null )
				link.target = menuItem.target;
		}
		else
		    link.href = "javascript:;";
		
		browser.SetInnerText( link, menuItem.text );
		browser.InsertElement( td, link );
		
		menuItem.linkElement = link;
		
		this.configureElement( menuItem, td );
		browser.InsertElement( output, tr );
	}
	
	this.configureElement = a; function a( menuItem, element )
	{
		element.menu        = this;
		element.menuItem    = menuItem;
		element.onclick     = function(){this.menu.onMenuItemClick(this.menuItem);}
		element.onmouseover = function(){this.menu.onMenuItemMouseOver(this,this.menuItem);}
		element.onmouseout  = function(){this.menu.onMenuItemMouseOut(this,this.menuItem);}
		
		menuItem.element    = element;
	}
	
	this.createSeparator = createSeparator; function createSeparator( mItem, output )
	{
		var tr = document.createElement( "tr" );
		var td = document.createElement( "td" );
		td.className = "Separador";
		
		browser.InsertElement( tr, td );
		browser.InsertElement( td, document.createElement("div") );
		browser.InsertElement( output, tr );
	}
	
	this.onFocus = function()
	{
		window.focus();
		this.base.onFocus();
	}
	
	this.onBlur = function()
	{
		if( this.itemExpanded != null )
			this.collapseItem( this.itemExpanded );
		
		this.base.onBlur();
	}
}

function MenuItem( id, text, url )
{
	this.id      = id;
	this.element = (id)?document.getElementById( id ):null;
	this.text    = (text)?text:null;
	this.url     = (url)?url:null;
	this.menu    = null;
	this.panel   = null;
	
	this.target           = null;
	this.onClick          = null;
	this.items            = new MenuItemCollection(this);
	this.linkElement      = null;
	this.childVisible     = null;
	this.clildListWidth   = null;
	this.childListElement = null;
	this.parent           = null;
	this.enabled          = true;
	
	this.Init = a; function a()
	{
		//this.element.onmouseover = function(){browser.Trace("Element");}
	}
}

// MenuItemCollection
var MenuItemCollection = function( menuItem, menu )
{
	Extends( Collection, this );
	
	this.menuItem = menuItem;
	this.menu     = (menu)?menu:null;
}

MenuItemCollection.prototype = new function()
{
	this.menuItem = null;
	this.menu     = null;
	
	this.Add = function( menuItem )
	{
		if( this.menuItem != null )
		{
			menuItem.menu   = this.menuItem.menu;
			menuItem.parent = this.menuItem;
		}
		else
		{
			menuItem.menu   = this.menu;
			menuItem.parent = this.menu;
		}
		
		this.base.Add( menuItem );
	}
}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();