// Variables
var menuLinks = [];
var divSpans = [];
var divs;
var hideTimer;
var showTimer;
var divIndex;

$(document).ready (function () {
	menuLinks = getMenuLinks();
	divSpans = getDivSpans();
	divs = $("div.content");
	
	setupContentDiv();
	setupMenuButs();
	setupDivSpans();
});

function getMenuLinks () {
	var a = [];	
	$("#topMenu a").each(function (i) {a.push($(this))});
	return a;
}

function getDivSpans () {
	var a = [];	
	divs > $("span.divinfo").each (function (i) {a.push($(this))});
	return a;
}

function setupContentDiv () {
	divs.hide();
	divs.mouseover(function () {
		clearTimeout(hideTimer);
	});
	divs.mouseout(function () {
		$(this).trigger("hideDivs");
	});
}

function setupMenuButs () {
	$("#topMenu a").each(function (i) {
		$(this).mouseover(function () {showDiv(i)});
		$(this).mouseout (function () {
			divs.trigger("hideDivs");
		});
	});
}

function setupDivSpans () {
	var div;
	for (var i = 0; i < divSpans.length; i++) {
		div = divSpans[i];
		div.hide();
	}
	divs.bind("hideDivs", hideDivsTimer);
}

function showDiv (i) {
	// Prevent content div from hiding
	clearTimeout(hideTimer);
	
	divs.slideDown("normal");
	
	// Hide previous div
	if (divIndex != null) hideDiv(divIndex);
	
	// Update tip index and show div
	divIndex = i;
	divSpans[i].show();
}


/*** Hide divs ***/
function hideDiv (i) {
	divSpans[i].hide();
}
function hideDivs () {
	divs.slideUp("normal");
}
function hideDivsTimer (e) {
	hideTimer = setTimeout("hideDivs()", 1500);
}

function trace (output) {
	$("#trace").html(output);
}
