var length = 0;
var maxLength	 = 4;
var currentPosition = 0;
var margin = 0;

function leftClick() {
	var ulmargin = 149 + margin*2;
	$('.posters > ul').animate({"marginLeft": "+="+ulmargin+"px"}, "normal");
	currentPosition -= 1;
	if (currentPosition <= 0) {
		currentPosition = 0;
		$('#todayLeft').removeClass('active');
		$("#todayLeft").unbind("click");
	}

	if ((length - currentPosition) > maxLength	) {
		$('#todayRight').addClass('active');
		$('#todayRight').click(rightClick);
	}

}

function rightClick() {
	var ulmargin = 149 + margin*2;
	$('.posters > ul').animate({"marginLeft": "-="+ulmargin+"px"}, "normal");
	currentPosition += 1;
	if ((length - currentPosition) <= maxLength	) {
		$('#todayRight').removeClass('active');
		$("#todayRight").unbind("click");
	}

	if (currentPosition > 0) {
		$('#todayLeft').addClass('active');
		$('#todayLeft').click(leftClick);
	}
}

function initPosters() {
	length = $('.poster').length;
	currentPosition = 0;
	maxLength = Math.floor($('.posters').width()/149);
	$('.posters > ul').animate({"marginLeft": "0px"}, "slow");

	$('.poster').stop();
	$('#todayLeft').removeClass('active');
	$("#todayLeft").unbind("click");
	$('#todayRight').removeClass('active');
	$("#todayRight").unbind("click");
	if (length > maxLength	) {
		margin = Math.floor(($('.posters').width() - maxLength*149)/(maxLength*2));
		$('.poster').animate({"marginLeft": margin+"px", "marginRight": margin+"px"}, "slow");
		$('#todayRight').addClass('active');
		$('#todayRight').click(rightClick);
	}
	else {
		margin = Math.floor(($('.posters').width() - length*149)/(length*2));
		$('.poster').animate({"marginLeft": margin+"px", "marginRight": margin+"px"}, "slow");
	}
}

$(".posters").ready(function(){
	initPosters();

	$(window).resize(initPosters);

	$('.poster').hover(function() {
		$(this).find("img.no_active").addClass('active');
	}, function() {
		$(this).find("img.no_active").removeClass('active');
	});
});
