﻿var myArray = new Array();
swapClass($(".noscript"), "noscript", "script")

$(document).ready(function() {
    $("#navbar li").each(function(i) {
        myArray[this.id + "_opacity"] = 0;
        myArray[this.id + "_firefliesFrame"] = 0;
        //        myArray[this.id + "_opacityInterval"] = 0;
        myArray[this.id + "_firefliesInterval"] = 0;
    });

    $(".nav_selected").each(function() { startAnim(true, this); });

    $("#navbar li").not(".nav_selected").mouseenter(function() { startAnim(true, this); })
                                .mouseleave(function() { startAnim(false, this); });

    $("div.robutton").mouseenter(function() { swapClass($(this), "robutton", "robutton_over") })
        .mouseleave(function() { swapClass($(this), "robutton_over", "robutton") });

    $("div[id^=tabbutton]").click(function() {
        changeTab($(this), true);
    });

    if ($("div#cyclerslideshow").length > 0) {
        var cycle = $("div#cyclerslideshow").cycle({
            timeout: 10000,
            before: cyclerOnBefore,
            pager: $("div[id^=ssBox]")
        });
    }

    $(".jumpicon").mouseenter(function() {
        $(this).css("position", "relative").css("top", "-2px");
    }).mouseleave(function() {
        $(this).css("position", "").css("top", "");
    });
});
function swapClass(el, oldClass, newClass) {
    el.removeClass(oldClass); el.addClass(newClass);
}
function cyclerOnBefore(curr, next) {
    setTimeout('cyclerChanged("' + $(next).attr("index") + '", "' + $(next).attr("link") + '")', 500);
}
function cyclerChanged(index, link) {
    $("a[id^=ssLink]").attr("href", link);
    cyclerFillBox(index);
}
function cyclerFillBox(index) {
    $("div[id^=ssFill]").stop();
    $("div[id^=ssFill]").css("width", "0px");
    $("div[id=ssFill" + index + "]").css("width", "1px");
    $("div[id=ssFill" + index + "]").animate({ width: '100%' }, 9250);

    //$("div[id^=ssBox]").empty();
    //$("div[id=ssBox" + index + "]").append("<img src='images/timer.gif' />");
}
function changeTab(activebutton, runTabChangedCode) {
    //var contentid = activebutton.attr("contentid");
    //var index = contentid.substring(contentid.indexOf('_')+1);
    var index = activebutton.attr("index");
    $("div[id^=tabcontentbox_]").css("display", "none");
    $("div[id=tabcontentbox_" + index + "]").css("display", "");

    swapClass($("div.tabactive"), "tabactive", "tabinactive");
    swapClass(activebutton, "tabinactive", "tabactive");

    if (runTabChangedCode) {
        eval(activebutton.attr("ontabchanged"));
    }
}
function roundaboutSetIndex(index) {
    $("ul#roundabout").roundabout_animateToChild(index == '1' ? '2' : index == '2' ? '1' : index);
}

function startAnim(start, button) {
    var id = button.id.substring(4);
    $("img", button).attr("src", "/images/button_" + id + (start ? "_over" : "") + ".png?t");
//    clearInterval(myArray[button.id + "_opacityInterval"]);
    if (start) {
        clearInterval(myArray[button.id + "_firefliesInterval"]);
        myArray[button.id + "_firefliesFrame"] = 1;
        myArray[button.id + "_firefliesInterval"] = setInterval("runAnim('" + button.id + "_fireflies', 75, 78, true)", 34);
    }
    //    myArray[button.id + "_opacityInterval"] = setInterval("changeOpacity(" + (start ? "true" : "false") + ", '" + button.id + "')", 15);
    changeOpacity((start ? true : false), button.id);
}
function runAnim(id, frameHeight, stopFrame, repeat, interval, frameValue) {
    myArray[id + "Frame"] += 1;
    if (myArray[id + "Frame"] == stopFrame) {
        if (repeat)
            myArray[id + "Frame"] = 0;
        else
            return clearInterval(myArray[id + "Interval"]);
    }

    var button = document.getElementById(id);
    button.style.backgroundPosition = '0 ' + -((frameHeight * myArray[id + "Frame"])) + 'px';
    //alert(button.style.backgroundPosition);
}
function changeOpacity(over, id) {
    myArray[id + "_opacity"] = (over ? 100 : 0);

/*    myArray[id + "_opacity"] = myArray[id + "_opacity"] + (over ? 5 : -5);

    if (myArray[id + "_opacity"] < 0)
        myArray[id + "_opacity"] = 0;
    else if (myArray[id + "_opacity"] > 100)
        myArray[id + "_opacity"] = 100;
*/
    var opacityValue = myArray[id + "_opacity"];
    var opacitySpan = document.getElementById(id + "_opacity");

    if (typeof opacitySpan.style.opacity == "string")
        opacitySpan.style.opacity = opacityValue / 100;
    else if (opacitySpan.filters.length > 0) {
        opacityValue = over ? 100 : 0;
        opacitySpan.filters[0].opacity = opacityValue;
    }

    opacitySpan.style.display = (opacityValue == 0 ? 'none' : 'block');
    //alert(opacitySpan.style.display);

/*    if (opacityValue <= 0 || opacityValue >= 100) {
        clearInterval(myArray[id + "_opacityInterval"]);
        if (opacityValue <= 0) {
            clearInterval(myArray[id + "_firefliesInterval"]);
        }
    }*/
}