var autoslide = true;
var autotime = 5000;

function loadSlide(item) {
	if ( !item.hasClass('active') )
	{
		scroll(0,0);

		var metadata = item.metadata();

		var slideshow = item.closest('div.slideshow');
		var container = slideshow.find('a.slideshow-image');

		slideshow.find('a.item').removeClass('active');
		item.addClass('active');

		var file = item.find('img:first').attr('src');

		container.image(file, function(){
			slideshow.find('div.slideshow-header h1').text(metadata.title);
			slideshow.find('div.slideshow-action a').attr('href', metadata.url);

			container.attr('href', metadata.url);
			container.find('img:last').hide().fadeIn('slow', function() {
				$(this).prev().remove();
			});
		});
	}
}

$(function() {

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

		autoslide = false;

		return false;
	});

	var tooltip = $('<div class="tooltip"><div class="inner"></div></div>');

	$('div.slideshow a.item').hover(
		function(){
			var metadata = $(this).metadata();
			tooltip.find('div.inner:first').text(metadata.title);

			var offset = $(this).offset();
			offset.top += 45;
			offset.left -= 5;
			tooltip.css(offset);

			$('body').append(tooltip);
		},
		function(){
			tooltip.remove();
		}
	);

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

			loadSlide(item);
		}
	});

});