
//
// Welcome to the SNAZZY MENU OF DOOM!!! (aka kmnu)
// include this js file at the bottom of the page - the page elements being used
// have to be loaded before we can run the attach function
//
// This is a JavaScript menu that contains all you hovering functions for opening and such, but
// it not make the elements for you. You make the HTML elements, this menu will cause them to
// appear/disappear.
//


// the delays!
kmnu_open_delay = 100;
kmnu_close_delay = 300;


// add the opening functions to these elements
kmnu_add("gameLink", "gameMenu", false);
kmnu_add("forumLink", "forumMenu", false);
kmnu_add("helpLink", "helpMenu", false);
kmnu_add("accountLink", "accountMenu", false);
kmnu_add("builderLink", "builderMenu", false);
kmnu_add("dmLink", "dmMenu", false);


// these elements are our menus
kmnu_attach(
	"gameMenu",
	"forumMenu",
	"helpMenu",
	"accountMenu",
	"builderMenu",
	"dmMenu"
);




//
// think of this as a kind of "state" array. it tracks what state the menu is in.
// when the user hovers over the menu, the state changes, so any previous close
// requests are ignored.
//

var kmnu_state;
var kmnu_cur = "";


// note! in the functions below, I user the dollar ($) function, which is basically
// a robust shortcut for document.getElementById. If you don't like it, do a find ->
// replace on this script, I don't use $ anywhere else (except these comments)



// nice function for attaching the menu-calling functions
// to an object

function kmnu_add(id, obj_id, doClick) {

	if($("#"+id)[0] && $("#"+obj_id)[0]) {

		$("#"+id)[0].onmouseover = function onmouseover(event) {kmnu_open(obj_id);};
		if(doClick)
			$("#"+id)[0].onclick = function onclick(event) {kmnu_real_open(obj_id, kmnu_state[obj_id]); return false;};
		$("#"+id)[0].onmouseout = function onmouseout(event) {kmnu_cancel(obj_id);};

		//element.onmouseover = function onmouseover(event) {kmnu_hover(this);};
		//element.onmouseout = function onmouseout(event) {kmnu_close(this);};
	}

}



// will cancel a request if you dont hover long enough!
// not if it's already open, though, or things will mess up

function kmnu_cancel(id) {

	if($("#"+id)[0]) {

		if($("#"+id)[0].style.display!="block") {

			kmnu_state[id]++;

		} else {

			kmnu_close($("#"+id)[0]);

		}

	}

}



// this function triggers an open-menu call!
// for a nice quick delay in opening

function kmnu_open(id) {

	if($("#"+id)[0].style.display!="block") {

		setTimeout("kmnu_real_open('" + id + "', " + kmnu_state[id] + ")", kmnu_open_delay);

	} else {

		kmnu_hover($("#"+id)[0]);

	}

}


// this function opens the menu and makes sure to close
// any other open menus

function kmnu_real_open(id, state) {

	if($("#"+id)[0] && $("#"+id)[0].style.display!="block" && kmnu_state[id]==state) {

		if(kmnu_cur) {

			kmnu_real_close(kmnu_cur, kmnu_state[kmnu_cur]);

		}

		$("#"+id)[0].style.display = "block";
		kmnu_cur = id;

		//kmnu_close($(id));

	}

}


// this facilitates the delay of closing when the
// mouse leaves the menu

function kmnu_close(obj) {

	setTimeout("kmnu_real_close('" + obj.id + "', " + kmnu_state[obj.id] + ")", kmnu_close_delay);

}


// this function actually closes the menu
// it only closes if the menu hasnt changed states since it was called
// direct calls will always close because there's no time for the state to change

function kmnu_real_close(id, state) {

	if($("#"+id)[0] && kmnu_state[id]==state) {

		$("#"+id)[0].style.display = "none";
		kmnu_state[id] = 0;

	}

}


// when the menu is hovered over, we must change
// the state to ignore all previous close requests

function kmnu_hover(obj) {

	kmnu_state[obj.id]++;

}


// this function attaches the events to the objects that are
// going to be used as a menu
// pass it a list of elements or strings (ids), or both! we dont care!

function kmnu_attach() {

	kmnu_state = Array(arguments.length);

	for(var i=0; i<arguments.length; i++) {

		var element = $("#"+arguments[i])[0];

		if(element) {

			element.onmouseover = function onmouseover(event) {kmnu_hover(this);};
			element.onmouseout = function onmouseout(event) {kmnu_close(this);};

			kmnu_state[element.id] = 0;

		}

	}

}

