//
//	jQuery Slider
//

jQuery(document).ready(function() {

	//Speed of the slideshow
	var speed = 6000;
	
	//You have to specify width and height in #slider CSS properties
	//After that, the following script will set the width and height accordingly */
	/* console.log(jQuery('#slider-mask').width()); */
	jQuery('#slider-mask').width(560);
	jQuery('#slides li').width(560);
	jQuery('#slides').width(jQuery('#slider').width() * jQuery('#slides li').length);
	jQuery('#slider-mask, #slides li, #excerpt-mask, #excerpt li').height(jQuery('#slider').height());
	
	//Assign a timer, so it will run periodically
	var run = setInterval('newsscoller(0)', speed);	
	
	jQuery('#slides li:first-child, #excerpt li:first-child').addClass('selected');

	//Pause the slidershow with clearInterval
	jQuery('#btn-pause').click(function () {
		clearInterval(run);
		return false;
	});

	//Continue the slideshow with setInterval
	jQuery('#btn-play').click(function () {
		run = setInterval('newsscoller(0)', speed);	
		return false;
	});
	
	//Next Slide by calling the function
	jQuery('#btn-next').click(function () {
		newsscoller(0);	
		return false;
	});	

	//Previous slide by passing prev=1
	jQuery('#btn-prev').click(function () {
		newsscoller(1);	
		return false;
	});	
	
	//Mouse over, pause it, on mouse out, resume the slider show
	jQuery('#slider').hover(
	
		function() {
			clearInterval(run);
		}, 
		function() {
			run = setInterval('newsscoller(0)', speed);	
		}
	); 	
	
});


function newsscoller(prev) {

	//Get the current selected item (with selected class), if none was found, get the first item
	var current_image = jQuery('#slides li.selected').length ? jQuery('#slides li.selected') : jQuery('#slides li:first-child');
	var current_excerpt = jQuery('#excerpt li.selected').length ? jQuery('#excerpt li.selected') : jQuery('#excerpt li:first-child');

	//if prev is set to 1 (previous item)
	if (prev) {
		
		//Get previous sibling
		var next_image = (current_image.prev().length) ? current_image.prev() : jQuery('#slides li:last-child');
		var next_excerpt = (current_excerpt.prev().length) ? current_excerpt.prev() : jQuery('#excerpt li:last-child');
	
	//if prev is set to 0 (next item)
	} else {
		
		//Get next sibling
		var next_image = (current_image.next().length) ? current_image.next() : jQuery('#slides li:first-child');
		var next_excerpt = (current_excerpt.next().length) ? current_excerpt.next() : jQuery('#excerpt li:first-child');
	}

	//clear the selected class
	jQuery('#excerpt li, #slides li').removeClass('selected');
	
	//reassign the selected class to current items
	next_image.addClass('selected');
	next_excerpt.addClass('selected');

	//Scroll the items
	jQuery('#slider-mask').scrollTo(next_image, 800);		
	jQuery('#excerpt-mask').scrollTo(next_excerpt, 800);					
	
}