/*------------------------------------------------------------------------
 * set_cookie(name, value, days)
 * 
 * Set a cookie with the name and value passed as the first two arguments, 
 * set to expire in the number of days specified in the third argument.
 *------------------------------------------------------------------------*/

function set_cookie(name, value, days) {
    var expires;

    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toGMTString();
    }
    else 
        expires = "";

    document.cookie = name + "=" + value + expires + "; path=/";
}


/*------------------------------------------------------------------------
 * get_cookie(name)
 * 
 * Returns the value of the cookie identified by the name argument.
 *------------------------------------------------------------------------*/

function get_cookie(name) {
    var namestr  = name + "=";
    var cookbits = document.cookie.split(';');
    var n;

    for(n = 0; n < cookbits.length; n++) {
        var c = cookbits[n];

        /* remove leading whitespace */
        while (c.charAt(0) == ' ') 
            c = c.substring(1, c.length);

        /* if the name start this cookie fragment, return the value */
        if (c.indexOf(namestr) == 0) 
            return c.substring(namestr.length, c.length);
    }
    return null;
}

/*------------------------------------------------------------------------
 * functions to handle multiple handler for onload and onunload events.
 *------------------------------------------------------------------------*/

var onload_functions   = new Array();
var onunload_functions = new Array();

function page_onload(func) {
    onload_functions.push(func);
}

function page_onunload(func) {
    onunload_functions.push(func);
}

function page_load() {
    for(var i = 0; i < onload_functions.length; i++) {
        try { eval(onload_functions[i]); }
        catch(err) { alert(err) }
    }
}

function page_unload() {
    for(var i = 0; i < onunload_functions.length; i++)
        eval(onunload_functions[i]);
}

window.onload   = page_load;
window.onunload = page_unload;

// Retrieve position of object
// Return (top, left) position
function getPosition(obj){
    for (var left=0, top=0;
         obj != null;
         left += obj.offsetLeft, top += obj.offsetTop, obj = obj.offsetParent);
    return {top: top,left: left};
}

// Configure links and headings to show a hints box.
function setHints() {
	var ref = new Array();
	
	// Anchors (links)
   x = document.getElementsByTagName('a');
   for (i=0;i<x.length;i++) {
   	// "nav" links
   	if(x[i].className == 'nav') {
   		x[i].onmouseover = new Function('showHint(this)');
   		x[i].onmouseout = new Function('hideHint()');
   	}
   }
}

function showHintText(text) {
   document.getElementById("hint").innerHTML = text;
   document.getElementById("hint").style.display = 'block';
}

function showHint(obj) {
   x = obj.getElementsByTagName('span');
   var text = '';
   for (i=0;i<x.length;i++) {
      text += x[i].innerHTML;
   }
   document.getElementById("hint").innerHTML = text;
   document.getElementById("hint").style.display = 'block';
   pos = getPosition(obj);
   box = document.getElementById("hint");
   document.getElementById("hint").style.left = pos.left - (box.offsetWidth/2) + (obj.offsetWidth/2);
   document.getElementById("hint").style.top = pos.top + obj.offsetHeight;
}

function hideHint() {
	document.getElementById("hint").style.display = 'none';
}


/*------------------------------------------------------------------------
 * fullscreen_on()
 * fullscreen_off()
 * 
 * Turn fullscreen mode on/off by setting the #body class
 *------------------------------------------------------------------------*/

function fullscreen_on() {
    document.getElementById("body").className = "wide";
    set_cookie("body", "wide", 365);
}

function fullscreen_off() {
    document.getElementById("body").className = "";
    set_cookie("body", "", 365);
}

/*------------------------------------------------------------------------
 * switch_element(node, class)
 * 
 * Toggle the node's className between "open $class" and "shut $class"
 *------------------------------------------------------------------------*/

function switch_element(node, classname) { 
    node.className =
    node.className == 'open ' + classname
                    ? 'shut ' + classname
                    : 'open ' + classname;
    return false;
}

function switch_section(section) { 
    return switch_element(section.parentNode.parentNode, 'section');
}

function switch_subsection(subsect) { 
    return switch_element(subsect.parentNode.parentNode, 'subsection');
}

/*------------------------------------------------------------------------
 * switch_tag_class(root, tag, from, to)
 * 
 * Switch all tag elements under the root from one class to another.
 *------------------------------------------------------------------------*/

function switch_tag_class(root, tag, cfrom, cto) {
    var nodes = root.getElementsByTagName(tag);
    var n, node;
 
    for (n = 0; (node = nodes[n]); n++) {
        if (node.className == cfrom) {
            node.className = cto;
        }
    }
}

page_onload('setHints()');


