
$(document).ready(function() {
	
	// not slider related
	/*$('.main-blok1').mouseenter(function(){$('.main-blok1 p, .main-blok1 h1').css('text-decoration', 'underline');}).mouseleave(function(){$('.main-blok1 p, .main-blok1 h1').css('text-decoration', 'none');});
	$('.main-blok2').mouseenter(function(){$('.main-blok2 p, .main-blok2 h1').css('text-decoration', 'underline');}).mouseleave(function(){$('.main-blok2 p, .main-blok2 h1').css('text-decoration', 'none');});
	$('.main-blok3').mouseenter(function(){$('.main-blok3 p, .main-blok3 h2').css('text-decoration', 'underline');}).mouseleave(function(){$('.main-blok3 p, .main-blok3 h2').css('text-decoration', 'none');});
		
	$('.with-moar').each(function(){
		$(this).find('.moar').click(function(){
			$(this).hide();
			$(this).parent().find('.moar-here').slideDown();
		});
		$(this).find('.less').click(function(){
			$(this).parent().parent().find('.moar').show();
			$(this).parent().parent().find('.moar-here').slideUp();
		})
	});
	*/
	
	
	
	//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
	$('#mask-gallery').width($('#slider').width());	
	$('#slider-gallery').css( 'width', $('#slider').width() * $('#slider-gallery div.slider-item').length  );
	$('.slider-item').hide();
	$('.slider-item:first').fadeIn();
	$('#mask-gallery, #slider-gallery').height($('#slider').height());
	
	$('.slider-item-linked').mouseenter(function(){
		$(this).find('h1').css('text-decoration', 'underline');
	}).mouseleave(function(){
		$(this).find('h1').css('text-decoration', 'none');
	});
	
	//Assign a timer, so it will run periodically
	var run = setInterval('newsscoller(0)', speed);	
	
	$('#slider-gallery .slider-info:first, #slider-buttons a: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;
	});	
	
	$('#btn-1').click(function () {
		newsscoller(1);	
		return false;
	});
	$('#btn-2').click(function () {
		newsscoller(2);	
		return false;
	});	
	$('#btn-3').click(function () {
		newsscoller(3);	
		return false;
	});	
	$('#btn-4').click(function () {
		newsscoller(4);	
		return false;
	});	
	$('#btn-5').click(function () {
		newsscoller(5);	
		return false;
	});
	
	//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_image = $('#slider-gallery .selected').length ? $('#slider-gallery .selected') : $('#slider-gallery .slider-item:first');
	var current_btn = $('#slider-buttons .selected').length ? $('#slider-buttons .selected') : $('#slider-buttons .slider-item:first');
	current_image.stop().fadeTo(500, 0, function(){ $(this).css('display', 'none') });

	//if prev is set to -1 (previous item)
	if (prev==-1) {
		
		//Get previous sibling
		var next_image = (current_image.prev().length) ? current_image.prev() : $('#slider-gallery .slider-item:last');
		var next_btn = (current_btn.prev().length) ? current_btn.prev() : $('#slider-buttons a:last');
	
	//if prev is set to 0 (next item)
	} else if (prev==0) {
		
		//Get next sibling
		var next_image = (current_image.next().length) ? current_image.next() : $('#slider-gallery .slider-item:first');
		var next_btn = (current_btn.next().length) ? current_btn.next() : $('#slider-buttons a:first');
	//any other goto prev value
	} else {
		
		//Get next sibling
		var next_image = $('#slider-gallery .slider-item:nth-child('+prev+')');
		var next_btn = $('#slider-buttons a:nth-child('+prev+')');
	}

	//clear the selected class
	$('#slider-gallery .slider-item, #slider-buttons a').removeClass('selected');
		
	//reassign the selected class to current items
	next_image.addClass('selected');
	next_image.stop().fadeTo(350, 1);
	next_btn.addClass('selected');

	//Scroll the items
	//$('#mask-gallery').stop().scrollTo(next_image, 1000);					
	
}

