/**
 * Common scripts for Teide Golf Canaries pages
 *
 * @author  Matt Kynaston <matt@kynx.org>
 * @package tgc
 */

// pixels left wanting by middle column in IE???
// test more - is this 3px per item in menu??
colHeightFudge = 9;
debug = false;
/**
 * Sets the heights of given columns to match that of first argument
 *
 * @usage   setColHeights('main', 'col1' [, 'col2']...)
 *
 * @todo    Mozilla offset height returns sizes of visible area (clientHeight)?
 *
 * @param   string      mainId      id of main content area
 * @param   string                  one or more columns to set heights for
 */
function setColHeights(mainId) {
    if (!/MSIE/.test(navigator.userAgent)) return;
    
    if (!(document.getElementById))
        return;
    
    // get Height of main content area
    if (!document.getElementById(mainId).offsetHeight) 
        return;
    mainHeight = document.getElementById(mainId).offsetHeight;
    if (!mainHeight) return;
    if (debug) alert('mainHeight: ' + mainHeight);

    // loop through other ids passed as arguments finding tallest
    cols = new Array();
    num = 0;
    for (i=1; i<arguments.length; i++) {
        col = document.getElementById(arguments[i]);
        if (!col) continue;
        
        colHeight = col.offsetHeight;
        if (colHeight > mainHeight) {
            mainHeight = colHeight;
        }
        else { 
            cols[num] = col;
            num++;
        }
        if (debug) alert(arguments[i] + ' height: ' + colHeight);
    }
    if (debug) alert('mainHeight: ' + mainHeight);

    // now loop through cols to resize...
    for (i=0; i<cols.length; i++) {
        col = cols[i];

        // find last div and calc existing col height
        children = col.childNodes;
        if (children.length == 0) continue;
        
        colHeight = 0;
        last = false;
        num_children = 0;
        for (j=0; j<children.length; j++) {
            if (children[j].tagName == 'DIV') {
                num_children++;
                last = children[j];
                colHeight += children[j].offsetHeight;
            }

        }
        if (!last) continue;
        
        
        lastHeight = last.offsetHeight;
        newHeight = mainHeight - colHeight + lastHeight - colHeightFudge;
        if (newHeight < lastHeight) continue; 
        
        last.style.height = newHeight + 'px';

        // see if we've got a head and body to resize...
        headHeight = false;
        innerDivs = last.childNodes;
        for (k=0; k<innerDivs.length; k++) {
            div = innerDivs[k];
            if (div.tagName != 'DIV' || !div.className) continue;
            
            className = div.className;
            if (className == 'boxhead') {
                headHeight = div.offsetHeight;
            }
            else if (className == 'boxbody' && headHeight) {
                div.style.height = (newHeight - headHeight) + 'px';
            }
        }
    }
}

/**
 * Stops selecting separators in drop downs
 *
 * A separator has a value that starts with a '-'
 *
 * @param   object    objSelect   select object to check
 */
function checkSeparator(objSelect)
{
    selIndex = objSelect.selectedIndex;
    if (objSelect.options[selIndex].value.indexOf('-') == 0) {
        objSelect.selectedIndex++;
    }
}

