//menu functions
//ihoapm@gmail.com
function getObj(id)
{
	return document.getElementById(id);
}

function offsetLeft(o)
{
	var i = 0;
	while(o!=null && o.offsetParent!=null) 
	{
		i += o.offsetLeft;
		o = o.offsetParent;
	}
	return i + (o ? o.offsetLeft : 0);
}

function offsetTop(o)
{
	var i = 0;
	while (o!=null && o.offsetParent!=null) 
	{
		i += o.offsetTop;
		o = o.offsetParent;
	}
	return i + (o ? o.offsetLeft : 0);
}

function setFocus(controlName)
{	
	var ctrl = document.all(controlName);
	if(!ctrl && !(ctrl = document.getElementById(controlName)))
		return;
	if(ctrl.length)
		ctrl = ctrl[0];
	if(ctrl.type=="text")
	{
		ctrl.focus();
		ctrl.value = ctrl.value + "";
		ctrl.select();
	}
	else
	{
		ctrl.focus();
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
var prevId = "";
var nHideTimer = 0;
var oPrevPopup = null;
var oMenuCon = null;
var oCurrentSubItems = null;
var selectedMID = "";
var hideDy = 1;
function hideMenu(id)
{
	var obj = getObj(prevId);
	if(obj)
	{
		obj.style.display = "none";
	}
	if(oPrevPopup)
	{
		oPrevPopup.childNodes[0].style.color = "#FFFFFF";
		if(selectedMID!=oPrevPopup.id)
			oPrevPopup.style.backgroundColor = "#0A8B87";
	}
	prevId = "";
	if(oCurrentSubItems)
		oCurrentSubItems.style.visibility="visible";
}

function startShow(otd, menu_id, flag)
{
	if(!flag)
	{
		nHideTimer = setTimeout("hideMenu()", 200);
		return;
	}
	
	if(nHideTimer)
	{
		clearTimeout(nHideTimer);
		nHideTimer = 0;
	}
	if(prevId != menu_id)
	{
		hideMenu();
		prevId = menu_id;
	}
		
	//window.status = menu_id + ", " + flag;
	var menu = getObj(menu_id);
	if(!menu)
		return;
	if(otd && otd.offsetParent)
	{			
		otd = otd.offsetParent;
		x = offsetLeft(otd);
		y = offsetTop(otd);
		menu.style.display = "block";
		right = offsetLeft(oMenuCon) + oMenuCon.offsetWidth;
		if(right < menu.offsetWidth + x)
			x = offsetLeft(oMenuCon) + oMenuCon.offsetWidth - menu.offsetWidth;
		//else
		//	x = x + otd.offsetWidth/2 - menu.offsetWidth/2;
		menu.style.left = x + "px";
		menu.style.top = (y + otd.offsetHeight - 2) + "px";
		otd.childNodes[0].style.color = "#EEFC06";
		otd.style.backgroundColor = "#0095D9";
		oPrevPopup = otd;
	}
	
	if(oCurrentSubItems)
		oCurrentSubItems.style.visibility="hidden";
}

function setSubItemsPos(osubItems)
{
	oselItem = getObj(selectedMID);
	if(!oselItem) return;	
	x = offsetLeft(oselItem);
	right = offsetLeft(oMenuCon) + oMenuCon.offsetWidth;	
	if(right < x + osubItems.offsetWidth)
		x = right - osubItems.offsetWidth + 3;
	else
		x -= 5;
	osubItems.style.top = "-2px";
	osubItems.style.left = x + "px";
}

function buildMenu(container, type, selectedMenu)
{
	var items = LPMENUS;
	var strMenu = "";
	for(i=0; i<items.length; ++i)
	{
		var module = items[i][1];
		menu_id = "navbar_popup_" + i;
		if(typeof(module)!="string")
			module = "#";
		
		s_id = "menu_" + module;
		selectedMID = "menu_" + selectedMenu;
		
		if(selectedMenu!=module)
			strMenu += "<td height='30' id='" + s_id + "' align='center'><a class='popup' href='?module=" + module + "' onmouseover=\"startShow(this,'" + menu_id + "',true);\" onmouseout=\"startShow(this,'" + menu_id + "',false)\">" + items[i][0] + "</a></td>";
		else
			strMenu += "<td height='30' id='" + s_id + "' align='center' bgcolor='#0095D9'><a class='popup' href='?module=" + module + "'>" + items[i][0] + "</a></td>"; // onmouseover=\"startShow(this,'" + menu_id + "',true);\" onmouseout=\"startShow(this,'" + menu_id + "',false)\"
		
		if(items[i].length > 2)
		{
			var omenu = document.createElement("div");
			omenu.id = menu_id;
			omenu.style.position = "absolute";
			omenu.style.display = "none";
			omenu.style.background = "url(images/silveribg.jpg)";
			omenu.style.paddingTop = "3px";
			omenu.style.paddingBottom = "3px";
			omenu.style.borderBottom = "1px solid #EEEEEE";
			
			stmp = "<table cellspacing='0' cellpadding='0'><tr>";
			for(j=2; j<items[i].length; ++j)
			{
				subItems = items[i][j];
				if(typeof(subItems)=="object")
				{
					itemText = items[i][j][0];
					subModule = items[i][j][1];
					sEvents = " onmouseover=\"startShow(null,'" + menu_id + "',true)\" onmouseout=\"startShow(null,'" + menu_id + "',false)\" ";
					stmp += "<td ><a class='menuItem' href='?module=" + module + (subModule.length?"&sub=" + subModule:"") + "' " + sEvents + ">" + itemText + "</a></td>";
					if(j<items[i].length-1)
						stmp += "<td " + sEvents + " width='1'> | </td>";
				}
			}
			omenu.innerHTML = stmp + "</tr></table>";
			document.body.appendChild(omenu);
		}//if
		
		//menu container
		oMenuCon = getObj(container);		
		if(i<items.length-1)
			strMenu += "<td width='1' style='font-size:1px;border-right:1px solid #FAFAFA;'>&nbsp;</td>";
	}//for
	getObj(container).innerHTML = "<table width='100%' cellpadding='0' cellspacing='0'><tr>" + strMenu + "</tr></table>";
}//end buildMenu


////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
function jsRoller(id, left)
{
	//props
	this.containerId = "js_roll_" + id;
	this.roller = document.getElementById(id);
	this.container = null;
	this.started = false;
	this.rollId = 0;
	this.dy = 1;
	this.delta = 0;
	this.itemsCount = 0;
	
	//self pointer
	jsRoller.prototype._pthis = this;
	
	//public methods
	//add item: image, title, id
	this.addItem = function(image, url, title){
		var oitem = document.createElement("<div>");
		var cx = 200;
		oitem.align="center";				
		oitem.innerHTML = "<a href='" + url + "' title=\"" + title + "\"><img src='" + image + "' style='border:0px;'></a><div style='height:2px;'></div>";
		this.container.appendChild(oitem);
		
		//resize the roller
		if(!this.itemsCount){
			this.roller.style.width = parseInt(this.roller.childNodes[0].offsetWidth) + "px";					
		}
		this.roller.style.height = parseInt(this.roller.childNodes[0].offsetHeight) + "px";
		++this.itemsCount;
	}
	
	this.setTop = function(y){
		this.roller.style.top = y;
	}//setTop
	
	//stop timeout
	this.startRoller = function(){
		if(this.roller.style.display != "block")
			this.roller.style.display = "block";
		if(this.rollId){
			clearTimeout(this.rollId);
			this.rollId = 0;
			this.started = false;
		}		
		this.rollId = setTimeout("jsRoller.prototype._runRolling()", 300);
	}//stop			
				
	//rolling
	jsRoller.prototype._runRolling = function(){
		var ptr = jsRoller.prototype._pthis;
		var roll = ptr.roller;
		var height = parseInt(roll.childNodes[0].offsetHeight);
		var top = parseInt(roll.style.top);
		var scrollTop = document.body.scrollTop;
		var endY =  scrollTop + (document.body.clientHeight)/2 - height/2;
		
		if(!ptr.started)
		{
			if(top < scrollTop - height)
				top = scrollTop - height + 10;
			else if(top > scrollTop + document.body.clientHeight)
				top = scrollTop + document.body.clientHeight + 10;
			ptr.started = true;
			ptr.delta = Math.abs(top - endY);
			ptr.dy = top>endY ? -1 : 1;
		}
		
		ptr.dy = -parseInt(((top - endY)*20)/ptr.delta);
		if(ptr.dy==0)
			ptr.dy = (top>endY ? -1:1);
		var done = false;
		if(top==endY)
			done = true;
		else
		{
			if(ptr.dy<0)
				done = (top+ptr.dy)<endY;
			else if(ptr.y>0)
				done = (top+ptr.dy)>endY;
		}
		
		if(done)
		{
			clearTimeout(ptr.rollId);
			this.started = false;
			return;
		}				
		
		var dis = Math.abs(top - endY);			
		var timeout = parseInt(1000 / dis);//parseInt(ptr.delta/Math.abs(top - endY));			
		roll.style.top = top + ptr.dy; roll.style.border = "0px solid #EEEEEE";
		ptr.rollId = setTimeout("jsRoller.prototype._runRolling()", timeout>=100 ? 100 : timeout);				
	}//end rolling
	
	this.hide = function(){
		this.roller.style.display = "none";
	}
	
	////////////////////////////////////////////////////////
	//initialize
	this._initialize = function(){
		this.roller.innerHTML = "<table align='center' cellspacing='0' cellpadding='0'><tr><td id='" + this.containerId + "' align='center'></td></tr></table>"; //make sure that content is empty :-)
		this.container = document.getElementById(this.containerId);
		this.setTop(1);
		if(window.screen.availWidth>800){
			this.roller.style.display = "block";
			this.startRoller();			
		}
	}	
	//call init
	this.roller.style.left = left;
	this._initialize();
}//end jsRoller////////////////////////////////////////////////////////

