(function($) {
	var totalCount = 0,selector,options,firstPos = 0,isRunning = false;
	$.fn.simple_slider = function(settings) {
		settings = $.extend({}, $.fn.simple_slider.defaults, settings);
		selector = this.selector;
		options = settings;
		//get the number of images
		totalCount = $(selector + " li").size();
		//init
		_init();
		function _init(){
			//hide them all (with the exception of the first X images)
			$(selector + " li").each(function(i){
				if (i >= options.display){
					this.style.display = "none";
				}
			});
			//put actions (onclick) on the buttons for navigation
			//left
			$("#" + options.leftID).click(function (){
				if (isRunning == false){
					_goLeft();
				}
			});
			//right
			$("#" + options.rightID).click(function (){ 
				if (isRunning == false){
					_goRight();
				}
			});
			$("#" + options.leftID).addClass("pre_end");
			_checkNavigation();
		}
		function _goLeft(){
			isRunning = true;
			if (firstPos > 0){
				//remove the last one
				$(selector + " li:eq("+ (firstPos + options.display - 1) + ")").fadeOut("slow", function (){
					firstPos--;
					//add one from the beginning
					$(selector + " li:eq("+ (firstPos) +")").fadeIn("slow",function(){
						isRunning = false;
						_checkNavigation();
					});
				});				
			} else {
				isRunning = false;
			}
		}
		function _goRight(){
			isRunning = true;
			if (firstPos + options.display < totalCount){
				//remove the first one
				$(selector + " li:eq("+ firstPos +")").fadeOut("slow", function (){
					firstPos++;
					//add one from the end
					$(selector + " li:eq("+ (firstPos + options.display - 1) +")").fadeIn("slow",function(){
						isRunning = false;
						_checkNavigation();
					});
				});				
			} else {
				isRunning = false;
			}
		}
		function _checkNavigation(){
			//left
			if (firstPos == 0){
				$("#" + options.leftID).attr("class","pre_end");
			} else {
				$("#" + options.leftID).attr("class","pre");
			}
			//right
			if ( (firstPos + options.display) >= totalCount){
				$("#" + options.rightID).attr("class","next_end");
			} else {
				$("#" + options.rightID).attr("class","next");
			}
		}
	}
	$.fn.simple_slider.defaults = {
		display				:	3,
		leftID				:	null,
		rightID				:	null
	};
})(jQuery);
