﻿var Gallery = {
    images: null,
    thumbnails: null,

    currentIndex: 0,
    fadeDelay: 5000,
    autoDelay: 8000,
    texts: null,
    display: null,

    init: function(displayText) {
        $(document).ready(function() {
            Gallery._init(displayText)
        });
    },

    _init: function(displayText) {
        Gallery.images = $('#gallery .image');
        Gallery.thumbnails = $('#galleryList a');
        Gallery.texts = displayText;
        Gallery.display = $('#imgDesc');

        if (!Gallery.images.size() && !Gallery.thumbnails.size()) return;

        Gallery.images.each(function(index) {
            if (index > 0) {
                $(this).css({ display: 'none' });
            } else {
                $(this).css({ display: 'block' });
            }
        });
        $(Gallery.thumbnails[0]).addClass('active');

        Gallery.thumbnails.each(function(index) {
            $(this).click(function() {
                $('#gallery').stopTime("autoplay");
                $(Gallery.images[Gallery.currentIndex]).fadeOut(Gallery.fadeDelay);
                $(Gallery.thumbnails[Gallery.currentIndex]).removeClass('active');
                Gallery.currentIndex = index;
                $(Gallery.images[Gallery.currentIndex]).fadeIn(Gallery.fadeDelay);
                $(Gallery.thumbnails[Gallery.currentIndex]).addClass('active');
                $(Gallery.display).html(Gallery.texts[Gallery.currentIndex]);
                return false;
            });
        });

        $('#navigate .ulleft li a').each(function(index) {
            $(this).click(function() {
                $('#gallery').stopTime("autoplay");
                if ($(this).attr('rel') == 'play') {
                    $('#gallery').everyTime(Gallery.autoDelay, "autoplay", Gallery.auto);
                }
                return false;
            });
        });

        $('#navigate .ulright li a').each(function(index) {
            $(this).click(function() {
                $('#gallery').stopTime("autoplay");
                Gallery.auto($(this).attr('rel'));
                return false;
            });
        });

        $(Gallery.display).html(Gallery.texts[Gallery.currentIndex]);
        $('#gallery').everyTime(Gallery.autoDelay, "autoplay", Gallery.auto);
    },
    auto: function(action) {
        var nextIndex = Gallery.currentIndex;
        if (action == 'previous') {
            if ((Gallery.currentIndex - 1) < 0)
                return;
            nextIndex--;
        }
        else if (action == 'next') {
            if ((Gallery.currentIndex + 1) == Gallery.images.length)
                return;
            nextIndex++;
        }
        else {
            nextIndex++;
            if (nextIndex == Gallery.images.length)
                nextIndex = 0;
        }
        $(Gallery.images[Gallery.currentIndex]).fadeOut(Gallery.fadeDelay);
        $(Gallery.thumbnails[Gallery.currentIndex]).removeClass('active');
        Gallery.currentIndex = nextIndex;
        $(Gallery.images[Gallery.currentIndex]).fadeIn(Gallery.fadeDelay);
        $(Gallery.thumbnails[Gallery.currentIndex]).addClass('active');
        $(Gallery.display).html(Gallery.texts[Gallery.currentIndex]);
    }
}