﻿/// <reference path="jquery-1.5.1.min.js" />

var lefts = 0;

$.fn.imagesLoaded = function (callback) {
    var elems = this.filter('img'), len = elems.length;
    elems.bind('load', function () {
        if (--len <= 0) { callback.call(elems, this); }
    }).each(function () {
        if (this.complete || this.complete === undefined) {
            var src = this.src;
            this.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
            this.src = src;
        }
    }); return this;
};

$(document).ready(function () {


    $('#splashscreen').delay(1000).fadeIn(2000);

    $('#splashscreen').delay(5000).fadeOut(2000);

    applyResize();

    $('.gallerynext').click(function () { $('.galleryprev').show(); galleryScroll(-jQuery('#scrollbar').height() + 130); });
    $('.galleryprev').click(function () { galleryScroll(jQuery('#scrollbar').height() - 130); });
    $('.mainpicture').imagesLoaded(function () {
        $('div#progress').hide();
        $('div#progress2').hide();
    });
    $('.gallerythumb').click(function () {

        var url = $(this).attr('pic');

        $('div#progress').show();
        $('div#progress2').show();
        $(".mainpicture").attr('src', url);

    });

    var images = $('.mainpicture').length;

    if (images == 1)
        return;

    var timer = setInterval(cycleImages, 5000);

    $('.mainpicture').css({ 'opacity': 0.0 });

    cycleImages();
});


var counter = -1;

function cycleImages() {

    var images = $('.mainpicture').length;


    counter++;

    if (counter == images) {
        counter = 0;
    }

    var index = 0;

    $('.mainpicture').each(function () {
        if (counter == index) {
            $(this).animate({ opacity: 1.0 }, 1000, function () { });
        }
        else {
            var currentopacity = $(this).css("opacity");
            if (currentopacity == 1.0) {
                $(this).animate({ opacity: 0.0 }, 1000, function () { });
            }
        }

        index++;

    });
    
} 


$(window).resize(function () {

    applyResize();

});

var galleryScroll = function (movement) {



    var cp = parseInt($('#scrollbar').css('top'));

    cp = cp + movement

    if (cp > 0) {
        $('#scrollbar').animate({ top: 80 }, 1000, function () {
            // Animation complete.
        });
        return;
    }
    var scrollbarh = jQuery('#scrollbar').height();
    var tableHeight = jQuery('#galleryTable').height();

    if (tableHeight + cp > 0) {

        $('#scrollbar').animate({
            top: '+=' + movement
        }, 1000, function () {
            // Animation complete.
        });
    }
}

$('.mainpicture').click(function () {

    $('.mainpicture').stop().fadeTo(3000, 0);

}
);

var applyResize = function () {

    var realw = jQuery(this).width() - lefts;
    var realh = jQuery(this).height();

    $('#scrollbar').css({ height: realh - 0 });
    $('#scrollbar').height(realh - 0);

    var h = realw * 2 / 3;
    var w = realh * 3 / 2;

    if (w < realw) {
        var top = (realh - h) / 2;
        $('.mainpicture').css({ left: lefts + 0, top: top }).width(realw).height(h);
    }
    else {
        var left = (realw - w) / 2;
        
        $('.mainpicture').css({ top: 0, left: left + lefts }).width(w).height(realh);
    }
}
