// TROY Nav version 0.1b
// Copyright Troy University 2005-2006
// All Rights Reserved

// Declares default style sheet location
var str_dir = str_baseurl + "troynav/";
var menuID = "";

// CSS Class Names
var subclass = "submenu";
var subclass2 = "submenu2";
var str_itemclass = "submenu_item";
var str_linkclass = "submenu_link";
var str_divclass = "subclass";
var subpointer = "";
var array_sections = new Array();
var menu = new Array();
var menuout = new Array();
var prefix = "";
var array_divs = new Array();
var divlayers = new Array();
var int_div = 0;
var prevLayerID = "";
var prevLayer = 0;
var menuLevel = 0;
var menuFlow = '00';
var menuSeq = '01';
var menuCnt = '01';
var int_submenus = 0; 
var int_count1 = 0;
var int_count2 = 0;
var int_sections = 0;
var str_submenus = "";
var blnTrigger = false;
var bln_case1 = 0;
var toold = false;
//var str_browser = navigator.appName;

function initvars(mvars)
{
    cleanArray = new Array();
    int_div = 0;
    int_submenus = 0;             // Number of submenus
    int_count1 = 0;
    int_count2 = 0;
    int_sections = 0;
    str_submenus = "";
	menuLevel = 0;
    bln_case1 = 0;
    menuFlow = '00';
    menuLevel='00';
    menuSeq = '01';
    menuCnt = '01';
    blnTrigger = false;
    prefix = mvars;   
    menu = window[prefix + 'd'];
    array_divs = window[prefix + 'divs'];
    divlayers = window[prefix + 'layers'];
    subpointer = window[prefix + 'pointer'];
    menuout = window[prefix + 'out'];
    build_menus();
    window[prefix + 'divs'] = array_divs;
    window[prefix + 'layers'] = divlayers;
    window[prefix + 'out'] = menuout;
    //Remove menu date from temporary storage
    //and prepare values for the next menu
    prefix = "";
    menu = cleanArray;
    array_section = cleanArray;
    array_divs = cleanArray;
    divlayers = cleanArray;
    menuout = cleanArray;
}

//Calls build_submenus to build all submenus for a menu
function build_menus()
{
    int_submenus = menu.length;
    if (int_submenus != 0)
    {
        // The first while statement cycles through the sub-menus array
        // and the second while statement cycles through sub-menu elements

        while (int_count1 < int_submenus){
        	str_submenus = "";
         	flowAdd(true);
            build_submenus(prefix);     
            menuout[int_count1] = str_submenus;
            int_count1++;
            int_count2 = 0;
            menuSeq = "01";
            menuCnt = '01';
        }
    }
}
function flowAdd(blnOper){
	var cnv_num = 0;
	cnv_num = menuFlow - 0;
	if (blnOper == true){
		cnv_num++;  
	}else{
		cnv_num--;  
	}	
	if (cnv_num < 10){
		var cnv_str = String(cnv_num);
		menuFlow = '0' + cnv_str;
	}else{
		var cnv_str = String(cnv_num);
		menuFlow = cnv_str;   
	}   
}

//Converts a string "number" to an integer, increments the value,
//and converts it back to a two-digit string.
function s_add(snum){
	snum = snum - 0;
    snum++;
	var tmp_cnt = String(snum);
    if (snum < 10){
    	tmp_cnt = "0" + tmp_cnt;	  
    }
	return tmp_cnt;	  
}
//Manipulates the MenuSeq value used to identify DIVs
function sequenceOper(int_oper){
	var s_length = menuSeq.length;
	var temp_seq = "";
	var intSeq = 0;
	var intSeq = "";
	var intPair = 0;
	var strPair = "";
    switch (int_oper){
    	case 1:
			menuCnt = s_add(menuCnt);
			menuSeq += menuCnt;
			break;
		case 2:
			if (s_length > 2){
				s_length = s_length - 2;  
			}			
			for (cnt_str2 = 0; cnt_str2 < s_length; cnt_str2++){
				temp_seq += menuSeq.charAt(cnt_str2);  
			}
			menuSeq = temp_seq;	
			break;
		default:	
			break;
	}
}
//Builds a submenu from array data
function build_submenus()
{
    var divrel = "";
    var str_subid = prefix + 'i' + int_count1;
    divID = str_subid + 'd' + int_count2;
    if (menuLevel > 0){
        str_divclass = subclass2;
        divrel = '<div class="rel">';
    }else{
        str_divclass = subclass;
        dvrel = '';
    }
	if (blnTrigger == true){
	   	int_count2++; 
	   	blnTrigger = false;
	}
    str_submenus += divrel + '<div id="' + prefix + menuFlow + menuSeq + '" class="' + str_divclass + '">';
    array_divs[int_div] = prefix + menuFlow + menuSeq;
    divlayers[int_div] = menuLevel;
    
    int_div++;
    var int_subitems = menu[int_count1].length; // Number of items in a submenu

    while (int_count2 < int_subitems)
        {
        var str_subitemid = prefix + menuFlow + menuSeq + 'sub' + int_count2;
        var str_desc = menu[int_count1][int_count2][0];
        var str_link = menu[int_count1][int_count2][1];
        var str_target = menu[int_count1][int_count2][2];
		var menuHeight = menuLevel + 1;

        switch (menu[int_count1][int_count2][3])
        {
            case "0":

                str_submenus += '<div class="' + str_itemclass + '" id="' + str_subitemid + '"';
                str_submenus += ' onmouseover="highlight(\'' + prefix + menuFlow + menuSeq + '\', 5, \'' + str_subitemid + '\', event)"';
                str_submenus += ' onmouseout="highlight(\'' + prefix + menuFlow + menuSeq + '\', 4, \'' + str_subitemid + '\', event)">';
                str_submenus += '<a href="';
                str_submenus += str_link;
                str_submenus += '" target="' + str_target + '" class="' + str_linkclass + '" id="'+ str_subitemid + 'a">';
                str_submenus += str_desc + '</a></div>';
            break;

            case "1":
				var tmp_cnt = s_add(menuCnt);
				
                str_submenus += '<div class="' + str_itemclass + '" id="' + str_subitemid + '"';
                str_submenus += ' onmouseover="highlight(\'' + prefix + menuFlow + menuSeq + tmp_cnt + '\', 7, \'' + str_subitemid + '\', event)"';
                str_submenus += ' onmouseout="highlight(\'' + prefix + menuFlow + menuSeq + tmp_cnt + '\', 6, \'' + str_subitemid + '\', event)">';
                var str_section = "";
                str_section += '<a href="';
                str_section += str_link;
                str_section += '" target="' + str_target + '" class="' + str_linkclass + '" id="' + str_subitemid + 'a">';
                str_section += '<span class="pointer" id="' + str_subitemid + 'p">' + subpointer + '</span>';
                str_section += str_desc + '</a></div>';
                array_sections[int_sections] = str_section;
                int_sections++;
                menuLevel++;
                blnTrigger = true;
            	sequenceOper(1);
                build_submenus(prefix);             

            break;

            case "2":
                str_submenus += '<div class="' + str_itemclass + '" id="' + str_subitemid + '"';
                str_submenus += ' onmouseover="highlight(\'' + prefix + menuFlow + menuSeq + '\', 5, \'' + str_subitemid + '\', event)"';
                str_submenus += ' onmouseout="highlight(\'0' + prefix + menuFlow + menuSeq + '\', 4, \'' + str_subitemid + '\', event)">';
                str_submenus += '<a href="';
                str_submenus += str_link;
                str_submenus += '" target="' + str_target + '" class="' + str_linkclass + '" id="'+ str_subitemid + 'a">';
                str_submenus += str_desc + '</a></div></div>';
                if (menuLevel > 0){
                	str_submenus += '</div>';  
	                menuLevel--;
    	            int_sections--;
        	        str_submenus += array_sections[int_sections];
                }
				sequenceOper(2);

                break;
// Handles a submenu item that begins a new submenu but is the last submenu in the originating one.                
            case "3":
				var tmp_cnt = s_add(menuCnt);
				
                str_submenus += '<div class="' + str_itemclass + '" id="' + str_subitemid + '"';
                str_submenus += ' onmouseover="highlight(\''+ prefix + menuFlow + menuSeq + tmp_cnt + '\', 7, \'' + str_subitemid + '\', event)"';
                str_submenus += ' onmouseout="highlight(\''+ prefix + menuFlow + menuSeq + tmp_cnt + '\', 6, \'' + str_subitemid + '\', event)">';
                var str_section = "";
                str_section += '<a href="';
                str_section += str_link;
                str_section += '" target="' + str_target + '" class="' + str_linkclass + '" id="' + str_subitemid + 'a">';
                str_section += '<span class="pointer" id="' + str_subitemid + 'p">' + subpointer + '</span>';
                str_section += str_desc + '</a></div></div>';
                if (menuLevel > 0){
                	str_section += '</div>';

                }
                array_sections[int_sections] = str_section;
                int_sections++;
                menuLevel++;
				blnTrigger = true;

                sequenceOper(1);
                build_submenus(prefix);
				if (menuLevel > 0){
					int_sections--;
                	menuLevel--;
	                str_submenus += array_sections[int_sections];   
				}
				sequenceOper(2); 

            break;
            default:
                alert('The menu is not properly defined.');
            break;
        }
                int_count2++;
    }
}
//Browser detection - not implemented at this time
//function set_style()
//{
//  if (str_browser.search(/microsoft/i) == 0)
//    {
//        toold = true;
//        return;
//    }
//}

//Displays a submenu
function show_layer(str_slayer)
{
    document.getElementById(str_slayer).style.visibility = "visible";
}

//Hides a submenu
function hide_layer(str_hlayer)
{
  if (str_hlayer){
    document.getElementById(str_hlayer).style.visibility = "hidden";
  }
}

//Calls hide_layers() to hide all submenus
function hide_all(mID0)
{
  
  
    var changeItem = window[menuID + 'prevItemID'];
    var initState = window[menuID + 'prevCase'];
  
    var hadivs = window[menuID + 'divs'];
    var hacnt = window[menuID + 'layercnt'];

    if ((mID0 != menuID) && (menuID != "")){
        hadivs = window[mID0 + 'divs'];
        hacnt = window[mID0 + 'layercnt'];
        changeItem = window[mID0 + 'prevItemID'];
        initState = window[mID0 + 'prevCase'];
    }
    for (int_cnt4 = 0; int_cnt4 < hacnt; int_cnt4++){
    	hide_layer(hadivs[int_cnt4]);
    }
    
    
    //if (initState != 1){
		if(document.getElementById(changeItem).className == "menu_item_over"){
			document.getElementById(changeItem).className = "menu_item";
			document.getElementById(changeItem + 'a').className = "menu_link";
		}
		if(document.getElementById(changeItem).className == "menu_item_top_over"){
			document.getElementById(changeItem).className = "menu_item_top";
			document.getElementById(changeItem + 'a').className = "menu_link_top";
		}
			if (initState == 3){
				document.getElementById(changeItem + 'p').className = "main_pointer";
			}
	//	}   
}

//Calls hide_layer() to hide all submenus except the current layer
function hide_most(mID1, currLayer)
{
	var hdivs = window[mID1 + 'divs']; //Div Name
	var hcnt = window[mID1 + 'layercnt']; //Number of Divs

	if ((mID1 !== menuID) && (menuID !== "")){
		hdivs = window[mID1 + 'divs'];
		hcnt = window[mID1 + 'layercnt'];	  
	}

	var llength = currLayer.length;
	var hprev = window[mID1 + 'prevLayerID'];
	var hprev_length = hprev.length;


	var mflow = currLayer.charAt(3);
	mflow += currLayer.charAt(4);
	

	for(cnt01 = 0; cnt01 < hcnt; cnt01++){
		var cdiv = hdivs[cnt01];
		var clength = cdiv.length;
		var cflow = cdiv.charAt(3);
		cflow += cdiv.charAt(4);
		var cnt02 = 5;
		var shw_lyr = true;
		var tempLayer = currLayer + "01";
			if(cflow == mflow){
			  	if (cdiv == currLayer){
					shw_lyr = true;
				}else{  
					if(clength < llength){					
						while((cnt02 < clength) && (shw_lyr == true)){
					 		var c_char = cdiv.charAt(cnt02);
							var l_char = currLayer.charAt(cnt02);
							if (c_char != l_char){
							 	shw_lyr = false; 
							 }
							 cnt02++;  
					 	} 	
					}else if (clength == llength){
						shw_lyr = false;					
					}else{
						shw_lyr = false;  
					} 
				}
			}else{
				shw_lyr = false;	
			}
			
			if(shw_lyr == true){
			  		show_layer(cdiv);
			}else{
					hide_layer(cdiv);  
			}		
	}
}
//Starts the timer used for delaying the hiding of a submenu
function start_timer(mID1)
{
    if (window[mID1 + 'timerOn'] == false){
      	//var pItem = window[mID1 + 'prevItemID'];
      	//", " + pItem + 
        var timerDelay = window[mID1 + 'timerMs'];
        window[mID1 + 'timerID'] = setTimeout("hide_all('" + mID1 + "')", timerDelay);
        window[mID1 + 'timerOn'] = true;
    }
}

//Stops the timer used for delaying the hiding of a submenu
function stop_timer(mID2)
{
    if (window[mID2 + 'timerOn']){
        clearTimeout(window[mID2 + 'timerID']);
        window[mID2 + 'timerID'] = null;
        window[mID2 + 'timerOn'] = false;
    }
}

// highlight() changes the cell background color and the appearance
// of the pop-up menu pointer during mouseover and mouseout events
// and makes submenus appear or dissappear
function highlight(str_chgdivid, int_state, str_chgitem, e)
{
    menuID = str_chgitem.charAt(0);
    menuID += str_chgitem.charAt(1);
    menuID += str_chgitem.charAt(2);
    
    prevLayerID = window[menuID + 'prevLayerID'];
    prevCase = window[menuID + 'prevCase'];
    
    prevItemID = window[menuID + 'prevItemID'];
    
	var prev_length = prevLayerID.length;
    
   	var div_length = str_chgdivid.length;
    
    var str_chglink = str_chgitem + 'a';
    var pointerID = str_chgitem + 'p';

	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();

    switch (int_state){
        case 0:

        break;
            
        case 1:
            if(str_chgitem != prevItemID){
            
				if (prevItemID != ""){
					if (prevCase == 3){
					document.getElementById(prevItemID + 'p').className = "main_pointer";
					if(document.getElementById(prevItemID).className == "menu_item_over"){
						document.getElementById(prevItemID).className = "menu_item";
						document.getElementById(prevItemID + 'a').className = "menu_link";
					}
					if(document.getElementById(prevItemID).className == "menu_item_top_over"){
						document.getElementById(prevItemID).className = "menu_item_top";
						document.getElementById(prevItemID + 'a').className = "menu_link_top";
					}
					
					}
            	}
            }
			

        	window[menuID + 'prevItemID'] = str_chgitem;
        	window[menuID + 'prevCase'] = int_state;
            
			hide_all(menuID);

			
        break;
            
        case 2:
			start_timer(menuID);
        break;

        case 3:
            stop_timer(menuID);
        	window[menuID + 'prevItemID'] = str_chgitem;
        	window[menuID + 'prevCase'] = int_state;
			hide_most(menuID, str_chgdivid);

            if(prevItemID.charAt(3) == "i"){
            
				if (prevItemID != ""){
					if(document.getElementById(prevItemID).className == "menu_item_over"){
						document.getElementById(prevItemID).className = "menu_item";
						document.getElementById(prevItemID + 'a').className = "menu_link";
					}
					if(document.getElementById(prevItemID).className == "menu_item_top_over"){
						document.getElementById(prevItemID).className = "menu_item_top";
						document.getElementById(prevItemID + 'a').className = "menu_link_top";
					}
					
					if (prevCase == 3){
					document.getElementById(prevItemID + 'p').className = "main_pointer";
					}
            	}
            }
            
			if (document.getElementById(str_chgitem).className == "menu_item"){
				document.getElementById(str_chgitem).className = "menu_item_over";
            	document.getElementById(str_chglink).className = "menu_link_over";
			}
			if (document.getElementById(str_chgitem).className == "menu_item_top")
			{
				document.getElementById(str_chgitem).className = "menu_item_top_over";
				document.getElementById(str_chglink).className = "menu_link_top_over";
			}
            	document.getElementById(pointerID).className = "main_pointer2";

        break;

        case 4:
            document.getElementById(str_chgitem).className = "submenu_item";
            document.getElementById(str_chglink).className = "submenu_link";
            
            start_timer(menuID);
        break;
                
        case 5:
        	stop_timer(menuID);
            document.getElementById(str_chgitem).className = "submenu_item_over";
            document.getElementById(str_chglink).className = "submenu_link_over";          
            hide_most(menuID, str_chgdivid);
        break;
                
        case 6:
            document.getElementById(str_chgitem).className = "submenu_item";
            document.getElementById(str_chglink).className = "submenu_link";
            document.getElementById(pointerID).className = "pointer";
    		start_timer(menuID);
        break;
                
        case 7:
			stop_timer(menuID);
            document.getElementById(str_chgitem).className = "submenu_item_over";
            document.getElementById(str_chglink).className = "submenu_link_over";
            document.getElementById(pointerID).className = "pointer2";
            hide_most(menuID, str_chgdivid);
        break;
                
        default:
            str_error += "Invalid menu item type specified. Value must be 0, 1, 2, or 3.\n";
        break;
    }
        window[menuID + 'prevLayerID'] = str_chgdivid;
}