/* Namespace */
var Mays = window.Mays || {};
Mays.gallery = [];
Mays.galleryCnt = 0;
Mays.gallerySpeed = 6000;

(function ($) {
    $(function () {

        if ($('#gallery').length) {
            var gimgs = [];
            $('#gallery span').each(function () {
                Mays.gallery.push($(this).text());
            });
            Mays.gallery.push($('#gallery img').attr('src'));
            if (Mays.gallery.length && Mays.gallery.length > 1) setTimeout("Mays.swapGallery()", Mays.gallerySpeed);
        }

    });

    $(window).bind("load", function () {
        //empty
    });
})(jQuery);

Mays.swapGallery = function (arr) {
    var $divs = $('#gallery .gimg div');
    if ($divs.length == Mays.gallery.length) {
        if (Mays.galleryCnt == 0) Mays.galleryCnt = Mays.gallery.length - 1;
        else Mays.galleryCnt--;
        $divs.eq(Mays.galleryCnt).hide();
        $divs.each(function (el, i) {
            var curz = parseInt($(this).css('z-index'));
            if (curz > 10)
                $(this).css('z-index', (curz - 10))
        });
        $divs.eq(Mays.galleryCnt)
            .css('z-index', (Mays.gallery.length * 10))
            .fadeIn(2000, function () {
                setTimeout("Mays.swapGallery()", Mays.gallerySpeed);
            });
    } else {
        var img = new Image();
        $(img).load(function () {
            Mays.galleryCnt++;
            $('<div />').css({ 'position': 'absolute', 'z-index': (Mays.galleryCnt * 10) })
                .hide()
                .append(img)
                .prependTo('#gallery div.gimg')
                .fadeIn(2000, function () {
                    setTimeout("Mays.swapGallery()", Mays.gallerySpeed);
                });
        }).attr('src', Mays.gallery[Mays.galleryCnt]);
    }
};

$(document).ready(function () {

    $('ul.hide').animate({
        opacity: 1,
        height: 'toggle'
    }, 800, function () {
        // Animation complete.
    });


    //Gallery load
    $(".gallery img.thumb").one("load", function () {
        $(this).fadeIn(1000);
    }).each(function () {
        if (this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6)) {
            $(this).trigger("load");
        }
    });

    //Gallery Animation
    $("img.thumb").click(function () {
        var id = $(this).parent('a').attr("id").substring(1);
        var img = new Image();
        $(img).load(function () {
            $('.main div').empty()
                    .append(img)
                    .hide()
                    .fadeIn(500);
        }).attr('src', $(this).attr('rel'))
            .attr('id', 'i' + id);
        return false;
    });

    $('.main div').click(function () {
        var id = $('.main div img').attr("id").substring(1);
        var $a = $('#a' + id);
        if ($a.next('a.a-thumb').length > 0) {
            $a.next('a.a-thumb').children('img').click();
        }
        else {
            $('.gallery a.a-thumb:first').children('img').click();
        }

        return false;
    });
$('#galnext').click(function () { 
    $('.main div').click();
    return false;
  });
});

function historyBack() {
    history.back();
}

