var heights = new Array();

function getObj(id)
{
    return document.getElementById(id);
}

function addClass(obj, newClass)
{
    if(!contains(obj.className, newClass)){
        if(obj.className.length > 0){
            obj.className = obj.className + " " + newClass;
        } else {
            obj.className = newClass;
        }
    }
}

function changeBorderColor(obj, color)
{
    obj.style.borderColor = color;
}

function clearField(obj, str)
{
    if(obj.value == str){
        obj.value = '';
    }
}

function contains(subject, str)
{
    var result = false;
    if(subject.toLowerCase().indexOf(str) >= 0){
        result = true;
    }
    return result;
}

function getHeight(id)
{
    var i;
    var height = 0;
    
    for(i = 0; i < heights.length; i++){
        if(heights[i][0] == id){
            height = heights[i][1];
            break;
        }
    }
    
    return height;
}

function hideObj(objId)
{
    getObj(objId).style.display = 'none';
}

function hideObjSubMenu(menuItem, subMenu)
{
    var height = getHeight(subMenu.id);
    
    removeClass(menuItem, 'opened');
    addClass(menuItem, 'closed');
    if(height > 0){
        subMenu.style.overflow = 'hidden';
        subMenu.style.height = height + 'px';
        subMenu.style.display = 'block';
        slideIn(subMenu.id, height - Math.round(height / 10), height);
    } else {
        subMenu.style.display = 'none';
    }
}

function initLinks()
{
  for (i in document.links) {
    link = document.links[i];
    if (link.rel && link.rel.indexOf('external')!=-1) {
      link.onclick = onExternalLinkActivate;
      link.onkeypress = onExternalLinkActivate;
    }
  }
}

function initMenu()
{
    var items = getObj('menu').getElementsByTagName("div");
    var i;
    var temp;
    
    // figure out the size of each sub menu
    for(i = 0; i < items.length; i++){
        if(items[i].className == 'subMenu'){
            temp = items[i].style.display;
            items[i].style.display = 'block';
            heights.push(new Array(items[i].id, items[i].offsetHeight));
            items[i].style.display = temp;
        }
    }
}

function initSite()
{
    initMenu()
    initLinks()
}

function inputField(obj, str)
{
    if(obj.value == ''){
        obj.value = str;
    }
}

function menuClick(item)
{
    var temp;
    
    temp = item.id.split('Menu');
    temp = getObj(temp[0] + 'SubMenu');
    
    if(temp.style.display == 'none'){
        showObjSubMenu(item, temp);
    } else {
        hideObjSubMenu(item, temp);
    }
}

function onExternalLinkActivate()
{
  window.open(this.href);
  return false;
}

function removeClass(obj, strClass)
{
    // if it is not the initial class it will have a space
    // we must check for this first, if not we will leave extra white space
    if(contains(obj.className, " " + strClass))
    {
        var newClass = strRemove(obj.className, " " + strClass);
        obj.className = newClass;
    }
    // check for our object to have an initial class
    if(contains(obj.className, strClass))
    {
        var newClass = strRemove(obj.className, strClass);
        obj.className = newClass;
    }
}

function showObj(objId)
{
    getObj(objId).style.display = 'block';
}

function showObjSubMenu(menuItem, subMenu)
{
    var height = getHeight(subMenu.id);
    
    removeClass(menuItem, 'closed');
    addClass(menuItem, 'opened');
    if(height > 0){
        subMenu.style.overflow = 'hidden';
        subMenu.style.height = '0px';
        subMenu.style.display = 'block';
        slideOut(subMenu.id, Math.round(height / 10), height);
    } else {
        subMenu.style.display = 'block';
    }
}

function slideIn(objId, current, goal)
{
    var obj = getObj(objId);
    var newCurrent;
    var out;
    
    if(current <= 0){
        obj.style.height = '0px';
        obj.style.overflow = 'hidden';
        obj.style.display = 'none';
    } else {
        obj.style.height = current + 'px';
        newCurrent = current - Math.round(goal / 10);
        setTimeout("slideIn('" + objId + "', " + newCurrent + ", " + goal + ")", 40);
    }
}

function slideOut(objId, current, goal)
{
    var obj = getObj(objId);
    var newCurrent;
    var out;
    
    if(current >= goal){
        obj.style.overflow = 'visible';
        obj.style.display = 'block';
        obj.style.height = 'auto';
    } else {
        obj.style.height = current + 'px';
        newCurrent = current + Math.round(goal / 10);
        setTimeout("slideOut('" + objId + "', " + newCurrent + ", " + goal + ")", 40);
    }
}

function strRemove(s, t)
{
    i = s.indexOf(t);
    r = "";
    if (i == -1){
        return s;
    }
    r += s.substring(0,i) + strRemove(s.substring(i + t.length), t);
    return r;
}

