
/* MENU FUNCTIONS CORE FILE
 * ========================
 *
 * (C) Alexander Shepherd 2009
 *
 * This file contains:
 *   +GLOBAL variable declarations
 *   +The menu class, with it's functions
 *   +Position Function
 *   +GLOBAL functions for handling the opening / closing of menus
 *
 * THIS IS THE UNCOMPRESSED, BETA VERSION.
 * NOT FOR PUBLIC RELEASE.
 *
 * IF YOU HAVE BEEN SOLD / GIVEN THIS BY ANY PARTY OTHER THAN SWEEB,
 * PLEASE REMPORT IT TO ALEXANDER.SHEPEHRD@GMAIL.COM
 *
 */

	var _menuSystem = Array();
		_menuSystem._MENUS = Array();
		_menuSystem._TIMER = null;
		_menuSystem._MENU_LIST = Array();
		_menuSystem._GLOBALS = Array();
			_menuSystem._GLOBALS.lastTop = false;
			_menuSystem._GLOBALS.lastLeft = false;
			_menuSystem._GLOBALS._TOP = '_body';
	
	//MENU FUNCTION:
	//Create this to add a new menu / submenu
	function menu (div_class, top, fadeDelay, created) {
		
		this.type = 'down';
		this.last_id = 0;
		this.append = Math.floor(Math.random()*10000);
		
		if (!fadeDelay) {
			this.fadeDelay = 500;
		} else {
			this.fadeDelay = fadeDelay;
		}
		
		if (_menuSystem._GLOBALS.lastTop) {
			this.bufferTop = _menuSystem._GLOBALS.lastTop;
		} else {
			this.bufferTop = 0;
		}
		
		if (_menuSystem._GLOBALS.lastLeft) {
			this.bufferLeft = _menuSystem._GLOBALS.lastLeft;
		} else {
			this.bufferLeft = 0;
		}
		
		_menuSystem._MENUS['menu'+this.append] = Array();
		
		if (!created) {
			//document.write('<div id="menu' + this.append + '"></div>');
			Obj = document.createElement('div');
			Obj.id = 'menu' + this.append;
			document.getElementById('sub_holder').appendChild(Obj);
		} else {
			document.getElementById(created).id = 'menu' + this.append;
			_menuSystem._GLOBALS._TOP = 'menu' + this.append;
		}
		
		Obj = document.getElementById('menu' + this.append);
		Obj.className = div_class;
		
		if (!top) {
			Obj.style.visibility = 'hidden';
			Obj.style.position = 'absolute';
			Obj.subFadeDelay = this.fadeDelay;
			
			Obj.onmouseout = function() {
				start_close_subs(this.subFadeDelay);
			};
			
			_menuSystem._MENU_LIST[_menuSystem._MENU_LIST.length]='menu'+this.append;
		} else {			
			document.body.onclick = function() {
				close_subs();
			};
		}
	
	}
	
	//Menu.addItem() FUNCTION:
	//Add an Item to an menu
	//text and href are required.
	//Include suMenu to add a sub menu to this item.
	menu.prototype.addItem = function(text, href) {
		
		this.last_id += 1;
		did = 'menu' + this.append + this.last_id;
		
		if (href===undefined) {
			href = 'javascript:void(0);';
		}
		
		Obj = document.createElement('a');
			
		Obj.id = did;
		Obj.innerHTML = '<span>'+text+'</span>';
		Obj.href = href;
		//Obj.className = this.sub_class;
		Obj.subParent = 'menu' + this.append;
		
		Obj.subFadeDelay = this.fadeDelay;
		//Obj.subHover = this.classHover;
		//Obj.subNormal = this.sub_class;
		
		if (this.type=='along') {
			Obj.style.styleFloat = 'left';
			Obj.style.cssFloat = 'left';
		} else {
			Obj.style.display = 'block';
		}
		
		Obj.onmouseover = function() {
			start_sort_subs(this.subParent, this.subFadeDelay);
			//hover_sub(this.id, this.subHover);
		};
		
		Obj.onmouseout = function() {
			//hover_sub(this.id, this.subNormal);
		};
			
		document.getElementById('menu'+this.append).appendChild(Obj);
		
		_menuSystem._MENUS['menu'+this.append][this.last_id] = 'item';
		
		return did;

	};
	
	//Ids are randomly assigned.
	//Use this for joining a sub menu to an item.
	menu.prototype.getId = function() {
		return 'menu' + this.append;
	};
	
	menu.prototype.subMenu = function(subMenu, menu_id) {
		
		if (!menu_id) {
			menu_id = this.last_id;
		}
		
		if (this.type=='along') {
			pos = 'under';
		} else {
			pos = 'right';
		}
		
		did = 'menu'+this.append+menu_id;
		
		Obj = document.getElementById(did);
		
		Obj.subMenu = subMenu;
		Obj.subPos = pos;
		
		Obj.subHeight = this.bufferTop;
		Obj.subWidth = this.bufferLeft;
		
		Obj.onmouseover = function() {
			open_sub(this.id, this.subMenu, this.subPos, this.subHeight, this.subWidth);
			//hover_sub(this.id, this.subHover);
		};
		
		Obj.onmouseout = function() {
			start_close_subs(this.subFadeDelay);
			//hover_sub(this.id, this.subNormal);
		};
		
		_menuSystem._MENUS['menu'+this.append][menu_id] = subMenu;
		_menuSystem._MENUS[subMenu]._PARENT_MENU = 'menu'+this.append;
		_menuSystem._MENUS[subMenu]._PARENT_ITEM = menu_id;
		
	};
	
	menu.prototype.setBufferLeft = function(left) {
		_menuSystem._GLOBALS.lastLeft = left;
		this.bufferLeft = left;
	};
	
	menu.prototype.setBufferTop = function(top) {
		_menuSystem._GLOBALS.lastTop = top;
		this.bufferTop = top;
	};
	
	//Position Functions
	function getLeft(This){
		var el = This;var pL = 0;
		while(el){pL+=el.offsetLeft;el=el.offsetParent;}
		return pL;
	}
	
	function getTop(This){
		var el = This;var pT = 0;
		while(el){pT+=el.offsetTop;el=el.offsetParent;}
		return pT;
	}
	
	//Opens a submenu
	function open_sub(parent, child, pos, parent_height, parent_width) {

		sort_subs(child);
		
		parent = document.getElementById(parent);
		childObj = document.getElementById(child).style;
		
		childObj.visibility = 'visible';
		var top = getTop(parent);
		var left = getLeft(parent)-1;
	
		if (pos=='under') {
			top = top*1 + parent_height*1;
		} else {
			left = left*1 + parent_width*1+1;
		}
		
		childObj.left = left + 'px';
		childObj.top = top + 'px';
		
		_menuSystem._MENUS[child]._STATUS = 'open';

	}
	
	//Begins the timer to hude the menu
	function start_close_subs(fade) {
		clearTimeout(_menuSystem._TIMER);
		_menuSystem._TIMER = setTimeout('close_subs();', fade);
	}
	
	function start_sort_subs(child, fade) {
		clearTimeout(_menuSystem._TIMER);
		_menuSystem._TIMER = setTimeout('sort_subs(\'' + child + '\');', fade);
	}
	
	//Closes the submenu
	function close_subs() {
		clearTimeout(_menuSystem._TIMER);
		//document.getElementById(child).style.visibility = 'hidden';
		//_menuSystem._MENUS[child]['_STATUS'] = 'hidden';
		
		
		for (i=0; i<=_menuSystem._MENU_LIST.length-1; i++) {
			document.getElementById(_menuSystem._MENU_LIST[i]).style.visibility = 'hidden';
		}
		
	}
	
	//Figures out which parent menus to keep open
	function sort_subs(child) {
		
		clearTimeout(_menuSystem._TIMER);
		
		current_test = child;
		keep_alive = Array();
		keep_alive[current_test]=true;
		finished = false;
		
		while (!finished) {
			temp = _menuSystem._MENUS[current_test]._PARENT_MENU;
			if (temp) {
				keep_alive[temp]=true;
				current_test = temp;
			} else {
				finished = true;
			}
		}
		
		menus = document.getElementsByName('menus');
		
		for (i=0; i<=_menuSystem._MENU_LIST.length-1; i++) {
			if (keep_alive[_menuSystem._MENU_LIST[i]]!==true) {
				document.getElementById(_menuSystem._MENU_LIST[i]).style.visibility = 'hidden';
			}
		}
	}
	
	//Changes the class of a menu item when you hover over it.
	function hover_sub(sub_item, sub_class) {
		document.getElementById(sub_item).className = sub_class;
	}