// cf.js - site-wide JS for the the.cf
//
// Author: attila <attila@stalphonsos.com>
//
// Time-stamp: <2007-06-21 12:27:59 snl@cluefactory.com>
//
////////
//
// The purpose of this JS code is to make the navigation bar
// on the left side of the WebApp web pages feel a little more responsive
// in JS-enabled browsers.  I wanted each <LI>
// element to "light up" when moused over, and I wanted any mouse
// click in the <LI>'s area to have the same effect as clicking
// on the link that it contained.  The code in LiLight() covers
// the first requirement, and the code in LightHook() covers
// the second.  To turn it on just call cf_InstallLightHooks()
// somewhere convenient; make sure to give your anchors IDs that
// start with "link_" in order for them to be picked up.
///////

var GLOBAL_IsMSIE = false;

function Studlify(string) {
    var parts = string.split("-");
    var studly = "";
    for (var i = 0; i < parts.length; i++) {
        if (!i) {
            studly += parts[i];
        } else {
            studly += parts[i].substr(0,1).toUpperCase() + parts[i].substr(1);
        }
    }
    return studly;
}

function DerefElementId(eltId) {
    var elt = null;
    if (typeof(eltId) == 'object') {
        elt = eltId;
    } else if (document.getElementById) {
        elt = document.getElementById(eltId);
    } else if (document.all) {
        elt = document.all[eltId];
    }
    return elt;
}

function SetElementStyle(elt,what,val) {
    elt = DerefElementId(elt);
    if (elt == null)
        return;
    var style = elt;
    if (elt.style) {
        style = elt.style;
    }
    style[what] = val;
    var stud = Studlify(what);
    if (what != stud) {
        style[stud] = val;
    }
}

function FindParent(elt,type) {
    while ((elt.nodeName != 'BODY') && (elt.nodeName != type) && elt.parentNode) {
        elt = elt.parentNode;
    }
    if ((elt != null) && (elt.nodeName != type)) {
        elt = null;
    }
    return elt;
}

function GetStyle(elt) {
    var style = elt;
    if (elt.style) {
        style = elt.style;
    }
    return style;
}

function GetElt(elt) {
    if (typeof(elt) == 'string') {
        elt = DerefElementId(elt);
    }
    return elt;
}

function Dim(elt) {
    //var style = GetStyle(elt);
    SetElementStyle(elt,"background-color","transparent");
    SetElementStyle(elt,"border-color","transparent");
    //style.backgroundColor = 'transparent';
    //style.borderColor = 'transparent';
}

function Light(elt) {
    //var style = GetStyle(elt);
    SetElementStyle(elt,"background-color","#cacaca");
    SetElementStyle(elt,"border-color","black");
    //style.backgroundColor = '#cacaca';
    //style.borderColor = 'black';
}

function LiLight(anchor,state) {
    anchor = GetElt(anchor);
    var orig_anchor = anchor;
    if (anchor == null) {
        return;
    }
    var li = FindParent(anchor,'LI');
    if (li) {
        if (state) {
            Light(li);
        } else {
            Dim(li);
        }
    }
}

function LightHook(elt) {
    var elt = GetElt(elt);
    if (elt) {
        var link = elt;
        elt.onmouseover = function(event) { LiLight(elt,1); };
        elt.onmouseout = function(event) { LiLight(elt,0); };
        elt = FindParent(elt,'LI');
        if (elt) {
            elt.onmouseover = function(event) { LiLight(elt,1); };
            elt.onmouseout = function(event) { LiLight(elt,0); };
            elt.onclick = function(event) { location.href = link.href; };
        }
    }
}

function cf_InstallLightHooks() {
    var interesting = new RegExp("^link_");
    for (var i = 0; i < document.links.length; i++) {
        var link = document.links[i];
        if (("id" in link) && interesting.test(link.id)) {
            LightHook(link);
        }
    }
    for (var i = 0; i < arguments.length; i++) {
        var hook = arguments[i];
        LightHook('link_'+arguments[i]);
    }
}

// IE-only JS fu: invoked from conditional comment, below

var cf_Height_Percent = 0.95;

function cf_GenericInit() {
    var height_px = Math.round(window.innerHeight * cf_Height_Percent);;
    var height = "" + height_px + "px";
    var ih = "" + (height_px-45) + "px";
    SetElementStyle("outside","height",height);
    SetElementStyle("main","height",ih);
    SetElementStyle("sidebar","height",ih);
    SetElementStyle("copyright","height","18px");
    SetElementStyle("copyright","border-color","black");
    window["onresize"] = cf_GenericInit;
}

function cf_MSIEInit() {
    GLOBAL_IsMSIE = true;
    cf_GenericInit();
}

// Non-MSIE browsers invoke this at the end of the page

function cf_NonMSIEInit() {
    cf_GenericInit();
}

// Local variables:
// mode: C
// indent-tabs-mode: nil
// c-basic-offset: 4
// End:
