/**
 * @version $Id: scroll.js,v 1.3 2008-10-10 10:41:25 fch Exp $
 * @package oenobiol.botoescroll
 * MOOTOOLS 1.2
 * @copyright Copyright (C) 2006 BROCELIA. All rights reserved.
 */

var extendFunction = {
	vaparla: function(y){
		if(y<0) y = 0;
		else if(y>1) y = 1;
		//return this.custom(this.element.scrollTop, y*(this.element.scrollHeight-this.element.offsetHeight));

		this.element.scrollTop = y*(this.element.scrollHeight-this.element.offsetHeight);
	},
	bouge: function(y){
		this.set(0, this.element.scrollTop + y);

	}
};

var botoescroll_i = 0;
var botoescroll_effets = Array();
var botoescroll_effets2 = Array();


function botoescroll(c, options) {

	var tabSize = options.height ? options.height : 200;

	if(c.scrollHeight>tabSize) {
		// hauteur dépassée => on met une scrollbar

		if(!options.width)			options.width = 10;
		if(!options.top)			options.top = 0;
		if(!options.pageRate)		options.pageRate = 0.80;
		if(!options.thumbHeightMin)	options.thumbHeightMin = 6;

		var i = botoescroll_i;
		var pageSize = Math.round(tabSize * options.pageRate);	// nb px pour le chgt de page
		var trackSize = tabSize-(2*options.top);

		if(!options.thumbHeight || options.thumbHeight=='' || options.thumbHeight==0) {
			options.thumbHeight = Math.ceil(pageSize*trackSize/c.scrollHeight);
			if(options.thumbHeight < options.thumbHeightMin) options.thumbHeight = options.thumbHeightMin;
		}

		var scrollStart = options.top;
		var scrollEnd = tabSize - options.top - options.thumbHeight;
		var scrollHeight = scrollEnd - scrollStart;

		var d = new Element('div');
		d.id = 'botoescrollbar'+i;
		d.className = 'botoescrollbar';
		if(options.startHidden) d.style.visibility = 'hidden';
		d.style.position = 'absolute';
		d.style.marginLeft = (c.scrollWidth - options.width)+'px';

		// bouton up
		var divUp = new Element('div');
		divUp.className = 'botoescrollbar_up';
		divUp.id = 'botoescrollbar_up'+i;
		divUp.style.cursor = 'pointer';
		d.appendChild(divUp);

		var divTrack = new Element('div');
		divTrack.className = 'botoescrollbar_track';
		divTrack.style.height = trackSize+'px';
		d.appendChild(divTrack);

		var divThumb = new Element('div');
		divThumb.id = 'botoescrollbar_thumb'+i;
		divThumb.className = 'botoescrollbar_thumb';
		divThumb.style.position = 'absolute';
		divThumb.style.top = scrollStart+'px';
		divThumb.style.height = options.thumbHeight+'px';
		divTrack.appendChild(divThumb);

		// bouton down
		var divDown = new Element('div');
		divDown.className = 'botoescrollbar_down';
		divDown.id = 'botoescrollbar_down'+i;
		divDown.style.cursor = 'pointer';
		d.appendChild(divDown);

		var d2 = document.createElement('div');
		d2.className = 'botoescroll_content';
		d2.style.overflow = 'hidden';
		d2.style.height = tabSize+'px';
		d2.style.marginRight = options.width+'px';
		var t;
		while(t=c.firstChild) {
			d2.appendChild(t);
		}

		c.appendChild(d);
		c.appendChild(d2);


		// bug position thumb sous ie
		if(!$chk(divThumb.getStyle('left').toInt())) {
			divThumb.style.left = (divTrack.getLeft()-d.getLeft())+'px';
		}


		// actions
		var pas = Math.round(pageSize*scrollHeight/(d2.scrollHeight-d2.offsetHeight));	// nb de px sur la scrollbar pour le chgt de page
		eval("divUp.addEvent('click', function() { botoescroll_effets["+i+'].bouge(-'+pageSize+"); var from = $('botoescrollbar_thumb"+i+"').getStyle('top').toInt(); var to = from - "+pas+"; if(to<"+scrollStart+") to = "+scrollStart+"; botoescroll_effets2["+i+"].start('top',from, to); });");
		eval("divDown.addEvent('click', function() { botoescroll_effets["+i+'].bouge('+pageSize+"); var from = $('botoescrollbar_thumb"+i+"').getStyle('top').toInt(); var to = from + "+pas+"; if(to>"+scrollEnd+") to = "+scrollEnd+"; botoescroll_effets2["+i+"].start('top',from, to); });");
		//eval("divDown.addEvent('dblclick', function() { botoescroll_effets["+i+'].down(); });');

		eval('var onDrag = function() { botoescroll_effets['+i+'].vaparla((parseInt(this.element.style.top.substring(0, this.element.style.top.length-2)) - '+scrollStart+') / '+scrollHeight+'.0); }');
		divThumb.makeDraggable({onDrag:onDrag, modifiers: {y: 'top', x:null}, limit: {y: [scrollStart, scrollEnd]}, snap: false});
	var scroller = new Fx.Scroll(d2);
	scroller = $extend(scroller, extendFunction);
		botoescroll_effets[i] = scroller;
		botoescroll_effets2[i] = new Fx.Tween(divThumb, 'top');
		botoescroll_effets2[i].scrollStart = scrollStart;

		botoescroll_i++;
	}
}