//WRAP Local Authorities - Publication Slider

var currentPubLeftIndex = 0;
var totalPubs = 0;
var eachPubWidth = 220;
var pubsPerRow = 1;

$(document).ready(function () {	
	totalPubs = jQuery('#pub_wrap li').length;
	if(totalPubs > pubsPerRow){
		$('#pub_list .inner_right').addClass('top_space');
		$('#pub_wrap').append('<div id="pub_nav"><span class="prev">Prev</span><span class="count">1/12</span><span class="next">Next</span></div>');
		$('#pub_list').css('width', (totalPubs*eachPubWidth) + 'px');
		$('#pub_nav .count').text( '1/' + totalPubs);
		$('#pub_nav .next').click(function(event){ pubScrollNext(); event.preventDefault(); });
		$('#pub_nav .prev').click(function(event){ pubScrollPrev(); event.preventDefault(); });
	}
});

//scrollers
function pubScrollNext(){
	var newIndex = currentPubLeftIndex + 1;
	if(newIndex > (totalPubs-pubsPerRow)){ 
		//go back to 1st one
		newIndex = 0;
		jQuery('#pub_list').animate({ marginLeft: -(newIndex*eachPubWidth)}, 'medium');
	}else{
		jQuery('#pub_list').animate({ marginLeft: -(newIndex*eachPubWidth)}, 'fast');
	}
	$('#pub_nav .count').text((newIndex+1) + '/' + totalPubs);
	currentPubLeftIndex = newIndex;
}	
function pubScrollPrev(){
	var newIndex = currentPubLeftIndex - 1;
	if(newIndex < 0){ 
		//go to last one
		newIndex = (totalPubs-pubsPerRow);
		jQuery('#pub_list').animate({ marginLeft: -(newIndex*eachPubWidth)}, 'medium');
	}else{
		jQuery('#pub_list').animate({ marginLeft: -(newIndex*eachPubWidth)}, 'fast');
	};
	$('#pub_nav .count').text((newIndex+1) + '/' + totalPubs);
	currentPubLeftIndex = newIndex;
}
