function lib_bwcheck(){ //Browsercheck (needed)
	this.ver=navigator.appVersion;
	this.agent=navigator.userAgent;
	this.dom=document.getElementById?1:0;
	this.opera5=(navigator.userAgent.indexOf("Opera")>-1 && document.getElementById)?1:0;
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie7=(this.ver.indexOf("MSIE 7")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6||this.ie7;
	this.mac=this.agent.indexOf("Mac")>-1;
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie || this.ns4 || this.ns6 || this.opera5);
	
	return this;
}

var bw = lib_bwcheck();
// A unit of measure that will be added when setting the position of a layer.
var px = bw.ns4||window.opera?"":"px";


// ==============================================================================================


// indica si el scroll fue cargado.
var scrolltextLoaded = false

//If you want it to move faster you can set this lower, it's the timeout:
var speed = 15;

var scroll_step = 7;

//Sets variables to keep track of what's happening
var loop, timer;


/**
 * Calls the scrolling functions. Also checks whether the page is loaded or not.
 */
function scroll(speed){
	if (scrolltextLoaded){
		loop = true;
		if (speed > 0) 
			scrolledPane.down(speed);
		else 
			scrolledPane.up(speed);
	}
}

/**
 * Stops the scrolling (called on mouseout)
 */
function noScroll(){
	loop = false;
	if (timer) 
		clearTimeout(timer);
}

/**
 * evento disparado al mover el slider
 */
function scroll_on_slide(v){
	var top = -(scrolledPane.offsetDiff * v);
	scrolledPane.moveIt(0,top,false);
}

/**
 * Clase scrolled Pane, encargada de todo.
 */
function ScrolledPane(contentPane, viewPort, scrollBar, scrollTrack, scrollSlider){
	this.contentPane = $(contentPane);
	this.viewPort = $(viewPort);
	this.scrollBarDiv = $(scrollBar);
	this.scrollTrack = $(scrollTrack);
	this.scrollSlider = $(scrollSlider);

	this.contentPaneRect = Element.getDimensions(this.contentPane);
	this.viewPortRect = Element.getDimensions(this.viewPort);

	this.scrollHeight = this.viewPortRect.height+25;
	this.clipHeight = this.contentPaneRect.height;
}

/**
 * inicializa el scroll
 */
ScrolledPane.prototype.init = function(){
	if (this.needScrollBars()){
		this.show();
		this.offsetDiff = this.scrollHeight - this.clipHeight;	
		this.moveIt(0,0,true);
		
		init_wheel();
	}
	else {
		this.contentPane.style.width = (this.contentPaneRect.width +25)+px;
	}
	
	this.viewPort.style.display = '';
}

/**
 * indica si necesita scrollbars.
 */
ScrolledPane.prototype.needScrollBars = function(){
	return (this.viewPortRect.height > this.contentPaneRect.height);
}

/**
 * Muestra el scrollbar
 */
ScrolledPane.prototype.show = function(){
	//this.contentPane.style.width = (this.contentPaneRect.width -25)+px;
	this.scrollBarDiv.show();
	var t = this;
	this.controlSlider = new Control.Slider(this.scrollSlider, this.scrollTrack, {
		axis:'vertical',
        onSlide: scroll_on_slide,
		onChange: scroll_on_slide
        });
}

/**
 * mueve el viewport
 */
ScrolledPane.prototype.moveIt = function(x,y, update){
	this.x = x;
	this.y = y;
	this.viewPort.style.left = this.x + px;
	this.viewPort.style.top = this.y + px;
	
	if (update)
		this.updateSliderPosition();
}

/**
 * Makes the object go up
 */
ScrolledPane.prototype.down = function(move){

	var scroll_height = this.clipHeight - this.scrollHeight;

	if (this.y > scroll_height){
		this.moveIt(0, this.y - move,true);
		if (loop) 
			setTimeout("scrolledPane.down("+move+")",speed);
	}
}

/**
 * Makes the object go down
 */
ScrolledPane.prototype.up = function(move){
	if (this.y<0){
		this.moveIt(0,this.y - move,true);
		if (loop) 
			setTimeout("scrolledPane.up("+move+")",speed);
	}
}

/**
 * actualiza la posicion del slider cuando el usuario navega
 */
ScrolledPane.prototype.updateSliderPosition = function(){
	var offset = (-this.y) / this.offsetDiff;
	
	this.controlSlider.setValue(offset);
}


var scrolledPane;

/**
 * Inicializa la barra de scroll.. verifica si es necesaria.
 */
function scrolltextInit(){
	scrolledPane = new ScrolledPane('scrolledpane','contentpane','scrollbar','scrollbarTrack','scrollbarSlider');
	scrolledPane.init();
	scrolltextLoaded = true;
	
	replace_links();
}


/** 
 * ////////////////////////////////////////////////////////
 * wheel
 * ////////////////////////////////////////////////////////
 */
 
function init_wheel(){
	/* Initialization code. */
	if (window.addEventListener)
		window.addEventListener('DOMMouseScroll', wheel, false);
	window.onmousewheel = document.onmousewheel = wheel;
}

function handle_wheel(delta) {
	if (scrolltextLoaded){
		if (delta < 0) 
			scrolledPane.down(scroll_step*2);
		else 
			scrolledPane.up(-scroll_step*2);
	}
}

function wheel(event){
	
	var delta = 0;
	if (!event){
		event = window.event;
	}
	
	if (event.wheelDelta) {
		delta = event.wheelDelta / 120;
		if (window.opera){
			delta = -delta;
		}
	} 
	else if (event.detail) {
		delta = -event.detail/3;
	}
	
	if (delta){
		var rect = Element.getRect($('scrolledpane'));
		var pt = get_pointer_pos(event);
		
		rect.width += 30;

		// verifico que se encuentre dentro del scrollpane
		if (pt.x > rect.left && 
			pt.x < rect.left + rect.width &&
			pt.y > rect.top &&
			pt.y < rect.top + rect.height){

			handle_wheel(delta);
			
			Event.stop(event);
		}
	}
}

function get_pointer_pos(event){
	var results = {x:0,y:0};

	if (bw.ie){
		results.x = event.clientX+(document.documentElement.scrollLeft || document.body.scrollLeft);
		results.y = event.clientY+(document.documentElement.scrollTop || document.body.scrollTop);
	}
	else if (event.screenX){
		results.x = event.screenX+(document.documentElement.scrollLeft || document.body.scrollLeft);
		results.y = event.screenY+(document.documentElement.scrollTop || document.body.scrollTop);
	}
	else {
		results.x = Event.pointerX(event);
		results.y = Event.pointerY(event);
	}
	
	return results;
}


/** 
 * ////////////////////////////////////////////////////////
 * ANCHORSS
 * ////////////////////////////////////////////////////////
 */
function replace_links(){
	var args = new Array();
	args[0] = 'a[href]';
	
	var anchors = Selector.findChildElements($('contentpane'), $A(args));
	//var anchors = $('contentpane').getElementsByTagName('a');
	
	var prefix = location.href + '#';
	
	anchors.each(function(anchor){ 
		var href = anchor.href;
		if (href.indexOf(prefix) == 0){
			var name = href.substring(prefix.length);
			anchor.href = "javascript:scroll_to('"+name+"');";
		}
	});
}



function scroll_to(name){
	
	var anchor = $$('a[name="'+name+'"]');
	//alert(anchor.length);
	if (anchor.length > 0){
		anchor = anchor[0];
		
		var pos1 = Element.getRect(anchor); //  Position.offsetParent(anchor);
		var pos2 = Element.getRect($('contentpane'));
		
		if (anchor)	
			scrolledPane.moveIt(0, (pos2.top - pos1.top) + 14,true); 
	}
}

//Call the init on page load if the browser is ok...
if (bw.bw) 
	onload = scrolltextInit;

