/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


function gallery() {

	//if no IMGs have the show class, grab the first image
	var current = ($('#gallery a.show')?  $('#gallery a.show') : $('#gallery a:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = current.next().length ? current.next() : $('#gallery a:first');

	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 2000);

	//Hide the current image
	current.animate({opacity: 0.0}, 2000)
	.removeClass('show');

}


$(document).ready(function() {

	$('#gallery a').css({opacity: 0.0});
	gallery();
	setInterval('gallery()',4000);

	//Speed of the slideshow
	var speed = 5000;

	//You have to specify width and height in #slider CSS properties
	//After that, the following script will set the width and height accordingly
	$('#mask-excerpt, #excerpt li').height($('#slider').height());

	//Assign a timer, so it will run periodically
	var run = setInterval('newsscoller(0)', speed);

	$('#excerpt li:first').addClass('selected');

	//Mouse over, pause it, on mouse out, resume the slider show
	$('#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_excerpt = $('#excerpt li.selected').length ? $('#excerpt li.selected') : $('#excerpt li:first');

	//if prev is set to 1 (previous item)
	if (prev) {

		//Get previous sibling
		var next_excerpt = (current_excerpt.prev().length) ? current_excerpt.prev() : $('#excerpt li:last');

	//if prev is set to 0 (next item)
	} else {

		//Get next sibling
		var next_excerpt = (current_excerpt.next().length) ? current_excerpt.next() : $('#excerpt li:first');
	}

	//clear the selected class
	$('#excerpt li').removeClass('selected');

	//reassign the selected class to current items
	next_excerpt.addClass('selected');

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

}