var lstlinea = '';
var lstparada = '';
var refreshing = false;
var effect = 'appear';

function mostrarLineas() {

	parxmlhttp = getXMLHttp();

	setWorking( 'working' );
	parxmlhttp.open("GET", "/code/getlineas.php", true);
	parxmlhttp.onreadystatechange = function() {
		if (parxmlhttp.readyState == 4) {
			document.getElementById( 'clistalineas' ).innerHTML = parxmlhttp.responseText;
			//setWorking( 'idle' );
			Effect.toggle( 'clistalineas', effect, { duration: 0.7 } );
			//document.getElementById( 'clistalineas' ).style.display = 'block';
		}
	}
	parxmlhttp.send(null);

}

function ocultarLineas() {

	setWorking( 'idle' );
	Effect.toggle( $( 'clistalineas' ), effect, { duration: 0.7 } );

}

function mostrarParadas( idlinea ) {

	parxmlhttp = getXMLHttp();

	lstlinea = idlinea;

	$('linea').value = idlinea;
	parxmlhttp.open("GET", "/code/getparadas.php?idl=" + idlinea, true);
	parxmlhttp.onreadystatechange = function() {
		if (parxmlhttp.readyState == 4) {
			/* Si no es un refresco automático limpiamos el panel de información de la parada */
			if (!refreshing) {
				document.getElementById( 'cinfoparada' ).innerHTML = '';
			} else {
				refreshing = false;
			}
			document.getElementById( 'cparadas' ).innerHTML = parxmlhttp.responseText;
			if ($('clistalineas').style.display != 'none') {
				ocultarLineas();
			}
		}
	}
	parxmlhttp.send(null);
}

function ocultaInfoParadas() {
	//Effect.Fade( 'cinfoparada', {duration:1.0} );
	document.getElementById( 'cinfoparada' ).style.display = 'none';
	//new Effect.Opacity( 'cinfoparada', {duration: 1.0, from: 1.0, to: 0.0, afterFinish: $( 'cinfoparada' ).style.display = 'none'} );
	//new Effect.Opacity( 'cinfoparada', {duration: 1.0, from: 1.0, to: 0.0, queue: 'end', scope: 'info', limit: 1} );
	lstparada = '';
}

function mostrarInfoParadas( map, idlinea, idparada ) {

	infxmlhttp = getXMLHttp();

	lstparada = idparada;

	tmp = idparada.split( '::' );
	idparada = tmp[0];
	idorden = tmp[1];
	$('linea').value = idlinea;
	infxmlhttp.open("GET", "/code/getparadas.php?idl=" + idlinea + "&idp=" + idparada + "&ido=" + idorden, true);
	infxmlhttp.onreadystatechange = function() {
		if ((infxmlhttp.readyState == 4) && (lstparada > '')) {
			document.getElementById( 'cinfoparada' ).innerHTML = infxmlhttp.responseText;
			if ($('cinfoparada').style.display == 'none') {
				document.getElementById( 'cinfoparada' ).style.left = (posX + 20) + 'px';
				document.getElementById( 'cinfoparada' ).style.display = 'block';
				wd = document.body.clientWidth - posX - 80;
				if (wd > 300) wd = 300;
				wd = wd + 'px';
				document.getElementById( 'cinfoparada' ).style.width = wd;
				//$(map).onmouseout = null;
				//Effect.Appear( 'cinfoparada', {duration:1.0, afterFinish: $(map).onmouseover = ocultaInfoParadas} );
				//Effect.Opacity( 'cinfoparada', {duration:1.0, from: 0.0, to: 1.0, afterFinish: $( 'cinfoparada' ).style.display = 'block'} );
				//document.getElementById( 'cinfoparada' ).style.display = 'block';
				//Effect.Opacity( 'cinfoparada', {duration:1.0, from: 0.0, to: 1.0, queue: 'end', scope: 'info', limit: 1} );
			}
		}
	}
	infxmlhttp.send(null);
}

function refrescar() {

	/* marcamos la variable refreshing para no eliminar el panel de información si se está mostrando */
	refreshing = true;
	if (lstlinea > '') {
		mostrarParadas( lstlinea );
	}
	if (lstparada > '') {
		mostrarInfoParadas( lstlinea, lstparada );
	} else {
		ocultaInfoParadas();
	}

}

function init() {

	$( 'wrkng' ).style.display = 'none';
	$( 'clistalineas' ).style.display = 'none';
	ocultaInfoParadas();
	//$( 'cinfoparada' ).style.display = 'none';
	//$( 'lineas' ).selectedIndex = 0;
	mostrarParadas( $('linea').value );

}

function MouseMove(e) {

	posX=(isNS4||isNS6)?e.pageX:event.clientX+document.body.scrollLeft;
	posY=(isNS4||isNS6)?e.pageY:event.clientY+document.body.scrollTop;

}

function setWorking( state ) {
	var e = $('wrkng');
	if (state == 'working') {
		e.style.top = document.body.scrollTop;
		Effect.Appear( 'wrkng', { to: 0.7, duration: 0.7 } );
		//e.style.display = 'block';
	} else {
		Effect.Fade( 'wrkng', { duration: 0.7 } );
		//e.style.display = 'none';
	}
}

setInterval( "refrescar()", 30000 );

var isNS4=document.layers?true:false;
var isIE=document.all?true:false;
var isNS6=!isIE&&document.getElementById?true:false;

if(isNS4) {
	document.captureEvents(Event.MOUSEMOVE);
} else if(isNS6) {
	document.addEventListener('mousemove', MouseMove, true);
} else {
	document.onmousemove=MouseMove;
}

window.onload = init;