﻿GNgui.GNContextMenu = Class.create( );
GNgui.GNContextMenu.prototype = 
{
  initialize : function( divID, parentElement, offsetElement )
  {
    if ( offsetElement != null )
    {
      this.offsetElement = offsetElement.obj;
    }
    this.menuCount = 0;
    this.obj = $ce( "div" );
    this.obj.style.zIndex = 25;
    this.obj = $e( this.obj );
    this.obj.id = divID;
		this.obj.className = "GNContextMenu";
		this.obj.GNContextMenu = this;
    this.obj.innerHTML = '<table class="ContextMenuTable" cellspacing="0" cellpadding="0" id="table' + divID + '"></table>';
    $hide(this.obj);
    this.obj.Persistent = true;
    $ac( $e( parentElement ), this.obj );
    this.type ='';
  },

  Show : function( MenuLeft, MenuTop )
  {
	for (i in RightMenu.Tabs)
	{
		if (RightMenu.Tabs[i].Close != null)
		{
			RightMenu.Tabs[i].Close();
		}
	}
	
    var ParentWidth = $ew( this.obj.parentElement );
    var ParentHeight = $eh( this.obj.parentElement );
    
    var MenuWidth = $ew( this.obj );
    var MenuHeight = $eh( this.obj );
    
    if ( ( MenuLeft + MenuWidth ) > ParentWidth )
    {
	    $el( this.obj, MenuLeft - MenuWidth );
    }
    else
    {
	    $el( this.obj, MenuLeft );
    }
    
    if ( ( MenuTop + MenuHeight ) > ParentHeight )
    {
		  $et( this.obj, MenuTop - MenuHeight );
    }
    else
    {
	    $et( this.obj, MenuTop );
    }
    
    $show(this.obj);
  },
  
  ShowSubMenu : function( MenuObj )
  {
    var ParentWidth = $ew( MenuObj.obj.parentElement );
    var ParentHeight = $eh( MenuObj.obj.parentElement );

    var ParentMenuLeft = $el( MenuObj.offsetElement );
    var ParentMenuWidth = $ew( MenuObj.offsetElement );
    var ParentMenuTop = $et( MenuObj.offsetElement );
    var ParentMenuRight = ParentMenuLeft + ParentMenuWidth;

    var DataTable = $e( "table" + MenuObj.offsetElement.id );
    var MenuRow = DataTable.rows[0];
    var RowHeight = MenuRow.clientHeight;

    var MenuWidth = $ew( MenuObj.obj );
    var MenuHeight = $eh( MenuObj.obj );
    
    if ( ( ParentMenuRight + MenuWidth ) > ParentWidth )
    {
	    $el( MenuObj.obj, ParentMenuLeft - MenuWidth );
    }
    else
    {
	    $el( MenuObj.obj, ParentMenuRight );
    }
    
    if ( ( ParentMenuTop + ( MenuObj.MenuRow * RowHeight ) + MenuHeight ) > ParentHeight )
    {
		  $et( MenuObj.obj, ParentMenuTop + ( ( MenuObj.MenuRow ) * RowHeight ) - MenuHeight );
    }
    else
    {
	    $et( MenuObj.obj, ParentMenuTop + ( ( MenuObj.MenuRow - 1 ) * RowHeight ) );
    }

    $show(MenuObj.obj);
  },
  
  ShowWindow : function( WindowObj )
  {
    var x = $el( WindowObj.offsetElement );
    var w = $ew( WindowObj.offsetElement );
    var y = $et( WindowObj.offsetElement );
    
    WindowObj.Show( x + w , y + 12 );
  },
  
  AddSubMenu : function( divID, caption, MenuObj )
  {
    MenuObj.MenuRow = this.AddItem( divID, caption, true, false, function( ){GNgui.GNContextMenu.prototype.ShowSubMenu( MenuObj );} );
  },
  
  AddCommand : function( divID, caption, onClickFunction )
  {
    this.AddItem( divID, caption, false, true, onClickFunction );
  },

  AddWindow : function( divID, caption,  WindowObj )
  {
    WindowObj.Hide( );
    this.AddItem( divID, caption, true,true, function( ){GNgui.GNContextMenu.prototype.ShowWindow( WindowObj );} );
    WindowObj.offsetElement = this.obj;
  },
  
  AddBreak : function ( )
  {
    var DataTable = $e( "table" + this.obj.id );
  	var row = DataTable.insertRow( DataTable.rows.length );
  	
    row.style.height="3px";
    row.style.backgroundColor = "#000000";
    
	  var Cell = row.insertCell( 0 );

	  Cell = row.insertCell( 1 );
	  Cell = row.insertCell( 2 );
  },
  
  AddItem : function( divID, caption, showArrow, ClosePopup, onClickFunction )
  {
    var DataTable = $e( "table" + this.obj.id );
  	
  	var row = DataTable.insertRow( DataTable.rows.length );
    row.style.backgroundColor = "#000000";
    
	  var Cell = row.insertCell( 0 );
	  
	  Cell.className = 'GNContextMenuTableLeft';

	  Cell = row.insertCell( 1 );
	  Cell.className = 'GNContextMenuTableCenter';
	  Cell.innerHTML = '<span class="dropdownMenuText" id="SpanCell' + divID + '">' + caption + '</span>';
	  Cell.onclick = onClickFunction;

	  Cell = row.insertCell( 2 );
	  
    if ( showArrow )
  	{
  	  Cell.className = 'GNContextMenuTableRight';
    }
    else
    {
  	  Cell.className = 'GNContextMenuTableRightNoArrow';
    }
    
	  var element = $e( "SpanCell" + divID );
    Event.observe( row, 'mouseover',GNgui.GNContextMenu.prototype.MouseOver.bindAsEventListener( element ) );
    Event.observe( row, 'mouseout', GNgui.GNContextMenu.prototype.MouseOut.bindAsEventListener( element ) );
    Event.observe( row, 'mousedown', GNgui.GNContextMenu.prototype.MouseDown.bindAsEventListener( this ) );
    Event.observe( row, 'mouseup', GNgui.GNContextMenu.prototype.MouseUp.bindAsEventListener( onClickFunction, ClosePopup, this ) );

    return DataTable.rows.length;
  },
  
  MouseOver : function( event )
	{
	  this.className = 'dropdownMenuTextHover';
	  Event.stop( event );
	  return false;
	},
	
	MouseOut : function( event )
	{
	  this.className = 'dropdownMenuText';
	  Event.stop( event );
	  return false;
	},
	
	MouseUp : function( event, Close )
	{
		if( Close )
		{
			CancelContextMenu( );
		}
	  Event.stop( event );
	  return false;
	},
	
	MouseDown : function( event )
	{
	  Event.stop( event );
	  return false;
	}
}

function CancelContextMenu( )
{
	var MenuArray = document.getElementsByClassName( "GNContextMenu", $e( "MappingScreenContentDiv" ) );
	l=MenuArray.length;
  for ( var index = 0; index < l; ++index ) 
  {  
    $hide(MenuArray[index]);
  }
	return false;
}

function Catch( event )
{
	Event.stop( event );
	return false;
}
