//SOURCE: Paul Graves
//http://wiki.script.aculo.us/scriptaculous/show/EffectsTreasureChest

Effect.divSwap = function(element, container) {
    var div = document.getElementById(container);
    var nodeList = div.childNodes;
    var queue = Effect.Queues.get('menuScope');

    if (queue.toArray().length < 1) {
        if (Element.visible(element) == false) {
            for (i=0; i < nodeList.length; i++) {
                if (nodeList.item(i).nodeName == "DIV" && 
                    nodeList.item(i).id != element) {

                    if (Element.visible(nodeList.item(i)) == true) {
                        Effect.SlideUp(nodeList.item(i), {
                            queue : {
                                position :'end',
                                scope    :'menuScope',
                                limit    : 2
                            }
                        });
                    }
                }
            }

            Effect.SlideDown(element, {
                queue : {
                    position : 'end',
                    scope : 'menuScope',
                    limit : 2
                }
            });
        }
    }
}

function highlightElementInSeries(selected, series) {
    for (var elementId in series) {
        if (elementId == selected) {
            $(elementId).setAttribute("src", series[elementId].onImage);
        }
        else {
            $(elementId).setAttribute("src", series[elementId].offImage);
        }
    }
}

function imageSwapOn(element, image) {
    if (!$(element).initialState) {
        $(element).initialState = $(element).getAttribute("src");
    }
    $(element).setAttribute("src", image);
}

function imageSwapOff(element) {
    if ($(element).initialState) {
        $(element).setAttribute("src", $(element).initialState);
    }
}

function setInputValue(elementId, value) {
    if ($(elementId)) {
        $(elementId).value = value;
    }
}

function rotateImageByTimeout(element, imageList, timeout){
    if (imageList.length == 0) return false;

    currentIndex = imageList.indexOf($(element).getAttribute('src'));

    nextIndex = 
        currentIndex == imageList.length - 1 ?
            0 : currentIndex + 1;

    $(element).setAttribute("src", imageList[nextIndex]);

    timeoutString = "rotateImageByTimeout('"+ element + "', " + imageList.toSource() + ", " + timeout + ")";

    setTimeout(timeoutString, timeout);
}
