var showcase_widget = {
	counter: -1,
	items: [],
	
	start: function(){
		// Sanity
		if( $('#showcase_details').length < 1 ) return;
		
		// Fetch JSON
		$.ajax( {
			url:'http://www.placenorthwest.co.uk/showcase_widget/ajax.php',
			cache: true,
			dataType: 'json',
			success: function( data, textStatus, jqXHR ){
				showcase_widget.items = data;
				
				// Start timer
				showcase_widget.next();
				setInterval ( "showcase_widget.next()", 10000 );
			},
			error: function( jqXHR, textStatus, errorThrown ){
				//console.log(textStatus);
			},
			timeout:20000
		});
	},
	next: function(){
		// Fade last one out
		$('#showcase_details').fadeOut('slow', function(){
			// Replace
			showcase_widget.counter++;
			if( showcase_widget.counter >= showcase_widget.items.length ) showcase_widget.counter = 0;
			var item = showcase_widget.items[showcase_widget.counter];

			if( item ){
				$('.showcase_link').attr('href', 'http://www.placenorthwest.co.uk/showcase/listings/view/'+item.id);
				
				var img = new Image();
				img.src = 'http://www.placenorthwest.co.uk/showcase/thumbs?w=187&h=120&zc=1&src='+item.image;
				img.id = 'showcase_photo';
				
				$('#showcase_photo').replaceWith(img);
				$('#showcase_photo').css('border','0 none');
				$('#showcase_photo').css('height','120px');
				$('#showcase_photo').css('width','187px');
				
				$('#showcase_name').html( item.name );
				if( item.description ) $('#showcase_description').html( item.description+'...' );
				else $('#showcase_description').html( '' );
				// Next one in
				$('#showcase_details').fadeIn();
			}
		});
		
	}
};

$( function(){
	showcase_widget.start();
});
