
// Config
var first_picture = 1;
var picture_count = 7;
var slideshow_prefix = 'site-slideshow-';
var slideshow_time = 6000;

// Functions Start
var current_picture = first_picture;
var timer_id;
var animation_lock = false;

function slideshow_change ( picture_id ) {
	if ( picture_id ) {
		if ( animation_lock == false ) {
			animation_lock = true;
			clearTimeout( timer_id );
			$('#'+slideshow_prefix+'button_'+current_picture).removeClass('active');
			$('#'+slideshow_prefix+'button_'+picture_id).addClass('active');
			$('#'+slideshow_prefix+'picture_'+current_picture).fadeOut( 500, function() {
				$('#'+slideshow_prefix+'picture_'+picture_id).fadeIn( 500, function() {
					animation_lock = false;
					timer_id = setTimeout( 'slideshow_change()', (slideshow_time-1000) );
				});
			});
			current_picture = picture_id;
		}
	} else {
		animation_lock = true;
		var this_picture = current_picture;
		current_picture++;
		if ( current_picture > picture_count ) {
			current_picture = 1;
		}
		var next_picture = current_picture;
		// Effects + new Timer
		$('#'+slideshow_prefix+'button_'+this_picture).removeClass('active');
		$('#'+slideshow_prefix+'button_'+next_picture).addClass('active');
		$('#'+slideshow_prefix+'picture_'+this_picture).fadeOut( 500, function() {
			$('#'+slideshow_prefix+'picture_'+next_picture).fadeIn( 500, function() {
				animation_lock = false;
			});
			timer_id = setTimeout( 'slideshow_change()', slideshow_time-500 );
		});
	}
}

$( function() {
	for ( var i = 1; i <= picture_count; i++ ) {
		if ( i != first_picture ) {
			$('#'+slideshow_prefix+'picture_'+i).hide();
		} else {
			$('#'+slideshow_prefix+'picture_'+i).show();
			$('#'+slideshow_prefix+'button_'+i).addClass('active');
		}
	}
	
	$('.site-pictures-slideshow-buttons li div').click( function() {
		// get ID
		var button_id = $(this).attr('id');
		var id = button_id.substr( 22, 2 );
		// don't change prefix config or it won't work in IE6
		if ( id.substr( 0, 1 ) == "_" ) {
			id = id.substr( 1, 1 );
		}
		// call function
		if ( id != current_picture ) {
			slideshow_change( id );
		}
	});
	
	timer_id = setTimeout( 'slideshow_change()', slideshow_time );
});

