 jQuery(function($){
		
	$(function(){
		
		$('.item').each(function(i){
			if(i!=0){
				$(this).css('opacity', .2);
			}
			$(this).attr('rel', i);
		});
		
		var leftpad = ($(window).width()-940)/2-10;
		$('#billboard #rotator').css('paddingLeft', leftpad);
		
		var currentItem = 0;
		var maxItems = $('.banner').size();
		
		var startScroll = function(toNum){
		var nextEl = $('a.banner[rel=' + toNum + ']');
			
		$('#billboard').scrollTo(nextEl, {
				'duration': 500,
				'offset': {left: (-($(window).width()-940)/2+10)},
				'axis' : 'x'
			});
		};
		
		function slideleft() {
			if( currentItem > 0 ) {
				$(".item[rel='"+currentItem+"']").animate({'opacity':.2}, 500);
				currentItem--;
				startScroll(currentItem);
				$(".item[rel='"+currentItem+"']").animate({'opacity':1}, 500);
			}
		};
		
		function slideright() {
			if( currentItem < maxItems - 1 ) {
				$(".item[rel='"+currentItem+"']").animate({'opacity':.2}, 500);
				currentItem++;
				startScroll(currentItem);
				$(".item[rel='"+currentItem+"']").animate({'opacity':1}, 500);
			}
		};
		
		autorotate = function(){
			play = setInterval(function(){
				slideright();
				if (currentItem == maxItems - 1) {
				currentItem=-1;
				startScroll(currentItem);
				$(".item[rel='"+currentItem+"']").animate({'opacity':1}, 500);
				}
			}, 6000); 
		};
		
		autorotate();
		
		$(document).keydown(function(e){
			if (e.keyCode==37) {
				slideleft();
			} else
			if (e.keyCode==39) {
				slideright();
			}
		});
		
		$('.item').click(function(){
			if( currentItem == $(this).attr('rel')) {					
			}
			else{
				$('#billboard').scrollTo(this, {
				'duration': 250,
				'offset': {left: (-($(window).width()-940)/2+10)},
				'axis' : 'x'
				});
				$(this).animate({'opacity':1}, 500);
				$(".item[rel='"+currentItem+"']").animate({'opacity':.2}, 500);
				currentItem = $(this).attr('rel');
				return false
			}
		});
		
		$('#next').click(function(){
			slideright();
			clearInterval(play);
			$('#pause').addClass('paused');
		});
		
		$('#prev').click(function(){
			slideleft();
			clearInterval(play);
			$('#pause').addClass('paused');
		});
		
		$('#pause').click(function() {
			if ($(this).hasClass('paused')){
				autorotate();
				$(this).removeClass('paused');
			}else{
				clearInterval(play);
				$(this).addClass('paused');
			}
		});
		
	});
});
