// JavaScript Document
// Global Variables
var timerId;  // global timerId for subMenu timer
var timerIdSM; // global timerId when navigating subMenu

var page;  // used to determine which page and therefore which menu button to turn 'on'
var isPopup = 0;  // flag for whether or not popup menu is open

/*** Start variables for popup menu ***/
var popwidth = 200;   // popup width
var Xoffset = 60;
var Yoffset = 20;

// id browsers
var iex = (document.all);
var nav = (document.layers);
var old = (navigator.appName == "Netscape" && !document.layers 
	   && !document.getElementById);
var n_6 = (window.sidebar);
/*** End variables for popup menu ***/

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_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_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];}
}

/*  This function will set a timeout for the popup menu once mouseout */
function menuTimeout () {
	clearTimeout(timerId);
	timerId = setTimeout('kill()', 200);
}

/* Will destroy the timer set by menuTimeOut and then swap the image */
function subMenuSwap (image, loc) {
	clearTimeout(timerId);
	//MM_swapImage(image,'',loc,1);
}

/* Will set a timer to kill the popup menu and also swap with the original image */
function subMenuKill () {
	clearTimeout(timerId);
	timerId = setTimeout('kill()', 200);
	MM_swapImgRestore();
}

// write content & display.  Will display according to which page being shown 
function popup (select) {
  var content = "<table class=submenu border=0 cellspacing=0 cellpadding=0><tr><td";
  isPopup = 1;
  // always clear any timers
  clearTimeout(timerId);
  
  content += " onMouseOver=\"clearTimeout(timerId); this.className='highlight';\"";
  content += " onMouseOut=\"this.className='submenu_link'; subMenuKill()\">";
  content += "<a class='submenu_link' href='research.html'>:: Projects</a>";
  content += "</td></tr>";

  content += "<tr><td";
  content += " onMouseOver=\"clearTimeout(timerId); this.className='highlight';\"";
  content += " onMouseOut=\"this.className='submenu_link'; subMenuKill()\">";
  content += "<a class='submenu_link' href='approaches.html'>:: Approaches</a>";
  content += "</td></tr>";

  content += "<tr><td";
  content += " onMouseOver=\"clearTimeout(timerId); this.className='highlight';\"";
  content += " onMouseOut=\"this.className='submenu_link'; subMenuKill()\">";  
  content += "<a class='submenu_link' href='publications.html'>:: Publications</a>";
  content += "</td></tr>";

  content += "<tr><td";
  content += " onMouseOver=\"clearTimeout(timerId); this.className='highlight';\"";
  content += " onMouseOut=\"this.className='submenu_link'; subMenuKill()\">";
  content += "<a class='submenu_link' href='press.html'>:: Press</a>";
  content += "</td></tr></table>";
  yyy = Yoffset;
  skin.width=popwidth;
  
  	// position of the top left corner of the popup menu
  	var placeholder_x = 0;
	var placeholder_y = 0;
	var current = document.getElementById("research");
	while (current.offsetParent != null) {
  		placeholder_x += current.offsetLeft;
		placeholder_y += current.offsetTop;
		current = current.offsetParent;
	}

	placeholder_x += 325;
	placeholder_y += 20;
	//alert(placeholder_y);
	if (iex) {  // for some reason ie does not recognize the top margin set by the body_table tag in the css
		//placeholder_y += 13;
	}
	
  skin.top = placeholder_y + "px";
  skin.left = placeholder_x +"px";

  if (nav) {
    skin.document.open();
    skin.document.write(content);
    skin.document.close();
    skin.visibility="visible";
  }

  if (iex) {
    pup.innerHTML = content;
    skin.visibility = "visible";
  }

  if (n_6) {
    document.getElementById("pup").innerHTML = content;
    skin.visibility = "visible";
  }
}

// park content box
function kill()
{
  if (!old) {
  	if (isPopup) {
	    // reverting button of parent of popup menu back to off status
		/* NOTE:  this is a simple fix since Research is the only one with a popup menu
		   If there may be multiple popup menus in future, then a check must be necessary 
		   to determine which menu is being killed and therefore which button to revert to
		   off status
		 */
		 if (page != "research") {  // revert button to off status
  			//var x = MM_findObj('RESEARCH');
			//x.src = 'Images/research_btn.gif';
		 }  // otherwise we are on research page so keep research button 'on'
	}
    yyy = -1000;
    skin.visibility = "hidden";
    //skin.width = 0;
	isPopup = 0;
  }
}

/*  This function will display the correct status of the button based on which webpage is presently
	loaded */
function btnStatus () {
	var cur_page = location.pathname;
	if (cur_page.indexOf('index') > 0) {  document.images['HOME'].src = 'Images/home_btn_on.gif';  }
	else if (cur_page.indexOf('people') > 0) {  document.images['PEOPLE'].src = 'Images/people_btn_on.gif'; }
	else if (cur_page.indexOf('research') > 0 || cur_page.indexOf('publications') > 0) { 
		document.images['RESEARCH'].src = 'Images/research_btn_on.gif';
	} 
	else if (cur_page.indexOf('social') > 0) {  document.images['SOCIAL'].src = 'Images/social_btn_on.gif';  }
	else if (cur_page.indexOf('environment') > 0) {  document.images['ENVIRONMENT'].src = 'Images/environment_btn_on.gif';  }
	else if (cur_page.indexOf('positions') > 0) {  document.images['POSITIONS'].src = 'Images/positions_btn_on.gif';  }
}
/*  This function will display information about the pictures in the banner */
function printInfo(which) {
  var content = "";
  if (which == 0) {
	  content += "Umm...not too sure what this is."
  } else if (which == 1) {
	  content += "Christmas colors.";
  } else if (which == 2) {
	  content += "...ask Dan";
  } else if (which == -1) {
	  content += "";
  }
  
  	var placeholder_x = 0;
	var placeholder_y = 0;
	var current = document.getElementById("research");
	while (current.offsetParent != null) {
  		placeholder_x += current.offsetLeft;
		placeholder_y += current.offsetTop;
		current = current.offsetParent;
	}

	placeholder_x += 260;
	placeholder_y -= 110;
  	info_table.width=150;
  	// position of the top left corner of the popup menu
  	info_table.top = placeholder_y + "px";
  	info_table.left = placeholder_x +"px";

  if (nav) {
    info_table.document.open();
    info_table.document.write(content);
    info_table.document.close();
    info_table.visibility="visible";
  }

  if (iex) {
    info.innerHTML = content;
    info_table.visibility = "visible";
  }

  if (n_6) {
    document.getElementById("info").innerHTML = content;
    info_table.visibility = "visible";
  }
}
	
/* This function will kill the description info box on the banner */
function killInfo () {
	if (nav) {
    info_table.document.open();
    info_table.document.write("");
    info_table.document.close();
    info_table.visibility="hidden";
  }

  if (iex) {
    info_table.innerHTML = "";
    info_table.visibility = "hidden";
  }

  if (n_6) {
    document.getElementById("info").innerHTML = "";
    info_table.visibility = "hidden";
  }
}

/* Initializes the timer user to display animation in function animate 
   Will search for div tag using input drawer
*/
function initAnimation(document, drawer) {
	mydiv = document.getElementById(drawer);
	if (mydiv.style.visibility == "visible") {  // close window
		closeWindow(drawer);
		
	} else {
		mydiv.style.visibility = "visible";
		mydiv.style.width = "612px";
		mydiv.style.height = "0px";
		anim_timerID = setTimeout("animate(document,'"+drawer+"')", 40);
	}
}

/*  Animates pulldown to reveal more images */
function animate (document, drawer) {
	mydiv = document.getElementById(drawer);
	// retreive integer values of width and height
	regex = /(\d+)\w*/;
	width_str = mydiv.style.width;
	height_str = mydiv.style.height;
	width_int = width_str.replace(regex, "$1");
	height_int = height_str.replace(regex, "$1");

	if (anim_timerID) {
		clearTimeout(anim_timerID);
		anim_timerID = 0;
	}

	if (height_int == 200) {  // once desired size reached, stop animation
		stopAnimation();
	} else {  // increase size of div
		//mydiv.style.width = parseInt(width_int) + 20 + "px";
		mydiv.style.height = parseInt(height_int) + 20 + "px";
		anim_timerID = setTimeout("animate(document,'"+drawer+"')", 40);
	}
}

/* Stops the animation from function animate */
function stopAnimation() {
	clearTimeout(anim_timerID);
	anim_timerID = 0;
}

/* Hides image window */
function closeWindow(drawer) {
	// change button back to 'Show Images'
	document.getElementById(drawer + '_btn').value = "More Images";

	document.getElementById(drawer).style.visibility = "hidden";
}

/* Loads images into a div tag.  
   Images are selected from input image directory
   Tag is hidden at first */
function loadImg(document, drawer, directory, imgarr) {
	var mydiv = document.getElementById(drawer);

	// change button to 'Hide Images'
	mybtn = document.getElementById(drawer + '_btn');
	mybtn.value = "Hide Images";

	var myhtml = "<table border=0><tr>";
	for (var i = 0; i < imgarr.length; i++) {
		myhtml += "<td class=contact_info>" +
			  "<a href='Images/" + directory + 
			  "/" + imgarr[i][1] + "'><img src='Images/" + directory + 
			  "/" + imgarr[i][0] + "'></a><br>" + imgarr[i][2] +
			  "</td>";
	}	
	myhtml += "</tr>";
	myhtml += "<tr><td colspan=" + imgarr.length + " align=center>";
	myhtml += "</td></tr></table>";
	mydiv.innerHTML = myhtml;
}