/**
 * Global JS Functions for the entire site.
 * All sub-categories can have their own javascript files so keep specifics out
 * Jordan Sterling
 */
//String helper functions
String.prototype.startsWith=function(str){return this.substring(0, str.length)==str;};
String.prototype.endsWith=function(str){return this.substring(this.length-str.length)==str;};
String.prototype.contains=function(str){ return this.indexOf(str) != -1; };

/**
 * Givbevn a css class, and the current class list of an element, remove the
 * given class from the current list and return the new class list.
 */
function removeClass(class_to_remove, list_of_classes)
{
    var startOfClassToRemove = list_of_classes.indexOf(class_to_remove);
    if(startOfClassToRemove == -1) //could not find the class to remove
    {
        return list_of_classes;    //so return the original list back again
    }
    //if we could find the class to remove, we just return 2 substrings concatd
    //1. Everything from the start to the index -1, to skip the first letter
    //2. Everything from that index + remove class length until the end.    
    return list_of_classes.substring(0, startOfClassToRemove-1) + list_of_classes.substring(startOfClassToRemove + class_to_remove.length);
}
/**
 * Given a css class, and the current class list of an element, add the given
 * class to the current list and return the new class list.
 */
function addClass(class_to_add, list_of_classes)
{
    var curr_classes = list_of_classes.split(" ");
    for(var existing_class in curr_classes)
    {
        if(existing_class == class_to_add) //the class we are going to add
        {                                  //is already on this elem, give
            return list_of_classes;        //back the original class list
        }
    }
    //We looped through all the classes but didnt find the one we need to add
    //so we are clear to add the class to the list. One special trick:
    //If the list is currently empty, just return the new class. If not, you
    //need to add a space before the new class name.
    return list_of_classes.length === 0 ? class_to_add : list_of_classes + " " + class_to_add;
}
/**
 * For fancy user inputs, on focus you need to remove the original text
 * and add the class "userinputselected". if the class "userinputtype"
 * is found, it should be removed.
 */
function inputfocus(elem, orig)
{
    elem.className = addClass("userinputselected", elem.className);
    elem.className = removeClass("userinputtype", elem.className);
    if(orig == elem.value)
    {
        elem.value = "";
    }
}
/**
 * For fancy user inputs, on blur you need to de-highlight them and if
 * they didnt write anything, add back in the helper text
 */
function inputblur(elem, orig)
{
    elem.className = removeClass("userinputselected", elem.className);
    if(elem.value.length === 0)
    {
        elem.className = removeClass("userinputtype", elem.className);
        elem.value = orig;
    }
    else
    {
        elem.className = addClass("userinputtype", elem.className);
    }
}
/**
 * Performs a search function on a query.
 * This just forwards you to the search results page
 * passing the search text as a GET variable.
 */
function search(query)
{
    window.location = "/info/search?q=" + escape(query).replace(/\s/gi, "+");
}
/**
 * Called whenever someone types a key in the search box on a normal page.
 * If the key was the enter key, it calls search() on whatever is in the box.
 */
function processKey(e)
{
    if (null === e)
    {
        e = window.event;
    }
    else if(e.keyCode == 13) //13 is the enter key
    {
        search(document.getElementById("searchbox").value);
    }
}

function getPrefix(){return document.getElementById("thePrefixOfTheCurrentPage").value;}

/**
 * should be called when ANY page loads every single time.
 * This goes trough each link on a page, and if that link has as one of
 * its css classes "outlink" then it will make that link open in a new
 * window.   
 * 
 * This is a replacement for target="_blank"
 */
function openInNewWindow(link)
{
    window.open(link.href);
}
/**
 * this init() function is called whenever ANY page loads.
 * If any anchor <a> has a class "outlink" it will be assigned
 * an onclick function forcing it to open the link in a new window.
 */
function init()
{
    if (!document.getElementsByTagName) { return false; }
    var links = document.getElementsByTagName("a");
    var max = links.length;
    var i = -1;
    while(++i < max)
    {
        if(links[i].className.contains("outlink"))
        {
            links[i].setAttribute("onclick", links[i].getAttribute("onclick") + "openInNewWindow(this);");
        }
    }
}
