/*
 * DropDownMenu.js
 * Javascripts used for producing dropdown menus
 *
 * @copyright Copyright 2007-2009 Tenchi Marketing Pte Ltd
 * @version $Id: DropDownMenu.js 2009-07-09
 */

// Set the common variables
var timeout 	        = 500;
var closetimer		= 0;
var dropdownmenuitem    = 0;

// Open menu
function openmenu(parentElement, id) {	
	// Cancel menu close timer
	cancelclosetime();

	// close old layer
	if(dropdownmenuitem) {
		dropdownmenuitem.style.visibility = 'hidden';
		dropdownmenuitem.style.position = 'absolute';
	}

	// get new layer and show it
	dropdownmenuitem = document.getElementById(id);
	dropdownmenuitem.style.visibility = 'visible';
        dropdownmenuitem.style.position = 'absolute';
	var posx = findPosX(parentElement) +  parentElement.offsetWidth;
	var posy = findPosY(parentElement);
	dropdownmenuitem.style.left = posx + 'px';
	dropdownmenuitem.style.top = posy + 'px';
}

// Close menu
function closemenu() {
	if(dropdownmenuitem) dropdownmenuitem.style.visibility = 'hidden';
}

// Set menu close timer
function menuclosetime() {
	closetimer = window.setTimeout(closemenu, timeout);
}

// Cancel menu close timer
function cancelclosetime() {
	if(closetimer) {
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

// Close menu when clicked outside
document.onclick = closemenu; 
