// JavaScript Document

(function($) {
    $.fn.extend({
        scroller: function(options) {
			
			var defaults = {
    			  nextId: 'nextBtn',
				  prevId: 'prevBtn',
				  scrollValue: 50,
				  speed: 500,
				  hideButtons: 'false',
				  direction: 'horizontal',
				  viewWidth: 'auto',
				  viewHeight: 'auto'
			};

			
			var options = $.extend(defaults, options);
			
			return this.each(function() { 
				var obj = $(this);
				var op  = options;
			
			$('#' + op.prevId).css({'cursor': 'pointer'});
			$('#' + op.nextId).css({'cursor': 'pointer'});
			
			var content  = obj.html();
			var innerBox = '';
			
			if (op.direction == 'horizontal') {
				innerBox = '<div style="width:' + op.viewWidth + ';height:' + op.viewHeight + ';">' + content + '</div>';
			} else { 
				innerBox = '<div style="height:' + op.viewHeight + ';width:' + op.viewWidth + ';">' + content + '</div>';	
			}
			
			obj.html(innerBox);

			var childHeight = obj.children('div:first').height();
			var childWidth  = obj.children('div:first').width();
			
			if (op.direction == 'horizontal' && childHeight <= obj.height() || op.direction == 'vertical' && childWidth <= obj.width()) { 
				$('#' + op.prevId).hide();
				//$('#' + op.nextId).hide();	
			}
			
			
			if (op.hideButtons == 'true') {
				$('#' + op.prevId).hide();	
			}
			
			
			if (op.direction == 'horizontal') {
				$('#' + op.nextId).click(function(e){
					obj.scrollDown(obj, op.scrollValue, op.speed, op.hideButtons, op.nextId, op.prevId);	
				});
				
				$('#' + op.prevId).click(function(e){
					obj.scrollUp(obj, op.scrollValue, op.speed, op.hideButtons, op.nextId, op.prevId);	
				});
			} else if (op.direction == 'vertical') {
				$('#' + op.nextId).click(function(e){
					obj.scrollRight(obj, op.scrollValue, op.speed, op.hideButtons, op.nextId, op.prevId);	
				});
				
				$('#' + op.prevId).click(function(e){
					obj.scrollLeft(obj, op.scrollValue, op.speed, op.hideButtons, op.nextId, op.prevId);	
				});
			}
			
			})
        }
    });
	
	$.fn.scrollRight = function(parent, scrollValue, speed, hideButtons, nextBtn, prevBtn)
	{
		var parentInfo   = parent.offset();
		var target       = parent.children('div:first');
		var targetInfo   = target.offset();
		var nextPosition = (targetInfo.left - scrollValue); 
		var maxValue     = ((parentInfo.left + parent.width()) - target.width());
		
		if (nextPosition > maxValue) {
			target.animate({marginLeft: "-=" + scrollValue + "px"}, speed);
			
			if (hideButtons == 'true') {
				$('#' + prevBtn).fadeIn(500);	
			}
		} else {
			var buffer   = (maxValue - nextPosition);
			var newValue = (scrollValue - buffer);
			target.animate({marginLeft: "-=" + newValue + "px"}, speed);
			
			if (hideButtons == 'true') {
				$('#' + prevBtn).fadeIn(500);
				$('#' + nextBtn).fadeOut(500);	
			}	
		}
	}
	
	$.fn.scrollLeft = function(parent, scrollValue, speed, hideButtons, nextBtn, prevBtn)
	{
		var parentInfo   = parent.offset();
		var target       = parent.children('div:first');
		var targetInfo   = target.offset();
		var nextPosition = (targetInfo.left + scrollValue); 
		var maxValue     = parentInfo.left;
		
		if (nextPosition <= maxValue) {
			target.animate({marginLeft: "+=" + scrollValue + "px"}, speed);
			
			if (hideButtons == 'true') {
				$('#' + nextBtn).fadeIn(500);	
			}
			
			if (nextPosition == maxValue) {
				$('#' + prevBtn).fadeOut(500);	
			}
		} else {
			target.animate({marginLeft: "0px"}, speed);
			
			if (hideButtons == 'true') {
				$('#' + nextBtn).fadeIn(500);
				$('#' + prevBtn).fadeOut(500);	
			}	
		}
	}
	
	$.fn.scrollDown = function(parent, scrollValue, speed, hideButtons, nextBtn, prevBtn)
	{
		var parentInfo   = parent.offset();
		var target       = parent.children('div:first');
		var targetInfo   = target.offset();
		var nextPosition = (targetInfo.top - scrollValue); 
		var maxValue     = (parentInfo.top + parent.height());
		
		if (nextPosition > (maxValue - target.height())) {
			target.animate({marginTop: "-=" + scrollValue + "px"}, speed);
			
			if (hideButtons == 'true') {
				$('#' + prevBtn).fadeIn(500);	
			}
		} else {
			var buffer = ((maxValue - target.height()) - nextPosition);
			var newValue = (scrollValue - buffer);
			target.animate({marginTop: "-=" + newValue + "px"}, speed);
			
			if (hideButtons == 'true') {
				$('#' + prevBtn).fadeIn(500);	
				$('#' + nextBtn).fadeOut(500);	
			}	
		}
	}
	
	$.fn.scrollUp = function(parent, scrollValue, speed, hideButtons, nextBtn, prevBtn)
	{
		var parentInfo   = parent.offset();
		var target       = parent.children('div:first');
		var targetInfo   = target.offset();
		var nextPosition = (targetInfo.top + scrollValue); 
		var maxValue     = parentInfo.top;
		
		if (nextPosition < maxValue) {
			$(target).animate({marginTop: "+=" + scrollValue + "px"}, speed);
			
			if (hideButtons == 'true') {
				$('#' + nextBtn).fadeIn(500);	
			}
		} else {
			$(target).animate({marginTop: "0px"}, speed);
			
			if (hideButtons == 'true') {
				$('#' + nextBtn).fadeIn(500);	
				$('#' + prevBtn).fadeOut(500);	
			}
		}
	}
	
})(jQuery);



