﻿var moveNextTimer;

// Gallery Initialisation
$(document).ready(function(e) {
$("#GalleryBox img").bind("click", function(e) { clearInterval(moveNextTimer); moveTo($(this)); });
    $("#GalleryBox img").css({ width: 0, height: 0, marginTop: 65, display: "none" }); //general
    $("#GalleryBox img:last").css({ width: 127, height: 117, display: "block" }); // placeholder
    $("#GalleryBox img:last").prev().css({ width: 280, height: 233, marginTop: 0, display: "block" })// last item
    $("#GalleryBox img:last").prev().prev().css({ width: 127, height: 117, display: "block", marginTop: 65 }); // second to last item
    $("#GalleryBox").css("visibility", "visible");
    $("#GalleryBox img:last").prev().addClass("main");
    moveNextTimer = setInterval("moveNext()", 2500);
});


function moveNext() { moveTo($("#GalleryBox img.main").next()); }


function moveTo(obj) {
    if (obj.hasClass("main")) {
        openInNewWindow(obj.attr("rel"));
    };
    if (typeof (obj.next()[0]) == "undefined")  // Has reached the end of the list, need to wrap from the start
        obj.after(obj.siblings().filter(":first"));
    if (typeof (obj.prev()[0]) == "undefined")  // Has reached the start of the list, need to wrap from the end
        obj.before(last = obj.siblings().filter(":last"));

    obj.next().next().animate({ width: 0, height: 0, marginLeft: 0, marginRight: 0, marginTop: 65 }, 500, "swing", function(e) { $(this).css("display", "none").removeClass("main"); })
    obj.next().css("display", "block").animate({ width: 127, height: 117, marginTop: 50, marginLeft: 5, marginRight: 5 }, 500, "swing").removeClass("main")
    obj.css("display", "block").animate({ width: 280, height: 233, marginTop: 0, marginLeft: 5, marginRight: 5 }, 500, "swing").addClass("main")
    obj.prev().css("display", "block").animate({ width: 127, height: 117, marginTop: 50, marginLeft: 5, marginRight: 5 }, 500, "swing").removeClass("main")
    obj.prev().prev().animate({ width: 0, height: 0, marginLeft: 0, marginRight: 0, marginTop: 65 }, 500, "swing", function(e) { $(this).css("display", "none").removeClass("main"); })
}

function openInNewWindow(url) {
    var newWindow = window.open(url, '_blank');
    newWindow.focus();
    return false;
}