var autoslide = true;
var autotime = 5000;

$.fn.image = function(src, f, e) {
	var i = new Image();
	var t = $(this);
	$(i).load(function(){t.append($(this));f();}).error(e).attr('src', src);
};

function loadSlide(item) {
	if ( !item.hasClass('active') )
	{
		var slideshow = item.closest('div.gallery');
		var container = slideshow.find('div.image a');

		slideshow.find('div.item').removeClass('active');
		item.closest('div.item').addClass('active');

		var file = container.find('img:first').attr('src').replace(new RegExp("(.*)/.*$"),"$1") + '/' + 
					item.find('img:first').attr('src').replace(new RegExp("^.*/(.*)$"),"$1");

		slideshow.find('div.loading').stop().animate({opacity: 'show'}, {queue: false, duration: 250});

		container.image(file, function(){
			container.find('img:last').hide().fadeIn('slow', function() {
				$(this).prev().remove();

				slideshow.find('div.loading').stop().animate({opacity: 'hide'}, {queue: false, duration: 250});
			});
		});
	}
}

$(function() {

	$('div.gallery').mouseenter(function(){
		$(this).find('div.scrollable').stop().animate({bottom: '0px'}, {queue: false, duration: 250});
	});

	$('div.gallery').mouseleave(function(){
		$(this).find('div.scrollable').stop().animate({bottom: '-52px'}, {queue: false, duration: 500});
	});

	$('div.gallery div.item a').click(function(){
		loadSlide($(this));

		autoslide = false;

		return false;
	});

/*
	$.timer(autotime, function (timer) {
		if ( !autoslide ) 
		{
			timer.stop();
		} else {
			var item = $('div.gallery div.item a.active').next('a');
			if ( !item.length ) { item = $('div.gallery div.item a:first'); }

			loadSlide(item);
		}
	});
*/
});