// JavaScript Document

function prepMenu() { 
//---------------- GET URL VALUES FROM PAGE	--------------- ---------------
	//-------------------------------------------------------------------------
	indexID = document.getElementById('som');
	indexHref = indexID.href.split("/");
	siteLevel = indexHref.length-1;
	//
	URLnum = siteLevel-1;
	myURL = document.URL;
	myHref = myURL.split("/");
	// GET A WORKING ROOT DIRECTORY LEVEL
	rootHref = "";
	for (i = 0; i<=siteLevel-1; i++){
	rootHref = rootHref + indexHref[i]+"/";
	}
	myLevel = myHref.length-1; // remove the .shtml page
	jsLevel= myLevel-siteLevel;
}
	//
function doMenu() { //v3.0
	
	//---------------- CLASS ASSIGNMENT FOR SPECIFIC MENU LINKS ---------------
	//-------------------------------------------------------------------------

 	for (k = 0; k<=jsLevel; k++) 
 		{
		id = 'level'+k;
		idNodes = document.getElementById(id); // get the <ul> menu links
		if (idNodes !== null) {
		nodeNum = idNodes.childNodes.length;
		} else {
			break;
		}
		// check to see if the page is at the root level and if so make the nav menu white
		if (jsLevel == 0) {
			idNodes.className = "navCurrLine";
			}
		// SURF THE <li> tags
		for (n =0 ; n < nodeNum; n++) // get all nodes in level "x"
			{
			nd = idNodes.childNodes[n].childNodes.length;
			if (nd !== 0) // THIS IS AN HREF
				{
					hrefNode = idNodes.childNodes[n].childNodes[0]; // get the href from the node
					nodeHref = hrefNode.href.split("/"); // split the href into a comma separated text
					hLength = nodeHref.length;
					
					// FIND TERMINAL LINKS AND SET THEIR STYLES
					var nodeLast = nodeHref[hLength-1];
					//
					
					if (nodeLast.substr(0, 5) !== "index" && hrefNode.className!=="nav") {	
					 	hrefNode.className = "navTerminal";
						}
					// COMPARE THE PAGE URL (myHref) TO EACH URL (nodeHref) IN THE NODE
					// IF A MATCH IS FOUND (IT BETTER!) THEN PROCEED TO 
					// CHANGE THE STYLE OF THE LINK 
						navPath = parseInt(k + URLnum + 1);
						nextNode = document.getElementById("level"+(jsLevel));
					if (myHref[navPath] == nodeHref[navPath] && myHref[navPath]!=="") 
					// myHref[navPath]!=="" prevents the possiblility of both being equal to nothing
						{
						hrefNode.className = "navSelect"; // HIGHLIGHT THE LINKS IN THE MENU PATH
  						 // CHECK TO SEE IF THE LINK LAST CLICKED IS AN INDEX PAGE
						if ( (k) == (jsLevel - 1)) {	
							
							var page = nodeHref[navPath+1];
							
							if (page.substr(0, 5) == "index") {
									// CHANGE THE CURRENT LINK TO SELECTED
									hrefNode.className = "navSelect";
									// CHANGE THE NEXT LINE WHITE
									if (nextNode !== null) 
										{
										nextNode.className = "navCurrLine";
										}
								} 
							}
						if ( k == jsLevel) {	// the last level
							if (nextNode !== null && nextNode.childNodes.length !== 0 && jsLevel !== 0) {
								// THIS IS THE LAST LEVEL SO TURN OTHER LINKS BACK TO NAV
								nextNode.className = "nav";
								hrefNode.className = "navSelect";
									
								}
							}
							}
						}
					}
					
				}
			}	 					
 
//---------------- COMPARE ARRAYS PROTOTYPE -------------------------------
//-------------------------------------------------------------------------

Array.prototype.compareArrays = function(arr) {
    if (this.length != arr.length) return false;
    for (var i = 0; i < arr.length; i++) {
        if (this[i].compareArrays) { //likely nested array
            if (!this[i].compareArrays(arr[i])) return false;
            else continue;
        }
        if (this[i] != arr[i]) return false;
    }
    return true;
}

//---------------- RANDOM IMAGE GENERATOR ---------------------------------
//-------------------------------------------------------------------------
	
function random_image() { 
//
	//var path = "http://mba2006.som.yale.edu/"
	var path = rootHref; // generated by "getMenus" srcipt
	 image_array = new Array(); 
	 for (i=0; i<15; ++i) {
	 	if (i < 100) {
	 		if (i < 10) 
				{
	 		 		var im =  "00" + i;
	  			} else {
	   				var im =  "0" + i;
	  			}
	  		} else {
	  		var im = i;
	  		}
	 image_array[i] = path+'imager/' + im + '.jpg'; 
	} // for

	count = image_array.length; 
	random = Math.floor(Math.random() * 15); 
	return(image_array[random])
}

// GENERAL SCRIPT THAT TAKES A NUMBER AND A DIRECTORY
// SHOULD REPLACE RAQNDOM_IMAGE IN ALL INSTANCES

function randomPicture(num, dir) { 
//
	//var path = "http://mba2006.som.yale.edu/"
	//var path = rootHref; // generated by "getMenus" srcipt
	 image_array = new Array(); 
	 for (i=0; i<num; ++i) {
	 	if (i < 100) {
	 		if (i < 10) 
				{
	 		 		var im =  "00" + i;
	  			} else {
	   				var im =  "0" + i;
	  			}
	  		} else {
	  		var im = i;
	  		}
	 image_array[i] = dir + im + '.jpg'; 
	} // for

	count = image_array.length; 
	random = Math.floor(Math.random() * count);
	//document.write("random = " + random);
	return(image_array[random])
}

//---------------- IMAGE GALLERY  ---------------------------------
//-------------------------------------------------------------------------
var galleryArray = new Array();
var actualImage=0;


function loadGallery(numImages, dir, wid){ //,  xmlDoc, firstCaption
	
	// if javascript is not enabled, the buttons just don't work

	for (i = 0; i<numImages; i++)
	{
	 	if (i < 100) 
		{
	 		if (i < 10) 
				{
	 		 	var im =  "00" + i;
	  			} else {
	   			var im =  "0" + i;
	  			}
	  	} else {
	  		var im = i;
		}
		//
		var imPath = dir + im + ".jpg";
		Img = new Image();
		Img.src = imPath;
		galleryArray.push(Img);
	}
	
	if (galleryArray.length>0) 
		{
	// random slide appears first -- [ disabled ]
 	//var ranNum = Math.round(Math.random() * (galleryArray.length - 1));
	document.write("<div id=\"randomimage\" align=\"left\" class=\"slideControls\" > <img alt=\"Yale School of Management\" "
				  + "width="+wid+" id=\"slideshowImage\" src=\""+galleryArray[0].src+"\" />");
		}
	document.write("<div id=\"gallerynav\"><div align=\"left\"><a href=\"javascript:change(-1)\">"
				+ "<img border=0 alt=\"Back\" src=\"/images/galleryback.gif\"></a>"
				+ "<a href=\"javascript:change(1)\"><img  border=0 alt=\"Next\" src=\"/images/gallerynext.gif\"></a></div></div>"
 				+ "<div id=\"gallerycaption\"> </div></div>");
	//
	
	
	//xCaption = xmlDoc.getElementsByTagName('caption');
	//document.getElementById("gallerycaption").firstChild.nodeValue = firstCaption;
}

// direction: +1 next, -1 back
function change(direction) {
	//document.write("xCaption.length = "+xCaption.length+"<br>");
  if (galleryArray.length>0) {
    actualImage = actualImage+direction;
    if (actualImage > galleryArray.length-1)  {actualImage=0} else if (actualImage < 0) {actualImage=galleryArray.length-1;}
    document.getElementById("slideshowImage").src = galleryArray[actualImage].src;
	// check to make sure the object exists
	/*
	if (xCaption) {
	theCaption = xCaption[actualImage].childNodes[3].firstChild.nodeValue;
	document.getElementById("gallerycaption").firstChild.nodeValue = theCaption;
	}*/
  }
}


//---------------- STANDARD DREAMWEAVER JS SCRIPTS ---------------------------------
//-------------------------------------------------------------------------

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;
}

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_findObj(n, d) { //v4.01
  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 && d.getElementById) x=d.getElementById(n); return x;
}

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];}
}

     