var Site = function (a) {
        for (opt in a) this.options[opt] = a[opt]
    };
Site.prototype = {
    isTransitionInProgress: false,
    isMusicPlaying: false,
    fMusicPlayer: null,
    nextSwitch: null,
    fMusicPlayerIsLoaded: false,
    options: {
        enableKeyboard: true,
        autoSwitch: true,
        pauseOnPictureFor: 3500,
        transitionSpeed: 800,
        flashPlayerId: "fMusicPlayer"
    },
    init: function () {
        var a = this;
        $(window).bind("resize", this.handleResizing);
        $("#musicbutton").click(function (e) {
            e.preventDefault();
            a.toggleMusic.apply(a, arguments)
        });
        $("#btn_next").click(function () {
            a.nextImage.apply(a, arguments)
        });
        $("#btn_prev").click(function () {
            a.prevImage.apply(a, arguments)
        });
        if ($.browser.mozilla) $("#musicplayer").bind("ended", this.loopFirefox);
        if (this.options.enableKeyboard) {
            var b = ($.browser.mozilla) ? window : document.body;
            $(b).keyup(function (e) {
                switch (e.keyCode) {
                case 39:
                    a.nextImage();
                    break;
                case 37:
                    a.prevImage();
                    break
                }
            })
        }
    },
    loopFirefox: function (e) {
        this.currentTime = 0;
        this.play()
    },
    flashPlayerLoaded: function () {
        this.flashMusicPlayerIsLoaded = true;
        this.isMusicPlaying = true;
        this.fMusicPlayer = $("#" + this.options.flashPlayerId).get(0)
    },
    toggleMusic: function (e) {
        if (!this.flashMusicPlayerIsLoaded) {
            var a = $("#musicplayer").get(0);
            if (a.volume == 1) {
                this.isMusicPlaying = false;
                a.pause();
                a.volume = 0
            } else {
                this.isMusicPlaying = true;
                a.currentTime = 0;
                a.volume = 1;
                a.play()
            }
        } else {
            this.isMusicPlaying = !this.isMusicPlaying;
            this.fMusicPlayer.toggleMusic()
        }
        $("#musicbutton").text((this.isMusicPlaying ? "sound on" : "sound off"))
    },
    nextImage: function (e) {
        if (this.isTransitionInProgress) {
            return
        }
        this.cancelNextScheduledImage();
        var a = $("figure.active");
        var b = (a.next().is("figure") ? a.next() : a.parent().children(":first"));
        this.isTransitionInProgress = true;
        this.switchImages(a, b)
    },
    prevImage: function (e) {
        if (this.isTransitionInProgress) return;
        var a = $("figure.active");
        var b = (a.prev().is("figure") ? a.prev() : a.parent().children(":last"));
        this.isTransitionInProgress = true;
        this.switchImages(a, b)
    },
    switchImages: function (a, b) {
        var c = this;
        var d = b.children("img").get(0).complete;
        if (d) {
            a.stop().fadeOut(c.options.transitionSpeed, function () {
                a.removeClass("active").css("display", "none");
                b.addClass("active");
                c.handleResizing();
                b.hide().fadeIn(c.options.transitionSpeed, function () {
                    c.isTransitionInProgress = false;
                    c.scheduleNextImage()
                })
            });
            return
        }
        $("#loading").hide().fadeIn(c.options.transitionSpeed, function () {
            a.removeClass("active").css("display", "none");
            b.addClass("active");
            if (d) {
                c.handleResizing();
                c.hideLoading()
            } else {
                imageLoaded()
            }
        })
    },
    hideLoading: function () {
        var a = this;
        $("#loading").fadeOut(this.options.transitionSpeed, function () {
            a.isTransitionInProgress = false;
            a.scheduleNextImage()
        })
    },
    cancelNextScheduledImage: function () {
        clearTimeout(this.nextSwitch);
        this.nextSwitch = null
    },
    scheduleNextImage: function () {
        var a = this;
        this.cancelNextScheduledImage();
        if (!this.isTransitionInProgress && this.options.autoSwitch) {
            this.nextSwitch = setTimeout(function () {
                a.nextImage()
            }, this.options.pauseOnPictureFor)
        }
    },
    handleResizing: function () {
        var a = $("figure.active img").css("left", "0");
        var b = $(window).width();
        var c = $(window).height();
        var d = a.width();
        var e = a.height();
        var f = d / e;
        var g = b;
        var h = Math.round(g / f);
        if (h < c) {
            h = c;
            g = Math.round(h * f);
            var i = Math.round((g - b) / 2);
            a.css("left", -i + "px")
        }
        a.width(g);
        a.height(h)
    }
};

function flashIsLoaded() {
    site.flashPlayerLoaded()
}
function imageLoaded() {
    if (!$("figure.active img").get(0).complete) {
        site.isTransitionInProgress = true;
        setTimeout(imageLoaded, 100);
        return
    }
    site.handleResizing();
    site.hideLoading()
}
