jQuery(function($) {
    function setupPresentation(selector, titleSelector) {
        $(selector).click(function(evt) {
            var $popup = $(this);
            var $prev = $popup.prevAll(selector).eq(0).find("a");
            var $next = $popup.nextAll(selector).eq(0).find("a");

            $.get($popup.find("a").attr("href"), function(responseText) {
                var overlayOptions = {
                    top: 'center',
                    load: true,
                    closeOnClick: false,
                    closeOnEsc: true,
                    fixed: false,
                    onClose: function(evt) {
                        $popup.remove();
                    }
                };

                var popupH1 = $(responseText).find("#wok-main-panel-wrapper h1");
                var popupH2 = $(responseText).find("#wok-main-panel-wrapper h2");
                var popupContent = $(responseText).find("#wok-main-panel").children();
                var $popup = $('<div class="popup"></div>').hide().appendTo("body")
                    .append(popupContent);
                var $navigation = $('<div class="presentation"></div>');
                if ($prev.length) {
                    $('<a class="popup-prev" href="'+$prev.attr("href")+'">«</a>')
                        .click(function() { $prev.click(); return false; })
                        .appendTo($navigation);
                }
                $navigation.append($('<span></span>').text($(titleSelector).text()));
                if ($next.length) {
                    $('<a class="popup-next" href="'+$next.attr("href")+'">»</a>')
                        .click(function() { $next.click(); return false; })
                        .appendTo($navigation);
                }
                if ($next.length || $prev.length)
                    $popup.append($navigation);

                $popup.overlay(overlayOptions);
            });
            return false;
        });
    }

    $(".ellipsis").ellipsize();
    setupPresentation(".popup_true", ".roll h2");

    window.cycleGallery = function() {
        var index = window.cycleGalleryNext;
        var $images = $(".gallery .images");
        var $clone = $images.clone().css("background", "url("+cycleGalleryData[index].img+") center no-repeat")
            .find(".highlight h1").text(cycleGalleryData[index].h1).end()
            .find(".highlight h2").text(cycleGalleryData[index].h2).end();
        $images.before($clone).animate({opacity: 0}, 1000, function() {
            $(this).remove();
        });
        window.cycleGalleryNext = (index + 1) % cycleGalleryData.length;
    };
    window.cycleGalleryNext = 1;

    if (window.location.pathname == "/") {
        window.cycleGalleryData = [];
        $(".gallery .navigation-data").each(function() {
            window.cycleGalleryData.push({
                img: $(this).find("img").attr("src"),
                h1:  $(this).find("h1").text(),
                h2:  $(this).find("h2").text()
            });
        });
        setInterval("cycleGallery()", 5000);
    }

    $("#news-box ul").cycle({ fx: 'scrollLeft'});

});

