/**
 * @version $Id: oe_scroll.js,v 1.10 2007/09/17 09:50:46 cg Exp $
 * @package oenobiol.botoescroll
 * @copyright Copyright (C) 2006 BROCELIA. All rights reserved.
 */

Fx.TfScroll = Fx.Scroll.extend({
	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){
		return this.scrollTo(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 = 12;
		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+"].custom(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+"].custom(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});

		botoescroll_effets[i] = new Fx.TfScroll(d2);
		botoescroll_effets2[i] = new Fx.Style(divThumb, 'top');
		botoescroll_effets2[i].scrollStart = scrollStart;

		botoescroll_i++;
	}
}


/** va jusqu'à l'objet dont l'id est passé en paramètre
 *  si il y a une scrollbar autour, on lance les effets pour y aller
 *  sinon il ne se passe rien
 *  @param id id de l'objet dom qui doit être en haut de la zone de scroll ou name de l'ancre
 */
function oe_scroll_goTo(id) {
	if(botoescroll_i<=0) return false;
	var done = false;
	for(var i=0;i<botoescroll_i;i++) {
		var e = botoescroll_effets[i].element.getElementById(id);
		if(e) {
			_oe_scroll_goTo(i, e);
			done = true;
		} else {
			botoescroll_effets[i].element.getElements('a').each(
				function(a) {
					if(a.name == id) {
						if(a.getTop()>a.parentNode.getTop()+15)	 {
							_oe_scroll_goTo(i, a);
						} else {
							_oe_scroll_goTo(i, a.parentNode);
						}
						done = true;
					}
				}
			);
		}
	}
	return done;
}

function _oe_scroll_goTo(i, e) {
	var x = $(e).getTop() - botoescroll_effets[i].element.getTop();
	var from = botoescroll_effets2[i].element.getStyle('top').toInt();
	var to = botoescroll_effets2[i].element.parentNode.offsetHeight * x / botoescroll_effets[i].element.scrollHeight;
	if(to<0) to = 0;
	else if(to>botoescroll_effets2[i].element.parentNode.offsetHeight - botoescroll_effets2[i].element.offsetHeight) {
		to = botoescroll_effets2[i].element.parentNode.offsetHeight - botoescroll_effets2[i].element.offsetHeight;
	}

	to += botoescroll_effets2[i].scrollStart.toInt();

	botoescroll_effets[i].scrollTo(0, x);
	botoescroll_effets2[i].custom(from, to);
}



/* si une ancre est demandée dans l'url on s'y rend après le chargement de la page
 * attention : il y a encore un bug d'affichage de la zone de scroll sous firefox dans ce cas
 * ajoute la fonction à tout les liens commencant par #
 */

window.addEvent('load', function() {
	if(location.hash && location.hash.length>0) {
		setTimeout('oe_scroll_goTo(location.hash.substring(1));', 300);
	}
	$$('a').each(function(a) {
		if(a.href && a.href.length>location.href.length+2 && a.href.indexOf('#')==location.href.length) {
			a.onclick = function() {
				return !oe_scroll_goTo(a.href.substring(location.href.length+1));
			};
		}
	});
});
