// EDITABLE
// path to images
var imagePath = '/html/images/';

//activation button - shift
var keyCode   = 16; 

// EDITABLE

var mouseX;
var mouseY;
var popupMenuObjs = [];
var keyPressed;
var zIndex = 10;

// assign functions to handle events
document.onmousemove  = getMouseXY;
document.onmouseup    = mouseUp;
//document.onkeydown    = keyDown;
//document.onkeyup      = keyUp;
document.onmouseover  = mouseOver;
//document.oncontextmenu=new Function("return false");

function keyDown(e)
{
	e = e || window.event; 
	keyPressed = e.keyCode == keyCode ? true : false;
}


function keyUp()
{
	keyPressed = false;
}


function mouseUp(e)
{	
	e = e || window.event; 
	if(e.which == 1 || (!e.which && e.button == 1))
	{ 
		//document.body.focus();
		for(var i = 0; i < popupMenuObjs.length; i++)
		{
			popupMenuObjs[i].show();
		}
		
	}
	else if(e.which == 2 || (!e.which && e.button == 4))
	{
		hideAll();
	}
	// hide popupmenus
  	else
  	{
  		//keyPressed = false;
  		for(var i = 0; i < popupMenuObjs.length; i++)
  		{
  			popupMenuObjs[i].hide();
  		}
  	}
}


function mouseOver(e)
{
	e = e ? e : ((window.event) ? window.event : "");
	var elem = (e.target) ? e.target : e.srcElement;
	if(elem.nodeName == 'TD')
		elem = elem.parentNode;
	for(var i = 0; i < popupMenuObjs.length; i++)
	{
		popupMenuObjs[i].over(elem.id);
	}
}


// function to retrieve mouse x-y position
function getMouseXY(e) 
{
	if (document.all) 
	{ // grab the x-y pos if browser is IE
    	mouseX = event.clientX + document.documentElement.scrollLeft;
    	mouseY = event.clientY + document.documentElement.scrollTop;
  	} 
  	else 
  	{  // grab the x-y pos if browser is NS
    	mouseX = e.pageX;
    	mouseY = e.pageY;
  	}  
  	
  	// catch possible negative values in NS4
  	if (mouseX < 0){mouseX = 0}
  	if (mouseY < 0){mouseY = 0}  

  	return true;
}

function hideAll()
{
	for(var i = 0; i < popupMenuObjs.length; i++)
	{
		popupMenuObjs[i].over("");
		popupMenuObjs[i].hide();
	}
}


/* CLASS MenuItem
 *
 *
 */
function MenuItem(id, name, text, type, url, icon, submenu, target) 
{
	this.id   = id;
	this.name = name;
	this.text = text;
	this.type = type;
	this.url  = url;
	this.icon = icon;
	this.target = target ? target : '_self';
	this.submenu = (submenu && submenu.block) ? submenu : null;
	this.obj;
};


MenuItem.prototype.toString = function()
{
	var path = this.icon ? this.icon : imagePath + 'spacer.gif';
	var str = '<a href="#" id="';
	str += this.name + this.id + '" ';
	str += 'target="' + this.target + '" ';
	str += this.submenu ? 'class="itemSubmenu">' : '>';
	str += '<img src="' + path + '" border="0" height="20px" width="20px" align="absmiddle">';
	str += '&nbsp;' + this.text; 
	str += '</a>';
	return str;
};


MenuItem.prototype.createLink = function()
{
	this.obj = new getObj(this.name + this.id);
	if(this.url != null)
	{
		if(this.type == "PHP")
			this.obj.obj.href = eval(this.url);		
		else
			this.obj.obj.onclick = new Function( this.url + ";hideAll();");	
	}
};


MenuItem.prototype.displaySubmenu = function()
{
	if(this.submenu)
	{
		var win  = new winSize();
		var tmpX = this.obj.obj.parentNode.offsetLeft + this.obj.obj.offsetWidth + 5;
		var x = tmpX + this.submenu.block.obj.offsetWidth > win.width ? this.obj.obj.parentNode.offsetLeft - this.submenu.block.obj.offsetWidth : tmpX ;
		var y = this.obj.obj.parentNode.offsetTop + this.obj.obj.offsetTop;
		this.submenu.displayAt(x, y);
	}
};


MenuItem.prototype.hideSubmenu = function()
{
	if(this.submenu)
	{
		this.submenu.hide();
	}
};


MenuItem.prototype.over = function(id) 
{
	if(id == (this.name + this.id))
	{
		this.displaySubmenu();
	}
	else
	{
		this.hideSubmenu();
	}
};


/* CLASS Separator
 *
 *
 */
function Separator()
{}


Separator.prototype.toString = function()
{
	var str = '<hr>';
	return str;	
};


Separator.prototype.createLink = function()
{};


Separator.prototype.hideSubmenu = function()
{};


Separator.prototype.over = function()
{};


/* CLASS HTMLBlock
 *
 *
 */
function HTMLBlock(id, action)
{
	this.id  = id;
	this.action = action; 
}



/* CLASS PopupMenu
 *
 *
 */
function PopupMenu(name) 
{
	this.name  = name;
	this.items = [];
	this.enabledBlocks = [];
	this.visible = true;
	this.block   = '';
	this.overID  = '';
	this.titled  = false;
};


PopupMenu.prototype.add = function(name, type, url, icon, submenu, target) 
{
	this.items[this.items.length] = new MenuItem(this.items.length, this.name, name, type, url, icon, submenu, target);
};


PopupMenu.prototype.addSeparator = function() 
{
	this.items[this.items.length] = new Separator();
};


PopupMenu.prototype.enable = function(id, action) 
{
	this.enabledBlocks[this.enabledBlocks.length] = new HTMLBlock(id, action);
};

PopupMenu.prototype.setTitle = function(title) 
{
	this.obj = new getObj('title' + this.name);
	if(this.obj.obj.innerText)
		this.obj.obj.innerText = title;
	else
		this.obj.obj.textContent = title;	
};


PopupMenu.prototype.over = function(id) 
{
	this.overID = id;
	if(this.visible)
	{
		if(this.isMenuItemID(id))
		{
			for(var i = 0; i < this.items.length; i++)
			{
				this.items[i].over(this.overID);
			}
		}
	}
}


PopupMenu.prototype.toString = function() 
{
	var str = '<div id="';
	str += this.name + '" class="popupmenu">';
	if(this.titled)
		str += '<div class="menuTitle" id="title' + this.name + '">untitled</div>';
	for(var i = 0; i < this.items.length; i++) 
	{
		str += this.items[i];	
	}	
	str += '</div>';
	return str;
};


PopupMenu.prototype.createLinks = function() 
{
	for(var i = 0; i < this.items.length; i++) 
	{
		this.items[i].createLink();	
	}	
};


PopupMenu.prototype.activate = function(titled)
{
	this.titled = titled;
	document.write(this);
	this.block = new getObj(this.name);
	this.hide();
	popupMenuObjs[popupMenuObjs.length] = this;
};


PopupMenu.prototype.show = function()
{	
	var block = this.getEnabledBlock(this.overID);
	if(block)
	{
		eval(block.action);
		this.displayAt(mouseX - this.block.obj.offsetWidth - 10, mouseY);
	}
	else
	{
		this.hide();
	}
};


PopupMenu.prototype.displayAt = function(x, y)
{
	var win    = new winSize();
	var scroll = new winScroll();
	this.createLinks();
	this.block.style.left = Math.min(x, win.width  + scroll.x - this.block.obj.offsetWidth)  +  "px";
	this.block.style.top  = Math.min(y, win.height + scroll.y - this.block.obj.offsetHeight) + "px";
	this.block.style.visibility = 'visible';
	this.visible = true;
	this.block.style.zIndex = zIndex++;
};


PopupMenu.prototype.hide = function()
{
	if(this.visible && !this.isMenuItemID(this.overID))
	{
		for(var i = 0; i < this.items.length; i++)
		{
			this.items[i].hideSubmenu();
		}
		this.block.style.visibility = 'hidden';
		this.visible = false;
		this.block.style.zIndex = 0;
		zIndex--;
	}
};


PopupMenu.prototype.getEnabledBlock = function(id)
{
	for(var i = 0; i < this.enabledBlocks.length; i++) 
	{
		if(this.enabledBlocks[i].id == id)
			return this.enabledBlocks[i];
	}
	return false;
};


PopupMenu.prototype.isMenuItemID = function(id) 
{
	return (id && id.length > this.name.length && id.substring(0, this.name.length) == this.name);
};



function winSize()
{
	this.width  = 0;
	this.height = 0;
  	
	if( typeof( window.innerWidth ) == 'number' ) 
	{
    	//Non-IE
    	this.width  = window.innerWidth;
    	this.height = window.innerHeight;
  	} 
  	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
  	{
    	//IE 6+ in 'standards compliant mode'
    	this.width  = document.documentElement.clientWidth;
    	this.height = document.documentElement.clientHeight;
  	} 
  	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
  	{
	    //IE 4 compatible
	    this.width  = document.body.clientWidth;
	    this.height = document.body.clientHeight;
  	}
}

function winScroll() {
  var x = 0; 
  var y = 0;
  if( typeof( window.pageYOffset ) == 'number' ) 
  {
    //Netscape compliant
    this.x = window.pageXOffset;
    this.y = window.pageYOffset;
  } 
  else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) 
  {
    //DOM compliant
    this.x = document.body.scrollLeft;
    this.y = document.body.scrollTop;
  } 
  else if( document.documentElement) 
  {
    //IE6 standards compliant mode
    this.x = document.documentElement.scrollLeft;
    this.y = document.documentElement.scrollTop;
  }
  
}

