window.requestAnimFrame = (function() {
	return  window.requestAnimationFrame   || 
			window.webkitRequestAnimationFrame || 
			//window.mozRequestAnimationFrame    || 
			window.oRequestAnimationFrame      || 
			window.msRequestAnimationFrame     || 
			function(/* function */ callback, /* DOMElement */ element){
				window.setTimeout(callback, 1000 / 60);
			};
})();
window.requestTimeout = function(fn, delay) {
    if( !window.requestAnimationFrame       && 
        !window.webkitRequestAnimationFrame && 
        //!window.mozRequestAnimationFrame    && 
        !window.oRequestAnimationFrame      && 
        !window.msRequestAnimationFrame)
            return window.setTimeout(fn, delay);

    var start = new Date().getTime(),
        handle = new Object();

    function loop(){
        var current = new Date().getTime(),
        delta = current - start;

        delta >= delay ? fn.call() : handle.value = requestAnimFrame(loop);
    };

    handle.value = requestAnimFrame(loop);
    return handle;
};

window.clearRequestTimeout = function(handle) {
    window.cancelAnimationFrame ? window.cancelAnimationFrame(handle.value) :
    window.webkitCancelRequestAnimationFrame ? window.webkitCancelRequestAnimationFrame(handle.value) :
    window.mozCancelRequestAnimationFrame ? window.mozCancelRequestAnimationFrame(handle.value) :
    window.oCancelRequestAnimationFrame	? window.oCancelRequestAnimationFrame(handle.value) :
    window.msCancelRequestAnimationFrame ? msCancelRequestAnimationFrame(handle.value) :
    clearTimeout(handle);
};

var timeout_handler = null;
$(document).ready(function() {
	var total = $('.testimonials ol li').length;
	var hovering = false;
	var current_slide = 0;
	var slide_width = $('.testimonials').width();
	var autoplay = true;
	var timer = 10000;
	
	//$('.testimonials li').css('display', 'none');
	$('.testimonials ol li:first-child').addClass('active');
	$('#testimonials-total').text(total+'');
	$('.testimonials ol li').hover(function() {
		hovering = true;
	}, function() {
		hovering = false;
	}).css('float', 'left');
	$('.testimonials ol').css('width', $('.testimonials ol li').length*slide_width).css('position', 'relative');
	$('#testimonials-autoplayer > a').click(function() {
		autoplay = !autoplay;
		if ( autoplay ) $(this).text('ON'); else $(this).text('OFF');
	});
	//$('#testimonials-autoplayer a').click();
	
	next_slide = function(index, outside) {
		slide_width = $('.testimonials').width();
		$('.testimonials ol').css('width', $('.testimonials ol li').length*slide_width).css('position', 'relative');
		
		if ( arguments.length == 4 ) {
			index = arguments[2];
			outside = arguments[3];
		} else if ( arguments.length == 2 && arguments[1] !== undefined ) {
			index = arguments[0];
			outside = arguments[1];
		} else {
			index = undefined;
			outside = undefined;
		}
		
		if ( hovering ) return requestTimeout(next_slide, timer);
		if ( ! autoplay && ! outside ) return timeout_handler = requestTimeout(next_slide, timer);
		
		current_slide++;
		if ( index != null && index != undefined ) current_slide = index;
		if ( current_slide < 0 ) current_slide = $('.testimonials ol li').length-1;
		if ( current_slide >= $('.testimonials ol li').length ) current_slide = 0;
		
		left = current_slide * slide_width;
		$('.testimonials ol').stop(false, false).animate({'left': -left});
		
		$('.testimonials ol li').removeClass('active')
		$('#testimonials-current').text(current_slide+1);
		
		timeout_handler = requestTimeout(next_slide, timer);
	}
	
	$('#testimonials-navigation-next, #testimonials-navigation-prev').click(function() {
		clearRequestTimeout(timeout_handler);
		index = null;
		if ( $(this).attr('id') == 'testimonials-navigation-prev' ) {
			index = current_slide-1;
		}
		next_slide(index, true);
	});
	
	timeout_handler = requestTimeout(next_slide, timer);
});
