/*
 * Scroll Class Prototype
 * 
 * 
 * @param elm =  Element
 * @param width = Int
 * @param height = Int
 * @param buttonHeight = int
 */
function CustomScrollBar(elm,width,height,buttonHeight,marginTop) {
	this.id = Math.round(Math.random() * 100);
	this.scrollElms = '<div id="scrollbar_'+this.id+'" class="scrollbar" ><div id="scroll_top_'+this.id+'" class="scroll_top" ></div><div id="scroll_bar_'+this.id+'"  class="scroll_bar" ></div><div id="scroll_bot_'+this.id+'" class="scroll_bot" ></div></div>';
	this.elm = elm;
	this.width = width;
	this.height = height;
	this.marginTop = marginTop;
	this.buttonHeight = buttonHeight;
	elm.innerHTML = '<div id="scroll_internal_'+this.id+'" class="scroll_internal" style="width:'+(this.width-this.buttonHeight)+'px;" >'+elm.innerHTML+'</div>'+this.scrollElms;
	elm.className = 'scroll_wrapper';
	elm.style.height = height+'px';
	elm.style.width  = width+'px';
	
	this.scrollBar = AJS.$('scroll_bar_'+this.id);
	this.scrollContent = AJS.$('scroll_internal_'+this.id);
	
	if (this.scrollContent.scrollHeight < this.height) {
		AJS.$('scrollbar_'+this.id).style.display = 'none';
		this.scrollContent.width = this.width+'px';
		elm.style.height = this.scrollContent.scrollHeight+'px';
		return this;
	}
	
	this.scrollBarHeight = (1 / this.scrollContent.scrollHeight * height) * (height - this.buttonHeight*2);
	this.scrollBar.style.height = this.scrollBarHeight+'px';
	
	this.scrollBarDrag = false;
	this.scrollPercent = 0;
	this.scrollMinPos = this.marginTop + this.buttonHeight;
	this.scrollMaxPos = this.marginTop + this.height - this.buttonHeight ;
	AJS.bindMethods(this);
	AJS.AEV(document,'mousemove',this.scrollBarHandler);
	AJS.AEV(this.scrollBar,'mousedown',this.scrollBarDragHandler);
	AJS.AEV(document,'mouseup',this.scrollBarDragHandler);
	this.val = 0;
	var scope = this;
	AJS.AEV(AJS.$('scroll_top_'+this.id),'mousedown',function (e) {scope.val = window.setInterval(function(){scope.scrollFurther(-1);},15);});
	AJS.AEV(AJS.$('scroll_bot_'+this.id),'mousedown',function (e) {scope.val = window.setInterval(function(){scope.scrollFurther(1);},15);});
	AJS.AEV(document,'mouseup',this.stopScroll);
}

CustomScrollBar.prototype.stopScroll = function(event) {
	window.clearInterval(this.val);
};

CustomScrollBar.prototype.scrollBarDragHandler = function(event) {
	if (event.type == 'mousedown') {
		this.scrollBarDrag = true;
	}
	else {
		this.scrollBarDrag = false;
	}
};

CustomScrollBar.prototype.scrollBarHandler = function(event) {
	if (this.scrollBarDrag) {
		newPos = Math.min(this.scrollMaxPos,Math.max(this.scrollMinPos,event.clientY));
		this.scrollPercent = 100 / (this.height - this.buttonHeight*3) * (newPos - this.scrollMinPos);
		this.scrollPercent = Math.min(100,Math.max(0,this.scrollPercent));
		this.setBar();
		this.setText();
	}
};

CustomScrollBar.prototype.setText = function () {
	var textPos = (this.scrollContent.scrollHeight-(this.height * 0.80)) / 100 * this.scrollPercent;
	this.scrollContent.style.marginTop = -textPos+'px';
};

CustomScrollBar.prototype.setBar = function() {
	this.scrollBar.style.marginTop = ((this.height - (this.buttonHeight*2) - (this.scrollBarHeight)) / 100 * this.scrollPercent)+'px';
};

CustomScrollBar.prototype.scrollFurther = function(arg) {
	this.scrollPercent = Math.min(100,Math.max(0,this.scrollPercent+arg));
	this.setText();
	this.setBar();
};


