var Scroll = null;
var Scrollbar = function(parametres) {
	// Debug
//	this.trace_box = $("trace");
	// Element Setting
	this.content_box = $("scroll-content");
	this.objects_box = $("scroll-bars");
	this.objects = new Array($("scroll-up"),$("scroll-area"),$("scroll-point"),$("scroll-down"));
	// Size Setting
	this.size_info = new Array();
	this.move_size = new Array();
	this.move_space = 20;
	this.move_count = 0;
	// Scrollbar Style Setting
	for (parametre in parametres) {
		this[parametre] = parametres[parametre];
	}
	this.scroll_status = false;
	this.scroll_y = 0;
	this.point_start = 0;
	this.point_now = 0;
	this.point_y = 0;

	Scroll = this;
	this.init(true);
};

Scrollbar.prototype = {
	init : function(evt) {
		this.size_info[0] = this.content_box.scrollHeight;
		this.size_info[1] = this.content_box.offsetHeight;
		// Content Size < Area Size = Scrollbar hidden
		if(this.size_info[0] > this.size_info[1]) {
			this.scroll_show();
		} else {
			this.scroll_hide();
			return;
		}
		this.size_info.push(this.size_info[1] - this.size_width * 2);
		var Temp = Math.ceil((this.size_info[0] - this.size_info[1]) / 100);
		if(Temp == 1) Temp = 1.5;
		this.size_info.push(parseInt(this.size_info[1] / Temp));
		this.move_size.push((this.size_info[2] - this.size_info[3]));
		this.move_size.push((this.size_info[0] - this.size_info[1])/this.move_size[0]);
		this.objects_box.scrollTop = 0;
		this.objects_box.style.width = this.size_width + "px";
		this.objects_box.style.height = this.size_info[1] + "px";
		this.objects[2].style.top = 0 + "px";
		this.objects[2].style.height = this.size_info[3] + "px";
		this.objects[1].style.top = this.size_width + "px";
		this.objects[1].style.height = this.size_info[2] + "px";
		this.objects[0].style.background = "url(" + this.scroll_up + ") no-repeat center center";
		this.objects[1].style.background = "url(" + this.scroll_ground + ") repeat-y center";
		this.objects[2].style.background = "url(" + this.scroll_point + ") repeat-y center";
		this.objects[3].style.background = "url(" + this.scroll_down + ") no-repeat center center";

		if(evt) {
			this.event_register();
		}
	},
	scroll_show : function() {
		this.objects_box.style.display = "block";
	},
	scroll_hide : function() {
		this.objects_box.style.display = "none";
	},
	event_register : function() {
		Event.observe(this.objects[2], "mousedown", this.event_point_press);
		Event.observe(this.objects[2], "mousemove", this.event_move);
		Event.observe(this.objects[0], "click", this.event_point_up);
		Event.observe(this.objects[3], "click", this.event_point_down);
		Event.observe(this.objects_box, "mousewheel", this.event_wheel);
		Event.observe(this.content_box, "mousewheel", this.event_wheel);
		if(window.addEventListener) {
			this.objects_box.addEventListener('DOMMouseScroll', this.event_wheel, false);
			this.content_box.addEventListener('DOMMouseScroll', this.event_wheel, false);
		}
		Event.observe(document.documentElement, "mousemove", this.event_move);
		Event.observe(document.documentElement, "mouseup", this.event_release);
	},
	event_release : function() {
		Scroll.scroll_status = false;
	},
	event_point_press : function(event) {
		Scroll.scroll_status = true;
		Scroll.point_start = Event.pointerY(event) - Scroll.point_y;
	},
	event_wheel : function(event) {
		var check;
		if(event.detail) {
			check = event.detail;
		}else{
			check = -event.wheelDelta;
		}
		if(check < 0) Scroll.point_y -= Scroll.move_space;
		if(check > 0) Scroll.point_y += Scroll.move_space;
		Scroll.point_valid();
		Scroll.point_move();
		Scroll.content_move();
	},
	event_point_up : function() {
		Scroll.point_y -= Scroll.move_space;
		Scroll.point_valid();
		Scroll.point_move();
		Scroll.content_move();
	},
	event_point_down : function() {
		Scroll.point_y += Scroll.move_space;
		Scroll.point_valid();
		Scroll.point_move();
		Scroll.content_move();
	},
	event_move : function(event) {
		if(!Scroll.scroll_status) return;
		Scroll.point_now = Event.pointerY(event);
		Scroll.point_y = Scroll.point_now - Scroll.point_start;
		Scroll.point_valid();
		Scroll.point_move();
		Scroll.content_move();
	},
	point_valid : function() {		
		if(Scroll.point_y < 1) Scroll.point_y = 0;
		if(Scroll.point_y > Scroll.move_size[0]) Scroll.point_y = Scroll.move_size[0];
	},
	point_move : function() {
		Scroll.objects[2].style.top = Scroll.point_y + "px";
	},
	content_move : function() {
		Scroll.content_box.scrollTop = parseInt(Scroll.point_y * Scroll.move_size[1]);
	}
}

