$(document).ready(function() {
	$.extend(sliderSettings, { 
		'afterChange' : function() {
			linkifyCaptionBox();
		},
		'afterLoad' : function() {
			linkifyCaptionBox();
		}
	});
	$('#image_slider').nivoSlider(sliderSettings);
	initializeLatestNewsSlider();

});			
function linkifyCaptionBox() {
	// delete preexisting click handlers on the caption box
	$('.nivo-caption').unbind('click');
	// get the href of the displayed img
	var active_url = $('.nivo-imageLink:visible').first().attr('href');
	$('.nivo-caption').bind('click', function() {
		window.location = active_url;
	})
}

function initializeLatestNewsSlider() {
	$(".latestnewsblock .clear").remove();
	$(".latestnews_article").wrapAll('<div id="latestnews_wrapper" />');
	$(".latestnews_article").removeClass('firstarticle');
	var blockswidth = $('.latestnews_article').length * 173 + 13;
	$('#latestnews_wrapper').css({
		'width': blockswidth+'px', 
		'position':'relative', 
		'left': '-10px'
	});
	$('<div id="clicker" />').appendTo('.latestnewsblock');
	$('<div id="clickl" />').appendTo('#clicker');
	$('<div id="clickr" />').appendTo('#clicker');

	$("#clickl").click(function() {
		if ($('#latestnews_wrapper').css('left') != '-10px' && !$('#latestnews_wrapper').hasClass('inmotion')) {
			$('#latestnews_wrapper').addClass('inmotion');
			$('#latestnews_wrapper').animate({
				left: '+=520'
				},
				'5000', 'swing', function() {
					$('#latestnews_wrapper').removeClass('inmotion');
				}
			);
			
		}
		
	});
	
	$("#clickr").click(function(e) {
		var leftness = parseInt($('#latestnews_wrapper').css('left'));
		var totalWidth = parseInt($('#latestnews_wrapper').css('width'));
		var preventMove = (leftness * -1 + 520) > totalWidth;

		if (!preventMove && !$('#latestnews_wrapper').hasClass('inmotion')) {
			$('#latestnews_wrapper').addClass('inmotion');
			$('#latestnews_wrapper').animate({
				left: '-=520'
			},
			'5000', 'swing', function() {
				$('#latestnews_wrapper').removeClass('inmotion');
			});
		}
	});
}

