var faderLoop = 0;

function initSemlyenHover() {
    if (objExists('navigation')) {
        var i = 0;
        var j = 0;
        var ulMenu = document.getElementById('navigation');
        var liSubs = ulMenu.childNodes;
        for (i = 0; i < liSubs.length; i++) {
            if (liSubs[i].nodeName == 'LI') {
                for (j = 0; j < liSubs[i].childNodes.length; j++) {
                    if (liSubs[i].childNodes[j].nodeName == 'UL') {
                        liSubs[i].onmouseover = ( function(n,m){ return function() {semlyenHoverOver(n,m);} } )(liSubs[i], liSubs[i].childNodes[j])
                        liSubs[i].onmouseout = ( function(n,m){ return function() {semlyenHoverOut(n,m);} } )(liSubs[i], liSubs[i].childNodes[j])
                    }
                }
            }
        }
    }
}

function initTextSlide() {
    if (objExists('postIt')) {
        var i = 0;
        var container = document.getElementById('postIt');
        var children = container.getElementsByTagName('DIV');
        
        if (children.length > 1) {
            for (i = 0; i < children.length; i++) {
                children[i].style.opacity = 0;
                children[i].style.display = 'none';
            }

            if (children.length >= 2) {
                children[0].className = 'c';
                children[0].style.opacity = 1;
                children[0].style.display = 'block';

                setInterval(function(){nextSlides(children)},6000);
            } else {
                children[0].style.opacity = 1;
                children[0].style.display = 'block';
            }

        }
    }
}

function nextSlides(children) {
    var current = 0;

    // Find current
    for (i = 0; i < children.length; i++) {
        if (children[i].className == 'c') {
            current = i;
        }
    }

    faderLoop = setInterval(function(){fadeToNext(children,current)},40);
}

function fadeToNext(children, current) {
    var i = 0;
    var next = 0;
    var o = 0;

    // Find next
    if (current < (children.length - 1)) {
        next = current + 1;
    } else {
        next = 0;
    }

    if (children[current].style.opacity > 0 && children[current].style.display == 'block') {
        // Fade down
        o = parseFloat(children[current].style.opacity);
        o = parseFloat(o - 0.05);
        children[current].style.opacity = o.toFixed(2);
        if (children[current].style.opacity == 0) {
            children[current].style.display = 'none';
            children[next].style.display = 'block';
        }
    } else {
        // Fade up
        o = parseFloat(children[next].style.opacity);
        o = parseFloat(o + 0.05);
        children[next].style.opacity = o.toFixed(2);
        if (parseInt(o) >= 1) {
            children[current].className = '';
            children[next].className = 'c';
            clearInterval(faderLoop);
        }
    }
}



function textSlide(children) {
    var i = 0;
    var first = 0;
    var next = 0;
    var o = 0;
    
    // Find first
    for (i = 0; i < children.length; i++) {
        if (children[i].className == 'c') {
            first = i;
        }
    }

    // Find next
    if (first < (children.length - 1)) {
        next = first + 1;
    } else {
        next = 0;
    }

    if (children[first].style.opacity > 0 && children[first].style.display == 'block') {
        // Fade down
        o = parseFloat(children[first].style.opacity);
        o = parseFloat(o - 0.05);
        children[first].style.opacity = o.toFixed(2);
        if (children[first].style.opacity == 0) {
            children[first].style.display = 'none';
            children[next].style.display = 'block';
        }
    } else {
        // Fade up
        o = parseFloat(children[next].style.opacity);
        o = parseFloat(o + 0.05);
        children[next].style.opacity = o.toFixed(2);
        if (parseInt(o) >= 1) {
            children[first].className = '';
            children[next].className = 'c';
        }
    }
}

function semlyenHoverOver(li, ul) {
    ul.style.display = 'block';
    li.style.backgroundColor = '#A1C130';
}
function semlyenHoverOut(li, ul) {
    ul.style.display = 'none';
    li.style.backgroundColor = '#006A39';
}

function objExists(theVal) {
    if (document.getElementById(theVal) != null) {
        return true;
    } else {
        return false;
    }
}

// This function alters search form submissions to provide GET values as well as POST
function searchAlter() {
    if (document.getElementsByName("query")) {
        var queryFields = document.getElementsByName("query");

        for (var i=0;i<queryFields.length;i++){
            queryFields[i].onkeyup = function () {
                this.form.action = 'http://www.picosearch.com/cgi-bin/ts.pl?qry=' + this.value;
            }
        }
    }

    var siteSearchForm = document.getElementById("siteSearchForm");
    if (siteSearchForm) {
//        siteSearchForm.onsubmit = __utmLinkPost(this);
    }
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function addUnloadEvent(func) {
    var oldonunload = window.onunload;
    if (typeof window.onunload != 'function') {
        window.onunload = func;
    } else {
        window.onunload = function() {
            oldonunload();
            func();
        }
    }
}

addLoadEvent(initSemlyenHover);
addLoadEvent(initTextSlide);
addLoadEvent(searchAlter);