// JavaScript Document

var myMenu_Switch       = 1;
var myMenu_Down         = 2;
var myMenu_KeyDown      = 4;
var myMenu_Disabled     = 8;
var myMenu_Group        = 16;
var myMenu_AllAllowedUp = 32;

function JCMenu ( MenuLayerName, ImageDir, Background, Xp, Yp, ZOrder )
{
  this . Background      = ImageDir + Background;	
	this . ImageDir        = ImageDir;
	this . MenuLayerName   = MenuLayerName;
	this . ZOrder          = ZOrder;
	
	
	this . Layers          = new Array ();
	this . NI              = new Array ();
	this . HI              = new Array ();
	this . DI              = new Array ();
	this . DH              = new Array ();
	this . Status          = new Array ();
	this . ImObj           = new Array (); 
	this . ClickEvents     = new Array ();
	
	this . BNumber  = 0;
	this . Obj      = null;
	this . Xp       = Xp;
	this . Yp       = Yp;
	this . HorV     = true;
	this . PadValue = 0;
	
	this . Generate        = Generate;
	this . LogButton       = LogButton;
	this . SetBuffers      = SetBuffers; 
	
	this . MenuMouseOver   = MenuMouseOver;
	this . MenuMouseOut    = MenuMouseOut;
	this . MenuMouseDown   = MenuMouseDown;
	this . MenuMouseUp     = MenuMouseUp;
	
	this . SetOverButton   = SetOverButton;
	this . SetOutButton    = SetOutButton;
	this . SetDownButton   = SetDownButton;
	this . SetUpButton     = SetUpButton;
	
	this . SetButtonOpacity = SetButtonOpacity;
	
	I       = new Image;
	I . src = this . Background;  // preaload background image
}

function SetButtonOpacity ( Value )
{
	  SetLayerOpacity ( this . Obj, Value + 20 );
		for ( var x = 0; x < this . Layers . length; x ++ )
		{
			SetLayerOpacity ( this . Layers [ x ], Value - 20 );
		}
}
	

function SetBuffers ( PadValue )
{
	this . PadValue = PadValue;
}



function Generate ( )
{
	this . Obj = GenerateDIV ( this . MenuLayerName );
 
	SetLayerPositionType    ( this . Obj );
	SetLayerPosition        ( this . Obj, this . Xp, this . Yp );
	SetLayerWidthHeight     ( this. Obj, 100, 100 );
	
	//SetLayerBackgroundColor ( this . Obj, "red" );
  SetLayerZIndex          ( this . Obj, "" + this . ZOrder );
	var I = BuildImageLayer         ( this . Obj );	
	
	var NewImage = new Image ();
	
	NewImage . src = this . Background;
	I . style . width  = String ( NewImage . width  ) + 'px'; // this is for IE
	I . style . height = String ( NewImage . height ) + 'px';
	
	SetLayerToImageDims     ( this . Obj, I );
	I . src = this . Background;
}

function LogButton ( X, Y, N, H, D, DH, Wid, Hei, OnClickEvent, Status )
{
	var I = new Image ( Wid, Hei );
	I . src = this . ImageDir + N;
	this . NI . push ( I );
	var I = new Image ( Wid, Hei );
	I . src = this . ImageDir + H;
	this . HI . push ( I );
	var I = new Image ( Wid, Hei );
	I . src = this . ImageDir + D;
	this . DI . push ( I );
	var I = new Image ( Wid, Hei );
	I . src = this . ImageDir + DH;
	this . DH . push ( I );
	
	this . ClickEvents . push ( OnClickEvent );
	
	this . Status . push ( Status );
	
	var Obj = GenerateDIV    ( this . MenuLayerName + '_' + this . BNumber );
	
	
		
	this . Layers . push ( Obj );
	
	var Im = BuildImageLayer ( Obj );
	
	Im . width = Wid;
	Im . height = Hei;
	
	this . ImObj . push ( Im );
	
	
	
	if ( ( Status & myMenu_Down ) > 0 ) Im . src = this . DI [ this . BNumber ] . src;
	else                                Im . src = this . NI [ this . BNumber ] . src;
	
	Obj . ButtonNumber = this . BNumber;
	Obj . onmouseover = this . MenuMouseOver;
	Obj . onmouseout  = this . MenuMouseOut;
	Obj . onmousedown = this . MenuMouseDown;
	Obj  .onmouseup   = this . MenuMouseUp;
	
	Obj . MenuParent  = this;
	
	SetLayerPositionType     ( Obj );
	
	if ( X == null || Y == null ) 
	{
		var Val = 0;
		
		if ( this . BNumber > 0 )
		{
			if ( X == null ) 
			{ 
			  Val = this . Layers [ this . BNumber - 1  ]   . offsetLeft  + I  . width  + this . PadValue + 1;
				SetLayerPosition         ( Obj, Val, this . Yp + Y );
			}
			else
			{
				Val = ( this . Layers [ x ] ) . height;
				Val += this . PadValue;
			}
		}
		else SetLayerPosition         ( Obj, this . Xp, this . Yp );
	}	
	else SetLayerPosition         ( Obj, this . Xp + X, this . Yp + Y );
	
	
	//SetLayerToImageDims     ( Obj, Im );
	SetLayerWidthHeight ( Obj, Wid, Hei );
	SetLayerZIndex          ( Obj, "" + ( this . ZOrder + 1 ) );
	this . BNumber ++;
}

function SetOverButton ( BN )
{	
	if ( ( this . Status [ BN ] & myMenu_Disabled ) > 0 ) return;
	
  if ( ( this . Status [ BN ] & myMenu_Down ) > 0 ) this . ImObj [ BN ] . src = this . DH [ BN ] . src;
	else                                              this . ImObj [ BN ] . src = this . HI [ BN ] . src;
}

function SetOutButton ( BN )
{	
	if ( ( this . Status [ BN ] & myMenu_Disabled ) > 0 ) return;
	this . Status [ BN ] = this . Status [ BN ] & ( ~ myMenu_KeyDown );
	
  if ( ( this . Status [ BN ] & myMenu_Down ) > 0 ) this . ImObj [ BN ] . src = this . DI [ BN ] . src;
	else                                              this . ImObj [ BN ] . src = this . NI [ BN ] . src;
}

function SetDownButton ( BN )
{
	if ( ( this . Status [ BN ] & myMenu_Disabled ) > 0 ) return;
	
	
	this . Status [ BN ] = this . Status [ BN ] | myMenu_KeyDown;
	this . ImObj [ BN ] . src = this . DH [ BN ] . src;  
}

function SetUpButton ( BN )
{
	var Flag = this . Status [ BN ];
	if ( ( Flag & myMenu_Disabled ) > 0 ) return;
		
	
	Flag = Flag & ( ~ myMenu_KeyDown );
	
	if  ( ( Flag & myMenu_Down ) > 0 )
	{
	  if ( ( Flag & myMenu_Switch ) > 0 )
		{
			if ( ( Flag & myMenu_Group ) > 0 && ( Flag & myMenu_AllAllowedUp ) == 0 ) return;
			
			Flag = Flag & ( ~ myMenu_Down );
			
			// trugger button event Here
			
			this . Status [ BN ] = Flag;
			this . ImObj [ BN ] . src = this . HI [ BN ] . src;
			if ( this . ClickEvents [ BN ] != null ) ( this . ClickEvents [ BN ] ) ( BN, false );
		}
	}
	else
	{
	  if ( ( Flag & myMenu_Switch ) > 0 )
		{
			
			this . ImObj [ BN ] . src = this . DH [ BN ] . src;
			if ( ( Flag & myMenu_Group ) > 0 ) 
			{
				for ( var x = 0; x < this . Status . length; x++ )
				{
					if ( x != BN )
					{
						if ( ( this . Status [ x ] & myMenu_Group ) > 0 )
						{
						  this . Status [ x ] = 	this . Status [ x ] & ( ~myMenu_Down );
							this . ImObj  [ x ] . src = this . NI [ x ] . src;
						}
					}
				}
			}
			Flag = Flag | myMenu_Down;
			this . Status [ BN ] = Flag;
			
		}
		else
		{
			this . ImObj [ BN ] . src = this . HI [ BN ] . src;
		}
		if ( this . ClickEvents [ BN ] != null ) ( this . ClickEvents [ BN ] ) ( BN, ( Flag & myMenu_Down ) > 0  );
		//trigger button event here 
		
	}
	
		
	
	
}

function MGetFullObject ( name )
{
  if ( document . getElementById )
  {
  	this . Obj   = document . getElementById ( name );	  
  }
  else if (document.all)
  {
	  this . Obj   = document . all [ name ];	  
  }
  else if (document.layers)
  {
   	this . Obj   = document . layers [ name ];   	
  }
}

function MenuMouseOver ( e )
{
  var targ;
    if (!e) var e = window.event
	// handle event
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();  // stops multiple event fireing

	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) targ = targ.parentNode; // defeat Safari bug
		
	var Mnu = this . MenuParent;
	Mnu . SetOverButton ( this . ButtonNumber );
	
}

function MenuMouseOut ( e )
{
  var targ;
    if (!e) var e = window.event
	
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();  // stops multiple event fireing

	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) targ = targ.parentNode; // defeat Safari bug
		
	var Mnu = this . MenuParent;
	Mnu . SetOutButton ( this . ButtonNumber );
	
}

function MenuMouseDown ( e )
{
  var targ;
    if (!e) var e = window.event
	
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();  // stops multiple event fireing

	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) targ = targ.parentNode; // defeat Safari bug
		
	var Mnu = this . MenuParent;
	Mnu . SetDownButton ( this . ButtonNumber );
	
}

function MenuMouseUp ( e )
{
  var targ;
    if (!e) var e = window.event
	
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();  // stops multiple event fireing

	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) targ = targ.parentNode; // defeat Safari bug
		
	var Mnu = this . MenuParent;
	Mnu . SetUpButton ( this . ButtonNumber );
	
}






