function getCookie(name)
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    
    for(var i = 0; i < ca.length; i++)
    {
        var c = ca[i];
        while (c.charAt(0) == ' ')
        {
            c = c.substring(1, c.length);
        }

        if (c.indexOf(nameEQ) == 0)
        {
            return c.substring(nameEQ.length,c.length);
        }
    }
    return null;
}

function setCookie(name, value, days)
{
    var cookie = name + "=" + value + ";";

    if (days)
    {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        cookie += " expires=" + date.toGMTString() + ";";
    }
    cookie += " path=/";

    document.cookie = cookie;
}

function rotateFrontPageImage()
{
    if( mainFeaturePics )
    {
        var lastMainFeaturePic = getCookie("lastMainFeaturePic");
        if( lastMainFeaturePic == null )
            thisMainFeaturePic = 0;
        else
            thisMainFeaturePic = (Number(lastMainFeaturePic)+1) % mainFeaturePics.length;

        document.getElementById( "mainFeaturePic" ).src = mainFeaturePics[thisMainFeaturePic];

        setCookie( "lastMainFeaturePic", thisMainFeaturePic );
    }
}

function randomFrontPageImage()
{
    if( mainFeaturePics )
    {
        var thisMainFeaturePic = Math.floor( Math.random() * mainFeaturePics.length );
        document.getElementById( "mainFeaturePic" ).src = mainFeaturePics[thisMainFeaturePic];
    }
}

// This is the IE7 JS hack
function IEHack() {
    if (document.all&&document.getElementById) {
        navRoot = document.getElementById("navigation_list");
        for (i=0; i<navRoot.childNodes.length; i++) {
            node = navRoot.childNodes[i];
            if (node.nodeName=="LI") {
                node.onmouseover=function() {
                    this.className+=" over";
                }
                node.onmouseout=function() {
                    this.className=this.className.replace(" over", "");
                }
            }
        }
    }
}

// Things to do onload
startList = function() {
    IEHack();

    if( document.getElementById("mainFeaturePic") ) {
/*        rotateFrontPageImage();*/
        randomFrontPageImage();
    }
}

window.onload=startList;
