(function($) {
	$.fn.easySlider = function(options){
		var defaults = {
			prevId: 		'prevBtn',
			prevText: 		' ',
			nextId: 		'nextBtn',	
			nextText: 		' ',
			speed: 			800,
			continuous:		false
		}; 
		var options = $.extend(defaults, options);  
		return this.each(function() {
			obj = $(this);
			var s = $("li", obj).length;
			var w = obj.width();
			var ts = s;
			var t = 0;
			var is_animate = false;
			
			//Propietats per slider de forma continua
			if(options.continuous){
				$("ul", obj).prepend($("ul li:last-child", obj).clone().css("margin-left","-"+ w +"px"));
				$("ul", obj).append($("ul li:nth-child(2)", obj).clone());
				$("ul", obj).css('width',(s+1)*w);
			};
			
			//Mesurem el ample total dels elements
			$("ul", obj).css('width',w*s);
			$("li", obj).css('float','left');

			//Amagar butons si no hi ha més d'una imatge
			if(s<=1) {
				$("a","#"+options.prevId).hide();
				$("a","#"+options.nextId).hide(); 
			}

			//Events
			$("a","#"+options.prevId).mouseover(function(){
				options.continuous = true;
				if(!is_animate){
					animate("prev");
				}
			});
			$("a","#"+options.nextId).mouseover(function(){
				options.continuous = true;
				if(!is_animate){
					animate("next");
				}
			});
			$("a","#"+options.prevId).mouseout(function(){
				options.continuous = false;
			});
			$("a","#"+options.nextId).mouseout(function(){
				options.continuous = false;
			});

			function animate(dir){
				if (! options.continuous) {
					is_animate = false;
					return;
				}
				
				is_animate = true;
				if(dir == "next"){
					t = (t>=ts) ? ts : t+1;
				} else {
					t = (t<=0) ? 0 : t;
				};
				
				var width = 0;
				var p = 0;
				if(dir=='next'){
					if(t<ts){
						width = $('#photo'+t).width()+5;
					}
				}
				else{
					if(t>=0){
						if(t==0){ t=1; }
						if(t==ts){t--;}
						width = $('#photo'+t).width()+5;
						t--;
					}
				}
				var marge = "";
				marge = $('ul', obj).css('margin-left');
				marge = marge.replace("px","");
				marge = parseInt(marge);
				if(dir=='next'){
					if(t==ts){
						is_animate = false;
						return;
					}
					else{
						p = (marge + width*-1);
					}
				}
				else{
					if(t==0){
						p = 0;
					}
					else{
						p = (marge + width);
					}
				}
				if(p != 0){$("ul",obj).animate({ marginLeft: p }, options.speed, function(){animate(dir);});}
				else {$("ul",obj).animate({ marginLeft: p }, options.speed, function(){animate(dir); is_animate = false; return;});}
			};
		});
	};
})(jQuery);