/* ##### GLOBAL VARS ##### */

//browser check
var IE = (navigator.appName == "Microsoft Internet Explorer");

//current drop down number
var current_num;

//drop down expand
var this_max_h; //max height of current menu
var alpha_counter;
var m_timer; //setTimeOut
var m_counter; //set height 0 - max

//vars used to check for menu exit
var check_timer; //setTimeOut
var check_counter; //counts up to the max
var delay_check_for = 25; //the max 
if(IE) delay_check_for = 20;

/* ##### FUNCTIONS ##### */

function loadLink(url) {	
	window.location = url;
}

function clearRightMenu() {
	var ul_tags = document.getElementsByTagName("ul");
	for(var i = 0; i < ul_tags.length; i++) if(ul_tags[i].className == "gn_rightMenu_ul") ul_tags[i].style.display = "none";
}

function hideMenus() {	
	for(var i = 0; i < dd_menu.length; i++) if(document.getElementById("gn_iframe_" + i)) document.getElementById("gn_iframe_" + i).style.display = "none";
	for(var i = 0; i < dd_menu.length; i++) if(document.getElementById("m_dropdown_" + i)) document.getElementById("m_dropdown_" + i).style.display = "none";
	current_num = null;	
}


function clearRollovers() {			
	var li_tags = document.getElementsByTagName("li");	
	for(var i = 0; i < li_tags.length; i++) if(li_tags[i].className == "gn_menu_li_o") li_tags[i].className = "gn_menu_li";
}

function showMenu(obj, num) {

	if(typeof(dd_menu) == "undefined" || dd_menu.length <= 0) {
		return;
	}
	if(typeof(navABGroup) != "undefined" && navABGroup == "A") {
		return;
	}
	
	//clear old menus
	hideMenus();
	clearRightMenu();
	
	//set current num
	current_num = num;
	
	//javascript data for this menu
	var menu_data = dd_menu[num];
	
	//menu divs
	var m_dropdown = document.getElementById("m_dropdown_" + num);
	var m_header = document.getElementById("m_header_" + current_num);
	var m_links = document.getElementById("m_links_" + current_num);	
	var m_win = document.getElementById("m_win_" + num);
	var m_hide = document.getElementById("m_hide_" + num);
	var m_iframe = document.getElementById("gn_iframe_" + num);
	
	//popuplate menu if empty
	if(m_hide.innerHTML == "") {
		
		var menu_ul = document.createElement('ul');
		menu_ul.className = "gn_menu_ul";
		
		for(var i = 0; i < menu_data.length; i++) {
			
			//set menu li properties
			var menu_li = document.createElement('li');
			menu_li.id = "link_row_" + num + "_" + i;
			menu_li.className = "gn_menu_li";
			menu_li.index = i;
			menu_li.innerHTML = "<a href='" + menu_data[i][0][1] + "'>" + menu_data[i][0][0] + "</a>";
			menu_li.onclick = function() { loadLink(menu_data[this.index][0][1]); };
			menu_li.over = false;
			
			
			//build right menu
			var right_menu = dd_menu[current_num][i];
			
			if(right_menu.length <= 1) {
				
				menu_li.onmouseover = function() { 
					
					clearRollovers();
					this.over = true;
					this.className = "gn_menu_li_o";
					
				}
				
				menu_li.onmouseout = function() { this.over = false; }
			
			}else { //has enough elements for a right menu
				 
				var rm_ul = document.createElement('ul');
				
				menu_li.onmouseover = function() { //show right menu, set offset top
				
					clearRollovers();
					this.over = true;
					this.className = "gn_menu_li_o";
					
					//adjust UL top based on item
					var li_iframe = document.getElementById("rm_iframe_" + this.index);
					//li_iframe.src = "icons/clear.gif";
					
					var ul_tags = this.getElementsByTagName('ul');
					var li_tags = this.getElementsByTagName('li');
					var li_height = li_tags[0].offsetHeight;	
					
					if(this.index > 0 && li_tags.length != 2) {
						ul_tags[0].style.top = (-1 * li_height) + "px";
						li_iframe.style.top = (-1 * li_height) + "px";
					}else if(this.index > 0 && li_tags.length == 4) {
						ul_tags[0].style.top = (-1 * li_height) + "px";
						li_iframe.style.top = (-1 * li_height) + "px";
					}else if(this.index > 0 && li_tags.length > 4) {
						ul_tags[0].style.top = (-2 * li_height) + "px";
						li_iframe.style.top = (-2 * li_height) + "px";
					}
					
					li_iframe.style.height = (ul_tags[0].offsetHeight - 1) + "px";
					li_iframe.style.display = "block";
					
				}
				
				menu_li.onmouseout = function() { 
					this.over = false; 
					document.getElementById("rm_iframe_" + this.index).style.display = "none";
				}
	
				
				for(var j = 1; j < right_menu.length; j++) {
					
					var rm_li = document.createElement('li');
					rm_li.className = "gn_rightMenu_li";
					//rm_li.innerHTML = right_menu[j][0];
					rm_li.innerHTML = "<a href='" + right_menu[j][1]+ "'>" + right_menu[j][0] + "</a>";
					rm_li.over = false;
					rm_li.index = j;
					rm_li.id = "rm_link_" + i;	
					rm_li.onmouseover = function() { 
						this.over = true;
						this.className = "gn_rightMenu_li_o";
					}
					rm_li.onmouseout = function() { 
						this.over = false;
						this.className = "gn_rightMenu_li";
					}
					/*rm_li.onclick = function() { 
						loadLink(right_menu[this.index][1]); 

					}*/
					
					rm_ul.appendChild(rm_li);
					
				}
				
				menu_li.appendChild(rm_ul);
				
				//create iframe back-mask
				var rm_iframe = document.createElement('iframe');
				rm_iframe.className = "gn_rm_iframe";
				rm_iframe.id = "rm_iframe_" + i;
				//rm_iframe.src = "icons/clear.gif";
				rm_iframe.frameBorder = 0;
				menu_li.appendChild(rm_iframe);
				
			}
			
			
			menu_ul.appendChild(menu_li);
			
			
		}
		
		//append menu
		m_hide.appendChild(menu_ul);
		
		//link win
		m_links.over = false;
		m_links.onmouseover = function() { this.over = true; }
		m_links.onmouseout = function() { this.over = false; }
		
		//set header mouse over ...
		m_header.over = false;
		m_header.onmouseover = function() {
			clearRollovers();
			clearRightMenu();
			this.over = true; 
		}
		
		m_header.onmouseout = function() { this.over = false; }
		
	}
	
	//get max height after div is hidden
	m_dropdown.style.visibility = "hidden";
	m_dropdown.style.display = "block";
	m_hide.style.display = "block";	
	this_max_h = document.getElementById("m_hide_" + num).offsetHeight;
	
	//set height to zero and div to visable before expand
	m_win.style.height = "0px";
	m_dropdown.style.visibility = "visible";
	m_dropdown.style.display = "block";
	
	//iframe
	m_iframe.style.display = "block";
	
	//set link div alpha to 0 before expanding
	alpha_counter = 0;
	setAlphaObj(m_links, 0);
	
	//expand menu & fade in
	clearTimeout(m_timer);
	m_counter = 0;
	expandMenu();
	
	//check for mouse out ...
	clearTimeout(check_timer);
	check_counter = 0;
	checkMouseOut();
	
}

function setAlphaObj(obj, opacity) {
	if(IE) {
		obj.style.filter = 'alpha(opacity=' + opacity + ')';
	}else {
		obj.style.opacity = obj.style.MozOpacity = obj.style.KhtmlOpacity = opacity / 100;
	}
}

function expandMenu() {
	
	var m_dropdown = document.getElementById("m_dropdown_" + current_num);
	var m_win = document.getElementById("m_win_" + current_num);
	var m_links = document.getElementById("m_links_" + current_num);	
	var m_iframe = document.getElementById("gn_iframe_" + current_num);
	
	if(m_counter < this_max_h && m_win) {
		
		//fade in
		alpha_counter += 20;
		if(alpha_counter <= 100) setAlphaObj(m_links, alpha_counter);
		
		m_win.style.overflow = "hidden";
		
		m_counter += 70;
		if(m_counter > this_max_h) m_counter = this_max_h;
		m_win.style.height = m_counter + "px";
		m_iframe.style.height = (m_counter+1) + "px";
		
		m_timer = setTimeout("expandMenu()", 32); 
		
	}else if(m_win) {
		
		setAlphaObj(m_links, 100);
		m_links.style.filter = "none"; //take off filter in IE to fix bug...
		m_win.style.overflow = "visible";
		
	}	
}

/* ##### CHECK FOR MOUSE OFF FUNCTION CURRENT MENUS FUNCTION ##### */

function checkMouseOut() {
	
	if(check_counter < delay_check_for) { //delay hide
		
		check_counter++;
		check_timer = setTimeout("checkMouseOut()", 34);
		 
	}else { //run check for if mouse is over any menu item
		
		var menu_header = document.getElementById("m_header_" + current_num);
		if(menu_header) {
			
			var m_links = document.getElementById("m_links_" + current_num);
			var m_hide = document.getElementById("m_hide_" + current_num);
			
			//check if mouse over header
			var mouseover = menu_header.over;
			if(m_links.over == true) mouseover = true;
			
			//check if mouse over links
			var menu_links = m_hide.getElementsByTagName('div');		
			if(menu_links) {
				for(var i = 0; i < menu_links.length; i++) {
					if(menu_links[i].over == true) mouseover = true;
				}
			}
			
			//check if right menu and mouse is over
			if(mouseover == false) {
				var right_menu = document.getElementById("gn_menuRight");
				if(right_menu && right_menu.over == true) mouseover = true;
			}
			
			if(mouseover == true) {
				check_counter = 0;
				check_timer = setTimeout("checkMouseOut()", 34);
			}else {
				hideMenus();	
			}
		}
		
	}

}

