/*
Purpose: generate hypertext links based on the menu tree structure in /INTROexmplmenu.js 
Input: None
Output: hypertext links
*/
function links_gen(){
	//get the menu location for the current page. Sample returned format from getCurrPage: Menu2_2_1 
	var currPage = getCurrPage();
	var currCategory; 
	
	if(currPage != null){
		 currCategory = currPage.split('_');
		 currCategory = currCategory[0];
	} else if((currCategory = getMainCategory()) != null){
		currPage = "";
	} else { 	
		document.write("Page not found and not in any known categories");
		return;
	}

	var numOfSublinks = eval(currCategory+'[3]');

	for(var j = 1; j <= numOfSublinks; j++){
		var sublink = currCategory + "_" + j;
		document.write('<p class="links">');
		gen_sublink(sublink, 0, currPage);
		document.write('</p>');
	}
}

/*
Purpose: generate all submenus rooted by menu link specified as link
Input:
	link: a sublink in menu format
	space: space indentation
	currPage: (menu format)current viewing page link in menu format or empty to print all menus hyperlinked 
Output: generate all submenus rooted by menu link
*/
function gen_sublink(link, space, currPage){
	var textToShow = eval(link+ '[0]');
	var currLink = eval(link+ '[1]');
	var numOfSublinks = eval(link+ '[3]');

	for(var i = 0; i < space; i++)
		document.write('&nbsp;');

	//no more sub links, just print itself
	if(numOfSublinks==0) {
		//Do not hyperlink current page
		if(link == currPage){
			document.write(textToShow + "<br>");
		} else {
			document.write('<a href="'+ currLink + '" class="links">');
			document.write(textToShow);
			document.write('</a><br>');
		}
		return; 
	} else { //there are more sub links, print itself and continue printing its sub links
		if(link == currPage){
			document.write(textToShow + "<br>");
		} else {
			document.write('<a href="'+ currLink + '" class="links">');
			document.write(textToShow);
			document.write('</a><br>');
		} 

		for(var j = 1; j <= numOfSublinks; j++){
			var sublink = link + "_" + j;
			var newSpace = space + 4;
			gen_sublink(sublink, newSpace, currPage);
		}
	}
}


/*

Purpose: get the category current page belongs to from the 6 menu categories

Requirement: Menui_1 (i is from 1 to 6)must exist and its link address must start with one of the main
cateory. For exmaple, /people/... or /academics/....

Input: None

Output: a menu location in menu format

*/

function getMainCategory(){
    //full name of the current page
    var currPageFull = location.href;
	var indexDoubleSlash = currPageFull.indexOf('//');
	var indexFirstSingleSlash = currPageFull.indexOf('/', indexDoubleSlash + 2);
	var currPageDir = currPageFull.substring(indexFirstSingleSlash);
	//another single slash
	var indexSingleSlash = currPageDir.indexOf('/', 1);
	currPageDir = currPageDir.substring(0, indexSingleSlash);

	//document.write("currPageDir: " + currPageDir + "<br>");
	var menu = "Menu";
	var numOfMenuCategory = 7;

	for(var i = 1; i <= numOfMenuCategory; i++){
		var subMenu = menu+i;
		var temp = subMenu +"_"+"1";
//		document.write("Sub menu: " + temp + "<br>");
		var menuLink = eval(temp + '[1]');
//		document.write("menuLink: " + menuLink + "<br>");
		indexSingleSlash = menuLink.indexOf('/', 1);
//		document.write("indexSingleSlash: " + indexSingleSlash + "<br>");
		menuLink = menuLink.substring(0, indexSingleSlash);
//		document.write("menuLink: " + menuLink + "<br>");
		if(menuLink == currPageDir){
			//document.write("Found");
			return subMenu;
		}
	}
	return null;
}



/*
Purpose: get the menu location(specifed in Menu Tree) for the current web page
Input: None
Output: a menu location sample format:Menu2_2_1
*/
function getCurrPage(){
    var currPageFull = location.href; //full name(absolute http address) of the current page
//	document.write("currPage: " + currPageFull + "<br>");
    var indexDoubleSlash = currPageFull.indexOf('//');
//	document.write("DoubeSlash index: " + indexDoubleSlash + "<br>");
    var indexFirstSingleSlash = currPageFull.indexOf('/', indexDoubleSlash + 2); //get the first occurence of single slash after a double slash
//	document.write("First Single Slash index: " + indexFirstSingleSlash + "<br>");
    var currPageDir = currPageFull.substring(indexFirstSingleSlash);//name of sub directory
    if(currPageDir == "/") //for cases like http://mitchell.physics.tamu.edu
        currPageDir = currPageFull;
//	document.write("sub directory: " + currPageDir + "<br>");
    var menu = "Menu";
    var numOfMenuCategory = 7;
    var result = null;

	//does this fix the problem?
	currPageDir = currPageFull;

    for(var i = 1; i <= numOfMenuCategory; i++){
        var subMenu = menu + i;
//        document.write( "subMenu: " + subMenu + "<br>" );
        var numSubMenu = eval(subMenu + '[3]');
        result = get_subMenu(subMenu, currPageDir);
        if(result != null){
//			document.write("RESULT != NULL*******");
//			break;
            return result;
        }

        //document.write("result: " + result + "<br>");
        //if(result != null)
          //  break;
    }

//  document.write("Search result: " + result + "<br>");
    return result;
}

/*
Purpose: get menu location for page specified in currPageDir
Input:
	menu: a given parent menu in menu format. Example:  Menu2_2
	currPageDir: the directory which the page is located or a hypertext link
	Output: all menu's submenus if exist. Sample format: /academics/undergrad_curriculum.html
	Output: a menu location or null if it does not exist
*/
function get_subMenu(menu, currPageDir){
    var numSubMenu = eval(menu + '[3]');
    var menuLink = eval(menu + '[1]');
    var result = null;

//	document.write("In get_subMenu function<br>");
//	document.write("menu: " + menu+ "<br>");
//	document.write("currPageDir: " + currPageDir + "<br>");
//	document.write("menuLink: " + menuLink + "<br><br>");
	//document.write(' '+menu);

    if(currPageDir == menuLink)
        return menu;
    else if(numSubMenu == 0){ //no more sub menus
        return null;
    }
    else{//search all its children menu items 
        for(var i = 1; i <= numSubMenu; i++){
            var subMenu = menu + "_" + i;
            result = get_subMenu(subMenu, currPageDir);
            if(result != null)
                return result;
        }
 
    }

}

/*
// Menu tree
//  MenuX=new Array(Text to show, Link, background image (optional), number of sub elements, height, width);
//  For rollover images set "Text to show" to:  "rollover:Image1.jpg:Image2.jpg"
Menu1=new Array("","/dept/welcome.html","/images/dept.jpg",5,24,125);

    Menu1_1=new Array("Welcome","/dept/welcome.html","",0,20,125);
    Menu1_2=new Array("History","/dept/history.html","",0,20,125);
    Menu1_3=new Array("Faculty Positions","/dept/positions.html","",0,20,125);
    Menu1_4=new Array("Directions","/dept/directions.html","",0,20,125);
    Menu1_5=new Array("Contact","/dept/contact.html","",0,20,125);


Menu2=new Array("","http://physics.tamu.edu/academics/degrees.html","http://physics.tamu.edu/images/acad.jpg",3,24,125);
    Menu2_1=new Array("Degrees","http://physics.tamu.edu/academics/degrees.html","",0,20,125);
    
    Menu2_2=new Array("Undergraduate","http://physics.tamu.edu/academics/undergrads.html","",6,20,125);
        Menu2_2_1=new Array("Curriculum","http://physics.tamu.edu/academics/undergrad_curriculum.html","",0,20,125);
        Menu2_2_2=new Array("Courses","http://physics.tamu.edu/academics/courses/list_courses.php?id=undergrad","",0,20,125);
        //Menu2_2_3=new Array("Admissions","http://physics.tamu.edu/academics/undergrad_admissions.html","",0,20,125);
        Menu2_2_3=new Array("Admissions","http://admissions.tamu.edu/","",0,20,125)
        Menu2_2_4=new Array("Financial Aid","https://financialaid.tamu.edu/","",0,20,125);
        Menu2_2_5=new Array("Contact","http://physics.tamu.edu/academics/undergrad_contact.html","",0,20,125);
        Menu2_2_6=new Array("Minor","http://physics.tamu.edu/academics/Minor.pdf","",0,20,125);

    Menu2_3=new Array("Graduate","http://physics.tamu.edu/academics/grads.html","",5,20,125);
        Menu2_3_1=new Array("Message","http://physics.tamu.edu/academics/headmsg.html","",0,20,125);
        Menu2_3_2=new Array("Courses","http://physics.tamu.edu/academics/courses/list_courses.php?id=grad","",0,20,125);
        Menu2_3_3=new Array("Admissions","http://physics.tamu.edu/academics/grad_admissions.html","",0,20,125);
        Menu2_3_4=new Array("Financial Aid","http://physics.tamu.edu/academics/grad_aid.html","",0,20,125);
        Menu2_3_5=new Array("Contact","http://graduateadvisor.physics.tamu.edu/","",0,20,125);
                                                                                                                    

Menu3=new Array("","/research/research.html","/images/research.jpg",2,24,125);
    Menu3_1=new Array("Research Areas","/research/research.html","",7,20,125);
        Menu3_1_1=new Array("Applied Physics","/research/list-applied.html","",0,20,125);
        Menu3_1_2=new Array("Astronomy","/research/list-astronomy.html","",0,20,125);
        Menu3_1_3=new Array("Atomic Physics","/research/list-atomic.html","",0,20,125);
        Menu3_1_4=new Array("Condensed Matter","/research/list-condensed.html","",0,20,125);
        Menu3_1_5=new Array("High Energy","/research/list-high_energy.html","",0,20,125);
        Menu3_1_6=new Array("Nuclear Physics","/research/list-nuclear.html","",0,20,125);
        Menu3_1_7=new Array("Quantum Optics","/research/list-quantum.html","",0,20,125);

    Menu3_2=new Array("Institutes","","",2);
        Menu3_2_1=new Array("Mitchell Institute","http://mitchell.physics.tamu.edu/","",0,20,125);
        Menu3_2_2=new Array("Cyclotron Institute","http://cyclotron.tamu.edu/","",0,20,125);


Menu4=new Array("","http://physics.tamu.edu/people/showgroup.php?group=faculty","http://physics.tamu.edu/images/people.jpg",7,24,125);
    Menu4_1=new Array("All Groups","http://physics.tamu.edu/people/showgroup.php?group=all","",0,20,125);
    Menu4_2=new Array("Faculty","http://physics.tamu.edu/people/showgroup.php?group=faculty","",0,20,125);
    Menu4_3=new Array("Research Personnel","http://physics.tamu.edu/people/showgroup.php?group=research","",0,20,125);
    Menu4_4=new Array("Staff","http://physics.tamu.edu/people/showgroup.php?group=staff","",0,20,125);
    Menu4_5=new Array("Graduate Students","http://physics.tamu.edu/people/showgroup.php?group=graduate","",0,20,125);
    Menu4_6=new Array("Undergrad Students","http://physics.tamu.edu/people/showgroup.php?group=undergrad","",0,20,125);
    Menu4_7=new Array("Department Contacts","http://physics.tamu.edu/dept/contact.html","",0,20,125);
                                                                                                                

Menu5=new Array("","/calendar/calview.shtml","/images/calendar.jpg",3,24,125);
    Menu5_1=new Array("Calendar","/calendar/calview.shtml","",0,20,125);
    Menu5_2=new Array("Colloquia Schedule","/schedules/seminars_and_colloquia.html","",0,20,125);
    Menu5_3=new Array("Seminars Schedule","/schedules/seminars_and_colloquia.html","",0,20,125);


Menu6=new Array("","","/images/services.jpg",3,24,125);
    Menu6_1=new Array("Computer Support","/services/comp_support.html","",0,20,125);
    Menu6_2=new Array("Electronics Shop","/services/elec_shop.html","",0,20,125);
    Menu6_3=new Array("Machine Shop","/services/mach_shop.html","",0,20,125);

*/

