jQuery.noConflict();
var scrollspeed = 800;
(function($) { 

$(document).ready(function() {
	createStructure();
	
	//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
	$('#mask-gallery, #gallery li').css("width", "950px");	
	$('#gallery').width($('#slider').width() * $('#gallery li').length);
	$('#mask-gallery, #gallery li, #mask-excerpt, .excerpta').each(function(){ $(this).css('height', $('#slider').height())+"px"});
	$('#mask-excerpt').css("height", '160px');
	//$('#mask-excerpt').css('top',$('#mask-gallery').position().top+($('#mask-gallery').height()/2)-($('#mask-excerpt').height()/2));
	//$('#mask-excerpt').css('left',$('#mask-gallery').position().left+$('#mask-gallery').width()-$('#mask-excerpt').width());

	//$("#slideto_buttons").css('top',$('#mask-gallery').position().top+$('#mask-gallery').height()-$("#slideto_buttons").height());
	//$("#slideto_buttons").css('left',$('#mask-gallery').position().left+$('#mask-gallery').width()-$("#slideto_buttons").width() - 50);		
	

	
	//Assign a timer, so it will run periodically
	var run = setInterval('newsscoller(0)', speed);	
	
	$('#gallery li:first, .excerpta: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);	
		return false;
	});	

	//Previous slide by passing prev=1
	$('#btn-prev').click(function () {
		newsscoller(1);	
		return false;
	});	
	
	//Mouse over, pause it, on mouse out, resume the slider show
	$('#slider').hover(
	
		function() {
			clearInterval(run);
		}, 
		function() {
			run = setInterval('newsscoller(0)', speed);	
		}
	); 

	$("#slideto_buttons span").click(function(){
		clearInterval(run);
		imgSpecify($("#slideto_buttons span").index(this)+1);
	});	
	$("#slideto_buttons span").mouseover(function(){
		$(this).addClass('hover');
	});
	$("#slideto_buttons span").mouseout(function(){
		$(this).removeClass('hover');
	});	
});




function imgSpecify(imgNo){
	//clear the selected class
	$('.excerpta, #gallery li, #slideto_buttons span').removeClass('selected');

	if (imgNo < 0 || imgNo > ($('#gallery li').length)){ return; }
	if (imgNo < 0 || imgNo > ($('.excerpta').length)){ return; }

	$('#gallery li:nth-child('+ imgNo +')').addClass('selected');
	$('.excerpta:nth-child('+ imgNo +')').addClass('selected');
	$('#slideto_buttons span:nth-child('+ imgNo +')').addClass('selected');
	$('#mask-gallery').scrollTo($('#gallery li:nth-child('+ imgNo +')'), scrollspeed);		
	$('#mask-excerpt').scrollTo($('.excerpta:nth-child('+ imgNo +')'), scrollspeed);					
	
}
function createStructure(){

	var modname = "fp_slides .moduletable";
	//$('#' + modname + ' ul').attr("id",'excerpt');
	//$('#' + modname + ' ul').wrap("<div id='mask-excerpt'></div>");
	
	$('#' + modname + ' img').parents('p').each(function(){ $(this).contents().wrap('<li></li>'); });
	$('#' + modname + ' img').parents('li').unwrap();
	$('#' + modname + ' li').wrapAll("<ul id='gallery'></ul>");

	
	
	$('#' + modname + ' ul#gallery').wrap("<div id='mask-gallery'></div>");
	
	$('#' + modname + ' div').wrapAll("<div id='slider'></div>");
	
	$('#' + modname + ' td').each(function(){ $(this).html("<div class='excerpta'>"+ $(this).html() + "</div>"); });
	$('div .excerpta').unwrap();
	$('div .excerpta').unwrap();
	$('div .excerpta').unwrap();
	$('div .excerpta').unwrap();
	$('div .excerpta').wrapAll("<div id='excerpt'></div>");
	$('div#excerpt').wrap("<div id='mask-excerpt'></div>");
	
	var sh ="";
	var c = 0;
	var sc = "";
	$('#gallery li').each( function() {
		c++;
		if (c==1) { sc = 'selected' }else{ sc ='';}
		sh +="<span class='"+ sc +"' id='sldimgbtn_" + c + "'>" + c + "</span>";
	});
	$("#slideholder .moduletable").append("<div id='slideto_buttons'>" + sh + "</div>");

}


})(jQuery);

function newsscoller(prev) {
	jQuery.noConflict();
	(function($) {
		//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 = $('.excerpta.selected').length ? $('.excerpta.selected') : $('.excerpta:first');
		var current_number = $('#slideto_buttons span.selected').length ? $('#slideto_buttons span.selected') : $('#slideto_buttons span: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() : $('.excerpta:last');
			var next_number = (current_number.pren().length) ? current_number.prev() : $('#slideto_buttons span: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() : $('.excerpta:first');
			var next_number = (current_number.next().length) ? current_number.next() : $('#slideto_buttons span:first');
		}

		//clear the selected class
		$('.excerpta, #gallery li, #slideto_buttons span').removeClass('selected');
		
		//reassign the selected class to current items
		next_image.addClass('selected');
		next_excerpt.addClass('selected');
		next_number.addClass('selected');

		//Scroll the items
		$('#mask-gallery').scrollTo(next_image, scrollspeed);		
		$('#mask-excerpt').scrollTo(next_excerpt, scrollspeed);					
	})(jQuery);	
}

