(function($) {
	$.fn.slider = function(options){
		// default configuration properties
		var defaults = {
			itemsWidth: 80,
			itemsCount: 6,
			currentEdge: 0,
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',
			nextText: 		'Next',
			orientation:	'', //  'vertical' is optional;
			speed: 			'normal',
			slideMultiplier:1
		};
		var options = $.extend(defaults, options);
		//alert( options.itemsCount );

		return this.each(function() {
			obj = $(this);
			var s = $("li", obj).length; //items in list

			var w = obj.width();//visible width
			var h = obj.height();

			options.objId = $( obj ).attr("id");

			options.total = s-1;

			var vertical = ( options.orientation == 'vertical' );

			$("ul", obj).css('width', s * w);

			if(!vertical) $("li", obj).css('float','left');

			$("#"+options.nextId).click(function(){ animate("next"); });
			$("#"+options.prevId).click(function(){ animate("prev"); });

			if ( options.total + 1 <= options.itemsCount ) {
				$("#"+options.nextId).remove( );
				$("#"+options.prevId).remove( );
			}


			if ( options.currentEdge > 0 ) {
				var border = options.itemsCount / 2 - 1;
				if ( options.currentEdge - border > 0 )
					options.currentEdge -= border;
				if ( options.currentEdge + options.itemsCount - 1 > options.total )
					options.currentEdge = options.total - ptions.itemsCount + 1;

				animate( "next", true );
			}

			function animate(dir, forceDraw){
				var proceed = false || forceDraw;
				if ( !forceDraw ) {
					//alert( options.total + ":" + options.itemsCount + ":" + options.currentEdge );
					if(dir == "next"){
						var edgeInc = options.total + 1 - options.currentEdge - options.itemsCount;
						if ( edgeInc > 0 ) {
							edgeInc = edgeInc < options.slideMultiplier ? edgeInc : options.slideMultiplier;
							options.currentEdge += edgeInc;
							//t = ( t >= options.total ) ? ts : t+1;
							proceed = true;
						}
					} else {
						if ( options.currentEdge > 0 ) {
							var edgeDec = options.currentEdge >= options.slideMultiplier ?  options.slideMultiplier : options.currentEdge;

							if ( edgeDec > 0 ) {
								options.currentEdge -= edgeDec;
								proceed = true;
							}
						}
					}
				}

				if ( !proceed )
					return;
				if(!vertical) {
					p = -1 * options.currentEdge * options.itemsWidth;
					$("ul", $( "#" + options.objId ) ).animate(
						//{ marginLeft: p },
						{ left : p + 'px'},
						options.speed
					);
				}
				else {
					p = (options.currentEdge*h*-1);
					$("ul", $( "#" + options.objId ) ).animate(
						{ marginTop: p },
						options.speed
					);
				}
			};

			// if( s>1 ) $( "a","#"+options.nextId ).fadeIn();
		});

	};

})(jQuery);
