var run, speed;

function getID(strID) {
	return strID.replace(/[^0-9]/gi, '');
}


$(document).ready(function() {
	$('#buttons li.selector:first img').attr('src',"images/imageGallerySelected.gif");
	$('#mask-gallery').scrollTo($('#gallery li:first'), 0);		
	$('#mask-excerpt').scrollTo($('#excerpt li:first'), 0);		
	
	//Speed of the slideshow
	speed = 8000;
	
	//You have to specify width and height in #slider CSS properties
	//After that, the following script will set the width and height accordingly
	$('#mask-gallery, #gallery li').width($('#slider').width());	
	$('#gallery').width($('#slider').width() * $('#gallery li').length);
	$('#mask-gallery, #gallery li, #mask-excerpt, #excerpt li').height($('#slider').height());
	
	//Assign a timer, so it will run periodically
	run = setInterval('newsscoller(0)', speed);	
	
	$('#gallery li:first, #excerpt li:first').addClass('selected');

	//Pause the slidershow with clearInterval
	$('#btn-pause').click(function () {
		clearInterval(run);
		return false;
	});

	//Continue the slideshow with setInterval
	$('#btn-play').click(function () {
		run = setInterval('newsscoller(0)', speed);	
		return false;
	});
	
	//Next Slide by calling the function
	$('#btn-next').click(function () {
		newsscoller(0);
		$(this).blur();
		return false;
	});	

	//Previous slide by passing prev=1
	$('#btn-prev').click(function () {
		newsscoller(1);
		$(this).blur();
		return false;
	});	
	
	//Mouse over, pause it, on mouse out, resume the slider show
	$('#slider').bind('mouseenter', function() {
		clearInterval(run);
	});
	
	$('#slider').bind('mouseleave', function() {
		run = setInterval('newsscoller(0)', speed);
	});
	
	//clicking an image selecting blob should jump to that image
	$('#buttons .selector').bind('click', function() {
		jumpTo(getID($(this).attr('rel')));
		$('a', $(this)).blur();
		return false;
	});
});


function newsscoller(prev) {
	clearInterval(run);

	//Get the current selected item (with selected class), if none was found, get the first item
	var current_image	 = $('#gallery li.selected').length ? $('#gallery li.selected') : $('#gallery li:first');
	var current_excerpt	 = $('#excerpt li.selected').length ? $('#excerpt li.selected') : $('#excerpt li:first');
	var current_button	 = $('#buttons li#selected').length ? $('#buttons li#selected') : $('#buttons li.selector:first');

	//if prev is set to 1 (previous item)
	if (prev) {
		
		//Get previous sibling
		var next_image 		= (current_image.prev().length) 	? current_image.prev() 		: $('#gallery li:last');
		var next_excerpt 	= (current_excerpt.prev().length) 	? current_excerpt.prev() 	: $('#excerpt li:last');
		var next_button 	= (current_button.prev('.selector').length) 	? current_button.prev('.selector') 	: $('#buttons li.selector:last');
	
	//if prev is set to 0 (next item)
	} else {
		
		//Get next sibling
		var next_image 		= (current_image.next().length) 	? current_image.next() 		: $('#gallery li:first');
		var next_excerpt 	= (current_excerpt.next().length) 	? current_excerpt.next() 	: $('#excerpt li:first');
		var next_button 	= (current_button.next('.selector').length) 	? current_button.next('.selector') 	: $('#buttons li.selector:first');
	}

	//clear the selected class
	$('#excerpt li, #gallery li').removeClass('selected');
	$('#buttons li.selector').attr('id','');
	$('#buttons li.selector img').attr('src','images/imageGalleryUnselected.gif');
	
	//reassign the selected class to current items
	//$('#debug').append("Selecting "+ next_button.attr('rel') + '<br/>');
	next_image.addClass('selected');
	next_excerpt.addClass('selected');
	next_button.attr('id','selected');
	$("img",next_button).attr('src',"images/imageGallerySelected.gif");

	//Scroll the items
	$('#mask-gallery').scrollTo(next_image, 800);		
	$('#mask-excerpt').scrollTo(next_excerpt, 800);					

	run = setInterval('newsscoller(0)', speed);	
}

function jumpTo(index) {
	clearInterval(run);
	
	index--;
	
	var image=$('#gallery li:eq('+index+')');
	var excerpt=$('#excerpt li:eq('+index+')');
	var button=$('#buttons li:eq('+(index+1)+')');
	
	//clear the selected class
	$('#excerpt li, #gallery li').removeClass('selected');
	$('#buttons li.selector').attr('id','');
	$('#buttons li.selector img').attr('src','images/imageGalleryUnselected.gif');
	
	//reassign the selected class to current items
	image.addClass('selected');
	excerpt.addClass('selected');
	button.attr('id','selected');
	$("img",button).attr('src',"images/imageGallerySelected.gif");

	//Scroll the items
	$('#mask-gallery').scrollTo(image, 800);		
	$('#mask-excerpt').scrollTo(excerpt, 800);					

	run = setInterval('newsscoller(0)', speed);
}