/*
	PullDownMenu class
*/

var SubMenuLayer = function(owner)
{
	this.owner = owner;
	this.layer;
	this.basePane = document.getElementById(owner.strParentPaneID);
}

SubMenuLayer.prototype.show = function(id)
{
	var menu = this.owner.objMenuStructure[id];
	
	if (!menu) {
		return;
	}
	
	this.layer = document.createElement('div');
	this.layer.style.backgroundColor = '#ffffff';
	this.layer.style.border     = '#b6565c 3px double';
	this.layer.style.padding    = '5px 10px';
	this.layer.style.marginLeft = this.owner.intPosX + 'px';
	this.layer.style.marginTop  = this.owner.intPosY + 'px';
	
	var i;
	for (i=0; i<menu.length; i++) {
		var name = menu[i][0];
		var url  = menu[i][1];
		var out  = menu[i][2];
		
		var cont = document.createElement('div');
		cont.style.fontFamily = 'San-Serif';
		if (i > 0) {
			cont.style.marginTop = '0.3em';
		}
		
		var anc  = document.createElement('a');
		anc.href = url;
		
		if (out) {
			anc.target = '_blank';
		}
		
		anc.appendChild(document.createTextNode(name));
		cont.appendChild(anc);
		this.layer.appendChild(cont);
	}
	
	this.basePane.appendChild(this.layer);
}

SubMenuLayer.prototype.update = function(id)
{
	this.hide();
	this.show(id);
}

SubMenuLayer.prototype.hide = function()
{
	try {
		this.basePane.removeChild(this.basePane.lastChild);
	}
	catch(e) {}
}


//==============================================================================

var PullDownMenu = function()
{
	this.strParentPaneID = 'pulldownSubMenu';
	
	this.objMenuStructure = new Array;
	this.objMenuLayer;
	
	this.intPosX = 0;
	this.intPosY = 0;
	
	this.isShowing = false;
}

PullDownMenu.prototype.showMenu = function(id, x, y)
{
	this.intPosX = x;
	this.intPosY = y;
	
	if (this.isShowing) {
		this.objMenuLayer.update(id);
	}
	else {
		if (this.objMenuLayer === void(0)) {
			this.objMenuLayer = new SubMenuLayer(this);
		}
		
		this.objMenuLayer.show(id);
		this.isShowing = true;
		currentobj     = this;
	}
}

PullDownMenu.prototype.hideMenu = function()
{
	if (this.isShowing) {
		this.objMenuLayer.hide();
		this.isShowing    = false;
	}
}

//==============================================================================

var currentobj;

window.onload = function()
{
	document.body.onclick = hide_pulldown;
}

function hide_pulldown()
{
	if (currentobj) {
		currentobj.hideMenu();
	}
}
