﻿function openWindow(url) {
    if ($("floatingWindow").style.display != "none") {
        closeFloatingWindow(function () { openWindow(url); });
        return;
    }

    var request = new Ajax.Updater(
        "floatingWinContent", url,
        {
            evalScripts: true,
            onComplete: revealWindow
        }
    );
}

function revealWindow(ev) {
    var t = parseInt(document.viewport.getScrollOffsets().top);
    if (t > 100) t -= 70;
    $("floatingWindow").style.top = t + "px";
    $("floatingWindow").style.visibility = "hidden";
    $("floatingWindow").style.display = "block";
    $("floatingWinContent").style.display = "block";
    var h = $("floatingWinContent").getDimensions().height;
    $("floatingWindow").style.height = h + "px";
    $("floatingWinContent").style.display = "none";
    $("floatingWindow").style.display = "none";
    $("floatingWindow").style.visibility = "visible";
    $("floatingWindow").grow({ duration: 1, afterFinish: revealWindowContent, transition: Effect.Transitions.EaseFromTo });
}

function revealWindowContent() {
    $("floatingWinContent").appear({ duration: 0.3 });
}

function closeFloatingWindow() {
    var callback;
    if (arguments.length == 1) callback = arguments[0];
    $("floatingWinContent").fade({ duration: 0.3, afterFinish: function () { Effect.Shrink("floatingWindow", { duration: 0.7, afterFinish: function() {
        if (callback) callback();
    } }); } });
}

var totalSlides = 7;
var currSlide = 1;
var topSlideIdx = 1;

Event.observe(window, "load", initSlideshow);

function initSlideshow() {
    setTimeout("nextSlide()", 1800);
}

function nextSlide() {
    var top, bottom;
    var start = false;

    top = "btsoe_slide_" + topSlideIdx;
    bottom = "btsoe_slide_" + (topSlideIdx == 1 ? 2 : 1);

    $("btsoe" + currSlide).className = "";
    if (++currSlide > totalSlides) { currSlide = 1 };
    $("btsoe" + currSlide).className = "selected";
    
    $(top).style.zIndex = "1";
    $(bottom).style.display = "none";
    $(bottom).style.zIndex = "2";


    Effect.Appear($(bottom), { duration: 0.8, afterFinish: function () { changeSlide(top); } });

}

function changeSlide(imgId) {
    topSlideIdx = topSlideIdx == 1 ? 2 : 1;
    var newImg = new Image();
    newImg.src = "/assets/btsoe_slide_" + currSlide + ".jpg";
    $(imgId).src = newImg.src;
    setTimeout("nextSlide()", 2900);
}
