// position class
function positionInfo(object) {

	var p_elm = object;

	this.getElementLeft = getElementLeft;
	function getElementLeft() {
		var x = 0;
		var elm;
		if(typeof(p_elm) == "object"){
			elm = p_elm;
		} else {
			elm = document.getElementById(p_elm);
		}
		while (elm != null) {
			x+= elm.offsetLeft;
			elm = elm.offsetParent;
		}
		return parseInt(x);
	}

	this.getElementWidth = getElementWidth;
	function getElementWidth(){
		var elm;
		if(typeof(p_elm) == "object"){
			elm = p_elm;
		} else {
			elm = document.getElementById(p_elm);
		}
		return parseInt(elm.offsetWidth);
	}

	this.getElementRight = getElementRight;
	function getElementRight(){
		return getElementLeft(p_elm) + getElementWidth(p_elm);
	}

	this.getElementTop = getElementTop;
	function getElementTop() {
		var y = 0;
		var elm;
		if(typeof(p_elm) == "object"){
			elm = p_elm;
		} else {
			elm = document.getElementById(p_elm);
		}
		while (elm != null) {
			y+= elm.offsetTop;
			elm = elm.offsetParent;
		}
		return parseInt(y);
	}

	this.getElementHeight = getElementHeight;
	function getElementHeight(){
		var elm;
		if(typeof(p_elm) == "object"){
			elm = p_elm;
		} else {
			elm = document.getElementById(p_elm);
		}
		return parseInt(elm.offsetHeight);
	}

	this.getElementBottom = getElementBottom;
	function getElementBottom(){
		return getElementTop(p_elm) + getElementHeight(p_elm);
	}
}


// menu
var menuWidth = 135;
var menuItemHeight = 21;
var menuHeaderHeight = 21
var menuPlusser = 5;
var menuOpenTimeout = 5;
var menuCloseTimeout = 1500;

var menuHeight;
var currentMenuHeight = 0;
var currentMenuWidth = 0;
var menuOpened = Array();
var closeTimer = null;
var closeTimerArray = Array();

var sourceObjG = null

function showMenu(sourceObj,parentID,menuID) {
	sourceObjG = sourceObj;
	setTimeout("showMenu2('"+parentID+"','"+menuID+"')",500);
}

function showMenu2(parentID,menuID)
{
	sourceObj = sourceObjG;
	menuClearCloseTimer(menuID);
	var sourcePos = new positionInfo(sourceObj);
	var sub = document.getElementById('sub_'+menuID);

	sub.style.top = sourcePos.getElementTop();

	if ( navigator.userAgent.indexOf ( 'MSIE' ) != -1 ) {

		sub.style.left = 5+sourcePos.getElementRight();
		if(sourceObj.className=='sub') {
			sub.style.left = parseInt(sub.style.left)-4;
			sub.style.top = parseInt(sub.style.top)+1;
		}

	} else {

		sub.style.left = sourcePos.getElementRight();
		if(sourceObj.className=='sub') {
			sub.style.left = parseInt(sub.style.left)-16;
		}
	}

	var elems = document.getElementById('link_'+menuID).childNodes;
	var numElems = 0;
	for(var i=0 ; i<elems.length ; i++ ) {
		if(elems[i].tagName=='A' && elems[i].id.substr(0,6)!='close_') {
			numElems++;
		}
	}
	menuHeight = menuHeaderHeight+(numElems*menuItemHeight);
	currentMenuHeight = 0;
	currentMenuWidth = 0;

	if(menuOpened['sub_'+parentID]) {
		closeMenu(menuOpened['sub_'+parentID]);
	}
	menuOpened['sub_'+parentID] = menuID;


	if(sub.style.display != 'block') {
		sub.style.height = menuHeaderHeight+'px';
		sub.style.display = 'block';
		addMouseOverFunction(sub,parentID);
		openMenuHori(menuID);
	}
}

function addMouseOverFunction(obj,menuID) {
	elems = obj.getElementsByTagName('a');
	for( i=0 ; i<elems.length ; i++ ) {
		if(typeof(elems[i].onmouseover)!='function') {
			elems[i].onmouseover = function() {
				menuClearCloseTimer(menuID);
			}
		}
	}
}

function openMenuHori(menuID) {
	menuObj = document.getElementById('sub_'+menuID);
	var plusser = 3;
	if(currentMenuWidth < menuWidth)
	{
		currentMenuWidth = currentMenuWidth+plusser;
		menuObj.style.width = currentMenuWidth+'px';
		setTimeout("openMenuHori('"+menuID+"')",5);
	} else {
		openMenuVert(menuID)
	}
}

function openMenuVert(menuID) {
	menuObj = document.getElementById('sub_'+menuID);
	if(currentMenuHeight < menuHeight)
	{
		currentMenuHeight = currentMenuHeight+menuPlusser;
		menuObj.style.height = currentMenuHeight+'px';
		setTimeout("openMenuVert('"+menuID+"')",menuOpenTimeout);
	} else {
		timeOut = setTimeout("closeMenuStart('"+menuID+"')",menuCloseTimeout);
		saveTimeOut(menuID,timeOut);
	}
}

function closeMenuStart(menuID) {
	if(childID = menuOpened['sub_'+menuID]) {
		closeMenuStart(childID);
	}
	closeMenu(menuID);
}

function closeMenu(menuID) {
	if(obj = document.getElementById('sub_'+menuID)) {
		obj.style.display = 'none';
	}
}

function menuMouseOut(srcObj) {
	timeOut = setTimeout("closeMenuStart('"+srcObj.id.substr(4,6)+"')",menuCloseTimeout);
	saveTimeOut(srcObj.id.substr(4,6),timeOut);
}
function menuClearCloseTimer(menuID) {
	clearTimeout(getTimeOut(menuID));
}

function saveTimeOut(menuID,timeOut) {
	closeTimerArray[menuID] = timeOut;
}
function getTimeOut(menuID) {
	return closeTimerArray[menuID];
}