(function($){

$.fn.simpleSlider = function(opt) {

	var defaults = {
		speed: 1000,
		pause: 2000,
		easing: "linear",
		pause_on_hover: true,
		size: 1,
		step: 1,
		loop: false,
		auto: false,
		controls_id: null,
		controls_show: true,
		controls_hide: true,
		prev_id: null,
		prev_text: "Prev",
		next_id: null,
		next_text: "Next",
		container: "ul",
		elements: "li"
	};

	var options = $.extend(defaults,opt);

	this.each(function(){

		var o = $(this);
		var c = $(options.container,o);
		var e = $(options.elements,o);
		var s = e.length;
		var ew = e.outerWidth(true);
		var eh = e.outerHeight(true);
		o.css("overflow","hidden");
		c.css("overflow","hidden");
		if (!options.vertical) {
			o.width(Math.round(parseInt(options.size)*ew));
			c.width((s < (options.size+options.step))? (options.size+options.step)*ew : s*ew);
			e.css("float","left");
		}
		else {
			o.height(Math.round(parseInt(options.size)*eh));
			c.height((s < (options.size+options.step))? (options.size+options.step)*eh : s*eh);
		}
		var index = 0;
		if (options.step > options.size) {
			options.step = options.size;
		}
		if (options.size >= s) {
			$("#"+options.prev_id).hide();
			$("#"+options.next_id).hide();
			options.prev_id = options.next.id = "none";
		}
		if (options.controls_show && options.controls_hide && !options.loop) {
			$("#"+(options.prev_id? options.prev_id : "slider_prev" )).hide();
		}
		var clickable = true;

		if (options.controls_show) {
			var p_control = (options.prev_id)? $("#"+options.prev_id) : $("<span id='slider_prev'>"+options.prev_text+"</span>") ;
			var n_control = (options.next_id)? $("#"+options.next_id) : $("<span id='slider_next'>"+options.next_text+"</span>") ;
			options.prev_id = (options.prev_id)? options.prev_id : "slider_prev" ;
			options.next_id = (options.next_id)? options.next_id : "sider_next" ;
			if (options.controls_id && (options.prev_id && options.next_id)) {
				
			}
			else if (options.controls_id) {
				$("#"+options.controls_id).prepend(p_control).append(n_control);
			}
			else {
				$(o).after($("<span id='slider_controls'></span>").prepend(p_control).append(n_control));
			}
			p_control.bind("click",function(event){
				event.preventDefault();
				if (options.loop) {
					animate_loop("prev",true);
				}
				else {
					animate_no_loop("prev",true);
				}
			});
			n_control.bind("click",function(event){
				event.preventDefault();
				if (options.loop) {
					animate_loop("next",true);
				}
				else {
					animate_no_loop("next",true);
				}
			});
		}

		function animate_no_loop(dir,clicked) {
			if (clickable) {
				clickable = false;
				var d = (dir)? dir : "next" ;
				var overflow = 0;
				var animate_dir = "";
				var new_index = "";
				switch (d) {
					case "next":
						animate_dir = "-";
						new_index = index+options.step;
						overflow = $(options.elements+":gt("+(index+options.size-1)+")",o).length;
						break;
					case "prev":
						animate_dir = "+";
						new_index = ((index-options.step) >= 0)? index-options.step : 0 ;
						overflow = $(options.elements+":lt("+(index)+")",o).length;
						break;
					default:
						break;
				}
				if (overflow > 0) {
					if (overflow >= options.step) {
						if (!options.vertical) {
							c.animate({"margin-left":animate_dir+"="+(options.step*ew)+"px"},{queue: false, duration: options.step*options.speed, easing: options.easing});
						}
						else {
							c.animate({"margin-top":animate_dir+"="+(options.step*eh)+"px"},{queue: false, duration: options.step*options.speed, easing: options.easing});
						}
					}
					else {
						if (!options.vertical) {
							c.animate({"margin-left":animate_dir+"="+((options.step-overflow)*ew)+"px"},{queue: false, duration: options.step*options.speed, easing: options.easing});
						}
						else {
							c.animate({"margin-top":animate_dir+"="+((options.step-overflow)*eh)+"px"},{queue: false, duration: options.step*options.speed, easing: options.easing});
						}
						new_index = ((new_index-index) > overflow)? index+overflow : new_index ;
					}
					index = new_index;
				}
				if (options.controls_show && options.controls_hide) {
					if (index == 0) {
						$("#"+options.prev_id).hide();
						$("#"+options.next_id).show();
					}
					else if (index == s-options.size) {
						$("#"+options.next_id).hide();
						$("#"+options.prev_id).show();
					}
					else {
						$("#"+options.prev_id).show();
						$("#"+options.next_id).show();
					}
				}
				clickable = true;
				if (clicked) {
					clearTimeout(timeout);
				}
				if (options.auto) {
					timeout = setTimeout(function(){
						animate_no_loop("next",false);
					},options.pause);
				}
			}
		}

		function animate_loop(dir,clicked) {
			if (clickable) {
				clickable = false;
				var d = (dir)? dir : "next" ;
				var overflow = 0;
				var animate_dir = "";
				var new_index = "";
				switch (d) {
					case "next":
						animate_dir = "-";
						new_index = index+options.step;
						overflow = $(options.elements+":gt("+(index+options.size-1)+")",o).length;
						break;
					case "prev":
						animate_dir = "+";
						new_index = index-options.step;
						overflow = $(options.elements+":lt("+(index)+")",o).length;
						break;
					default:
						break;
				}
				if (overflow >= options.step) {
					if (!options.vertical) {
						c.animate({"margin-left":animate_dir+"="+(options.step*ew)+"px"},{queue: false, duration: options.step*options.speed, easing: options.easing});
					}
					else {
						c.animate({"margin-top":animate_dir+"="+(options.step*eh)+"px"},{queue: false, duration: options.step*options.speed, easing: options.easing});
					}
					index = new_index;
				}
				else {
					var buffor = options.step-overflow;
					var re = null;
					switch (d) {
						case "next":
							re = $(options.elements+":lt("+buffor+")");
							//re.remove();
							re.clone(true).appendTo(c);
							if (!options.vertical) {
								//c.css("margin-left",parseInt(c.css("margin-left"))+options.step*ew);
								c.animate(
									{"margin-left":animate_dir+"="+(options.step*ew)+"px"},
									{queue: false, duration: options.step*options.speed, complete: function(){
											re.remove();
											c.css("margin-left",parseInt(c.css("margin-left"))+re.length*ew);
										}});
							}
							else {
								//c.css("margin-top",parseInt(c.css("margin-top"))+options.step*eh);
								c.animate(
									{"margin-top":animate_dir+"="+(options.step*eh)+"px"},
									{queue: false, duration: options.step*options.speed, complete: function(){
											re.remove();
											c.css("margin-top",parseInt(c.css("margin-top"))+re.length*eh);
										}});
							}
							break;
						case "prev":
							re = $(options.elements+":gt("+(e.length-buffor-1)+")");
							//re.remove();
							re.clone(true).prependTo(c);
							if (!options.vertical) {
								//c.css("margin-left",parseInt(c.css("margin-left"))-options.step*ew);
								c.animate(
									{"margin-left":animate_dir+"="+(options.step*ew)+"px"},
									{queue: false, duration: options.step*options.speed, complete: function(){
											re.remove();
											c.css("margin-left",parseInt(c.css("margin-left"))-re.length*ew);
										}});
							}
							else {
								//c.css("margin-top",parseInt(c.css("margin-top"))-options.step*eh);
								c.animate(
									{"margin-top":animate_dir+"="+(options.step*eh)+"px"},
									{queue: false, duration: options.step*options.speed, complete: function(){
											re.remove();
											c.css("margin-top",parseInt(c.css("margin-top"))-re.length*eh);
										}});
							}
							break;
						default:
							break;
					}
				}
				
				clickable = true;
				if (clicked) {
					clearTimeout(timeout);
				}
				if (options.auto && options.loop) {
					timeout = setTimeout(function(){
						animate_loop("next",false);
					},options.pause);
				}
			}
		}

		var timeout;

		if (options.auto && options.loop) {
			timeout = setTimeout(function(){
				animate_loop("next",false);
			},options.pause);
		}
		
		if (options.pause_on_hover && options.auto && options.loop) {
			o.hover(function(){
				clearTimeout(timeout)
			},function(){
				animate_loop("next",false);
			})
		}

	});
}

})(jQuery);
