/**
 * Created by JetBrains PhpStorm.
 * User: balaban
 * Date: 10.08.11
 * Time: 11:53
 * To change this template use File | Settings | File Templates.
 */
jQuery.fn.testimonial = function(method)
{

	var methods = {
		init : function(options)
		{
			$.extend(_options, options);
			_btnNext = container.find(_options.btnNext);
			_btnPrev = container.find(_options.btnPrev);
			init();
			return this;
		}
	};

	var _options =
	{
		'start'         :   0,
		'speed'         :   600,
		'autoRotate'    :   7000,
		'btnNext'       : '.btn_next',
		'btnPrev'       : '.btn_prev',
		'randomStart'   : true
	};

	var container = $(this);
	var _btnNext = null;
	var _btnPrev = null;
	var _in_progress = false;
	var _current = 0;
	var _interval = false;
	var _mouseovered = false;

	if ( methods[method] ) {
		return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
	} else if ( typeof method === 'object' || ! method ) {
		return methods.init.apply( this, arguments );
	} else {
		$.error( 'Method ' +  method + ' does not exist on jQuery.testimonial' );
	}


	function init()
	{
		container.find('ul').children('li').css('opacity', 0);
		container.find('ul').children('li').css('visibility', 'visible');
		if (_options.randomStart)
		{
			_options.start = Math.floor(Math.random()*(container.find('ul').children('li').length));

		}
		_current = _options.start;
		container.find('ul').children('li').eq(_options.start).css('opacity', 1);

		_btnNext.css('opacity', 0);
		_btnPrev.css('opacity', 0);
		assignButtons();
		if (_options.autoRotate)
		{
			_interval = setInterval(function() {next()}, _options.autoRotate);
		}
	}

	function next()
	{
		if (!_in_progress)
		{
			_in_progress = true;
			if (_interval)
			{
				clearInterval(_interval);
				_interval = false;
			}
			container.find('ul').children('li').eq(_current).animate({ opacity : 0 }, {queue: false, duration: _options.speed, complete: function() {
			}});
			if (_current == container.find('ul').children('li').length - 1) _current = 0;
			else _current++;
			container.find('ul').children('li').eq(_current).animate({ opacity : 1 }, {queue: false, duration: _options.speed, complete: function() {
				if (_options.autoRotate && !_mouseovered)
				{
					_interval = setInterval(function() {next()}, _options.autoRotate);
				}
				_in_progress = false;
			}});
		}
	}

	function prev()
	{
		if (!_in_progress)
		{
			_in_progress = true;
			if (_interval)
			{
				clearInterval(_interval);
				_interval = false;
			}
			container.find('ul').children('li').eq(_current).animate({ opacity : 0 }, {queue: false, duration: _options.speed, complete: function() {
			}});
			if (_current == 0) _current = container.find('ul').children('li').length - 1;
			else _current--;
			container.find('ul').children('li').eq(_current).animate({ opacity : 1 }, {queue: false, duration: _options.speed, complete:  function() {
				if (_options.autoRotate && !_mouseovered)
				{
					_interval = setInterval(function() {next()}, _options.autoRotate);
				}
				_in_progress = false;
			}});
		}
	}

	function assignButtons()
	{
		container.mouseover(function() {
			_mouseovered = true;
			_btnNext.css('opacity', 1);
			_btnPrev.css('opacity', 1);
			if (_interval)
			{
				clearInterval(_interval);
				_interval = false;
			}
		});
		container.mouseout(function() {
			_mouseovered = false;
			_btnNext.css('opacity', 0);
			_btnPrev.css('opacity', 0);
			if (_options.autoRotate)
			{
				_interval = setInterval(function() {next()}, _options.autoRotate);
			}
		});
		_btnNext.click(function(event) {
			event.preventDefault();
			next();
		});
		_btnPrev.click(function(event) {
			event.preventDefault();
			prev();
		});
	}
}
