/**
 * javascript_menu.js
 *
 * Javascript file
 * @description	Hierarchical menu operation
 * @copyright 	2007 Kaaterskil Management, LLC
 * @version		070825
 * @package		includes.modules.javascript
 */

/**
 * Arrange menu items
 * Arranges menu items equidistant from each other along a
 * menubar. The method sums the computed width of each menu
 * item and distributes the remainder to each as left- and
 * right-padding. For public pages.
 * @param menu_id		The topmost ul tag
 * @param container_id	The topmost div container (2 parents
 *						up from ul)
 * @param admin_test	The admin page has no container, so
 * 						we need to set the width manually.
 */
function arrange_menu_items(menu_id, container_id, admin_test){
	//browser/platform sniffing (for debugging)
	var agt			= navigator.userAgent.toLowerCase();
	
	var appVer		= navigator.appVersion.toLowerCase();
	var is_minor	= parseFloat(appVer);
	var is_major	= parseInt(is_minor);
	var ie_position	= appVer.indexOf('msie');
	if(ie_position != -1){
		is_minor	= parseFloat(appVer.substring(ie_position + 5, 
												  appVer.indexOf(';', ie_position)));
		is_major	= parseInt(is_minor);
	}
	
	var is_ie		= agt.indexOf('msie') != -1 ? true : false;
	var is_mac		= agt.indexOf('mac') != -1 ? true : false;
	var is_safari	= agt.indexOf('safari') != -1 ? true : false;
	var is_intel	= agt.indexOf('intel') != -1 ? true : false;
	
	var is_ppc		= agt.indexOf('ppc') != -1 ? true : false;
	var is_khtml	= agt.indexOf('khtml') != -1 ? true : false;
	var is_win		= agt.indexOf('win') != -1 ? true : false;
	var is_fx		= agt.indexOf('firefox') != -1 ? true : false;
	
	//get the number of menu items and the total text length
	var items = 0;
	var computed_width = 0;
	var ec = find_DOM(menu_id);
	if(ec && ec.childNodes){
		for(var i = 0; i < ec.childNodes.length; i++){
			var e = ec.childNodes[i];
			if(e.nodeName.toLowerCase() == 'li'){
				computed_width += e.offsetWidth;
				items++;
			}
		}
		
		//get the total div width
		if(!admin_test){
			var et = find_DOM(container_id);
			if(et.currentStyle){
				var total_width = parseInt(et.offsetWidth);
			}else{
				var total_width = parseInt(window.getComputedStyle(et, null).width);
			}
		}else{
			var total_width = 600;
		}
		
		//compute and distribute the excess space
		var total_space = total_width - computed_width;
		var padding = Math.floor((total_space / 2) / items, 0);
		
		//set the padding to each menu item
		//and position any child drop-down menu containers
		for(var i = 0; i < ec.childNodes.length; i++){
			var e = ec.childNodes[i];
			if(e.nodeName.toLowerCase() == 'li'){
				//set menu item spacing
				e.style.paddingLeft = padding + 'px';
				e.style.paddingRight = padding + 'px';
				
				/**
				 * Set drop-down menu position
				 * @param o adjusts menu position by its padding-left property.
				 * @param p.w adjusts for IE box model rendering
				 */
				for(var j = 0; j < e.childNodes.length; j++){
					var eu = e.childNodes[j];
					if(eu.nodeName.toLowerCase() == 'ul'){
						var p = get_coords(e);
						var o = padding > 20 ? padding - 20 : 0;
						if(is_ie && is_major < 7){
							p.w = e.offsetParent.offsetParent.offsetWidth - 6;
							var x = p.x + o - p.w;
							var y = p.y + e.offsetHeight;
						}else{
							var x = p.x + o - 1;
							var y = p.y + e.offsetHeight;
						}
						set_coords(eu, x, y);
						
						//set menu item css behavior if browser is ie
						if(is_ie){
							set_ie_menu_css(eu);
						}
					}
				}//end for
			}
		}//end for
	}
}

/**
 * Set menu css hover behavior for IE browsers.
 * Since IE only supports the hover pseudo class for link
 * elements, we assign the css properties to the parent LI
 * element as a class when a mouseover occurs on the LI's
 * child A element.
 * @param obj ul 	Menu container element (a UL element)
 */
function set_ie_menu_css(ul){
	for(var i = 0; i < ul.childNodes.length; i++){
		var e = ul.childNodes[i];
		if(e.nodeName.toLowerCase() == 'li'){
			
			//loop through the menu items
			for(var j = 0; j < e.childNodes.length; j++){
				if(e.childNodes[j].nodeName.toLowerCase() == 'a'){
					e.childNodes[j].onmouseover = function(){
						this.parentNode.className = 'menu_item_ie_mouseover';
					};
					e.childNodes[j].onmouseout = function(){
						this.parentNode.className = 'menu_item';
					};
				}
			}//end for
		}
	}
}

/**
 * Set menu behavior
 * First section does some basic browser sniffing so that
 * debugging can report on success. The second section sets
 * behaviors of drop-down elements and adjusts for IE
 * rendering conflicts.
 */
function set_menu(admin_test){
	//browser/platform sniffing (for debugging)
	var agt			= navigator.userAgent.toLowerCase();
	
	var appVer		= navigator.appVersion.toLowerCase();
	var is_minor	= parseFloat(appVer);
	var is_major	= parseInt(is_minor);
	var ie_position	= appVer.indexOf('msie');
	if(ie_position != -1){
		is_minor	= parseFloat(appVer.substring(ie_position + 5, 
												  appVer.indexOf(';', ie_position)));
		is_major	= parseInt(is_minor);
	}

	var is_ie		= agt.indexOf('msie') != -1 ? true : false;
	var is_mac		= agt.indexOf('mac') != -1 ? true : false;
	var is_safari	= agt.indexOf('safari') != -1 ? true : false;
	var is_intel	= agt.indexOf('intel') != -1 ? true : false;
	
	var is_ppc		= agt.indexOf('ppc') != -1 ? true : false;
	var is_khtml	= agt.indexOf('khtml') != -1 ? true : false;
	var is_win		= agt.indexOf('win') != -1 ? true : false;
	var is_fx		= agt.indexOf('firefox') != -1 ? true : false;
	
	//get menu items
	var ep = document.getElementById('nav').getElementsByTagName('li');
	for(var i = 0; i < ep.length; i++){
		//position admin menus
		var href = window.location.search;
		if(href.indexOf('?p=') != -1){
			for(var j = 0; j < ep[i].childNodes.length; j++){
				var e = ep[i].childNodes[j];
				if(e.nodeName.toLowerCase() == 'ul'){
					var p = get_coords(ep[i]);
					var o = parseInt(ep[i].style.paddingLeft) > 20 
							? parseInt(ep[i].style.paddingLeft) 
							: 0;
					if(is_ie && is_major < 7){
						p.w = ep[i].offsetParent.offsetParent.offsetWidth - 6;
						if(!admin_test){
							var x = p.x + o - p.w;
						}else{
							var x = p.x - 594;
						}
						var y = p.y + ep[i].offsetHeight;
					}else{
						var x = p.x - 1;
						var y = p.y + ep[i].offsetHeight;
					}
					set_coords(e, x, y);
					e.style.display = 'block';
					
					if(is_ie){
						set_ie_menu_css(e);
					}
				}
			}
		}else{
			for(var j = 0; j < ep[i].childNodes.length; j++){
				var e = ep[i].childNodes[j];
				if(e.nodeName.toLowerCase() == 'ul'){
					var p = get_coords(ep[i]);
					var o = parseInt(ep[i].style.paddingLeft) > 20 
							? parseInt(ep[i].style.paddingLeft) 
							: 0;
					if(is_ie && is_major < 7){
						p.w = ep[i].offsetParent.offsetParent.offsetWidth - 6;
						if(!admin_test){
							var x = p.x + o - p.w;
						}else{
							//var x = p.x - 594;
							var x = p.x - 1;
						}
						var y = p.y + ep[i].offsetHeight;
					}else{
						var x = p.x - 1;
						var y = p.y + ep[i].offsetHeight;
					}
					set_coords(e, x, y);
					e.style.display = 'block';
					
					if(is_ie){
						set_ie_menu_css(e);
					}
				}
			}
		}

		//mouseover behavior
		ep[i].onmouseover = function(){
			for(var j = 0; j < this.childNodes.length; j++){
				var e = this.childNodes[j];
				if(e.nodeName.toLowerCase() == 'ul'){
					e.style.visibility = 'visible';
					e.style.zIndex = 30000;
				}
			}
		};
		
		//mouseout behavior
		ep[i].onmouseout = function(){
			for(var j = 0; j < this.childNodes.length; j++){
				var e = this.childNodes[j];
				if(e.nodeName.toLowerCase() == 'ul'){
					e.style.visibility = 'hidden';
					e.style.zIndex = 0;
				}
			}
		};
	}
}

