/* 
///////////////////
J & C 2 Level Tab

version 1.0

To-Do:
* Make version that doesn't require extra code on page
* Make modular version that can transform a set of nested ULs (ul > li > ul > li)

//////////////////

function showSubMenu(masterid, id)
function hidesubmenus(submenuarray)
function instantset(degree)
function gradualfade(cur2) // Handles fade animation for Internet Explorer
function menuReset() // Function to reset submenu items
function initalizetab(tabid)
*/




/* Debug */

	function debug_note(note, priority) {
		if (priority == null) { priority = 1; } // If no priority level is set, set it to 1
		if (priority <= debug_level) { alert(note); } // If priority level is less than debug level, alert
	}
	var debug_level = 0; // Set to 0 to remove debug notices; otherwise, debug notices with priority less than or equal to this variable will display
	
/* End Debug */


var mastertabvar=new Object();
mastertabvar.baseopacity=0;
mastertabvar.browserdetect="";
var t=null;
var highlighting;
var selectedtab, lasthovertab;

function initalizetab(tabid){	
debug_note('InitializeTab');
	mastertabvar[tabid]=new Array();
	var menucontainer = document.getElementById(tabid);  // Finds the element with ID matching what was passed through
	
	var menuitems = menucontainer.getElementsByTagName("li"); // Finds list items contained with menucontainer(var) 
	
	for (var i=0; i<menuitems.length; i++){ // Loops through the elements
		if (menuitems[i].className=="selected")	 // If the current element has the class selected
			selectedtab = menuitems[i]; // Sets that element to selectedtab(var)
		
		if (menuitems[i].className=="no-menu selected")	 // If the current element has the class selected
			selectedtab = menuitems[i]; // Sets that element to selectedtab(var)
		
			
		if (menuitems[i].getAttribute("rel")) // If this menu item has a rel attribute . . 
		{ 
			menuitems[i].setAttribute("rev", tabid) // then set a "rev" attribute with the ID of the parent element: tabid(var)
			mastertabvar[tabid][mastertabvar[tabid].length]=menuitems[i].getAttribute("rel") // also, store ids of submenus of tab menu
			
			if (menuitems[i].className=="selected")				
				showSubMenu(tabid, menuitems[i].getAttribute("rel"));			
				
			/*
				menuitems[i].getElementsByTagName("a")[0].onmouseover=function(){
					if(selectedtab) selectedtab.className = "";
					if(lasthovertab) lasthovertab.className = "";
					this.parentNode.className = "selected";
					lasthovertab = this.parentNode;
					showSubMenu(this.parentNode.getAttribute("rev"), this.parentNode.getAttribute("rel"));
					if(t) clearTimeout(t);						
				}
			*/
			
			menuitems[i].onmouseover= function()
			{
				clearTimeout(t);
				if(selectedtab) selectedtab.className = "";
				if(lasthovertab) lasthovertab.className = "";
				this.className = "selected";
				lasthovertab = this;
				showSubMenu(this.getAttribute("rev"), this.getAttribute("rel"));					
			}
				
			/*
				menuitems[i].getElementsByTagName("a")[0].onmouseout=function(){
				resetMenuItems(); //add
				/*if (this.addEventListener){ //commented
					this.addEventListener('mouseout', resetMenuItems, true);
					//alert("mouseout listener added");
					//debugger;
				} else if (this.attachEvent){
					this.attachEvent('onmouseout', resetMenuItems);
					//alert("onmouseout event attached");
				}--/
			}*/
			
			document.getElementById(menuitems[i].getAttribute("rel")).onmouseover=function(){//add 
				clearTimeout(t);
			}
			
			document.getElementById(menuitems[i].getAttribute("rel")).onmouseout=function(){//add 
				resetMenuItems();
			}
			
		} 
		else 
		{
			menuitems[i].onmouseover=function(){
				clearTimeout(t);
				if(selectedtab) selectedtab.className = "";
				if(lasthovertab) lasthovertab.className = "";
				this.className = "selected";
				lasthovertab = this;				
				debug_note("112 hide");	
				hidesubmenus(mastertabvar["maintab"]);
			}
		}
	} // end for

	if (selectedtab == null) { alert('Selected was not set');} else {debug_note('Selected: ' + selectedtab);}


	
	menucontainer.onmouseout=function(e){
		if(!e) e = window.event;		
		var ele = e.srcElement || e.target;		
		if(ele.tagName.toLowerCase() != "div") return;
		resetMenuItems();
	}
}

// Shows the SubMenu
function showSubMenu(masterid, id){
debug_note('ShowSubMenus: masterid = ' + masterid + ', id = ' + id);
	if (typeof highlighting!="undefined")
		clearInterval(highlighting);
		submenuobject=document.getElementById(id);
		//mastertabvar.browserdetect= submenuobject.filters ? "ie" : (typeof submenuobject.style.MozOpacity=="string"? "mozilla" : "");
		mastertabvar.browserdetect= submenuobject.attachEvent ? "ie" : (typeof submenuobject.style.MozOpacity=="string"? "mozilla" : "");
		debug_note("134 hide");
		hidesubmenus(mastertabvar[masterid]);
		submenuobject.style.visibility="visible"; // Makes the active sub-menu visible
		submenuobject.style.display="block"; // Makes the active sub-menu display as a block
		instantset(mastertabvar.baseopacity);
		highlighting=setInterval("gradualfade(submenuobject)", 50);
}


// Resets the menu to the default tab
function menuReset(){
	debug_note("resetting menus");
	/*var allSubMenuItems = $$('div.submenustyle');
	for(var i=0;i<allSubMenuItems.length;i++){
		$$('div.submenustyle')[i].style.visibility="hidden";
		$$('div.submenustyle')[i].style.display="none";
	}*/
	debug_note("151");
	var menuitems = document.getElementById("maintab").getElementsByTagName("li");
	debug_note("153");
	debug_note("menuitems" + menuitems, 2);
	debug_note("155");
	for (var i=0; i<menuitems.length; i++){
		menuitems[i].className = "";
	}
	debug_note("159");
	if (selectedtab == null) { debug_note('Selected Tab variable is null');}
	selectedtab.className = "selected";	
	debug_note("161");
	hidesubmenus(mastertabvar["maintab"]);
	debug_note("163");	
	//debugger;
	initalizetab("maintab");
	debug_note("166");
}

// Hides the submenus, if any by setting CSS { display: none }
function hidesubmenus(submenuarray){
	debug_note('HideSubMenus: submenuarray = ' + submenuarray);
	for (var i=0; i<submenuarray.length; i++)
		document.getElementById(submenuarray[i]).style.display="none"; // Finds other sub-menus and hides them
}


// Calls menuReset() after 1 sec, which will reset the menu after 1 sec
function resetMenuItems() {
	//debug_note("setting timeout to 1sec.");
	clearTimeout(t);
	t=setTimeout('menuReset()', 1000);
	
}



/* Visual Effects */


function instantset(degree){
debug_note('instantset');
	if (mastertabvar.browserdetect=="mozilla"){
		submenuobject.style.MozOpacity=1;
	}
	else if (mastertabvar.browserdetect=="ie"){
		//submenuobject.filters.alpha.opacity=100;
	}
}

function gradualfade(cur2){
debug_note('gradualfade');
	if (mastertabvar.browserdetect=="mozilla" && cur2.style.MozOpacity<1)
		cur2.style.MozOpacity=Math.min(parseFloat(cur2.style.MozOpacity)+0.1, 0.99);
		//clearInterval(highlighting);
	else if (mastertabvar.browserdetect=="ie" && cur2.filters.alpha.opacity<100)
		cur2.filters.alpha.opacity+=10;
		//clearInterval(highlighting);
	else if (typeof highlighting!="undefined") //fading animation over
		clearInterval(highlighting);
}





/*

At this time, this function requires additional configuration in order to recognize navigation items that don't have sub-menus and trigger the reset menu functions.  Here is some sample code, written in jQuery, that identifies list items that don't have submenus (this li items are given a class of no-menu).

	<!-- Additional Navigation Menu Configuration -->
	<script type="text/javascript">
    	$(document).ready ( function () {
    		$('li.no-menu').mouseleave(function () { resetMenuItems(); }); // Triggers nav reset on non-expandable menu items
    		$('li#top-smallbiz, li#top-consumer, li#top-about, li#top-contact').mouseleave(function () { resetMenuItems(); }); // Triggers nav reset when mouse travels above navigation menu
    	});
    </script>
    <!-- End Additional Navigation Menu Configuration-->


*/