// Flyout config settings.
//-----------------------------------------------
var MENU_TOP_SPACE       = 3;
var MENU_LEFT_SPACE      = 4;
var PAUSE_IN_MILISECONDS = 600;


// Flyout runtime code.
//-----------------------------------------------

// Preload the menu states.
var menuState            = new Array();
var menuIdCollection     = new Array();
var actuatorIdCollection = new Array();

// Exit file if the browser does not support document.
if (!document.getElementById) {
    document.getElementById = function() { return null; }
}


// Flyout functions.
//-----------------------------------------------

// Add items to the arrays
function addMenu(menuId,actuatorId) {
	menuIdCollection[menuIdCollection.length] = menuId;
	actuatorIdCollection[actuatorIdCollection.length] = actuatorId;
}

// Initialize the menu.
function initializeMenu(menuIndex,actuatorIndex) {
	var menu     = document.getElementById(menuIdCollection[menuIndex]);
	var actuator = document.getElementById(actuatorIdCollection[actuatorIndex]);

	if (menu == null || actuator == null) {
		return;
	}
        
	// Assign style settings.
	menu.style.top  = getElementBottom(actuatorIdCollection[actuatorIndex]) + "px";
	menu.style.left = getElementLeft(actuatorIdCollection[actuatorIndex]) + "px";
	
	// Create method to handle mouseover of the actuator.
	actuator.onmouseover = function() {
		menuState[menuIndex]++;
		var menu = document.getElementById(menuIdCollection[menuIndex]);
		menu.style.display = "block";
	
		return false;
	}

	// Create method to handle mouseout of the actuator.
	actuator.onmouseout = function() {
		setTimeout("hideMenu('" + menuIndex + "')", PAUSE_IN_MILISECONDS);
		return false;
	}
	
	// Create method to handle mouseover of the menu.
	menu.onmouseover = function() {
		menuState[menuIndex]++;
		return false;
	}
	   
	// Create method to handle mouseout of the menu.
	menu.onmouseout = function() {
		setTimeout("hideMenu('" + menuIndex + "')", PAUSE_IN_MILISECONDS);
		return false;
	}
}

// Method to hide the menu.
function hideMenu(menuIndex) {
	var menu  = document.getElementById(menuIdCollection[menuIndex]);
	var count = --menuState[menuIndex];
	
	// If meny does not exist, exit.
	if (menu == null) return;
	
	// Check if any menu items are being hovered, if none, then hide.
	if (count > 0) return;
	
	//var display = menu.style.display;
	menu.style.display = "none";
}

// Method to obtain the left pixel location, given an object name.
function getElementLeft(Elem) {
	var elem;
	if (document.getElementById) {
		elem = document.getElementById(Elem);
	} else if (document.all) {
		elem = document.all[Elem];
	} else if (document.layers) {
		elem = document.layers[name];
	}
	
	xPos = elem.offsetLeft;
	tempEl = elem.offsetParent;
	while (tempEl != null) {
		xPos += tempEl.offsetLeft;
		tempEl = tempEl.offsetParent;
	}
	xPos += MENU_LEFT_SPACE;
	return xPos;
}

// Method to obtain the top pixel location, given an object name.
function getElementTop(Elem) {
	var elem;
	if (document.getElementById) {	
		elem = document.getElementById(Elem);
	} else if (document.all) {
		elem = document.all[Elem];
	} else if (document.layers) {
		elem = document.layers[name];
	}
	
	yPos = elem.offsetTop;
	tempEl = elem.offsetParent;
	while (tempEl != null) {
		yPos += tempEl.offsetTop;
		tempEl = tempEl.offsetParent;
	}
	yPos += MENU_TOP_SPACE;
	return yPos;
}

// Method to obtain the bottom pixel location, given an object name.
function getElementBottom(Elem) {
	var elem;
	if (document.getElementById) {	
		elem = document.getElementById(Elem);
	} else if (document.all) {
		elem = document.all[Elem];
	} else if (document.layers) {
		elem = document.layers[name];
	}
	
	yPos = elem.offsetTop;
	tempEl = elem.offsetParent;
	while (tempEl != null) {
		yPos += tempEl.offsetTop;
		tempEl = tempEl.offsetParent;
	}
	yPos += (elem.offsetHeight + MENU_TOP_SPACE);
	return yPos;
}

// Method to handle browser load event.
window.onload = function() {	
	for (i=0; i<menuIdCollection.length; ++i) {
	  initializeMenu(i,i);
	  menuState[i] = 0;
	}
}

// Method to handle browser resize event.
window.onresize = function() {
	// Refresh the flyout menus.
	for (i=0; i<menuIdCollection.length; ++i) {
	  initializeMenu(i,i);
	}
}


// Method to handle browser load event.
window.onload = function() {
	positionLoginButton();
	for (i=0; i<menuIdCollection.length; ++i) {
	  initializeMenu(i,i);
	  menuState[i] = 0;
	}
}

// Method to handle browser resize event.
window.onresize = function() {
	positionLoginButton();
	// Refresh the flyout menus.
	for (i=0; i<menuIdCollection.length; ++i) {
	  initializeMenu(i,i);
	}
}

function positionLoginButton()
{
	var btnLogin = getElem('login_button');
	var eleHeader = getElem('Wuctop2_heading_join');
	if (window.document.getElementById('Wuctop2_heading_join'))
	{
	var intBrowserFix = (checkBrowser('msie')) ? 8 : 0;
	var intTop = findPosY(eleHeader)+220 + intBrowserFix;
	var intLeft = findPosX(eleHeader)+380 + intBrowserFix;
	btnLogin.style.top = intTop + "px";
	btnLogin.style.left = intLeft + "px";
	btnLogin.style.visibility = "visible";
	}
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}
function checkBrowser(strBrowser) {	return navigator.userAgent.toLowerCase().indexOf(strBrowser) + 1; }
function getElem(elemID) { return window.document.getElementById(elemID);}
function findPosX(obj) {
	var curleft = 0;if (obj.offsetParent) {
	while (obj.offsetParent) {
		curleft += obj.offsetLeft
		obj = obj.offsetParent;
	}
	} else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}	
	

