//Parametres configurables
var scroll_visible=363;
var scroll_desp=4;
var scroll_retraso=0;
var cursor_offset=79;
var altura_botons=19;
var scroll_padsup=0;
var scroll_padinf=0;

//Variables no configurables
var scroll_max=0;
var scroll_inicial=0;
var scroll_actual=0;
var scroll_tendencia=0;
var EstaFentCursorScroll=false;
var OffsetScrollY=0;
var correccioOffset=0;

//Estandars
var IE = document.all?true:false;
var ns4=false; var ie4=false; var standard=false;
if (document.getElementById) {
  standard=true
} else if (document.all) {
  ie4=true
} else if (document.layers) {
  ns4=true
} else {
}

function mouCapaY(nomCapa,y) {
  if (ns4) { document.layers[nomCapa].top=y; }
  if (ie4) { document.all[nomCapa].style.top=y; }
  if (standard) { document.getElementById(nomCapa).style.top=y+'px'; }
}

// Mouse
function getMouseXY(e) {
  if (IE) { //para IE
	tempX = event.clientX + document.body.scrollLeft;
	tempY = event.clientY + document.body.scrollTop;
  } else { //para netscape
	tempX = e.pageX;
	tempY = e.pageY;
  }
  tempY=tempY-correccioOffset;
  if (EstaFentCursorScroll) {
	mouScroll(tempY);
	return false;
  }
}


function inicScroll() {

		scrollSiProcede();
		
		if (!IE) document.captureEvents(Event.MOUSEMOVE)
		document.onmousemove = getMouseXY;

		if (window.addEventListener){
			window.addEventListener('resize', actualitzaScroll, false);
		} else{
			window.attachEvent('onresize', actualitzaScroll);
		}	
		document.getElementById('cursor').onmousedown = CursorScrollOn;
		document.onmouseup  = CursorScrollOff;
}



function scrollSiProcede() {
	if (document.getElementById('scroll').offsetHeight > scroll_visible) {

		correccioOffset=document.getElementById('contenidor').offsetTop ;

		//Si no cabe el contenido, vamos a mostrar el scroll:
		document.getElementById('scroller').style.display='block';
		//document.getElementById('cos').className='conScroll';

		scroll_max=document.getElementById('scroll').offsetHeight - scroll_visible;
		scroll_inicial=document.getElementById('scroll').offsetTop;
		//correccioOffset=0;
		
		document.getElementById('scroll').style.height=scroll_visible+30+'px'; 

	} else {
		document.getElementById('scroller').style.display='none';
		document.getElementById('cos').className='sinScroll';
		document.getElementById('scroll').style.height=scroll_visible+30+'px'; 
	}
}



function actualitzaScroll() {
	if (document.getElementById('scroll').offsetHeight > scroll_visible) {
		//scroll_max=document.getElementById('scroll').offsetHeight - scroll_visible;
		//scroll_inicial=document.getElementById('scroll').offsetTop;
		correccioOffset=document.getElementById('contenidor').offsetTop ;
	}
}

function CursorScrollOn(e) {
	OffsetScrollY = parseInt(document.getElementById('cursor').offsetTop);
	EstaFentCursorScroll=true;
	return false;
}
function CursorScrollOff(e) {
	EstaFentCursorScroll=false;
	return;
}
function a(e) {
	return false;
}

function pujaScroll() {
	if (scroll_actual>0) {
		scroll_tendencia=scroll_desp * -1;
		autoScroll();
	} else {
		scroll_tendencia=0;		
	}
}
function baixaScroll() {
	if (scroll_actual<scroll_max) {
		scroll_tendencia=scroll_desp;
		autoScroll();
	} else {
		scroll_tendencia=0;		
	}
}
function paraScroll() {
	scroll_tendencia=0;
}
function autoScroll() {
	scroll_actual = scroll_actual + scroll_tendencia;
	mouCapaY('scroll', scroll_inicial - scroll_actual);
	if (scroll_actual<1) {
		scroll_tendencia=0;
		scroll_actual=0;
	}
	if (scroll_actual>=scroll_max-2) {
		scroll_tendencia=0;
		scroll_actual=scroll_max;
	}
	if (scroll_tendencia != 0) {
		setTimeout('autoScroll()', scroll_retraso)
	}
//	document.getElementById('scroll').style.clip = "rect("+(scroll_actual+2)+"px 278px "+(scroll_actual+scroll_visible)+"px 0px)";
	document.getElementById('scroll').style.clip = "rect("+(scroll_actual+scroll_padsup)+"px 1000px "+(scroll_actual + scroll_visible - scroll_padinf)+"px 0px)";
	

	scroll_percent=scroll_actual/scroll_max*100;


	mouCapaY ('cursor',scroll_inicial+altura_botons+(scroll_visible-altura_botons*2)*scroll_percent/100-cursor_offset);
	document.getElementById('scroll').style.height=(scroll_visible+30+scroll_actual)+'px'; 

}
function mouScroll(yyy) {
	if (yyy<scroll_inicial+altura_botons) {
		yyy=scroll_inicial+altura_botons;
	}
	if (yyy>scroll_inicial+scroll_visible-altura_botons) {
		yyy=scroll_inicial+scroll_visible-altura_botons;
	}
	mouCapaY ('cursor',yyy-cursor_offset);
	cursor_despla=yyy-(scroll_inicial+altura_botons);
	cursor_percent=cursor_despla/(scroll_visible-altura_botons*2)*100;
	scroll_actual=scroll_max*cursor_percent/100;
	scroll_tendencia=0;
	autoScroll();
	document.getElementById('scroll').style.height=(scroll_visible+30+scroll_actual)+'px'; 
}

function isDefined(variable) {
    return (typeof(window[variable]) == "undefined")?  false: true;
}

if (isDefined("scroll")) {
    if (window.addEventListener){
        window.addEventListener('load', inicScroll, false);
    }
    else{
        window.attachEvent('onload', inicScroll);
    }    
}

/* PDF FLOTANTE */

function sobrePdf() {
	document.getElementById('pdfFlotante').style.visibility='visible'; 
}
function foraPdf() {
	document.getElementById('pdfFlotante').style.visibility='hidden'; 
}
