// JavaScript Document

var current = 0;
var timer;
var userClick = false;
var animClick;
var clickReceived;
var doubleClicks = 0;
//CAN BE CHANGED
var speed = 5000;
var boxWidth = 831;
var boxHeight = 190;
var continuous = true;
var loop = true;
// FOR LOOP TO WORK continuous HAS TO BE FALSE


$(document).ready(function() {
	$("#prev").click(function(){ goPrev(true); return false; });
	$("#next").click(function(){ goNext(true); return false; });
	timer = setInterval(function(){ goNext(false); }, 10000);
	if(continuous){
		$('#textContainer .textBox').css('position','absolute');
		for(var i=0; i<$('#textContainer .textBox').length; i++) {
			$('#textContainer .textBox:eq('+i+')').css('left',(boxWidth*i))
		}
		$('#textContainer .textBox:last').clone().prependTo('#textContainer')
		$('#textContainer .textBox:first').css('left',-boxWidth);
		$('#textContainer .textBox:last').remove();
	}
	if(!loop){$('#prev').attr('class','disabled');}
});

function goPrev(userClick) {
	if(userClick){ clearInterval(timer); }
	if(!loop){ $('#next').attr('class','active'); }
	if(current != 0 || continuous) {
		current --;
		if(current == 0 && !loop) {$('#prev').attr('class','disabled');}
	}else {
		if(loop) {
			current = $('#textContainer div').length - 1;
		}
	}
	$('#textContainer').stop();
	$('#textContainer').animate({left:-(current * boxWidth) +"px"}, speed, function(){ callbackPrev(); });
	if(continuous){
		var firstLeft = parseInt($('#textContainer .textBox:first').css('left').replace('px','')) - boxWidth;
		var myTarget = $('#textContainer .textBox').length - doubleClicks - 1;
		$('#textContainer .textBox:eq('+myTarget+')').clone().prependTo('#textContainer')
		$('#textContainer .textBox:first').css('left',firstLeft);
	}
	doubleClicks++;
}

function goNext(userClick){
	if(!userClick && current == 2 && !continuous) return ;
	if(userClick){ clearInterval(timer); }
	if(!loop){ $('#prev').attr('class','active'); }
	if(current >= $('#textContainer div').length - 1 && !continuous) {
		if(loop) {
			current = 0;
		}
	}else {
		current++;
		if(current >= $('#textContainer div').length - 1 && !loop) {$('#next').attr('class','disabled');}
	}
	$('#textContainer').stop();
	$('#textContainer').animate({left:-(current * boxWidth) +"px"}, speed, function(){ callbackNext(); });
	if(continuous){
		var farthestLeft = parseInt($('#textContainer .textBox:last').css('left').replace('px','')) + boxWidth;
		$('#textContainer .textBox:eq('+doubleClicks+')').clone().appendTo('#textContainer')
		$('#textContainer .textBox:last').css('left',farthestLeft);
	}
	doubleClicks++;
}

function callbackNext(){
	if(continuous) {
		for(var i=0; i<doubleClicks; i++) {
			$('#textContainer .textBox:first').remove();
		}
		doubleClicks = 0;
	}
}

function callbackPrev(){
	if(continuous) {
		for(var i=0; i<doubleClicks; i++) {
			$('#textContainer .textBox:last').remove();
		}
		doubleClicks = 0;
	}
}