google.setOnLoadCallback(function() {
    $(document).ready(function() {
        initRotator();
    });
});

var run;
var slideLength;
var current = 0;
var speed = 5000;
var paused = false;
	
function initRotator(){
	slideLength = $('.slide').length;
	$('.slide').hide().eq(current).show();
	$('.slideText').hide().eq(current).show();
	
	run = setInterval('rotate()', speed);
	
	$('.rotatorNext').click(function() {
		current++;
		initFader();
	});
	
	$('.rotatorBack').click(function() {
		current--;
		initFader();
	});
	
	$('.rotatorPause').click(function() {
		if(paused){
			run = setInterval('rotate()', speed);
			paused = false;
		}
		else{
			clearInterval(run);
			paused = true;
		}
	});
}

function rotate(){
	$('.rotatorNext').click();
}

function initFader(){
	clearInterval(run);
	$('.rotatorFader').fadeIn(800, function() {
	    $('.rotatorFader').fadeOut(800);
	    getSlideEq();
	    $('.slide').hide().eq(current).show();
	    $('.slideText').hide().eq(current).show();
	    run = setInterval('rotate()', speed);
	});
}

function getSlideEq() {
	if(current > (slideLength - 1)){
		current = 0;
	}
	else if(current < 0)
	{
		current = (slideLength - 1);
	}
}
