// standard functions

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

// choose correct style sheet based on platform and browser

var platform = ""
var browser = "";
var browsername = navigator.appName;
var browserversion = parseInt(navigator.appVersion);

if (navigator.platform.indexOf("Mac")!= -1) {
	platform = "mac";
	if (browsername == "Netscape") {
		browser = "ns";
		if (browserversion >= 6) {
			document.write("<link rel='stylesheet' href='http://www.usc.edu/schools/medicine/assets/css/" + platform + "_ie.css' type='text/css'>");
		}
		else {
			document.write("<link rel='stylesheet' href='http://www.usc.edu/schools/medicine/assets/css/" + platform + "_" + browser + ".css' type='text/css'>");
		}
	}
	else if (browsername == "Microsoft Internet Explorer") {
		browser = "ie";
		document.write("<link rel='stylesheet' href='http://www.usc.edu/schools/medicine/assets/css/" + platform + "_" + browser + ".css' type='text/css'>");
	}
}
else if (navigator.platform.indexOf("Win")!= -1) {
	platform = "win";
	if (browsername == "Netscape") {
		browser = "ns";
		if (browserversion >= 6) {
			document.write("<link rel='stylesheet' href='http://www.usc.edu/schools/medicine/assets/css/" + platform + "_ie.css' type='text/css'>");
		}
		else {
			document.write("<link rel='stylesheet' href='http://www.usc.edu/schools/medicine/assets/css/" + platform + "_" + browser + ".css' type='text/css'>");
		}
	}
	else if (browsername == "Microsoft Internet Explorer") {
		browser = "ie";
		document.write("<link rel='stylesheet' href='http://www.usc.edu/schools/medicine/assets/css/" + platform + "_" + browser + ".css' type='text/css'>");
	}
}

//window.alert("<link rel='stylesheet' href='http://www.usc.edu/schools/medicine/assets/css/" + platform + "_" + browser + ".css' type='text/css'>");


// example1: detect browser type and version
//if (browsername == "Netscape")
//{
//	browser = "ns" + browserversion;
//}
//else {
//	if (browsername == "Microsoft Internet Explorer")
//	{
//		if (browserversion >= 4)
//		{
//			browser = "ie" + browserversion;
//		}
//		else
//		{
//			browser = "ie3";
//		}
//	}
//}

// example2: choose correct style sheet based on browser
//if (navigator.appVersion.indexOf("MSIE 4.5; Macintosh") != -1) { top.location.href = linkRef + "alerts/macie45info.asp"; } 
//if (navigator.appVersion.indexOf("Mac")!= -1) { // Mac Version 4 Browsers
//	if ((navigator.appVersion.indexOf("5.")== -1) && (navigator.appVersion.indexOf("6.")== -1)) {
//		document.write("<link rel='stylesheet' href='" + linkRef + "assets/mac.css' type='text/css'>");
//	}
//} else if (navigator.appName == "Netscape") { // Win NS  & Mac NS 6
//	if ((parseInt(navigator.appVersion)> 4) || (navigator.appVersion.indexOf("Mac")== -1)) { 
//		document.write("<link rel='stylesheet' href='" + linkRef + "assets/ns.css' type='text/css'>");
//	}
//}

// initialize page

function InitPage() { //hides nav layers and preloads nav images
resetMenus();
MM_showHideLayers('school','','hide','education','','hide','research','','hide','patientcare','','hide');
MM_preloadImages('http://www.usc.edu/schools/medicine/assets/img/menu/school_active_01.gif','http://www.usc.edu/schools/medicine/assets/img/menu/school_active_02.gif','http://www.usc.edu/schools/medicine/assets/img/menu/school_active_03.gif','http://www.usc.edu/schools/medicine/assets/img/menu/school_active_04.gif','http://www.usc.edu/schools/medicine/assets/img/menu/school_active_05.gif','http://www.usc.edu/schools/medicine/assets/img/menu/school_active_06.gif','http://www.usc.edu/schools/medicine/assets/img/menu/education_active_01.gif','http://www.usc.edu/schools/medicine/assets/img/menu/education_active_02.gif','http://www.usc.edu/schools/medicine/assets/img/menu/education_active_03.gif','http://www.usc.edu/schools/medicine/assets/img/menu/education_active_04.gif','http://www.usc.edu/schools/medicine/assets/img/menu/education_active_05.gif','http://www.usc.edu/schools/medicine/assets/img/menu/education_active_06.gif','http://www.usc.edu/schools/medicine/assets/img/menu/research_active_01.gif',
'http://www.usc.edu/schools/medicine/assets/img/menu/research_active_02.gif','http://www.usc.edu/schools/medicine/assets/img/menu/research_active_03.gif','http://www.usc.edu/schools/medicine/assets/img/menu/research_active_04.gif','http://www.usc.edu/schools/medicine/assets/img/menu/research_active_05.gif','http://www.usc.edu/schools/medicine/assets/img/menu/research_active_06.gif','http://www.usc.edu/schools/medicine/assets/img/menu/patientcare_active_01.gif','http://www.usc.edu/schools/medicine/assets/img/menu/patientcare_active_02.gif','http://www.usc.edu/schools/medicine/assets/img/menu/patientcare_active_03.gif','http://www.usc.edu/schools/medicine/assets/img/menu/patientcare_active_04.gif','http://www.usc.edu/schools/medicine/assets/img/education_ro.gif','http://www.usc.edu/schools/medicine/assets/img/research_ro.gif','http://www.usc.edu/schools/medicine/assets/img/school_ro.gif','http://www.usc.edu/schools/medicine/assets/img/patientcare_ro.gif')
}

// open printable version

function openPrintableVersion() {
var printableVersionURL;
printableVersionURL = ("http://www.usc.edu/schools/medicine/util/print/index.php?path="+document.location.pathname);
window.open(printableVersionURL,"","width=570px,resizable=yes,scrollbars=auto,toolbar=no,location=no,status=no");
}

// manage drop-down menus and auto-highlight section title

var menuActive = 0;
var lastDiv;
var timeOn = null; // LAYER SWITCHING CODE

var globalLayerName;
var globalImageName;
var globalImagePath;

var currentFolder;

function toggleMenu(layerName, task, imageName, imagePath) { 
    //this is so the divs still show when currentFolder is undefined
    	  
    if (currentFolder == null) {
    	thisPage = "";
    } else {
    	thisPage = currentFolder;
    }
    
    //if (thisPage != undefined) {	
    	if (task == "show") {
    		//leave the active image for this folder alone
    		if (thisPage != imageName.substr(0,thisPage.length)) {
			obj=MM_findObj(imageName); //get a browser compatible reference to the object
			obj.src = imagePath;       //change the image
    	    }
    	    menuActive = 1;
    	    //clearTimeout(timeOn);
    	    popupMenu(layerName, task);
    	} else {
    	    //set the timer to turn it off        
    	    menuActive = 0;
    	    
    	    globalLayerName = layerName; 
    	    globalImageName = imageName;
    	    globalImagePath = imagePath; 
    	    
    	    lastDiv = globalLayerName;    	    
    	    timeOn = setTimeout("hideDiv()", 400);   
    	}
    //}
}

function resetMenus() {
	//////////////////////////////////////////////////////////////////////////////
	//the arrays below correspond to folders and their activated menu images
	//////////////////////////////////////////////////////////////////////////////
	var menuItems = 	new Array('school', 'research', 'patientcare', 'education');
	var menuObjects = 	new Array('school_nav', 'research_nav', 'patientcare_nav', 'education_nav');
	var menuInactive = 	new Array('school.gif', 'research.gif', 'patientcare.gif', 'education.gif');
	var menuActive = 	new Array('school_ro.gif', 'research_ro.gif', 'patientcare_ro.gif', 'education_ro.gif'); 

	thisPage = currentFolder;
	
	var path = "http://www.usc.edu/schools/medicine/assets/img/";	
	var obj;
	//reset them all

		for (var i = 0; i < menuItems.length; i++) {
			obj = MM_findObj(menuObjects[i]);			
			//////////////////////////////////////////////////////
			//activate the page you are on, turn off other images
			//////////////////////////////////////////////////////
			if (thisPage != null) {
				if (thisPage == menuItems[i]) {
					obj.src = path + menuActive[i];
				} else {
					obj.src = path + menuInactive[i];
				}
			} else {
				obj.src = path + menuInactive[i];
			}
		}
	
}

function hideDiv() {
    thisPage = currentFolder;
    if (currentFolder == null) {
    	thisPage = "";
    } else {
    	thisPage = currentFolder;
    }   	    

    //if (thisPage != "") {
	    var layerName = globalLayerName;    //because my browser cant handle setTimeOut when funciton has parameters
	    if (menuActive == 0) {
	    	 //leave the active image for this folder alone
	    	if (thisPage != globalImageName.substr(0,thisPage.length)) {
	    		obj = MM_findObj(globalImageName);
	    		obj.src = globalImagePath; 
	    	}
		 popupMenu(globalLayerName, "hide");   //hide the layer
	        //popupMenu(layerName, "hide");   //hide the layer
	        //alert("Menu is hidden");
	    }        
     //}
}

function popupMenu(layerName, task) {
    var v;
    var obj = MM_findObj(layerName); //get a browser compatible reference to the object
    var path = "http://www.usc.edu/schools/medicine/assets/img/";	
    var thisPage = currentFolder;
    
    ////////////////////////////////////
    //If page is not fully loaded,
    //I.E. SOMETIMES loses the handle to the obj below
    //causing an error
    ////////////////////////////////////
    
    v = task;
    if (obj.style) {
        obj = obj.style;
        v=(v=='show')?'visible':(v='hide')?'hidden':v;  
    }
    
    obj.visibility=v;

    
    if (task == "show" && (globalLayerName != null)) {
    	if (globalLayerName != layerName) {
	        menuActive = 0;
	        clearTimeout(timeOn);
	      
	        ////////////////////////////////////////////
	        //turn off the last image
	        ////////////////////////////////////////////
	        //if (document.locate.folder.value != null) 
	        if (thisPage != null) {
	        	//if (document.locate.folder.value != globalLayerName) 
			if (thisPage != globalLayerName) {
				obj = MM_findObj(globalLayerName + "_nav");
			        obj.src = path + globalLayerName + ".gif";
			}
			//
		}
				
		if (lastDiv != layerName) popupMenu(lastDiv, "hide"); //turn off the last layer
		menuActive = 1;
	}
   }
}