(function($){
	$.fn.referenceSlider = function(options) {   
		var defaults = {
			orientation: 'horizontal',
			pagerPlus: null,
			pagerMinus: null,
			animationSpeed: 200,
			itemContainer: null,
			itemSelector: '',
			itemsPerPage: 4
		};
	
		var options = $.extend(defaults, options);
		
		return this.each(function() {
			var $cont = options.itemContainer;
			var $items = $cont.find(options.itemSelector);
			var itemCount = $items.length;
			
			if (itemCount < 1)
				return false;
			
			var itemSize = {
				width : $($items[0]).outerWidth(true),
				height : $($items[0]).outerHeight(true)
			};
			
			if (options.orientation == "horizontal"){
				var stepSize = itemSize.width * options.itemsPerPage;
			} else if (options.orientation == "vertical"){
				var stepSize = itemSize.height * options.itemsPerPage;
			}
			
			var isFirstPage = function(){
				if (options.orientation == "horizontal"){
					if (parseInt($cont.css("left").replace("px", "")) == 0)
						return true;
					else 
						return false;										
				} else if (options.orientation == "vertical"){
					if (parseInt($cont.css("top").replace("px", "")) == 0)
						return true;
					else 
						return false;
				}
			}
			var isLastPage = function(){
				if (options.orientation == "horizontal"){				
					if ($cont.outerWidth() <= Math.abs(parseInt($cont.css("left").replace("px", ""))) + stepSize)
						return true;
					else 
						return false;										
				} else if (options.orientation == "vertical"){
					if ($cont.outerHeight() <= Math.abs(parseInt($cont.css("top").replace("px", ""))) + stepSize)
						return true;
					else 
						return false;
				}
			}
			
			var refreshPagers = function(){				
				if (itemCount <= options.itemsPerPage){
					options.pagerMinus.fadeOut(options.animationSpeed);
					options.pagerPlus.fadeOut(options.animationSpeed);
					return true;
				}
				if (isFirstPage())
					options.pagerMinus.fadeOut(options.animationSpeed);
				else 
					options.pagerMinus.fadeIn(options.animationSpeed);
				
				if (isLastPage())
					options.pagerPlus.fadeOut(options.animationSpeed);
				else 
					options.pagerPlus.fadeIn(options.animationSpeed);
			}
			
			if (options.orientation == "horizontal"){
				$cont.css({width: (itemSize.width * itemCount) + "px", left:0});
				refreshPagers();
				options.pagerPlus.click(function(e){
					if (isLastPage())
						return false;
					var curLeft = parseInt($cont.css("left").replace("px", ""));
					var newLeft = (curLeft - stepSize);
					$cont.stop(false, true);
					$cont.animate({left:newLeft + "px"}, options.animationSpeed, null, refreshPagers);
					return false;
				});
				options.pagerMinus.click(function(e){
					if (isFirstPage())
						return false;
					var curLeft = parseInt($cont.css("left").replace("px", ""));
					var newLeft = (curLeft + stepSize);
					if (newLeft > 0)
						newLeft = 0;
					$cont.stop(false, true);
					$cont.animate({left:newLeft + "px"}, options.animationSpeed, null, refreshPagers);
					return false;
				});
			} else if (options.orientation == "vertical"){
				$cont.css({height: (itemSize.width * itemCount) + "px", top: 0});				
				options.pagerPlus.click(function(e){
					if (isLastPage())
						return false;
					var curTop = parseInt($cont.css("top").replace("px", ""));
					var newTop = (curTop - stepSize);
					$cont.stop(false, true);
					$cont.animate({top:newTop + "px"}, options.animationSpeed, null, refreshPagers);
					return false;
				});
				options.pagerMinus.click(function(e){
					if (isFirstPage())
						return false;
					var curTop = parseInt($cont.css("top").replace("px", ""));
					var newTop = (curTop + stepSize);
					if (newTop > 0)
						newTop = 0;
					$cont.stop(false, true);
					$cont.animate({top:newTop + "px"}, options.animationSpeed, null, refreshPagers);
					return false;
				});
			} else 
				throw "Invalid orientation";
		});
	};	
})(jQuery);
