
// Funció per mostrar/ocultar les incidencies de cada etapa a la ruta detallada
function miniincidencies(capa, src, arrayNames, arrayAffectations, arrayPoiCategory, arrayPoiTooltip, date ){

	//mostrar incidencies + canviar textos de mapa i incidencies
	if(document.getElementById(capa).innerHTML.indexOf("miniincidencies_id") < 0){
		var codiHtml = getHtmlIncidencias(arrayNames, arrayAffectations, arrayPoiCategory, arrayPoiTooltip,date);

		document.getElementById(capa).innerHTML = codiHtml;
		document.getElementById("linkInci" + capa.substring(4)).innerHTML = "<IMG src=\"img/rutas/incidencies_detall.gif\" border=\"0\"/>&#160;<A href=\"javascript:miniincidencies('" + capa + "', '" + src + "', " + arrayNames[0] + ", " + arrayAffectations[0] + ", " + arrayPoiCategory[0]  + ", " + arrayPoiTooltip[0] + ", '" + date + "')\" class=\"inici\">" + stringamagarInci + "</A>";
	
	//amagar mapa + canviar textos de mapa i incidencies
	} else {
		var codiHtml = " ";

		document.getElementById(capa).innerHTML = codiHtml;
		document.getElementById("linkInci" + capa.substring(4)).innerHTML = "<IMG src=\"img/rutas/incidencies_detall.gif\" border=\"0\"/>&#160;<A href=\"javascript:miniincidencies('" + capa + "', '" + src + "', " + arrayNames[0] + ", " + arrayAffectations[0] + ", " + arrayPoiCategory[0] + ", " + arrayPoiTooltip[0] + ", '" + date  + "')\" class=\"inici\">" + stringmostrarInci + "</A>";
		
	}
	document.getElementById("link" + capa.substring(4)).innerHTML =     "<IMG src=\"img/rutas/mapa_detall.gif\" border=\"0\"/>&#160;<A href=\"javascript:minimapa('" + capa + "', '" + src + "', " + arrayNames[0] + ", " + arrayAffectations[0] + ", " + arrayPoiCategory[0] + ", " + arrayPoiTooltip[0] + ", '" + date  + "')\" class=\"inici\">" + stringmostrarMapa + "</A>";
}

// Funció per mostrar/ocultar els mapes de cada etapa a la ruta detallada
function minimapa(capa, src, arrayNames, arrayAffectations, arrayPoiCategory, arrayPoiTooltip, date ){
	//mostrar mapa + canviar textos de mapa i incidencies
	if(document.getElementById(capa).innerHTML.indexOf("minimapa_id") < 0){
		var codiHtml = "<IMG id='minimapa_id' src='" + src + "' style='margin: 4px'/>";
		document.getElementById(capa).innerHTML = codiHtml;

		var codiHtml2;
		codiHtml2 =	"<IMG src=\"img/rutas/mapa_detall.gif\" border=\"0\"/>&#160;" 
			+ "<A href=\"javascript:minimapa('" + capa + "', '" 
												+ src + "', " 
												+ arrayNames[0] + ", " 
												+ arrayAffectations[0] + ", " 
												+ arrayPoiCategory[0] + ", " 
												+ arrayPoiTooltip[0] + ", '" 
												+ date  + "')\"" 
				+ "); class=\"inici\">" + stringamagarMapa + "</A>";
		document.getElementById("link" + capa.substring(4)).innerHTML = codiHtml2;

	//amagar mapa + canviar textos de mapa i incidencies
	} else {
		var codiHtml = " ";

		document.getElementById(capa).innerHTML = codiHtml;
		document.getElementById("link" + capa.substring(4)).innerHTML = "<IMG src=\"img/rutas/mapa_detall.gif\" border=\"0\"/>&#160;<A href=\"javascript:minimapa('" + capa + "', '" + src + "', " + arrayNames[0] + ", " + arrayAffectations[0] + ", " + arrayPoiCategory[0] + ", " + arrayPoiTooltip[0] + ", '" + date  + "')\" class=\"inici\">" + stringmostrarMapa + "</A>";

	}
	document.getElementById("linkInci" + capa.substring(4)).innerHTML = "<IMG src=\"img/rutas/incidencies_detall.gif\" border=\"0\"/>&#160;<A href=\"javascript:miniincidencies('" + capa + "', '" + src + "', " + arrayNames[0] + ", " + arrayAffectations[0] + ", " + arrayPoiCategory[0] + ", " + arrayPoiTooltip[0] + ", '" + date + "')\" class=\"inici\">" + stringmostrarInci + "</A>";
}

// devuelve el codigo html con las incidencias en forma de tabla
function getHtmlIncidencias(arrayNames, arrayAffectations, arrayPoiCategory, arrayPoiTooltip, date){

	var	codiHtml = '';
	var	tableWidth = '215px';
	var dateString = date.substr(0,date.length-3); //eliminem els segons de la data

	if (arrayNames.length > 1) {

		codiHtml+='<TABLE id="miniincidencies_id" WIDTH="'+ tableWidth + '" CELLSPACING="0" CELLPADDING="0">';
		codiHtml+='<TR>';
		codiHtml+='	<TD WIDTH="100%" BGCOLOR="000000">';
		codiHtml+='		<TABLE WIDTH="100%" CELLSPACING="1" CELLPADDING="1">';
		codiHtml+='			<TR>';
		codiHtml+='				<TD WIDTH="100%" BGCOLOR="#F76841" align="center">';
		codiHtml+='					<FONT class="text8blanc"><B>' + stringIncidencies + '</B>&#160;&#160;' +dateString +'</FONT>';
		codiHtml+='				</TD>';
		codiHtml+='			</TR>';
		codiHtml+='			<TR>';
		codiHtml+='				<TD WIDTH="100%" BGCOLOR="FFFFFF">';
		codiHtml+='					<FONT face="Arial,Helvetica" size="1">';
		codiHtml+='					<TABLE class="text8" border="0" WIDTH="100%">';
		for ( var i=1; i<arrayNames.length; i++ )
		{
			var linkHREF;
			linkHREF=arrayPoiTooltip[i]
			linkHREF=replaceAll(linkHREF,'\'','~1')	//codifiquem cometes simples
			linkHREF=replaceAll(linkHREF,'"','~2')	//codifiquem cometes simples
			linkHREF=replaceAll(linkHREF,'\u00A9','\'') //substituim el caracter de copyright per la cometa simple

			codiHtml+='<TR><TD><IMG width="16" SRC="img/inc/incidencia_' + arrayPoiCategory[i] + '.gif"></TD><TD colspan="2"><B>'+ arrayAffectations[i] + '</B></TD></TR>'
			codiHtml+='<TR><TD>&#160;</TD><TD>' + arrayNames[i] +'</TD><TD align="right"><A HREF="'+ linkHREF +'"><FONT class="text8">+info</FONT></A></TD></TR>';
		}
		codiHtml+='					</TABLE>';
		codiHtml+='				</TD>';
		codiHtml+='			</TR>';
		codiHtml+='		</TABLE>';
		codiHtml+='	</TD>';
		codiHtml+='</TR>';
		codiHtml+='</TABLE>';
		return codiHtml;

	// No deberia llegarse si se oculta el boton cuando no haya incidencias
	} else {
		codiHtml+='<TABLE WIDTH="'+ tableWidth + '" CELLSPACING="0" CELLPADDING="0">';
		codiHtml+='<TR>';
		codiHtml+='	<TD WIDTH="100%" BGCOLOR="000000">';
		codiHtml+='		<TABLE WIDTH="100%" CELLSPACING="1" CELLPADDING="1">';
		codiHtml+='			<TR>';
		codiHtml+='				<TD WIDTH="100%" BGCOLOR="#F76841" align="center">';
		codiHtml+='					<FONT size="1"><FONT COLOR="FFFFFF">Sense Incidčncies</FONT>';
		codiHtml+='				</TD>';
		codiHtml+='			</TR>';
		codiHtml+='		</TABLE>';
		codiHtml+='	</TD>';
		codiHtml+='</TR>';
		codiHtml+='</TABLE>';
		return codiHtml;
	}

}

// Funcion para formatear el texto de una fecha. 20041102091408 -->02/11/2004 09:14
function getFormattedDate(d) {
	var ret;
	// 20041102091408
	// 01234567890123
	if (d.length == 14)
		ret = d.substr(6,2) + "/" + d.substr(4,2) + "/" + d.substr(0,4) + " " + d.substr(8,2) + ":" + d.substr(10,2);

	return ret;
}

// Función para recargar el Frame PARENT con una URL
function botonTamano(par){
	parent.location.href = par + getURLLayerParams(par);
}

function replaceAll( str, from, to ) {
    var idx = str.indexOf( from );


    while ( idx > -1 ) {
        str = str.replace( from, to );
        idx = str.indexOf( from );
    }

    return str;
}

// Funció per activar/desactivar una capa
function activarDesactivarCapa(capa/*, categoria*/){

	if (document.getElementById(capa).style.visibility == 'hidden'){
		document.getElementById(capa).style.visibility = 'visible';
	} else {
		document.getElementById(capa).style.visibility = 'hidden';
	}

	document.getElementById("layerRetencionsVisibility").value=document.getElementById("layerRetencions").style.visibility;
	document.getElementById("layerObresVisibility").value=document.getElementById("layerObres").style.visibility;
	document.getElementById("layerConsVisibility").value=document.getElementById("layerCons").style.visibility;
	document.getElementById("layerMeteoVisibility").value=document.getElementById("layerMeteo").style.visibility;
}

//Realitza una petició d'un mapa afegint els parametres que permeten mantenir l'estat dels botons dels layers
function peticioMapa(url){
	document.location.href =  url + getURLLayerParams(url);
}

// Genera l'string de la url per mantenir l'estat
function getURLLayerParams(url) {
	var newURL = "";
	newURL += "&layerretencionsvisibility=" + document.getElementById("layerRetencions").style.visibility;
	newURL += "&layerobresvisibility=" + document.getElementById("layerObres").style.visibility;
	newURL += "&layerconsvisibility=" + document.getElementById("layerCons").style.visibility;
	newURL += "&layermeteovisibility=" + document.getElementById("layerMeteo").style.visibility;

	return newURL;
}


// Funció per mostrar la campa amb la informació d'un poi
function mostrarTooltip(posx,posy,txt,imgSource){

	// window.status= "X:" + event.x  + " Y:" + event.y

	x =posx+16;
	y =posy+16;


	var textHTML
	textHTML = "<TABLE cellpadding='0' cellspacing='0' border='0' width='170' height='100%'>";
	textHTML += "<TR>";
	textHTML += "<TD><IMG border='0' height='0' width='3' SRC='img/empty.gif'/></TD>";
	textHTML += "<TD><IMG SRC='"+ imgSource +"' width='16'><IMG border='0' height='0' width='5' SRC='img/empty.gif'/><SPAN class='text8'>" + txt + "</SPAN></TD>";
	textHTML += "</TR>";
	textHTML += "</TABLE>";

	document.getElementById("poi").style.zindex = 254;
	document.getElementById("poi").style.visibility = 'visible';
	document.getElementById("poi").style.left = parseInt(x);
	document.getElementById("poi").style.top = parseInt(y);
	document.getElementById("poi").innerHTML = textHTML;


}

function ocultarTooltip(){
	document.getElementById("poi").style.visibility = 'hidden';
}




// Mostra un tooltip amb la informació completa sobre l'incidencia
function mostrarToolTip2(level_description, date, road_code, pk_init, poblation, pk_end, affectation, direction, image,  string_date, string_road, string_init, string_poblation, string_end, string_affectation, string_dir){
	var textHTML;
	var stringTitol;

	level_description = replaceAll(replaceAll(level_description,'~2','"'),'~1','\'');
	road_code = replaceAll(replaceAll(road_code,'~2','"'),'~1','\'');
	poblation = replaceAll(replaceAll(poblation,'~2','"'),'~1','\'');
	affectation = replaceAll(replaceAll(affectation,'~2','"'),'~1','\'');
	direction = replaceAll(replaceAll(direction,'~2','"'),'~1','\'');

	var finestra = window.open("","Incidčncies","height=170,width=450,status=no,toolbar=no,menubar=no,location=no");
	finestra.document.close();
	var finestra = window.open("","Incidčncies","height=170,width=450,status=no,toolbar=no,menubar=no,location=no");

	finestra.document.write( '	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">');
	finestra.document.write( '<HTML>');
	finestra.document.write( '	<HEAD>');
	finestra.document.write( '		<TITLE>' + stringIncidencies + '</TITLE>');
	finestra.document.write( '		<LINK REL="stylesheet" HREF="mobilitat.css" TYPE="text/css"/>');
	finestra.document.write( '		<LINK REL="stylesheet" HREF="styles.css" TYPE="text/css"/>');
	finestra.document.write( '	</HEAD>');
	finestra.document.write( '	<BODY BGCOLOR="#EFEFE7" style="margin: 0px">');
	finestra.document.write( '	<CENTER>');
	finestra.document.write( '	<IMG SRC="img/empty.gif" border="0" width="1" height="10"/>');

	finestra.document.write('<TABLE id="miniincidencies_id" WIDTH="95%" CELLSPACING="0" CELLPADDING="0">');
	finestra.document.write('<TR>');
	finestra.document.write('	<TD WIDTH="100%" BGCOLOR="000000">');
	finestra.document.write('		<TABLE WIDTH="100%" CELLSPACING="1" CELLPADDING="1">');
	finestra.document.write('			<TR>');
	finestra.document.write('				<TD BGCOLOR="white" align="center">');
	//finestra.document.write('					<IMG SRC="img/empty.gif" border="0" width="8" height="3"/>');
	finestra.document.write('					<IMG SRC="' + image + '">');
	finestra.document.write('				</TD>');
	finestra.document.write('				<TD BGCOLOR="#F76841" align="center">');

	if (level_description != "") stringTitol=level_description
	else stringTitol=affectation;

	finestra.document.write( '					<IMG SRC="img/empty.gif" border="0" width="1" height="3"/>');
	finestra.document.write('					<FONT class="text10blanc"><B>' + stringTitol + '</B>&#160;&#160;</FONT><BR><FONT class="text8blanc">' + getFormattedDate(date) + '</FONT>' );
	finestra.document.write('				</TD>');
	finestra.document.write('			</TR>');
	finestra.document.write('			<TR>');
	finestra.document.write('				<TD WIDTH="100%" BGCOLOR="FFFFFF" colspan="2">');
	finestra.document.write('					<FONT face="Arial,Helvetica" size="1">');
	finestra.document.write('					<TABLE class="text8" border="0" WIDTH="100%">');
	
	finestra.document.write('<TR><TD width="80">&#160;&#160;<B>' + string_road + ':</B>&#160;</TD><TD>' + road_code +'</TD><TD align="right"></TD></TR>');
	finestra.document.write('<TR><TD>&#160;&#160;<B>' + string_init + ':</B>&#160;</TD><TD>Km.&#160;' + pk_init +'&#160;('+ poblation +')</TD><TD align="right"></TD></TR>');
//	finestra.document.write('<TR><TD>&#160;&#160;<B>' + string_poblation + ':</B>&#160;</TD><TD>' + poblation +'</TD><TD align="right"></TD></TR>');
	finestra.document.write('<TR><TD>&#160;&#160;<B>' + string_end + ':</B>&#160;</TD><TD>Km.&#160;' + pk_end +'</TD><TD align="right"></TD></TR>');
	finestra.document.write('<TR><TD>&#160;&#160;<B>' + string_affectation + ':</B>&#160;</TD><TD>' + affectation +'</TD><TD align="right"></TD></TR>');
	if (direction != "") {
		finestra.document.write('<TR><TD>&#160;&#160;<B>' + string_dir + ':</B>&#160;</TD><TD>' + direction +'</TD><TD align="right"></TD></TR>');
	}

	finestra.document.write('					</TABLE>');
	finestra.document.write('				</TD>');
	finestra.document.write('			</TR>');
	finestra.document.write('		</TABLE>');
	finestra.document.write('	</TD>');
	finestra.document.write('</TR>');
	finestra.document.write('</TABLE>');

	finestra.document.write( '	</CENTER>');
	finestra.document.write( '	</BODY>');
	finestra.document.write( '	</HTML>');

	
	finestra.focus();
	
	
	//finestra.document.getElementById("toolTipInfo").innerHTML = textHTML;

}

