/**
 * mooCustomScrollbar
 * @author Tenderfeel
 * @ver 1.0
 * @HOME http://tenderfeel.xsrv.jp/
 * 
 * @Need:mootools 1.2
 * 
 * Mootools Powered Scrollbarを機能拡張してみたもの
 * 
 * @Original 
 * http://solutoire.com/2008/03/10/mootools-css-styled-scrollbar/
 * 
 * ---MIT License--------------------------------------------
 * Copyright (c) 2008 Tenderfeel all rights reserved.
 * 以下に定める条件に従い、本ソフトウェアおよび関連文書の
 * ファイル（以下「ソフトウェア」）の複製を取得するすべての
 * 人に対し、ソフトウェアを無制限に扱うことを無償で許可します。
 * これには、ソフトウェアの複製を使用、複写、変更、結合、
 * 掲載、頒布、サブライセンス、および/または販売する権利、
 * およびソフトウェアを提供する相手に同じことを許可する権利も無制限に含まれます。
 * 
 * 上記の著作権表示および本許諾表示を、
 * ソフトウェアのすべての複製または重要な部分に記載するものとします。
 * 
 * ソフトウェアは「現状のまま」で、明示であるか暗黙であるかを問わず、
 * 何らの保証もなく提供されます。ここでいう保証とは、商品性、
 * 特定の目的への適合性、および権利非侵害についての保証も含みますが、
 * それに限定されるものではありません。作者または著作権者は、
 * 契約行為、不法行為、またはそれ以外であろうと、
 * ソフトウェアに起因または関連し、あるいはソフトウェアの使用
 * またはその他の扱いによって生じる一切の請求、損害、
 * その他の義務について何らの責任も負わないものとします。 
 * ----------------------------------------------------------
 */

var mooCustomScrollbar = new Class({
	Implements: [Options],

	options: {
		mode:"vertical",
		wheel:false,
		id:"scrollbar"
	},
	
	initialize: function(slideable, wrap, options) {
		this.setOptions(options);
		this.wrap = wrap;
		if(wrap.getFirst().hasClass("scroll-wrapper")==false)
			this.content = new Element("div",{"class":"scroll-wrapper",styles: this.wrap.getStyles("width","height","overflow")}).wraps(slideable);
		else
			this.content =wrap.getFirst();
		this.scrollbar = new Element("div",{id:this.options.id,"class":"scrollbar"});
		this.knob = new Element("div",{"class":"scroll-knob", styles: {'width': 754*754/this.content.getScrollSize().x}});
		this.scrollbar.grab(this.knob);
		slideable.grab(this.scrollbar);
		this.steps = (this.options.mode!="vertical"?(this.content.getScrollSize().x - this.content.getSize().x):(this.content.getScrollSize().y - this.content.getSize().y));
		var self = this;
		
		this.slider = new Slider(this.scrollbar, this.knob, {	
			steps: this.steps,
			mode: this.options.mode,
			wheel: this.options.mode,
			onChange: function(step){
				// Scrolls the content element in x or y direction.
				var x = (this.options.mode!="vertical"?step:0);
				var y = (this.options.mode!="vertical"?0:step);
				self.content.scrollTo(x,y);
			}
		}).set(0);
		this.act();
	},
	act:function(){
		var self = this;
		if( !(this.options.wheel) ){
			$$(this.wrap, this.scrollbar).addEvent('mousewheel', function(e){	
				e = new Event(e).stop();
				var step = self.slider.step - e.wheel * 30;	
				self.slider.set(step);					
			});
		}
		$(document.body).addEvent('mouseleave',function(){self.slider.drag.stop();});
		var bg = this.knob.getStyle("background-image");
		this.knob.addEvents({
			'mouseup':function(e){
				this.removeClass("down");
			},
			'mousedown':function(){
				this.addClass("down");
			}
		});
	}
});