/* http://simon.incutio.com/archive/2004/05/26/addLoadEvent */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
function getTop( elem ) {
    if( elem.offsetParent ) {
        for( var posY = 0; elem.offsetParent; elem = elem.offsetParent ) {
            posY += elem.offsetTop;
        }
        return posY;
    } else {
        return elem.offsetTop;
    }
}
function extendPage()
{
    var divs = document.getElementsByTagName('div');
    var second = false;
    for (var i = 0; i < divs.length; i++)
    {
        if (divs[i].className.search(/page_body_second/) != -1)
        {
            second = divs[i];
            break;
        }
    }
    if (!second)
    {
        return;
    }
    // get the height of the wrapper
    var wrapper = document.getElementById('wrapper');
    var height = 0;
    height = wrapper.offsetHeight;
    
    var sheight = second.offsetHeight;
    var stop = getTop(second);
    // curpad is the padding-bottom on a page_body_second
    var curpad = 30;
    // we add 29 for the space the footer covers up
    if (sheight + stop + 29 < height)
    {
        //alert('hey ('+sheight+' + '+stop+' + 29 ['+(sheight+stop+29)+'] < '+height+')');
        var goal = (height-stop-31);
        var newp = goal-sheight+curpad;
        //alert('New padding: '+newp);
        second.style.paddingBottom = newp+'px';
        curpad = newp;
    }
    else
    {
        //alert('no ('+sheight+' + '+stop+' + 29 ['+(sheight+stop+29)+'] < '+height+')');
    }
    
    // switch the color after the second page body part, before footer
    // (currently it's padding from #pagecontent, which is white; we
    // want #eeefea)
    var pc = document.getElementById('pagecontent');
    pc.style.paddingBottom = '0px';
    second.style.paddingBottom = (29+curpad)+'px';
}
function searchBoxClear()
{
    var sb = document.getElementById('topsearch_input');
    if (!sb) { return; }
    sb.onfocus = function()
    {
        if (this.value == 'Search Our Site')
        {
            this.value = '';
        }
        else
        {
            this.select();
        }
    }
    var mainsearch = document.getElementById('search_main_q');
    if (mainsearch)
    {
    	sb.value = mainsearch.value;
    }
}
addLoadEvent(extendPage);
addLoadEvent(searchBoxClear);
