document.write("<link href='/js/scroll/jquery-ui.css' rel='stylesheet' type='text/css' />");
document.write("<script src='/js/scroll/jquery.mousewheel.js' type='text/javascript'></script>");
document.write("<script src='/js/scroll/ui/ui.core.js' type='text/javascript'></script>");
document.write("<script src='/js/scroll/ui/ui.slider.js' type='text/javascript'></script>");

var navIE6 = false;

$(window).load(function(){ 				
	
	/* Actualizamos estilos (Depende si la web tiene le pie de forma absoluta */
	/* ---------------------------------------------------------------------- */	
	//$('#wPie').css("position","absolute");
	
	if (navIE6)
		return;
		
	/* Actualizamos estilos */
	$('html').css("overflow-y","hidden");
	$('#infoScroll').css("overflow","hidden");	
	$('#scroll').css("cursor","pointer");		
		
	/* Crear Scroll */
	/* --------------------------- */
	if (mostrarScroll())
		 CrearScroll();
	else
	{
		ajustarContenedor();
		$(window).bind('resize', CrearScroll );				
	}

});	
	

function CrearScroll()
{	
	if (!mostrarScroll())
	{		
		$('#scroll').slider('destroy')
		$('#scroll').css("display", "none");	
		$(window).bind('resize', CrearScroll );
		return false;		
	}
		
	$('#scroll').css("display", "block");
	
	/* Ajustar alto del contenedor */
	/* --------------------------- */
	$(window).bind('resize', ajustarContenedor );	
	
	/* Montamos el scroll */
	/* ------------------- */
	var alturaPantalla = $(window).height();
	var altura = $("#infoScroll").attr("scrollHeight");
	var pixelDespl = 20;						
	
	$("#scroll").slider({
			orientation: "vertical",
			range: "min",
			min: -100,
			max: 0,
			slide: handleSlide
		});				

	$('body').mousewheel(function(event, delta) {
		
		var salir=false;
		var scrollHeight = $("#infoScroll").height();
		var scrollTop = $("#infoScroll").attr("scrollTop");			
		
		if (delta > 0)		
			scrollTop = scrollTop-pixelDespl;												
		else if (delta < 0)															
			scrollTop = scrollTop+pixelDespl;							
				
		if (scrollTop > altura-scrollHeight)
			scrollTop = altura-scrollHeight;
		
		var porcentajeScroll = 100-((scrollTop*newPorcentaje)/(altura-scrollHeight));							
							
		porcentajeScroll = parseInt(porcentajeScroll);				
		
		if (porcentajeScroll>100) porcentajeScroll=100;
						
		$("#infoScroll").attr({ scrollTop:  scrollTop  });
		$('#scroll a').css("bottom", porcentajeScroll+"%");	
		
		return false; // prevent default
		
	});

	//----------------
	ajustarContenedor();
	
}

var margenTopBot = 180;

function mostrarScroll()
{	
	var alturaPantalla = $(window).height(); 	
	var alturaMenu = $("#wMenu").height(); 
	var alturaPie= $("#wPie").height(); 
	var offset = $("#infoScroll").offset();
	var alturaContenedor = alturaPantalla-parseInt(offset.top)-margenTopBot;
	var alturaInfo = $("#infoScroll").attr("scrollHeight");
	
	if (alturaInfo>alturaContenedor)
		return true;
	else
		return false;
	
}

/* Variable global */
var newPorcentaje;

function ajustarContenedor()
{		

	var alturaPantalla = $(window).height(); 	
	var alturaMenu = $("#wMenu").height(); 
	var alturaPie= $("#wPie").height(); 
	var offset = $("#infoScroll").offset();
	var alturaContenedor = alturaPantalla-parseInt(offset.top)-margenTopBot;
	var alturaInfo = $("#infoScroll").attr("scrollHeight");
	var alturaScroll = (alturaContenedor/alturaInfo)*alturaContenedor;
	//var alturaScroll = 86;
	
	$('#infoScroll').css("height",alturaContenedor+"px"); 
	
	if (!mostrarScroll())
	{
		CrearScroll();
		return false;
	}
	
	$('#scroll').css("height",alturaContenedor+"px");
	$('#scroll a').css("height",alturaScroll+"px");	
	$('#scroll a').css("margin-bottom",-alturaScroll+"px");
	
	//Calculamos el nuevo porcentaje en funcion del alto de la barra
	newPorcentaje = ((alturaContenedor-alturaScroll)*100)/alturaContenedor;
	newPorcentaje = parseFloat(newPorcentaje.toFixed(5));		
	
}

function handleSlide(e, ui) 
{		
	var new_uiValue;	
	
	new_uiValue = (-ui.value*100)/newPorcentaje;
	var maxScroll = $("#infoScroll").attr("scrollHeight") - $("#infoScroll").height();
	$("#infoScroll").attr({ scrollTop: new_uiValue * (maxScroll / 100) });
}
