﻿var oActiveMarketItem = null;
var oChildFlyoutTimeout = null;

$(document).ready(function() {
	var aMarketLinks = $(".region li");
	$.each(aMarketLinks, function() {
		if (this.getElementsByTagName("ul").length > 0) {
			var oLink = this.getElementsByTagName("a").item(0);
			oLink.onmouseover = function() {
				oActiveMarketItem = this;
				var oChildList = this.parentNode.getElementsByTagName("ul").item(0);
				var oChildFlyout = document.getElementById("child-flyout");
				if (oChildFlyout == null) {
					oChildFlyout = document.createElement("div");
					oChildFlyout.id = "child-flyout";
					
					
					oChildFlyout.onmouseover = function() {
						showFlyout();
					}
					oChildFlyout.onmouseout = function() {
						hideFlyout();
					}
					document.getElementById("page").appendChild(oChildFlyout);
				}
				oChildFlyout.innerHTML = "<div><ul>" + oChildList.innerHTML + "</ul></div>";
				showFlyout();
			}
			oLink.onmouseout = function() {
				hideFlyout();
			}
		}
	});
});

function showFlyout() {
	if (oChildFlyoutTimeout != null)
		clearTimeout(oChildFlyoutTimeout);
	
	var iPageDivX = $("#page").offset().left;
	var oLink = $(oActiveMarketItem);
	var iLinkX = oLink.offset().left;
	var iLinkFromPageDivX = iLinkX - iPageDivX;
	var iLinkWidth = oLink.innerWidth();
	
	var oFlyout = document.getElementById("child-flyout");
	
	var oChildList = oActiveMarketItem.parentNode.getElementsByTagName("ul").item(0);
	var iNumberOfChildItems = oChildList.getElementsByTagName("li").length;
	if (iNumberOfChildItems == 2)
		oFlyout.className = "child-flyout-2-rows";
	else
		oFlyout.className = "child-flyout-3-rows";	
	
	oFlyout.style.left = iLinkFromPageDivX + iLinkWidth + "px";
	oFlyout.style.top = oLink.offset().top - ($(oFlyout).innerHeight() / 2) + (oLink.innerHeight() / 2) + "px";
	
	if (document.all) {
		$("#child-flyout").show();
	} else {
		$("#child-flyout").fadeIn(vcc.iDelay);
	}
}

function hideFlyout() {
	oChildFlyoutTimeout = setTimeout(function() {
		var oChildFlyout = document.getElementById("child-flyout");
		if (oChildFlyout != null) {
			if (document.all)
				$(oChildFlyout).hide();
			else
				$(oChildFlyout).fadeOut(vcc.iDelay);
		}
	}, vcc.iDelay);
}

function countryTitle(oLink, sTitle) {
	if (sTitle == "")
		return;
	oLink.innerHTML = sTitle;
}