/**************************************************************************************************/
/***
/***	TERNSTYLE (TM) MENUS JAVASCRIPT DOCUMENT (ternMenus tm version 2.1)
/***	-----------------------------------------------------------------------
/***	Written by Matthew Praetzel. Copyright (c) 2007 Matthew Praetzel.
/***	-----------------------------------------------------------------------
/***	All Rights Reserved. Any use of these functions & scripts without written consent is prohibited.
/***
/**************************************************************************************************/

/*
{
	type : 'click/mouseover',
	css : 'className',
	hang : bool
}
*/

/*-----------------------
	ternMenus (tm) v2.1
-----------------------*/
var ternMenus = function (i) {
	var a = _ternmenus.instances,l = a.length;
	if(window == this) {
		i = _ternStyle.getNode(i);
		v = _ternStyle.inArray(a,i,'node');
		if(v !== false) {
			return a[v]['object'];
		}
		a[l] = new Array();
		a[l]['node'] = i;
		a[l]['object'] = new ternMenus(i);
		return a[l]['object'];
	}
	_ternStyle.getBrowserType.apply(this);
	this.tern = _ternStyle.getNode(i);
};
ternMenus.prototype = _ternmenus = {
	instances : [],
	init :
	function (a) {
		var th = this,m;
		for(k in a) {
			th[k] = a[k];	
		}
		eval('th.'+th.type+'=true;');
		this.a = th.tern.getElementsByTagName('a');
		th.get = function (e) { th.getMenu(e); };
		for(var i=0;i<this.a.length;i++) {
			m = this.a[i].parentNode.getElementsByTagName('ul')[0];
			if(m) {
				this.a[i].onmousedown = function () { return false; };
				ternEvents.addEvent(this.a[i],th.get,th.type,false);
			}
		}
		return th;
	},
	getMenu :
	function (e) {
		this.s = this.fixSource(e);
		this.p = this.s.parentNode;this.menu = this.p.getElementsByTagName('ul')[0];
		if(_ternStyle.hasClass(this.css,this.p)) {
			this.closeMenu(e);
		}
		else {
			ternStyle(this.p).addClass(this.css);
			this.showMenu(e);
		}
	},
	showMenu :
	function (e) {
		var th = this;
		ternStyle(this.menu).setStyle({display : 'block'});
		th.close = function (e) { th.closeMenu(e); };
		th.dclose = function (e) { th.docClose(e); };
		if(th.click) {
			ternEvents.addEvent(document,th.dclose,'mouseup',false);
		}
		if(th.mouseover) {
			ternEvents.addEvent(this.menu,th.close,'mouseout',false);
			ternEvents.addEvent(this.s,th.close,'mouseout',false);
		}
		//
		if(this.hang) {
			//th.track = function (e) { th.trackMenu(e); };
			//ternEvents.addEvent(document,th.track,'mousemove',false);
			//ternEvents.addEvent(this.a,th.get,'mouseover',false);
		}
	},
	trackMenu :
	function (e) {
		if(this.docClose(e,true) || !this.inMenuCoords(e)) {
			this.closeMenu(e);
		}
	},
	closeMenu :
	function (e) {
		if(this.click || (this.mouseover && !this.inMenuCoords(e))) {
			//hide menu
			ternStyle(this.menu).setStyle({display : 'none'});
			ternStyle(this.p).removeClass(this.css);
			//remove click events
			ternEvents.removeEvent(document,this.dclose,'mouseup',false);
			//ternEvents.removeEvent(document,this.track,'mousemove',false);
			//remove mouseover events
			ternEvents.removeEvent(this.menu,this.close,'mouseout',false);
			ternEvents.removeEvent(this.s,this.close,'mouseout',false);
			//ternEvents.removeEvent(this.a,this.get,'mouseover',false);
		}
	},
	docClose :
	function (e,b) {
		if(this.s != this.fixSource(e)) {
			if(b) { return false; }
			this.closeMenu(e);
		}
	},
	fixSource :
	function (e) {
		var s = ternEvents.eventSource(e);
		while(s.nodeName.toLowerCase() != 'a') {
			if(s == document.body) { break; }
			s = s.parentNode;
		}
		return s;
	},
	inMenuCoords : 
	function (e) {
		var m = ternEvents.mousePosition(e),f = ternStyle(this.menu).getData(),d = ternStyle(this.s).getData();
		if(this.isWithinCoords(m[0],m[1],f[3],f[0]+f[2],f[1]+f[3],f[2])) {
			return true;
		}
		if(this.isWithinCoords(m[0],m[1],d[3],d[0]+d[2],d[1]+d[3],d[2])) {
			return true;
		}
		return false;
	},
	isWithinCoords :
	function (x,y,t,r,b,l) {
		if(x > l && x < r && y > t && y < b) {
			return true;
		}
		return false;
	},
	killMenu :
	function () {
		var a = this.tern.getElementsByTagName('a');
		for(var i=0;i<a.length;i++) {
			ternEvents.removeEvent(a[i],this.get,this.type,false);
		}
	}
}