$(document).ready(function() {

	$('#gallery').slideshow({
		timeout: 5000,
		type: 'sequence'
	});

});


$.fn.slideshow = function(options)
{
    var settings = {
        timeout: '2000',
        type: 'sequence'
    }
    if(options)
        $.extend(settings, options);

    this.css('position', 'relative');

    var slides = this.find('img.slide').get();

    for ( var i = 0; i < slides.length; i++ ) {
        $(slides[i]).css('zIndex', slides.length - i).css('position', 'absolute').css('top', '0').css('left', '0');
    }

    $.slideshow.settings   = settings;
    $.slideshow.slides     = slides;
    $.slideshow.current    = 1;
    $.slideshow.last       = 0;
    $.slideshow.go();
};


$.slideshow = function() {}

$.slideshow.settings   = null;
$.slideshow.slides     = null;
$.slideshow.current    = 0;
$.slideshow.last       = 0;
$.slideshow.theTimeout = null;


$.slideshow.go = function ()
{
    $.slideshow.theTimeout = setTimeout((function(){$.slideshow.next();}), $.slideshow.settings.timeout);
}

$.slideshow.go2 = function ()
{
    $.slideshow.theTimeout = setTimeout((function(){$.slideshow.next();}), $.slideshow.settings.timeout / 2);
}

$.slideshow.stop = function ()
{
    if ($.slideshow.theTimeout == null)
        return;
    clearTimeout($.slideshow.theTimeout);
    $.slideshow.theTimeout = null;
}

$.slideshow.next = function()
{
    for (var i = 0; i < $.slideshow.slides.length; i++) {
        $($.slideshow.slides[i]).css('display', 'none');
    }
    $($.slideshow.slides[$.slideshow.last]).css('display', 'block').css('zIndex', '0');
    $($.slideshow.slides[$.slideshow.current]).css('zIndex', '1').fadeIn('slow', function()
    {
        $.ttSetGalleryImageTitle($.slideshow.slides[$.slideshow.last].id);
    });

    if ( ( $.slideshow.current + 1 ) < $.slideshow.slides.length ) {
        $.slideshow.current = $.slideshow.current + 1;
        $.slideshow.last    = $.slideshow.current - 1;
    }
    else {
        $.slideshow.current = 0;
        $.slideshow.last    = $.slideshow.slides.length - 1;
    }

    $.slideshow.go();
}