/*jslint white: true, browser: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, regexp: true, newcap: true, immed: true */
/*global glow: false, console: false*/

glow.ready(function () {
    var accordion = function (listNodeSpec, itemNodeSpec, tabHeightPx, displacementPx) {
        var listNodeList = glow.dom.get(listNodeSpec);
        var listItems = listNodeList.get(itemNodeSpec);

        var move = function (node, moveUp) {
            var nodeList, anim, positionKey;

            nodeList = glow.dom.get(node);
            positionKey = moveUp ? "upperPosition" : "lowerPosition";
            anim = glow.anim.css(nodeList, 0.5, {
                "bottom": {to: nodeList.data(positionKey)}
            }).start();
            nodeList.data("atTop", moveUp);
        };
        var moveUp = function () {
            move(this, true);
        };
        var moveDown = function () {
            move(this, false);
        };

        var onGalleryNavMouseover = function () {
            var thisNode, thisIndex;

            thisNode = glow.dom.get(this);
            if (!thisNode.hasClass("active")) {
                /* Remove "active" class from all but this element */
                listItems.removeClass("active");
                thisNode.addClass("active");

                thisIndex = thisNode.data("index");
                if (thisNode.data("atTop")) {
                    /* Move node and following siblings down */
                    listItems.slice(thisIndex).each(moveDown);
                }
                else {
                    /* Move preceding siblings up */
                    listItems.slice(0, thisIndex).each(moveUp);
                }
            }
        };

        listNodeList.addClass("accordion");
        listItems.each(function (i) {
            var zIndex = (listItems.length - i);
            var lowerPosition = (zIndex - 1) * tabHeightPx;
            var upperPosition = lowerPosition + displacementPx;
            glow.dom.get(this).data({
                    "lowerPosition": lowerPosition,
                    "upperPosition": upperPosition,
                    "index": i,
                    "atTop": false
                })
                .css({
                    "position": "absolute",
                    "z-index": zIndex,
                    "bottom": lowerPosition + "px"
                });
        });
        glow.events.addListener(listItems, "mouseover", onGalleryNavMouseover);
    };

    var onLoadGallery = function () {
        var mainImageEl = glow.dom.get("#gallery-img"),
            titleEl = glow.dom.get("#gallery-title"),
            metadataEl = glow.dom.get("#gallery-metadata"),
            descriptionEl = glow.dom.get("#gallery-description"),
            replaceGalleryItem = function (event) {
                var el = glow.dom.get(event.item),
                    imgEl = el.get("img")[0];

                mainImageEl.attr({
                    src: imgEl.src.replace("thumbs/", ""),
                    alt: imgEl.alt
                });
                titleEl.text(el.get(".gallery-title").text());
                metadataEl.text(el.get(".gallery-metadata").text());
                descriptionEl.text(el.get(".gallery-description").text());
            },
            handleCarouselClick = mainImageEl.hasClass("static") ?
                function () {} :
                replaceGalleryItem,
            carousel = new glow.widgets.Carousel("#carousel", {
                size: 4,
                animTween: glow.tweens.linear(),
                onItemClick: handleCarouselClick
            });
    };

    var slideshow = function (listNodeSpec, imageNodeSpec, linkNodeSpec) {
        var listNode = glow.dom.get(listNodeSpec);
        var listItems = listNode.get("> li");
        var imageNode = glow.dom.get(imageNodeSpec);
        var linkNode = glow.dom.get(linkNodeSpec);
        var duration = 1.0;
        var intervalMS = 5000;
        var curIndex = 0;
        var images;
        var links;

        var updateLink = function () {
            linkNode[0].href = links[curIndex].href;
            listItems.removeClass("active");
            glow.dom.get(listItems[curIndex]).addClass("active");
        };

        var reset = function () {
            images.css({"opacity": 1.0});
        };

        var step = function () {
            if (curIndex < images.length - 1) {
                glow.anim.fadeOut(images[curIndex], duration).start();
                curIndex += 1;
            }
            else {
                curIndex = 0;
                glow.anim.fadeIn(images[0], duration, {onComplete: reset}).start();
            }
            updateLink();
        };

        /* Exclude list items which contain no image */
        listItems.filter(function () {
            return glow.dom.get(this).get("img").length > 0;
        });

        /* Move images from list into div#featured-gallery */
        links = listItems.get("a");
        images = listItems.get("img");
        images.remove().insertAfter(imageNode);
        /* Remove the static image */
        imageNode.destroy();
        /* Stack the images on top of each other */
        images.each(function (i) {
            var zIndex = (images.length - i);
            glow.dom.get(this).css({
                "position": "absolute",
                "z-index": zIndex
            });
        });

        setInterval(step, intervalMS);
    };

    var bodyID = document.body.id;

    if (bodyID === "homepage") {
        /*accordion("#gallery-nav", "> li", 24, 540);*/
        slideshow("#gallery-nav", "#featured-gallery-img", "#featured-gallery-link");
    }
    else {
        onLoadGallery();
    }
});

