/**
 * jQuery Simple Slider Plugin
 * @name: jquery.simpleSlider-2.0.js
 * @author: André Mazzitelli - http://mazzitelli.com.br
 * @version: 2.0
 * @date: January 4, 2012
 * @category: jQuery plugin
 * @examples: Please, visit http://mazzitelli.com.br for more informations about this jQuery plugin
 */
(function($){

	$.fn.simpleSlider = function(customOptions){

		var options = {
			'interactive'           : false,
			'appendNavControlTo'    : '#navControl',
			'automatic'             : true,
			'previousButton'        : '#previousButton',
			'previousButtonContent' : 'previous',
			'nextButton'            : '#nextButton',
			'nextButtonContent'     : 'next',
			'navItemClass'          : 'navItem',
			'arrowNavItemClass'     : 'arrowNavItem',
			'numericNavItemClass'   : 'numericNavItem',
			'navItemActiveClass'    : 'active',
			'initial'               : 'random',
			'delay'                 : 5000,
			'fadeSpeed'             : 500
		};

		if(customOptions) $.extend(options, customOptions);

		return this.each(function(){

			var slide = this.children;
			var amount = slide.length;
			var currentIndex = (options.initial == 'random') ? getRandomSlide() : options.initial;
			if(options.automatic) var automaticLoop;

			function getRandomSlide()
			{
				return Math.floor(Math.random() * amount);
			}

			function init()
			{
				if(options.interactive) createControlNav();
				changeSlide(currentIndex);
			}

			function createControlNav()
			{	
				if(amount > 1) 
				{
					var controlNav = '<div class="' + options.navItemClass + ' ' + options.arrowNavItemClass + '" id="' + options.previousButton.substring(1) + '">' + options.previousButtonContent + '</div>';
					for(var i = 0; i < amount; i++)
					{
						controlNav += '<div data-index="' + i + '" class="' + options.navItemClass + ' ' + options.numericNavItemClass + '">' + (i + 1) + '</div>';
					}
					controlNav += '<div class="' + options.navItemClass + ' ' + options.arrowNavItemClass + '" id="' + options.nextButton.substring(1) + '">' + options.nextButtonContent + '</div>';
					$(options.appendNavControlTo).prepend(controlNav);
					setListeners();
				}
				else
				{
					$(options.previousButton).css('display', 'none');
					$(options.nextButton).css('display', 'none');
				}
			}

			function setListeners()
			{
				$(options.previousButton).click(function(){
					changeSlide('previous');
				});
				$(options.nextButton).click(function(){
					changeSlide('next');
				});
				$('.' + options.numericNavItemClass).bind('click', function(){
					if($(this).data('index') != currentIndex) changeSlide($(this).data('index'));
				});
			}

			function startLoop()
			{
				automaticLoop = setInterval(function(){
					changeSlide('next');
				}, options.delay)
			}

			function changeSlide(slideIndex)
			{
				if(amount > 1 && options.automatic) clearInterval(automaticLoop);
				fadeCurrentSlide('out');
				if(slideIndex == 'next') currentIndex = (currentIndex + 1 == amount) ? 0 : currentIndex + 1;
				else if(slideIndex == 'previous') currentIndex = (currentIndex - 1 < 0) ? amount - 1 : currentIndex - 1;
				else currentIndex = slideIndex;
				if(options.interactive)
				{
					$('.' + options.numericNavItemClass).each(function(){
						$(this).removeClass(options.navItemActiveClass);
						if($(this).data('index') == currentIndex) $(this).addClass(options.navItemActiveClass);
					});
				}
				if(fadeCurrentSlide('in'))
				{
					if(amount > 1 && options.automatic) startLoop();
				}
			}

			function fadeCurrentSlide(type)
			{
				if(type == 'in')
				{
					if($(slide[currentIndex]).fadeIn(options.fadeSpeed, function(){return true})) return true;
				}				
				else
				{
					if($(slide[currentIndex]).fadeOut(options.fadeSpeed, function(){return true})) return true;
				}
			}

			init();

		});

	};

})(jQuery);
