/*
	scroll_obj_id: nombre del objeto CGraphicScroll creado
	scroll_div: contenedor de la herramienta de scroll (donde se encuantran las flechas y el ascensor)
	scroll_content: contenedor externo del contenido a desplazar (MASCARA)
	scroll_mask: marco del contenido que será desplazado
	scroll_elevator: identificador del ascensor del scroll grafico
	scroll_bar: espacio en el cual se desplaza el ascensor
	bt_scroll_up: elemento grafico del scroll para mover hacia arriba
	bt_scroll_down: elemento grafico del scroll para mover hacia abajo
	content_offset: contiene un desplazamiento al final del scroll para mostra contenido que puede quedar oculto
	speed: velocidad del scroll
*/
function CGraphicScrollVertical(scroll_obj_id,scroll_div,scroll_mask,scroll_content,scroll_elevator,scroll_bar, bt_scroll_up, bt_scroll_down, content_offset, speed)
{
	//alert(scroll_obj_id+","+scroll_div+","+scroll_mask+","+scroll_content+","+scroll_elevator+","+scroll_bar+","+ bt_scroll_up+","+ bt_scroll_down+","+ content_offset+","+ speed);
	/* metodos*/
	this.scrollup = scrollup;
	this.scrolldown = scrolldown;
	this.stopScrolling = stopScrolling;
	this.setLoop = setLoop;
	this.scrollInit = scrollInit;
	/*atributos*/
	this.content_offset = content_offset;
	this.obj_scroll_id = scroll_obj_id;
	if (scroll_div)
	{
		this.obj_scroll_div = document.getElementById(scroll_div);		
	}
	else
		this.obj_scroll_div = null;
	this.obj_scroll_mask = document.getElementById(scroll_mask);
	this.obj_scroll_mask.position = "relative";
	this.obj_scroll_mask.style.overflow = "hidden";
	
	this.obj_scroll_content = document.getElementById(scroll_content);
	this.obj_scroll_content.className = scroll_obj_id;	
	this.obj_scroll_content.style.position = "relative";
	this.obj_scroll_content.style.top='0px';	

	
	if (speed)
		this.scroll_step = speed;
	else
		this.scroll_step = 5;
	
	if (scroll_elevator)
		this.obj_scroll_elevator = document.getElementById(scroll_elevator);
		
	if (scroll_bar)
		this.obj_scroll_bar = document.getElementById(scroll_bar);

	if (bt_scroll_up)
	{
		this.obj_bt_scroll_up = document.getElementById(bt_scroll_up);
		this.obj_bt_scroll_up.onmousedown=function () {window[scroll_obj_id].setLoop(true);window[scroll_obj_id].scrollup(true);};
		this.obj_bt_scroll_up.onmouseup=function(){window[scroll_obj_id].stopScrolling();};
		this.obj_bt_scroll_up.onmouseout=function(){window[scroll_obj_id].stopScrolling();};
		this.obj_bt_scroll_up.style.cursor = "pointer";
		this.obj_bt_scroll_up.className = "show_rel";   //  hide
	}
	if (bt_scroll_down)
	{
		this.obj_bt_scroll_down = document.getElementById(bt_scroll_down);
		this.obj_bt_scroll_down.onmousedown=function(){window[scroll_obj_id].setLoop(true);window[scroll_obj_id].scrolldown(true);};
		this.obj_bt_scroll_down.onmouseup=function(){window[scroll_obj_id].stopScrolling();};
		this.obj_bt_scroll_down.onmouseout=function(){window[scroll_obj_id].stopScrolling();};
		this.obj_bt_scroll_down.style.cursor = "pointer";
		this.obj_bt_scroll_down.className = "show_rel";    //   hide
	}
	/*metodos*/
	
	setMouseWheelScroll(scroll_mask,this.obj_scroll_content);
	window.setTimeout(scroll_obj_id+".scrollInit()",200);
}

function scrollInit()
{	
	this.scroll_content_up = 0;
	this.scroll_loop = true;
	this.scroll_desp = 0;
	this.scroll_elevator_up = 0;	
	/*
		h_dif: contiene la diferencia de alturas de la caja de texto (scroll_content) y la mascara de la caja (scroll_mask)
		h_divs: contiene la cantidad de segemntos de tamaño scroll_step que se necesitan para recorrer h_dif
		h_desp: contiene el desplazamiento que debe hacer en cada paso el scroll_elevator
	*/
	/**/
	//alert(this.obj_scroll_content.offsetHeight);

	this.obj_scroll_content.style.top='0px';//se pone el contenido en la posicion inicial
	
	if (this.obj_scroll_content.offsetHeight!=0)
	{
		this.h_dif = this.obj_scroll_content.offsetHeight - this.obj_scroll_mask.offsetHeight;
		this.h_divs = Math.ceil(this.h_dif/this.scroll_step);
		if (this.obj_scroll_bar)
			this.h_desp = Math.ceil(this.obj_scroll_bar.offsetHeight/this.h_divs);
	}
	else
	{
		this.h_dif=0;
		this.h_divs=0;
		this.h_desp=0;
	}
	
	/*alert("content: "+this.obj_scroll_content.offsetHeight+" > area: "+this.obj_scroll_mask.offsetHeight);
	alert("content: "+this.obj_scroll_content.offsetHeight+" > area: "+this.obj_scroll_mask.offsetHeight);*/
	if (this.obj_scroll_content.offsetHeight>this.obj_scroll_mask.offsetHeight&&this.obj_scroll_div)
	{
		this.obj_scroll_div.className="show_rel";
	}	
	if (this.obj_scroll_content.offsetHeight>this.obj_scroll_mask.offsetHeight)
	{
		this.obj_bt_scroll_down.className="show_rel";	
	}
	else
	{
		this.obj_bt_scroll_down.className="show_rel";	   //  hide
	}
}

function scrollup(lo)
{	
	if(this.h_dif>0&&this.scroll_content_up<0&&this.obj_scroll_elevator)
	{
		this.scroll_elevator_up -= this.h_desp;   //this.h_desp
		this.obj_scroll_elevator.style.top = this.scroll_elevator_up + "px";
		//alert("divs: "+divs+"\ndesp: "+desp+"\nobj_scroll_elevator.style.up: "+obj_scroll_elevator.style.up);
	}	
		
	this.scroll_content_up += this.scroll_step;
	if (!lo)
		this.scroll_content_up += this.scroll_step;
	this.obj_bt_scroll_down.className = "show_rel";
	
	if(this.scroll_content_up>0)
	{
		this.scroll_loop = false;
		this.scroll_content_up -= this.scroll_step;
		if (!lo)
			this.scroll_content_up -= this.scroll_step;
		this.obj_bt_scroll_up.className = "show_rel";    // hide
	}
	if (lo)
	{	
		this.obj_scroll_content.style.top = this.scroll_content_up + "px";
		setTimeout(this.obj_scroll_id+".scrollup("+this.scroll_loop+")",10);
	}
	else
	{
		this.obj_scroll_content.style.top = this.scroll_content_up + "px";
	}	
}

function scrolldown(lo)
{		
	if(this.h_dif&&this.obj_scroll_elevator)
	{
		this.scroll_elevator_up += this.h_desp;
		this.obj_scroll_elevator.style.top = this.scroll_elevator_up + "px";
		//alert("divs: "+divs+"\ndesp: "+desp+"\nobj_scroll_elevator.style.up: "+obj_scroll_elevator.style.up);
	}
	//alert('abajo '+this.h_dif);
	this.scroll_content_up -= this.scroll_step;
	if(!lo)
		this.scroll_content_up -= this.scroll_step;
	act_pos = this.obj_scroll_content.offsetHeight - this.obj_scroll_mask.offsetHeight;
	this.obj_bt_scroll_up.className = "show_rel";
	
	if (Math.abs(this.scroll_content_up)-this.content_offset >= act_pos)
	{			
		this.scroll_loop = false;
		this.scroll_content_up += this.scroll_step;
		if (!lo)
			this.scroll_content_up += this.scroll_step;

		this.obj_bt_scroll_down.className = "show_rel";    // hide
	}		
	if (lo)		
	{
		//alert(this.obj_scroll_content.style.up);
		this.obj_scroll_content.style.top = this.scroll_content_up + "px";					
		setTimeout(this.obj_scroll_id+".scrolldown("+this.scroll_loop+")",10);
		//alert(this.obj_scroll_content.style.up);
	}
	else
	{		
		this.obj_scroll_content.style.top = this.scroll_content_up + "px";		
	}
}

function stopScrolling()
{		
	this.scroll_loop=false;
}

function setLoop(b)
{
	this.scroll_loop=b;
}

function hide()
{
	this.className = "hide_abs";  
}

function hi()
{
	alert(this.obj_scroll_content.offsetHeight);
}

/**** MOUSE WHELL FUNCS **/

var speed = 3;
var scroll_wheel_listeners = 0;
function setMouseWheelScroll(div_id,div_obj)
{
	if (div_obj.addEventListener)
		div_obj.addEventListener('DOMMouseScroll', wheel, false);
	div_obj.onmousewheel /*= document.onmousewheel */ = wheel;
}

function handle(delta, div_obj) 
{
	if (delta < 0)
		window[div_obj.className].scrolldown(false);
		//div_obj.scrolldown();		
	else
		window[div_obj.className].scrollup(false);
		//div_obj.scrollBy(0,-speed*3);
}

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)
		handle(delta, this);
        if (event.preventDefault)
                event.preventDefault();
        event.returnValue = false;
}


