// Graham Leach wrote this JQuery plugin

(function($){
	$.fn.scrollPane = function(parameters){
		if(parameters.direction != 'width' && parameters.direction != 'height'){ throw 'invalid direction: '+parameters.direction; return; }
		if(typeof(parameters.time) != 'number'){ throw 'time must be a positive number'; return; }
		this.data('scrollDirection', parameters.direction);
		this.data('scrollTime', parameters.time);
		this.append('<div class="scrollElement" style="margin:0px;"></div>').children().each(function(){
			if(!$(this).hasClass('scrollElement')){
				$(this).parent().children('.scrollElement:first').append($(this).detach());
			}
		});
		this.children('.scrollElement:first').expand();
		return this;
	}
	$.fn.expand = function(){
		var px = 0;
		this.children().each(function(){
			switch($(this).parent().parent().data('scrollDirection')){
				case 'width'	:
					$(this).css('float', 'left');
					px = px + $(this).outerWidth(true);
					break;
				case 'height'	:
					$(this).css('clear', 'both');
					px = px + $(this).outerHeight(true);
					break;
			}
		});
		return this.css(this.parent().data('scrollDirection'), px+'px');
	}
	$.fn.previous = function(){
		switch(this.data('scrollDirection')){
			case 'width'	: return this.moveLeft(); break;
			case 'height'	: return this.moveUp(); break;
			default			: throw this.attr('id')+'.direction is undefined'; break
		}
	}
	$.fn.next = function(){
		switch(this.data('scrollDirection')){
			case 'width'	: return this.moveRight(); break;
			case 'height'	: return this.moveDown(); break;
			default			: throw this.attr('id')+'.direction is undefined'; break
		}
	}
	$.fn.moveLeft = function(){
		scrollElement = this.children('.scrollElement:first');
		marginLeft = scrollElement.extractPX('marginLeft');
		width = this.width();
		if(marginLeft >= 0){ return; }
		else if(marginLeft > (0 - width)){ target = 0; }
		else{ target = marginLeft + width; } 
		scrollElement.animate({ marginLeft: target+'px'}, this.data('scrollTime'));
		return this;
	}
	$.fn.moveRight = function(){
		scrollElement = this.children('.scrollElement:first');
		marginLeft = scrollElement.extractPX('marginLeft');
		scrollElementWidth = scrollElement.width();
		width = this.width();
		if(marginLeft - width <= 0 - scrollElementWidth){ return; }
		else{ target = marginLeft - width; } 
		scrollElement.animate({ marginLeft: target+'px'}, this.data('scrollTime'));
		return this;
	}
	$.fn.moveUp = function(){
		scrollElement = this.children('.scrollElement:first');
		marginTop = scrollElement.extractPX('marginTop');
		height = this.height();
		if(marginTop >= 0){ return; }
		else if(marginTop > (0 - height)){ target = 0; }
		else{ target = marginTop + height; } 
		scrollElement.animate({ marginTop: target+'px'}, this.data('scrollTime'));
		return this;
	}
	$.fn.moveDown = function(){
		scrollElement = this.children('.scrollElement:first');
		marginTop = scrollElement.extractPX('marginTop');
		scrollElementHeight = scrollElement.height();
		height = this.height();
		if(marginTop - height <= 0 - scrollElementHeight){ return; }
		else{ target = marginTop - height; }
		//alert(marginTop+' - '+height+' - '+target);
		scrollElement.animate({ marginTop: target+'px'}, this.data('scrollTime'));
		return this;
	}
	$.fn.extractPX = function(parameter){
		string = this.css(parameter);
		return string.replace('px', '') * 1;	
	}
})(jQuery);
