/*  functions.js
    Copyright the University of Illinois at Urbana-Champaign, 2007

    This file contains miscellaneous javascript functions for use in
    Benjamin Wandelt's website. Feel free to distribute, copy, use, or 
    modify in any way you like.
*/

/*  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    sendMailToContact
    Parameters: netID - netID of the recipient
    Sends an email to a UIUC email account in a way that makes it difficult
    for bots to harvest emails
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  */
function sendMailToContact(netID){
	var end1 = "uiuc";
	var end2 = "edu";

	location.href= "mailto:" + netID + "@" + end1 + "." + end2;
}

/*  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    sendMailToContact3
    Parameters: name - first part of the recipient's email address
                serv - something random to fake out bots that may be a bit
                       more sophisticated
                server - last part of the recipient's email address
    Sends an email to any email account in a way that makes it difficult for
    bots to harvest emails
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  */
function sendMailToContact3(name, serv, server){
	// serv is to fake out bots, so ignore it
	location.href= "mailto:" + name + "@" + server;
}


/*  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    toggleLinks
    Parameters: style - style of the current page
                imgID - ID of the plus/minux picture that we want to flip
                linksID - ID of the link div that we want to display
    Switches the arrow image next to the paper and then toggles the
    visibility of the actual link div.
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  */
function toggleLinks(style, imgID, linksID){
    theImg = document.getElementById(imgID);
    theLinks = document.getElementById(linksID);

    if (style == 'work'){
        arrow = 'images/work/w_arrow.gif';
        arrowup = 'images/work/w_arrowup.gif';
    }
    else {
        arrow = 'images/explore/e_arrow.gif';
        arrowup = 'images/explore/e_arrowup.gif';
    }

    if (theLinks.style.display == 'block'){
        theImg.src = arrow;
        theLinks.style.display = 'none';
    }
    else{
        theImg.src = arrowup;
        theLinks.style.display = 'block';
    }
}


/*  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    findPos
    Parameters: obj - an element in the DOM
    Returns:    an array containing the x and y coordinates of the object
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  */
function findPos(obj) {
    var retvals = new Array(2);
    
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    retvals[0] = curleft;
    retvals[1] = curtop;

    return retvals;
}

/*  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    getHeight
    Parameters: obj - an element in the DOM
    Returns:    an integer containing the height of obj in pixels.
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  */
function getHeight(obj){
    return obj.clientHeight;
}    

/*  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    showItem
    Parameters: id - ID of the object we want to display
    Displays a previously hidden object.
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  */
function showItem(id){
	document.getElementById(id).style.display = 'block';
}

/*  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    hideItem
    Parameters: id - ID of the object we want to hide
    Hides a previously visible object.
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  */
function hideItem(id){
	document.getElementById(id).style.display = 'none';
}

/*  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    initPosition
    Parameters: menuID - ID of the menu div we want to initialize
                elemID - ID of the element under which the menu will rest
    Initializes the position of a menu to be directly below it's associated
    object. Also makes sure that the menu isn't initially displayed. This
    needs to be done for every drop-down menu on the page.
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  */
function initPosition(menuID, elemID){
    menu = document.getElementById(menuID);
    elem = document.getElementById(elemID);

    pos = findPos(elem);
    x = pos[0];
    y = pos[1];

    y = y + getHeight(elem.parentNode);

    menu.style.position = 'absolute';
    menu.style.top = y+'px';
    menu.style.left = x+'px';

    menu.style.display = 'none';
}

/*  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    initMenus
    Add entries here when new drop-down menus are created
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  */
function initMenus(style){
  if (style == 'work'){
    initPosition('researchMenu', 'navResearch');
  }
  
  initPosition('peopleMenu', 'navPeople');
}

