	
j$(document).ready(function() {

	//Speed of the slideshow
	var 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
	//j$('#mask-gallery, #gallery li').width(j$('#slider').width());	
	//j$('#gallery').width(j$('#slider').width() * j$('#gallery li').length);
	j$('#mask-excerpt, #excerpt li').width(j$('#slider').width());	
	j$('#excerpt').width(j$('#slider').width() * j$('#excerpt li').length);
	//j$('#mask-gallery, #gallery li, #mask-excerpt, #excerpt li').height(j$('#slider').height());
	j$('#mask-gallery, #gallery li').height(j$('#slider').height());
	j$('#mask-excerpt, #excerpt li').height(j$('#excerpt').height());
	//Assign a timer, so it will run periodically
	var run = setInterval('newsslider(0)', speed);	
	
	j$('#gallery li:first, #excerpt li:first, #photo_tag li:first').addClass('selected');
	//j$('#photo_tag li.selected').css("background-color","#cfcf00");
	j$('#photo_tag li.selected').css("background-image","url(/img/banner_button_on.png)");
	
	j$('#photo_tag li').click(function () {
		//stop the slide show
		clearInterval(run);
		//j$(this).css("background-color","red");
		//j$('#photo_tag li').removeClass("selected");
		//go to the item
		goto(j$(this).attr('rel'));	
		//j$(this).addClass("selected").css("background-color","#cfcf00");
		j$(this).addClass("selected").css("background-image","url(/img/banner_button_on.png)");
		
			
		//resume the slideshow
		run = setInterval('newsslider(0)', speed);
		return false;
		}
	);

	//Pause the slidershow with clearInterval
	j$('#btn-pause').click(function () {
		clearInterval(run);
		return false;
	});

	//Continue the slideshow with setInterval
	j$('#btn-play').click(function () {
		run = setInterval('newsslider(0)', speed);	
		return false;
	});
	
	//Next Slide by calling the function
	j$('#btn-next').click(function () {
		newsslider(0);	
		return false;
	});	

	//Previous slide by passing prev=1
	j$('#btn-prev').click(function () {
		newsslider(1);	
		return false;
	});	
	
	//Mouse over, pause it, on mouse out, resume the slider show
	return;
	j$('#slider').hover(
	
		function() {
			clearInterval(run);
		}, 
		function() {
			run = setInterval('newsslider(0)', speed);	
		}
	); 	
	
});


function newsslider(prev) {
	//Get the current selected item (with selected class), if none was found, get the first item
	var current_image = j$('#gallery li.selected').length ? j$('#gallery li.selected') : j$('#gallery li:first');
	var current_excerpt = j$('#excerpt li.selected').length ? j$('#excerpt li.selected') : j$('#excerpt li:first');
	var current_photo_tag = j$('#photo_tag li.selected').length ? j$('#photo_tag li.selected') : j$('#photo_tag li:first');

	//if prev is set to 1 (previous item)
	if (prev) {
		
		//Get previous sibling
		var next_image = (current_image.prev().length) ? current_image.prev() : j$('#gallery li:eq(7)');//last');
		var next_excerpt = (current_excerpt.prev().length) ? current_excerpt.prev() : j$('#excerpt li:eq(7)');//last');
		var next_photo_tag = (current_photo_tag.prev().length) ? current_photo_tag.prev() : j$('#photo_tag li:last');
	
	//if prev is set to 0 (next item)
	} else {
		
		//Get next sibling
		var next_image = (current_image.next().length) ? current_image.next() : j$('#gallery li:eq(1)');//first');
		var next_excerpt = (current_excerpt.next().length) ? current_excerpt.next() : j$('#excerpt li:eq(1)');//first');
		var next_photo_tag = (current_photo_tag.next().length) ? current_photo_tag.next() : j$('#photo_tag li:first');
		
	}

	//clear the selected class
	j$('#excerpt li, #gallery li, #photo_tag li').removeClass('selected');
	//j$('#photo_tag li').css("background-color","#fff");
	j$('#photo_tag li').css("background-image","url(/img/banner_button_off.png)");
	
	//reassign the selected class to current items
	next_image.addClass('selected');
	next_excerpt.addClass('selected');
	next_photo_tag.addClass('selected');

	if(prev)
	{
		if(!current_image.prev().length)
		{
			j$('#mask-gallery').scrollTo(j$('#gallery li:last'),0);
			j$('#mask-excerpt').scrollTo(j$('#excerpt li:last'),0);//next_excerpt,0);
		}
	}else
	{
		if(!current_image.next().length)
		{
			j$('#mask-gallery').scrollTo(j$('#gallery li:first'),0);
			j$('#mask-excerpt').scrollTo(j$('#excerpt li:first'),0);//next_excerpt,0);
		}
	}/**/
	//Scroll the items
	j$('#mask-gallery').scrollTo(next_image, 800);		
	j$('#mask-excerpt').scrollTo(next_excerpt, 800);
	
	
	next_photo_tag.css("background-image","url(/img/banner_button_on.png)");
}

//Add this function after newslider function
function goto(item) {
	j$('#mask-gallery').scrollTo(item, 800);	
	j$('#mask-excerpt').scrollTo(item, 800);	
	
	//clear the selected class
	j$('#excerpt li, #gallery li, #photo_tag li').removeClass('selected');
	j$('#photo_tag li').css("background-image","url(/img/banner_button_off.png)");
	
	//reassign the selected class to current items
	//next_photo_tag.addClass('selected');

	j$(item).addClass('selected');
}
