
var Scrollbar = new Class({
    Extends: Slider,
	options: {
		orientation: 'vertical',
		discreet: false,
		divisor: 1.2,
		cursor: 14,
		wheel: false
	},    
 	initialize: function(frame, wrapper, options) {
		this.setOptions(options);
		this.wrapper = wrapper;
		this.frame = frame;
		
		var cn = (this.options.orientation == 'vertical') ? '.vscrollbar' : '.hscrollbar';
		this.parent(frame.getElement(cn), this.options);
		this.setPosition();
		this.setCursor();
		
		//if (this.options.discreet)
			//new DiscreetSlider(this.frame, this.container, this);

		if (this.options.wheel)
			this.frame.cloneEvents(this.container, 'mousewheel');
					
		this.addEvent('onChange', function() {
			wrapper.setStyle(this.mode.direction, -this.getValue());
		});
	},
	mousedown: function(event) {
		if (this.options.max < 0) return;
		this.parent(event);
	},
	mousewheel: function(event) {
		if (this.options.max < 0) return;
		this.parent(event);
	},		
	setPosition: function() {
		var fsize = this.frame.getSize(), d = 0,
			axe = 'x', direction = 'left', margin = {}, p, element;

		// Invese property
		if (this.mode.axe == 'x') {
			direction = 'top';
			axe = 'y';
			d = 1;
		}

		var marray = this.container.getStyle('margin').split(' ');
		margin.y = marray[0 + d].toInt() + marray[2 + d].toInt();
		margin.x = marray[1 + d].toInt();

		// Scrollbar position
		this.container.setStyle(this.mode.offset, fsize[this.mode.axe] - margin.y);
		p = fsize[axe] - margin.x - this.container.getSize()[axe];				
		this.container.setStyle(direction, p);

		// Scrollbar dimention
		element = this.container.getElementsWithAssociate('.up, .bar, .down');
		p = element.up.getSize()[this.mode.axe] + element.down.getSize()[this.mode.axe];
		element.bar.setStyle(this.mode.offset, this.container.getSize()[this.mode.axe] - p);
		this.container.setStyle('visibility', 'visible');
	},
	setCursor: function() {
		var coo = this.frame.getCoordinates(),
			woo = this.wrapper.getCoordinates(), p, part, diff;

		this.wrapper.setStyle('top', this.options.min);
		this.options.max = woo[this.mode.offset] - coo[this.mode.offset];
		
		if (this.options.max < 0) this.element.setNone();
		else this.element.setBlock();
		
		p = coo[this.mode.offset] - this.options.max;
		p = (p < this.options.cursor) ? this.options.cursor : p /= this.options.divisor;

		if (this.options.max < 0)
			p = 0;
		
		part = this.element.getElementsWithAssociate('.left, .middle, .right');
		diff = part.left.getSize()[this.mode.axe] - part.right.getSize()[this.mode.axe];
		part.middle.setStyle(this.mode.offset, p -diff);
		
		this.element.setStyle(this.mode.direction, 0).setStyle('visibility', 'visible');
	}	
});

