(function($) {
	$.fn.easySlider = function(options) {
		// default configuration properties
		var defaults = {
			prevId : 'prevBtn',
			prevText : 'Previous',
			nextId : 'nextBtn',
			nextText : 'Next',
			controlsShow : true,
			controlsBefore : '',
			controlsAfter : '',
			controlsFade : true,
			firstId : 'firstBtn',
			firstText : 'First',
			firstShow : false,
			lastId : 'lastBtn',
			lastText : 'Last',
			lastShow : false,
			vertical : false,
			speed : 350,
			auto : false,
			pause : 2000,
			continuous : false,
			numeric : false,
			numericId : 'controls'
		};

		var options = $.extend(defaults, options);

		this.each(function() {
					var obj = $(this);
					var s = $("li", obj).length;
					var w = $("li", obj).width();
					var h = $("li", obj).height();
					var clickable = true;
					obj.width(w);
					obj.height(h);
					obj.css("overflow", "hidden");
					var ts = s - 1;
					var currentSlide = 0;
					$("ul", obj).css('width', s * w);

					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);
					};

					if (!options.vertical)
						$("li", obj).css('float', 'left');

					if (options.controlsShow && s > 1) {
						var html = options.controlsBefore;
						html += '<ol id="' + options.numericId + '"></ol>';
						html += '<div id="prevNext">';
						html += '	<span id="' + options.nextId + '"></span>';
						html += '	<span id="' + options.prevId + '"></span>';
						html += '</div>';
						html += '<br style="clear: both" />';
						html += options.controlsAfter;
						$(obj).after(html);
					};

					if (options.numeric && s > 1) {
						for ( var i = 0; i < s; i++) {
							$(document.createElement("li")).attr('id', options.numericId + (i + 1)).html('<a rel=' + i + ' href=\"javascript:void(0);\">' + (i + 1) + '</a>').appendTo(
									$("#" + options.numericId)).click(
									function() {
										animate($("a", $(this)).attr('rel'), true);
									});
						};
					}
					$("#" + options.nextId).click(function() {
						animate("next", true);
					});
					$("#" + options.prevId).click(function() {
						animate("prev", true);
					});

					function setCurrent(i) {
						i = parseInt(i) + 1;
						$("li", "#" + options.numericId).removeClass("current");
						$("li#" + options.numericId + i).addClass("current");
					};
					
					function adjust() {
						if (currentSlide > ts) currentSlide = 0;
						if (currentSlide < 0) currentSlide = ts;
						
						$("ul", obj).css("margin-left",(currentSlide * w * -1));
						
						clickable = true;
					};

					function animate(dir, clicked) {
						if (clickable) {
							clickable = false;

							switch (dir) 
							{
								case "next":
									currentSlide = (currentSlide >= ts) ? (options.continuous ? currentSlide + 1 : ts) : currentSlide + 1;
									break;
								case "prev":
									currentSlide = (currentSlide <= 0) ? (options.continuous ? currentSlide - 1 : 0) : currentSlide - 1;
									break;
								default:
									currentSlide = parseInt(dir);							
									break;
							};

							$("ul", obj).animate({marginLeft : currentSlide * w * -1}, {queue : false, duration : options.speed, complete : adjust});
/*
							$("ul", obj).css({marginLeft : currentSlide * w * -1});
							adjust();
							*/
							if (options.numeric) setCurrent(currentSlide > ts ? 0 : (currentSlide < 0 ? ts : currentSlide));
						};
					};
					
					// init
					if (options.numeric) setCurrent(0);
				});
	};
})(jQuery);

