/*  SERGIO: He puesto la variable pos_indicador_x para que el indicador no aparezca mas a la derecha
    antes de que se pulse en el scroll
*/

	var velocidad_scroll = 10;
	var scroll_width = "15";
	var flechas_height = "11";
	var flechas_width = "13";
	var flecha_sup = "<img src='images/flecha_sup.gif' height='11' width='13' border='0'>"
	var centro = "images/centro.gif" 
	var altura_centro = 8;
	var scrolltimer
	var flecha_inf = "<img src='images/flecha_inf.gif' height='11' width='13' border='0'>"
	var indicador = "images/indicador.gif"
	var posy_indicador
  var pos_indicador_x	// SERGIO

	function genera_scroll(nombrediv, left, top, height, width)
	{
		var index = nombrediv.indexOf("Div")
		nombre = nombrediv.substring(0, index)
		index = nombrediv.indexOf("_cont")
		padre = nombrediv.substring(0,index);
		pos_indicador = left - scroll_width
    pos_indicador_x = left + 1; // SERGIO
		document.write ("<div id='" + nombrediv +"' style='position:absolute; left:"+scroll_width+"; top:0; height:" + height + "; width:" + (width - scroll_width) +";clip:rect(0px " + (width - scroll_width) + "px " + height + "px  0px )'>&nbsp;</div>")
		document.write ("<div id='control' style='position:absolute; left:" + left +";top:"+top+";width:" + scroll_width +";height:" + height + "'>")
		document.write ("<table cellpadding='0' cellpadding='0' cellspacing='0' height='" + height + "' width='" + scroll_width + "'><tr><td align='center' height='" + flechas_height + "'>")
		document.write ("<a href='#' onmouseout='stopScroll()' onmouseup='stopScroll()' onmousedown='scroll(\"" + nombre + "\", 1, " + velocidad_scroll + ")'>" + flecha_sup + "</a>")
		document.write ("</td></tr><tr><td align='center'>")
		document.write ("<a href='#' onmousedown='scroll_avanzado(\"" +  padre + "\")' onmouseup='stopScroll()' onmouseout='stopScroll()'><img src=" + centro + " width=" + flechas_width +" height=" + (height - (flechas_height * 2)) +" border='0'></a>")
		document.write ("</td></tr><tr><td align='center' height='" + flechas_height +"'>")
		document.write ("<a href='#' onmouseout='stopScroll()' onmouseup='stopScroll()' onmousedown='scroll(\"" +  nombre + "\", 0, " + velocidad_scroll + ")'>" + flecha_inf +"</a>")	
		document.write ("</td></tr></table>")
		document.write ("</div>") // SERGIO
		document.write ("<div id='indicador" + nombrediv + "' style='position:absolute;top:" + (flechas_height*1 + 1*1 + top) +";left:"+(pos_indicador_x)+";visibility:hidden'><img src='"+indicador+"' border='0'></div>")
		document.write ("</div>")
	}

	function scroll(id_capa, direccion, velocidad)
	{
		indicador = generardiv("indicador" + id_capa)
		capa = generar(id_capa)
		capadiv = generardiv(id_capa)
		var y_pos = parseInt(capa.top);
		if (ns4)
		{
			clip_top = capa.clip.top
			clip_right = capa.clip.right
			clip_bottom = capa.clip.bottom
			clip_left = capa.clip.left
		}else{
			pos_i = capa.clip.indexOf("(",0)
			pos_f = capa.clip.indexOf("px", pos_i)
			clip_top = capa.clip.substring(pos_i+1, pos_f)
			pos_i = pos_f + 2
			pos_f = capa.clip.indexOf("px", pos_i)
			clip_right = capa.clip.substring(pos_i, pos_f)
			pos_i = pos_f + 2
			pos_f = capa.clip.indexOf("px", pos_i)
			clip_bottom = capa.clip.substring(pos_i, pos_f)
			pos_i = pos_f + 2
			pos_f = capa.clip.indexOf("px", pos_i)
			clip_left = capa.clip.substring(pos_i, pos_f)
		}	
		//Primero miramos si quiere subir el scroll, o sea que baje el contenido
		if (direccion == 0)	
		{
			if (clip_bottom < capadiv.offsetHeight)
			{
				
				if (velocidad_scroll > (capadiv.offsetHeight - clip_bottom))
				{
					velocidad = capadiv.offsetHeight - clip_bottom
				}else{	
					velocidad = velocidad_scroll
				}
				capa.top = (y_pos - (velocidad));
				if (ns4)
				{
				 	capa.clip.top = clip_top*1 + velocidad*1
					capa.clip.bottom = clip_bottom*1 + velocidad*1
				}else{
					capa.clip = "rect(" + (clip_top*1 + velocidad*1) + "px "+ clip_right + "px " + (clip_bottom*1 + velocidad*1) + "px " + clip_left + "px)"
				}
				
				if (posy_indicador < mouse_y){
					scrolltimer = setTimeout("scroll('" + id_capa +"', 0,"+velocidad+")", 1);
				}
			}
		}
		if (direccion == 1)	
		{
			if (velocidad_scroll > clip_top)
			{
				velocidad = clip_top
			}else{	
				velocidad = velocidad_scroll
			}
			if (clip_top > 0)
			{
				capa.top = y_pos*1 + velocidad*1;
				if (ns4)
				{
					capa.clip.top = clip_top*1 - velocidad*1
					capa.clip.bottom = clip_bottom*1 - velocidad*1
				}else{
					capa.clip = "rect(" + (clip_top*1 - velocidad*1) + "px "+ clip_right + "px " + (clip_bottom*1 - velocidad*1) + "px " + clip_left + "px)"
				}
				if (posy_indicador > mouse_y){
					scrolltimer = setTimeout("scroll('" + id_capa +"', 1,"+velocidad+")", 1);
				}
			}
			
		}
		if (control_scroll(id_capa) > 0)		
		{
			altura_scroll = (clip_bottom - clip_top) - (flechas_height) * 2

			convertidor = (altura_scroll - (flechas_height * 1)) / (capadiv.offsetHeight - (clip_bottom - clip_top));
			pos_indicador = clip_top * convertidor;
			pos_indicador = pos_indicador + (flechas_height * 1);
			mover ("indicador" + id_capa, pos_indicador_x, pos_indicador + 1) // SERGIO
		}
	}
	
	function stopScroll() 
	{
		if (scrolltimer)
		{
			clearTimeout(scrolltimer);
		}
	}
	
	function cargar_scroll(capa, url)
	{
		cargar(capa, url);
	}
	function control_scroll(capa)
	{
		padre = capa.substring(0, capa.indexOf("_cont"))
		padrediv = generardiv(padre)
		padrediv.style.top
		indicador = generardiv("indicador" + capa)
		posy_indicador = parseInt(padrediv.style.top) + parseInt(indicador.style.top)
		capadiv = generardiv(capa)
		indicador = ("indicador" + capa)
		mostrar (indicador)
		pos_i = capadiv.style.clip.indexOf("(",0)
		pos_f = capadiv.style.clip.indexOf("px", pos_i)
		clip_top = capadiv.style.clip.substring(pos_i+1, pos_f)
		pos_i = pos_f + 2
		pos_f = capadiv.style.clip.indexOf("px", pos_i)
		clip_right = capadiv.style.clip.substring(pos_i, pos_f)
		pos_i = pos_f + 2
		pos_f = capadiv.style.clip.indexOf("px", pos_i)
		clip_bottom = capadiv.style.clip.substring(pos_i, pos_f)
		pos_i = pos_f + 2
		pos_f = capadiv.style.clip.indexOf("px", pos_i)
		clip_left = capadiv.style.clip.substring(pos_i, pos_f)
		zvisible = clip_bottom - clip_top
		zactiva = capadiv.offsetHeight
		if (zvisible < zactiva)
		{
			mostrar (indicador)
		}else{
			ocultar(indicador)
		}
		return (zactiva - zvisible)
	}
	
	function scroll_avanzado(id_capa)
	{
		padre = generardiv(id_capa)
		temporal = "indicador" + id_capa + "_cont"
		capa_indicador = generardiv(temporal)
		posy_indicador = parseInt(padre.style.top) + parseInt(capa_indicador.style.top)
		if (posy_indicador > mouse_y)
		{
			scroll(id_capa+ "_cont", 1, 1)
		}else{
			scroll(id_capa+ "_cont", 0, 1)
		}
	}                                                                                                                                
