ScrollArea = Class.create();
ScrollArea.prototype = {
	
	slider: null,
	
	orientation: null,
	
	content: null,
	track: null,
	handle: null,
	
	initialize: function(orientation,content,track,handle) {
	
		this.orientation 	= orientation;
		this.content		= content;
		this.track			= track;
		this.handle			= handle;
				
		//this.update();		
		
		ScrollArea.all.push(this);
		
	},
	
	set_scroll: function(v) {
		if (this.orientation == 'vertical') {
		    var outerH = Element.getDimensions($(this.content).parentNode).height;
		    var innerH = Element.getDimensions($(this.content)).height;

		    var h = innerH - outerH;

		    var top = -1.0 * v * h;


		    $(this.content).style.top = top;
			
			this.check_for_cuts();
		}
	},
	
	check_for_cuts: function() {
		if (this.slider && Element.visible(this.track)) {
	
			var v = this.slider.value;
	
			if (v>0) {
				$(this.content).parentNode.parentNode.addClassName('top_cut');
			}
			else {
				$(this.content).parentNode.parentNode.removeClassName('top_cut');
			}
			
			if (v<1) {
					$(this.content).parentNode.parentNode.addClassName('bottom_cut');
				}
				else {
					$(this.content).parentNode.parentNode.removeClassName('bottom_cut');
				}
		}
	},
	
	reset: function() {

		$(this.handle).style.top = '0px';
	
		if (this.slider) {
			if (this.slider) this.slider.setValue(0);
		}
		
		this.update();
	},
	
	update: function() {
				
		if (this.orientation == 'vertical') {
		    var outerH = Element.getDimensions($(this.content).parentNode).height;
		    var innerH = Element.getDimensions($(this.content)).height;
			
			if (outerH>=innerH)	{
				$(this.handle).style.top = '0px';
				if (this.slider) this.slider.setValue(0);
				Element.hide(this.track);
				
				this.check_for_cuts();
			}
			else {
				var value = -1;
				if (this.slider) {
					value = this.slider.value;
					this.slider.setDisabled();
				}

				$(this.track).addClassName('inited');


				Element.show(this.track);

				var obj = this;
				this.slider = new Control.Slider(this.handle,this.track, {
				        axis: this.orientation,
				        onSlide: function(v) {
				            obj.set_scroll(v);
				            $(obj.track).addClassName('active');
				        },
				        onChange: function(v) {
				            obj.set_scroll(v);
				            $(obj.track).removeClassName('active');
				        }
				    });
				
				if (value>-1) {
					this.slider.setValue(value);
				}
			}
		}
		
		this.check_for_cuts();
		
	}
	
}


ScrollArea.all = new Array();
ScrollArea.update_all = function() {
	for (var i=0; i<ScrollArea.all.length; i++) {
		ScrollArea.all[i].update();
	}
}


//Event.observe(window,'resize',ScrollArea.update_all);
