/**************************************************************************************************/
/***
/***	TERNSTYLE (TM) MENUS JAVASCRIPT DOCUMENT (ternMenus ™)
/***	-----------------------------------------------------------------------
/***	Written by Matthew Praetzel. Copyright (c) 2007 Matthew Praetzel.
/***	-----------------------------------------------------------------------
/***	All Rights Reserved. Any use of these functions & scripts without written consent is prohibited.
/***
/**************************************************************************************************/

/*-----------------------
	Initialize
-----------------------*/
var menuStances = [];
/*-----------------------
	ternMenus ™
-----------------------*/
var ternMenus = function (i) {
	var a = menuStances,l = a.length;
	if(window == this) {
		if(i) {
			for(var b=0;b<a.length;b++) {
				if(a[b]["node"] === i) {
					return a[b]["object"];
				}
			}
			a[l] = new Array();
			if(i) {
				a[l]["node"] = i;
			}
			a[l]["object"] = new ternMenus(i);
			return a[l]["object"];
		}
		else {
			return new ternMenus(i);
		}
	}
	this.browser = this.getBrowserType();
	this.tern = this.getNode(i);
	this.index = (l-1);
};
ternMenus.prototype = {
	show :
	function (mc,e,ee,ef,eef,t,l,d,scr) {
		var th = this;th.l = l;th.e = e;th.ee = ee;th.ef = ef;th.eef = eef;th.d = d;th.il = [];th.t = t;th.scr = scr;
		th.on = true;
		if(th.isArray(mc)) {
			for(var i=0;i<mc.length;i++) {
				th.setShow(mc[i]);
			}
		}
		else {
			th.setShow(mc);
		}
		return th;
	},
	addItem :
	function (mc) {
		this.setShow(mc);
	},
	keep :
	function (p) {
		ternMenus(p).show(this.tern,"mouseover","mouseout");
	},
	setShow : 
	function (i) {
		var th = this;
		if(th.mc) {
			var l = th.mc.length;
			th.mc[l] = th.getNode(i);
		}
		else {
			var l = 0;
			th.mc = new Array(th.getNode(i));
		}
		if(!th.show_fn) {
			th.show_fn = new Array();
		}
		if(!th.isArray(th.e)) {
			th.e = [th.e];
		}
		th.show_fn[l] = function () { th.menu(l); };
		for(var i=0;i<th.e.length;i++) {
			ternVENTS.addEvent(th.mc[l],th.show_fn[l],th.e[i],true);
		}
		th.km = function () { th.keepMenu(); };
		ternVENTS.addEvent(th.tern,th.km,"mouseover",true);
		if(th.browser == "opera") {
			th.hm = function () { th.hide(); };
			ternVENTS.addEvent(th.tern,th.hm,"mouseout",false);
		}
		if(th.ee && th.ee != "mouseout") {
			if(!th.isArray(th.ee)) {
				th.ee = [th.ee];
			}
			for(var i=0;i<th.ee.length;i++) {
				ternVENTS.addEvent(th.mc[l],function () { ternStyle(th.tern).setStyle("visibility","hidden"); },th.ee[i],false);
			}
		}
	},
	menu :
	function (i) {
		var th = this;
		if(th.on) {
			th.tm = i;
			if(th.timer) {
				clearTimeout(th.timer);
			}
			if(th.d == "behind") {
				th.placeBehind(i);
			}
			if(th.l) {
				th.move(i);
			}
			if(th.ef) { 
					th.ef(th.mc[i]); 
			}
			if(th.t == "show") {
				ternStyle(th.tern).setStyle("visibility","visible");
			}
			else if(th.t == "fade") {
				ternStyle(th.tern).fadeIn("medium");
			}
			if(!th.mm) {
				th.mm = function (e) {
					if(th.inMenuCoords(e)) {
						if(th.timer) {
							clearTimeout(th.timer);
						}
					}
				};
			}
			ternVENTS.removeEvent(document,th.mm,"mousemove",false);
			ternVENTS.addEvent(document,th.mm,"mousemove",false);
		}
	},
	keepMenu :
	function () {
		var th = this;
		if(th.timer) {
			clearTimeout(th.timer);
			if(!th.mm) {
				th.mm = function (e) {
					if(th.inMenuCoords(e)) {
						if(th.timer) {
							clearTimeout(th.timer);
						}
					}
				};
			}
			ternVENTS.removeEvent(document,th.mm,"mousemove",false);
			ternVENTS.addEvent(document,th.mm,"mousemove",false);
		}
	},
	removeShow :
	function () {
		var th = this;
		for(var i=0;i<th.mc.length;i++) {
			for(var a=0;a<th.e.length;a++) {
				ternVENTS.removeEvent(th.mc[i],th.show_fn[i],th.e[a],true);
			}
		}
		ternVENTS.removeEvent(th.tern,th.km,"mouseover",true);
	},
	inMenuCoords : 
	function (e) {
		var th = this;
		if(th.tm != null) {
			var m = ternVENTS.mousePosition(e),d = ternStyle(th.tern).getData();
			var md = ternStyle(th.mc[th.tm]).getData();
			if(th.isWithinCoords(m[0],m[1],md[3],md[0]+md[2],md[1]+md[3],md[2]) && th.on) {
				return true;
			}
			if(th.isWithinCoords(m[0],m[1],d[3],d[0]+d[2],d[1]+d[3],d[2]) && th.on) {
				return true;
			}
			th.hide();
		}
		else {
			ternVENTS.removeEvent(document,th.mm,"mousemove",false);
		}
	},
	hide : 
	function (e) {
		var th = this,i = th.tm;
		if(th.eef) { th.eef(th.mc[i]); }
		if(th.timer) {
			clearTimeout(th.timer);
		}
		th.timer = setTimeout(function () {
			ternVENTS.removeEvent(document,th.mm,"mousemove",false);
			if(th.browser == "opera") {
				ternVENTS.removeEvent(th.tern,th.hm,"mouseout",false);
			}
			if(th.d == "behind") {
				ternStyle(th.mc[i].childNodes).setStyle("z-index",0);
				ternStyle(th.mc[i]).setStyle("z-index",0);
				th.tern.parentNode.removeChild(th.tern);
				document.body.appendChild(th.tern);
			}
			//
			if(th.t == "show") {
				ternStyle(th.tern).setStyle("visibility","hidden");
			}
			else if(th.t == "fade") {
				ternStyle(th.tern).fadeOut("medium");
			}
			th.tm = null;
		},500);
	},
	placeBehind :
	function (i) {
		var th = this,id = ternStyle(th.mc[i]).getData();
		ternStyle(th.mc[i]).setStyle("height",id[1]).setStyle("width",id[0]);
		th.tern.parentNode.removeChild(th.tern);
		if(th.mc[i].childNodes.length > 0) {
			th.mc[i].insertBefore(th.tern,th.mc[i].childNodes[0]);
		}
		else {
			th.mc[i].appendChild(th.tern);
		}
		ternStyle(th.mc[i].childNodes).setStyle("position","absolute").setStyle("z-index",110);
		ternStyle(th.mc[i]).setStyle("z-index",100);
	},
	move :
	function (i) {
		var th = this,ii = ternStyle(th.mc[i]).getData(),mi = ternStyle(th.tern).getDimensions();
		var c = th.l.split(" ");
		var s = th.scr ? ternStyle().getScroll() : [0,0];
		//x
		if(c[0] == "center") {
			ternStyle(th.tern).setStyle("left",(ii[2]-((mi[0]-ii[0])/2)));
		}
		else if(c[0] == "right") {
			ternStyle(th.tern).setStyle("left",(ii[2]+ii[0]));
		}
		else if(!/[^\-0-9]+/.test(c[1])) {
			c[1] = parseInt(c[1]);
			ternStyle(th.tern).setStyle("left",parseInt(ii[2]+c[1]));
		}
		else {
			ternStyle(th.tern).setStyle("left",ii[2]);
		}
		//y
		if(c[1] == "center") {
			ternStyle(th.tern).setStyle("top",(ii[3]-((mi[1]-ii[1])/2))+s[1]);
		}
		else if(c[1] == "top") {
			ternStyle(th.tern).setStyle("top",parseInt(ii[3]-mi[1])+s[1]);
		}
		else if(!/[^\-0-9]+/.test(c[1])) {
			c[1] = parseInt(c[1]);
			ternStyle(th.tern).setStyle("top",((ii[3]+ii[1]+c[1])+s[1]));
		}
		else {
			ternStyle(th.tern).setStyle("top",((ii[3]+ii[1])+s[1]));
		}
		ternStyle(th.tern).setStyle("z-index",100);
	},
	isWithinCoords :
	function (x,y,t,r,b,l) {
		if(x > l && x < r && y > t && y < b) {
			return true;
		}
		else {
			return false;
		}
	}
}
ternMenus.prototype.extend = 
function (a,i) {
	var t = i != undefined ? i : this;
	for(k in a) {
		t[k] = a[k];
	}
	return t;
}
ternMenus.prototype.extend({
	destroy :
	function () {
		menuStances[this.index]["node"] = false;
	},
	getNode :
	function (i) {
		if(typeof(i) == "string" && document.getElementById(i)) {
			return document.getElementById(i);
		}
		else {
			return i;
		}
	},
	getBrowserType : 
	function () {
		var a = new Array("MSIE","NETSCAPE","FIREFOX","SAFARI");
		for(i=0;i<a.length;i++) {
			var r = new RegExp(a[i],"");
			if(r.test(navigator.userAgent.toUpperCase())) {
				return a[i];
			}
		}
		return "other";
	},
	isObject :
	function (o) {
		var th = this;
		if((typeof o == "object" && !!o) || th.isFunction(o)) {
			return true;
		}
		return false;
	},
	isIEObject :
	function (o) {
		var th = this;
		if(th.isObject(o) && typeof(o.constructor != "function")) {
			return true;
		}
		return false;
	},
	isFunction : 
	function (f) {
		return typeof f == "function";
	},
	isArray : 
	function (a) {
		var th = this;
		if(th.isObject(a) && typeof(a) != "string" && a.length > 0) {
			return true;
		}
		else {
			return false;
		}
	}
});
var loaded = loaded >=0 ? loaded+1 : 0;