var Scroll = Class.create(
{
	initialize: function(inElement, inOptions)
	{
		this.element = $(inElement);
		
		if(!this.element)
		{
			return;
		}
		
		this.options = {
			scrollTo: null,
			position: 0,
			height: 0,
			ignore: "max",
			handle: null,
			table: false,
			save: true
		};
			
		Object.extend(this.options, inOptions);
		
		// -------
		
		this.element.setStyle({
			overflowY:(this.element.style.overflowY.length == 0?"scroll":this.element.style.overflowY),
			overflowX:"hidden"
		});
		
		this.minimum = parseInt(this.element.getStyle("minHeight"));
		this.maximum = parseInt(this.element.getStyle("maxHeight"));
		
		if(this.options.ignore == "max" || this.options.ignore == "both")
		{
			this.maximum = 0;
		}
		
		if(this.options.ignore == "min" || this.options.ignore == "both")
		{
			this.minimum = 0;
		}
		
		// -------
		
		var positionID = "scroll_" + this.element.identify();
		
		this.position = $(positionID);
		
		if(!this.position)
		{
			this.position = this.element.insert({before: "<input type='hidden' name='" + positionID + "'>"}).previous();
		}

		// -------
		
		this.resizing = false;
		
		if(this.options.handle)
		{
			this.handle = $(this.options.handle);
			
			if(this.handle)
			{
				this.handle.setStyle({cursor: 'row-resize'});
			
				this.handle.observe('mousedown', this.mousedown.bindAsEventListener(this));
			}
		}
		
		if(this.options.height > 0)
		{
			var styles = {}
			
			if(this.maximum == 0)
			{
				styles.maxHeight = "";
			}
			
			if(this.minimum == 0)
			{
				styles.minHeight = "";
			}
			
			styles.height = this.options.height + "px";
		
			this.element.setStyle(styles);
		}
		else
		{
			var maximize = $(this.options.maximize);
			
			if(maximize)
			{
				var parent = maximize.up("." + this.options.maximizeTo);
				
				if(parent)
				{
					var styles = {}
					
					styles.height = (this.element.getHeight() + parent.getHeight() - maximize.getHeight()) + "px";
					
					this.element.setStyle(styles);
				}
			}
		}
		
		// ------------
		
		if(this.options.scrollTo)
		{
			var scrollTo = this.element.select('.' + this.options.scrollTo).first();
		
			if(scrollTo)
			{
				this.element.scrollTop = scrollTo.positionedOffset().top;
			}
		} 
		else if(this.options.position > 0)
		{
			this.element.scrollTop = this.options.position;
		}

		// ------------

		if(this.options.table)
		{
			this.lastline = this.element.select('.lastline').first();
			
			this.updateLastLine();
		}
	
		this.element.observe('scroll', this.update.bindAsEventListener(this));
		
		document.observe('behavior:resize', this.update.bindAsEventListener(this));
		
		document.observe('behavior:update', this.update.bindAsEventListener(this));
	},
	
	update: function()
	{
		this.updateLastLine();
		
		if(this.options.save)
		{
			this.position.value = this.element.scrollTop + "," + this.element.getHeight();
		}
	},
	
	updateLastLine: function()
	{
		if(this.lastline)
		{
			if(this.element.getHeight() >= this.element.scrollHeight)
			{
				this.lastline.setStyle({top: "0px"});
			}
			else
			{
				this.lastline.setStyle({top: "-1px"});
			}
		}
	},
	
	mousedown: function(inEvent)
	{
		if(this.handle)
		{
			if(inEvent.element().hasClassName("input"))
			{
				return;
			}
			
			this.mousemoveFunction = this.mousemove.bindAsEventListener(this);
			this.mouseupFunction = this.mouseup.bindAsEventListener(this);

			document.observe('mousemove', this.mousemoveFunction);
			document.observe('mouseup', this.mouseupFunction);
		
			this.sourceY = inEvent.pointerY();
			this.sourceHeight = this.element.getHeight();
			this.resizing = true;
			
			var styles = {}
			
			if(this.maximum == 0)
			{
				styles.maxHeight = "";
			}
			
			if(this.minimum == 0)
			{
				styles.minHeight = "";
			}
			
			styles.height = this.sourceHeight + "px";
		
			this.element.setStyle(styles);

			Event.stop(inEvent);
		}
	},
	
	mousemove: function(inEvent)
	{
		if(this.handle && this.resizing)
		{
			var height = this.sourceHeight + inEvent.pointerY() - this.sourceY;
	
			if(this.minimum > 0 && height < this.minimum)
			{
				height = this.minimum;
			}
	
			if(this.maximum > 0 && height > this.maximum)
			{
				height = this.maximum;
			}

			this.element.setStyle({height: height + "px"});
			
			document.fire('behavior:resize');
		}
	},
	
	mouseup: function(inEvent)
	{
		if(this.handle && this.resizing)
		{
			document.stopObserving('mousemove', this.mousemoveFunction);
			document.stopObserving('mouseup', this.mouseupFunction);
			
			this.resizing = false;
		}
	}
});