//- Popup window object for the context menu. -//
var cmPopup = window.createPopup();
cmPopup.document.createStyleSheet( '/administration/controls/contextmenu/contextmenu.css' );

//- Initialize the popup by displaying it once. There are issues with the popup until it's shown for the first time. -//
window.attachEvent( 'onload', CMInitialize );

function CMInitialize()
{
	//- Remove the event handler. -//
	window.detachEvent( 'onload', CMInitialize );
	
	cmPopup.document.body.onload = cmPopup.hide;
	cmPopup.show( -100, -100, 1, 1, window.document.body );
	setTimeout( cmPopup.hide, 1 );
}

//- Constructor function for creating a context menu object. -//
function ContextMenuObject()
{
	this.items = new Array();
	this.AddItem = CMAddItem;
	this.Show = CMShow;
}

//- Add menu elements. -//
function CMAddItem( itemText, itemAction, itemShortcut, itemImg, isEnabled )
{
	//- The entire HTML for the item. -//
	var rowHTML = '';
	
	//- A separator item. -//
	if ( arguments.length == 1 && itemText.toLowerCase() == 'separator' )
	{
		rowHTML = '<TR CLASS="cmSeparatorTR"><TD CLASS="cmIcon"><IMG CLASS="cmImage" SRC="/administration/images/shim.gif" WIDTH=16 HEIGHT=16></TD><TD NOWRAP COLSPAN="2"><DIV CLASS="cmSeparatorDIV"></DIV></TD></TR>';
	}
	//- A regular item. -//
	else
	{
		var start = '<TR CLASS="cmTRNormal" itemAction="' + itemAction + '">';
		//- Check to see if the item should be disabled. -//
		if ( arguments.length > 4 && !isEnabled )
			start = '<TR CLASS="cmTRDisabled" itemAction="' + itemAction + '" DISABLED="true">';
		var icon = '<TD CLASS="cmIcon"></TD>';
		//- Check to see if an image was specified. -//
		if ( arguments.length > 3 && itemImg.length > 0 )
		{
			icon = '<TD CLASS="cmIcon"><IMG CLASS="cmImage" SRC="'
				+ itemImg + '" WIDTH=16 HEIGHT=16></TD>';
		}
		var description = '<TD CLASS="cmText" NOWRAP>' + itemText + '</TD>';
		var shortcut = '<TD CLASS="cmShortcut"></TD>';
		if ( arguments.length > 2 && itemShortcut.length > 0 )
			shortcut = '<TD CLASS="cmShortcut">' + itemShortcut + '</TD>';
		var end = '</TR>';
		rowHTML = start + icon + description + shortcut + end;
	}
	
	//- Add the new HTML to the items array. -//
	this.items.push( rowHTML );
}

//- MouseClick functionality for the popup window. -//
function CMClick()
{
	//- Clear the variables on the document. -//
	this.document.parentWindow.highlightedRow = null;
	
	//- Get the action to be performed. -//
	var itemAction = this.getAttribute( 'itemAction' );
	
	//- Hide the context menu. -//
	cmPopup.hide();
	
	//- Don't continue if there's nothing to do. -//
	if ( itemAction == null )
		return;
	
	//- Perform the specified action. Give the popup time to hide. -//
	setTimeout( itemAction, 0 );
}

//- MouseOut functionality for the popup window. -//
function CMKeyDown()
{
	this.document.parentWindow.event.returnValue = false;
	
	//- Get the pressed key. -//
	var KEY_ENTER = 13;
	var KEY_ESCAPE = 27;
	var KEY_DOWN = 40;
	var KEY_UP = 38;
	var key = this.document.parentWindow.event.keyCode;
	if ( !( key == KEY_ENTER || key == KEY_ESCAPE || key == KEY_DOWN || key == KEY_UP ) )
		return;
	
	
	//- Set local variables. -//
	var rows = this.document.all.popupTable.rows;
	var rowsLen = rows.length;
	
	//- Grab the row that is currently highlighted. -//
	var currentRow = this.document.parentWindow.highlightedRow;
	
	
	//- Determine what to do based on the key pressed. -//
	if ( key == KEY_ENTER )
	{
		if ( currentRow != null )
			currentRow.click();
		else
		{
			cmPopup.hide();
			return;
		}
	}
	else if ( key == KEY_ESCAPE )
	{
		cmPopup.hide();
		return;
	}
	else if ( key == KEY_DOWN )
	{
		//- Unhighlight the current row and select the next one. -//
		if ( currentRow != null )
			CMNormal( currentRow );
		if ( currentRow == null || currentRow == rows[rows.length - 1] )
			currentRow = rows[0];
		else
			currentRow = rows[currentRow.rowIndex + 1];
		
		//- Make sure the row isn't disabled or it's a separator. -//
		var originalRow = currentRow;
		while ( currentRow.disabled || currentRow.className == 'cmSeparatorTR' )
		{
			if ( currentRow == rows[rows.length - 1] )
				currentRow = rows[0];
			else
				currentRow = rows[currentRow.rowIndex + 1];
			if ( currentRow == originalRow )
				break;
		}
	}
	else if ( key == KEY_UP )
	{
		//- Unhighlight the current row and select the next one. -//
		if ( currentRow != null )
			CMNormal( currentRow );
		if ( currentRow == null || currentRow == rows[0] )
			currentRow = rows[rows.length - 1];
		else
			currentRow = rows[currentRow.rowIndex - 1];
		
		//- Make sure the row isn't disabled or it's a separator. -//
		var originalRow = currentRow;
		while ( currentRow.disabled || currentRow.className == 'cmSeparatorTR' )
		{
			if ( currentRow == rows[0] )
				currentRow = rows[rows.length - 1];
			else
				currentRow = rows[currentRow.rowIndex - 1];
			if ( currentRow == originalRow )
				break;
		}
	}
	
	//- Highlight the new currentRow. -//
	CMHighlight( currentRow );
	
	//- Store the current row on the popup body. -//
	//currentRow.document.parentWindow.highlightedRow = currentRow;
}

//- MouseOver functionality for the popup window. -//
function CMMouseMove()
{
	if ( this.className == 'cmSeparatorTR' )
		return;
	
	if ( this != this.document.parentWindow.highlightedRow )
	{
		//- Unhighlight the previous row. -//
		CMNormal( this.document.parentWindow.highlightedRow );
		
		//- Highlight all the row. -//
		CMHighlight( this );
	}
}

//- MouseOver functionality for the popup window. -//
function CMMouseOver()
{
	if ( this.className == 'cmSeparatorTR' )
		return;
	
	//CMNormal( this.document.parentWindow.highlightedRow );
	
	//- Highlight all the row. -//
	CMHighlight( this );
}

//- MouseOut functionality for the popup window. -//
function CMMouseOut()
{
	if ( this.className == 'cmSeparatorTR' )
		return;
	
	//- Make the row appear normal. -//
	CMNormal( this );
}

//- Show the context menu. -//
function CMShow( srcObj, xOffset, yOffset )
{
	if ( this.items.length == 0 )
		return;
	
	//- Make sure the render area is written out. -//
	if ( !top.document.all.cmRenderArea )
	{
		var renderArea = top.document.createElement( '<DIV ID="cmRenderArea" STYLE="position: absolute; top: -1000; left: -1000;"></DIV>' );
		top.document.body.appendChild( renderArea );
	}
	
	//- Copy the HTML of the menu into the popup document. -//
	var tableStart = '<DIV CLASS="cmBorder" ID="cmContainer"><TABLE ID="popupTable" CLASS="cmTable" CELLSPACING="0" CELLPADDING="0" onselectstart="return false" ondragstart="return false">';
	var tableEnd = '</TABLE></DIV>';
	top.document.all.cmRenderArea.innerHTML = tableStart + this.items.join( '\n' ) + tableEnd;
	cmPopup.document.body.innerHTML = top.document.all.cmRenderArea.innerHTML;
	
	//- Add OnMouse events to each TR element. -//
	for ( var count = 0; count < cmPopup.document.all.popupTable.rows.length; count++ )
	{
		if ( cmPopup.document.all.popupTable.rows[count].className == 'cmSeparatorTR' )
			continue;
		
		with ( cmPopup.document.all.popupTable.rows[count] )
		{
			onmousemove = CMMouseMove
			onmouseover = CMMouseOver
			onmouseout = CMMouseOut
			onclick = CMClick
		}
	}
	
	//- Add key support so the user can press up or down. -//
	cmPopup.document.body.onkeydown = CMKeyDown;
	cmPopup.document.body.oncontextmenu = new Function( 'return false' );
	
	//- Determine the coordinates of where the menu should be displayed. -//
	var xPos = event.clientX;
	var yPos = event.clientY;
	if ( typeof( srcObj ) != 'undefined' )
	{
		if ( typeof( xOffset ) == 'undefined' ) xOffset = 0;
		if ( typeof( yOffset ) == 'undefined' ) yOffset = 0;
		
		xPos = ( srcObj.offsetLeft + xOffset ) - srcObj.document.body.scrollLeft;
		yPos = ( srcObj.offsetTop + yOffset ) - srcObj.document.body.scrollTop;
	}
	
	cmPopup.document.body.onload = new Function( 'this.document.parentWindow.highlightedRow = null;' );
	//cmPopup.document.body.onunload = document.activeElement.focus;
	
	//- Create the popup window. -//
	cmPopup.show( xPos, yPos, top.document.all.cmContainer.offsetWidth, top.document.all.cmContainer.offsetHeight, document.body );
}

//- Highlight a row. -//
function CMHighlight( row )
{
	//- Highlight all the TD's in the row. -//
	for ( var count = 0; count < row.cells.length; count++ )
	{
		if ( count == 0 )
		{
			var child = row.cells[count].firstChild;
			if ( child != null && child.tagName == 'IMG' )
			{
				child.runtimeStyle.border = '1px solid';
				child.runtimeStyle.borderColor = 'threedhighlight threedshadow threedshadow threedhighlight';
			}
			else
				row.cells[count].runtimeStyle.background = '#b6bdd2';
		}
		else
		{
			row.cells[count].runtimeStyle.background = '#b6bdd2';
			row.cells[count].runtimeStyle.color = 'black';
		}
	}
	
	//- Store the current row on the popup body. -//
	row.document.parentWindow.highlightedRow = row;
}

//- Highlight a row. -//
function CMNormal( row )
{
	if ( row == null )
		return;
	
	//- Highlight all the TD's in the row. -//
	for ( var count = 0; count < row.cells.length; count++ )
	{
		if ( count == 0 )
		{
			var child = row.cells[count].firstChild;
			if ( child != null && child.tagName == 'IMG' )
			{
				child.runtimeStyle.border = '';
				child.runtimeStyle.borderColor = '';
			}
			else
				row.cells[count].runtimeStyle.background = '';
		}
		else
		{
			row.cells[count].runtimeStyle.background = '';
			row.cells[count].runtimeStyle.color = '';
		}
	}
	
	//- Store the current row on the popup body. -//
	if ( row == row.document.parentWindow.highlightedRow )
		row.document.parentWindow.highlightedRow = null;
}