package classes.matuto.ui{

	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	import flash.events.ContextMenuEvent;
	
	public class RightMenu {
		
		
		private static var itens	:Array = new Array();
		
		
		public static function addMenuItem( caption :String, container :*, separator :Boolean = false, enabled :Boolean = true, visible :Boolean = true, call :Function = null ) :int
		{
			
			var menu	:ContextMenu = container.contextMenu;
			var item	:ContextMenuItem = new ContextMenuItem( caption, separator, enabled, visible );
			
			if(call is Function) item.addEventListener( ContextMenuEvent.MENU_ITEM_SELECT, call );
			
			if (!menu) menu = new ContextMenu();
			
			menu.hideBuiltInItems();
			menu.customItems.push(item);
			
			container.contextMenu = menu;
			
			itens.push( {item:item, call:call } );
			
			return itens.length-1;
			
			
		}
		
		
		public static function getItemByIndex( index :int ) :ContextMenuItem
		{
			
			if ( itens[index] ) return itens[index].item;
			
			return null;
			
		}
		
		
		public static function updateItemByIndex( index :int, caption :String = null, separator :Boolean = false, enabled :Boolean = true, visible :Boolean = true, call :Function = null ) :Boolean
		{
			
			if ( itens[index] ) {
				
				var item :ContextMenuItem = itens[index].item;
				
				if (caption) item.caption = caption;
				
				if ( arguments[2] ) item.separatorBefore = separator;
				if ( arguments[3] ) item.enabled = enabled;
				if ( arguments[4] ) item.visible = visible;
				if ( arguments[5] ) {
					
					try {
						item.removeEventListener( ContextMenuEvent.MENU_ITEM_SELECT, itens[index].call );
					}catch (e :Error) {
						
					}
					item.addEventListener( ContextMenuEvent.MENU_ITEM_SELECT, call );
				}
				
			}
			
			return false;
			
		}
		
		
	}
	
}
