/*
 *
 * @name jquery.filthySlider.js
 * @author Rifat Çağrı Ekin <cagri.ekin@gmail.com>
 * @version v0.0.2
 *
 */

(function( $ ){
	$.fn.filthySlider = function(options) {

		var defaults = {
			speed : 1500,
			animSpeed : 1000,
			pauseOnMouseOver: true,
			debug: false,
			changeEvent: "click",
			autoChange: true
		};
		var options = $.extend(defaults, options);

		return this.each(function() {
			var el = $(this);
			var arr = new Array( );
			var controllers = new Array();

			$(this).find("li").each(function(i,elem){
				arr[i] = $(this);
				if(i > 0) $(this).hide();
			});

			var index = 0;

			/*
			 * set active class in controller
			 */
			if(options.controllerActiveClass){
				options.controllerSelector.find("li:first").addClass(options.controllerActiveClass);
			}

			/*
			 * anonymous function to set active class
			 * @todo: put this somewhere else
			 */
			var setActiveClass = function(i){
				options.controllerSelector.find("li").removeClass(options.controllerActiveClass);
				controllers[index].addClass(options.controllerActiveClass);
			}

			/*
			 * function to execute with timing
			 */
			var onInterval = function(){
				arr[index].fadeOut(options.animSpeed);
				index++;
				if(index == arr.length) index = 0;
				arr[index].fadeIn(options.animSpeed);

				if(options.controllerActiveClass) setActiveClass(index);
			}

			/*
			 * set interval for first time
			 */
			if(options.autoChange){
				var interval = setInterval(onInterval,options.speed);
			}

			/*
			 * bind pause on mouse over
			 */
			if(options.autoChange){
				if(options.pauseOnMouseOver){
					$(this).bind("mouseenter",function(){
						window.clearInterval(interval);
					});
					$(this).bind("mouseleave",function(){
						interval = setInterval(onInterval,options.speed);
					});
				}
			}

			/*
			 * bind controllers
			 */
			if(options.controllerSelector) {
				options.controllerSelector.find("li").each(function(u,elem){
					controllers[u] = $(this);

					$(this).bind(options.changeEvent,function(){
						arr[index].fadeOut(options.animSpeed);
						index = u;
						arr[index].fadeIn(options.animSpeed);

						if(options.controllerActiveClass) setActiveClass(index);

						if(options.autoChange){
							window.clearInterval(interval);
							interval = setInterval(onInterval,options.speed);
						}

					});
				});
			}

			/*
			 * bind move left
			 */
			if(options.leftSelector){
				options.leftSelector.bind("click",function(){

					arr[index].fadeOut(options.animSpeed);
					index--;
					if(index < 0) index = (arr.length - 1);

					arr[index].fadeIn(options.animSpeed);

					if(options.controllerActiveClass) setActiveClass(index);

					if(options.autoChange){
						window.clearInterval(interval);
						interval = setInterval(onInterval,options.speed);
					}

				});
			}

			/*
			 * bind move right
			 */
			if(options.rightSelector){
				options.rightSelector.bind("click",function(){
					arr[index].fadeOut(options.animSpeed);
					index++;
					if(index == arr.length) index = 0;
					arr[index].fadeIn(options.animSpeed);

					if(options.controllerActiveClass) setActiveClass(index);

					if(options.autoChange){
						window.clearInterval(interval);
						interval = setInterval(onInterval,options.speed);
					}

				});
			}


		});
	};
})( jQuery );
