function ContextMenu ( myId ) {
	this.id = myId;
	this.oContainer = null;
	this.oTable = null;
	this.oTableBody = null;
	this.posx = 0;
	this.posy = 0;
	this.items = Array();
	
	if ( document.getElementById( this.id ) ) {
    this.oContainer = document.getElementById( this.id );
  } else {
  	this.oContainer = document.body.appendChild( document.createElement('div') );
  }
  
	with ( this.oContainer ) {
		id = this.id;
		className = 'context_container';
		style.display = 'none';
		onclick = function () { document.getElementById( id ).style.display = 'none'; };
	}
	this.oTable = this.oContainer.appendChild( document.createElement('table') );
  this.oTable.cellSpacing = "0";
  this.oTableBody = this.oTable.appendChild( document.createElement('tbody') );







	return true;
};

ContextMenu.type = 'ContextMenu';

ContextMenu.prototype.showHide = function() {
	if ( ( 'none' == this.oContainer.style.display ) || ( this.oContainer.style.top != this.posy + 'px' ) || ( this.oContainer.style.left != this.posx + 'px' ) ) {


		this.populateMenu();
		this.oContainer.style.top = this.posy + 'px';
		this.oContainer.style.left = this.posx + 'px';
		this.oContainer.style.display = 'block';
	} else {
		this.oContainer.style.display = 'none';
	}
	return true;
};

ContextMenu.prototype.hide = function( id, e ) {
  if ( !e )
    e = window.event;
  if ( e.srcElement.id != id )
    this.oContainer.style.display = 'none';
	return true;
};

ContextMenu.prototype.getMouseXY = function() {
	if ( this.e.pageX || this.e.pageY )	{
		this.posx = this.e.pageX;
		this.posy = this.e.pageY;
	} else if ( this.e.clientX || this.e.clientY ) {
		this.posx = this.e.clientX + document.documentElement.scrollLeft;
		this.posy = this.e.clientY + document.documentElement.scrollTop;


  }
	return true;
};

ContextMenu.prototype.onContextMenu = function( e ){
	this.e = e;
	this.getMouseXY();
	this.showHide();
	return false;
};
  
ContextMenu.prototype.addItem = function( label, url, target, js, icon, tooltip, status, items ) {

	
	var item = new ContextMenuItem();
	item.addLabel( label );
	item.addUrl( url );
	item.addTarget( target );
	item.addIcon( icon );
	item.addJs( js );
	this.items.push( item );
};
  
ContextMenu.prototype.populateMenu = function() {
	this.emptyMenu();
	for( i in this.items ) {
    row = this.oTableBody.appendChild( document.createElement('tr') );

		row.id = 'contextmenu'+i;
    row.onmouseover = function () { this.style.backgroundColor = '#f0f0f0'; };
    row.onmouseout = function () { this.style.backgroundColor = ''; };
		icon = row.appendChild( document.createElement('td') );
		icon.style.width = '24px';
		data = row.appendChild( document.createElement('td') );
		span = data.appendChild( document.createElement('span') );


		if( this.items[ i ].url && !this.items[ i ].js ) {
			eval( "row.onclick = function() { document.location = '" + this.items[ i ].url + "' }");

		} else {
			eval( "row.onclick = function() { " + this.items[ i ].js + " }");


		}
		span.innerHTML = this.items[ i ].label;
		
		if ( this.items[ i ].icon ) {
			img = icon.appendChild( document.createElement( 'img' ) );

			img.src = this.items[ i ].icon;
		}
	}
};
  
ContextMenu.prototype.emptyMenu = function() {
	while( this.oTableBody.firstChild )
		this.oTableBody.removeChild( this.oTableBody.lastChild );

};

function ContextMenuItem() {
	this.label = null;
	this.url = null;
	this.target = null;
	this.js = null;
	this.icon = null;
	this.tooltip = null;
	this.status = null;
	this.items = null;
};

ContextMenuItem.prototype.addLabel = function( data ) {
	this.label = data;
};
  
ContextMenuItem.prototype.addUrl = function( data ) {
	this.url = data;
};
  
ContextMenuItem.prototype.addTarget = function( data ) {
	this.target = data;
};
  
ContextMenuItem.prototype.addJs = function( data ) {
	this.js = data;
};
  
ContextMenuItem.prototype.addIcon = function( data ) {
	this.icon = data;
};
  
ContextMenuItem.prototype.addTooltip = function( data ) {
	this.label = data;
};
  
ContextMenuItem.prototype.addStatus = function( data ) {
	this.status = data;
};
  
ContextMenuItem.prototype.addItem = function( data ) {
	this.items.push( data );
};



