var tipo_origen;
var tipo_destino;

var url_gml_origen;
var url_gml_destino;

var via_destino;
var variante_destino;

var x_origen;
var y_origen;
var x_destino;
var y_destino;
var x_sitio_marcado;
var y_sitio_marcado;
var x_esquina;
var y_esquina;
var x_parada;
var y_parada;

var lowerLeft_x_destino;
var lowerLeft_y_destino;
var upperRight_x_destino;
var upperRight_y_destino;

var desc_origen;
var desc_destino;
var desc_sitio_marcado;
var desc_esquina;
var desc_parada;
var desc_paradas;
var desc_linea1;
var desc_linea2;

var dir_origen;
var dir_destino;
var dir_sitio_marcado;

var icon_origen;
var icon_destino;
var icon_lugar_marcado;
var icon_parada;

var popup_origen;
var popup_destino;
var popup_lugar_marcado;
var popup_parada;
var popup_parada1;
var popup_parada2;
var popup_parada3;
var popup_parada4;

var marker_origen;
var marker_destino;
var marker_lugar_marcado;
var marker_parada;
var marker_parada1;
var marker_parada2;
var marker_parada3;
var marker_parada4;

var markers_origen;
var markers_destino;
var markers_lugar_marcado;
var markers_parada;

var gmlURL_lugar_de_interes_origen;
var gmlURL_lugar_de_interes_destino;

function esLayerPoligono(layer){
	return layer==LAYER_POLIGONO_LUGAR_DE_INTERES_ORIGEN ||
		   layer==LAYER_POLIGONO_LUGAR_DE_INTERES_DESTINO ||
		   layer==LAYER_POLIGONO_LUGAR_DE_INTERES_MARCADO;
}

function esLayerOrigen(layer){
	return layer==LAYER_ORIGEN || layer==LAYER_LUGAR_DE_INTERES_ORIGEN;
}

function esLayerDestino(layer){
	return layer==LAYER_DESTINO || layer==LAYER_LUGAR_DE_INTERES_DESTINO;
}

function esLayerLugarMarcado(layer){
	return layer==LAYER_SITIO_MARCADO || layer==LAYER_LUGAR_DE_INTERES_MARCADO || layer==LAYER_POLIGONO_LUGAR_DE_INTERES_MARCADO;
}

function esPunto(tipo){
	return tipo == TIPO_ESQUINA ||
		   tipo == TIPO_DIRECCION || 
		   tipo == TIPO_LUGAR_DE_INTERES;
}

function limpiarRecorridos(){
	limpiarLayer(LAYER_RECORRIDO_A_PIE_ORIGEN);
	limpiarLayer(LAYER_RECORRIDO_OMNIBUS);
	limpiarLayer(LAYER_RECORRIDO_OMNIBUS_1);
	limpiarLayer(LAYER_RECORRIDO_OMNIBUS_2);
	limpiarLayer(LAYER_RECORRIDO_A_PIE_DESTINO);
	limpiarLayer(LAYER_PARADAS_RUTA);
	limpiarLayer(LAYER_RECORRIDO_A_PIE_TRANSBORDO);
	for (i=1; i<=4; i++)
		limpiarLayer(LAYER_PARADAS_NOTABLES);
	limpiarLayer(LAYER_RUTA_A_PIE);
	limpiarLayer(LAYER_RUTA_EN_AUTO);
}

function limpiarLayersMarcados(){
	limpiarLayer(LAYER_SITIO_MARCADO);
	limpiarLayer(LAYER_CALLE_MARCADA);
	limpiarLayer(LAYER_RECORRIDO_MARCADO);
	limpiarLayer(LAYER_PARADAS_RECORRIDO_MARCADO);
	limpiarLayer(LAYER_LUGAR_DE_INTERES_MARCADO);
	limpiarLayer(LAYER_POLIGONO_LUGAR_DE_INTERES_MARCADO);
	x_sitio_marcado = null;
	y_sitio_marcado = null;
}

function zoomOrigenDestinoCaminoAPie(){
	if (map.getLayersByName(LAYER_RUTA_A_PIE).size()==0)
		zoomOrigenDestino();
}

function zoomOrigenDestino(limXLL,limYLL,limXUR,limYUR){
	var newBbox, xLl, yLl, xUr, yUr;
	clearResultados(true);
	if (x_origen==undefined || y_origen==undefined)
		return;
	apagarPopups();
	if (esPunto(tipo_destino)){
		if (x_destino==undefined || y_destino==undefined)
			return;
		if (x_origen < x_destino){
			xLl = x_origen;
			xUr = x_destino;
		}
		else{
			xLl = x_destino;
			xUr = x_origen;
		}
		if (y_origen < y_destino){
			yLl = y_origen;
			yUr = y_destino;
		}
		else{
			yLl = y_destino;
			yUr = y_origen;
		}
	}
	else{
		if (lowerLeft_x_destino==undefined || lowerLeft_y_destino==undefined || 
				upperRight_x_destino==undefined || upperRight_y_destino==undefined)
			return;
		if (x_origen < lowerLeft_x_destino){
			xLl = x_origen;
			xUr = upperRight_x_destino;
		}
		else{
			if (x_origen < upperRight_x_destino){
				xLl = lowerLeft_x_destino;
				xUr = upperRight_x_destino;
			}
			else{
				xLl = lowerLeft_x_destino;
				xUr = x_origen;
			}
		}
		if (y_origen < lowerLeft_y_destino){
			yLl = y_origen;
			yUr = upperRight_y_destino;
		}
		else{
			if (y_origen < upperRight_y_destino){
				yLl = lowerLeft_y_destino;
				yUr = upperRight_y_destino;
			}
			else{
				yLl = lowerLeft_y_destino;
				yUr = y_origen;
			}
		}
	}
	if (limXLL!=undefined){
		if (limXLL<xLl)
			xLl = limXLL;
		if (limYLL<yLl)
			yLl = limYLL;
		if (limXUR>xUr)
			xUr = limXUR;
		if (limYUR>yUr)
			yUr = limYUR;
	}
	offset = 250;
	newBbox = createBBox(xLl-2*offset,yLl-offset,xUr+offset,yUr+2*offset);	
	map.zoomToExtent(newBbox);
}

function zoomToLayer(layerName){
	console.log("Haciendo zoom hacia la layer");
	layerArray = map.getLayersByName(layerName);
	if(layerArray.size()==0) {
		alert('no existen resultados ');
		return;
	}
	bBox2 = map.getLayersByName(layerName)[0].getDataExtent();
	extremos = bBox2.toArray();
	if (esLayerPoligono(layerName))
		offset = 200;
	else
		offset = 500;
	extremos[0] = extremos[0] - offset;
	extremos[1] = extremos[1] - offset;
	extremos[2] = extremos[2] + offset;
	extremos[3] = extremos[3] + offset;
	map.zoomToExtent(createBBox(extremos[0],extremos[1],extremos[2],extremos[3]));

}

function destroyLayer(layerName){

	layerArray = map.getLayersByName(layerName);
	if(layerArray.size()>0){
		map.removeLayer(layerArray[0],false);
		layerArray[0].destroyFeatures();
	}
}

function limpiarLayer(layerName){
	layerArray = map.getLayersByName(layerName);
	if(layerArray.size()>0){
		layerArray[0].destroy();
	}
	if (layerName==LAYER_CALLE_MARCADA)
		initCalleMarcada();
	if (layerName==LAYER_CALLE_DESTINO)
		initCalleDestino();
	if (layerName==LAYER_RECORRIDO_MARCADO)
		initRecorridoMarcado();
	if (layerName==LAYER_RECORRIDO_DESTINO)
		initRecorridoDestino();
	if (layerName==LAYER_PARADAS_RUTA)
		initParadasRuta();
	if (layerName==LAYER_PARADAS_RECORRIDO_MARCADO)
		initParadasRecorridoMarcado();
	if (layerName==LAYER_PARADAS_RECORRIDO_DESTINO)
		initParadasRecorridoDestino();
}

function loadResultado(layerTramoPieOrigen_GML_Url,layerTramoVariante1_GML_Url,layerTramoVariante2_GML_Url,layerTramoPieDestino_GML_Url,
		layerTramoPieTransbordo_GML_Url,cod_variante1,cod_variante1_circ,linea1,ordinal1,ordinal2,
		cod_variante2,cod_variante2_circ,linea2,ordinal3,ordinal4,x,y,paradas,limXLL,limYLL,limXUR,limYUR){
	apagarPopups();
	x_paradas = x;
	y_paradas = y;
	desc_paradas = paradas;
	desc_linea1 = linea1; 
	desc_linea2 = linea2; 
	if (layerTramoPieTransbordo_GML_Url == null)
		loadResultadoDirecto(layerTramoPieOrigen_GML_Url,layerTramoVariante1_GML_Url,layerTramoPieDestino_GML_Url, 
				cod_variante1,cod_variante1_circ,ordinal1,ordinal2,limXLL,limYLL,limXUR,limYUR);
	else
		loadResultadoCombinacion(layerTramoPieOrigen_GML_Url,layerTramoVariante1_GML_Url,layerTramoVariante2_GML_Url,layerTramoPieDestino_GML_Url,
				layerTramoPieTransbordo_GML_Url,cod_variante1,cod_variante1_circ,ordinal1,ordinal2,
				cod_variante2,cod_variante2_circ,ordinal3,ordinal4,limXLL,limYLL,limXUR,limYUR);

	// Dejo los marcadores de origen y destino arriba de las otras capas 
	limpiarLayer(LAYER_ORIGEN);
	limpiarLayer(LAYER_DESTINO);
	if (esPunto(tipo_origen)){
		agregarMarcador(x_origen,y_origen,LAYER_ORIGEN,desc_origen,dir_origen);
	}
	if (esPunto(tipo_destino)){
		agregarMarcador(x_destino,y_destino,LAYER_DESTINO,desc_destino,dir_destino);
	}
}

function loadResultadoDirecto(layerTramoPieOrigen_GML_Url,layerTramoVariante_GML_Url,layerTramoPieDestino_GML_Url,
						cod_variante,cod_variante_circ,ordinal1,ordinal2,limXLL,limYLL,limXUR,limYUR){
	console.log("Cargando resultado = " + layerTramoPieOrigen_GML_Url + ", " + layerTramoVariante_GML_Url + ", " + layerTramoPieDestino_GML_Url);
	gmlTramoPieOrigenURL = urlApp+"/"+layerTramoPieOrigen_GML_Url;
	gmlTramoVarianteURL = urlApp+"/"+layerTramoVariante_GML_Url;
	gmlTramoPieDestinoURL = urlApp+"/"+layerTramoPieDestino_GML_Url;
	clearResultados(false);
	zoomOrigenDestino(limXLL,limYLL,limXUR,limYUR);
	loadGMLLayer(LAYER_RECORRIDO_A_PIE_ORIGEN,gmlTramoPieOrigenURL,LAYER_RECORRIDO_A_PIE_ORIGEN,false);
	loadGMLLayer(LAYER_RECORRIDO_A_PIE_DESTINO,gmlTramoPieDestinoURL,LAYER_RECORRIDO_A_PIE_DESTINO,false);
	loadGMLLayer(LAYER_RECORRIDO_OMNIBUS,gmlTramoVarianteURL,LAYER_RECORRIDO_OMNIBUS,false);

	// Dejo las paradas arriba de las otras capas
	var filter;
	if (cod_variante_circ==0){
		filter = 'cod_variante = ' + cod_variante + ' and ordinal between ' + ordinal1 + ' and ' + ordinal2;
	}
	else{
		filter = 'cod_variante = ' + cod_variante + ' and ordinal >= ' + ordinal1 + ' or ' + 
				 'cod_variante = ' + cod_variante_circ + ' and ordinal <= ' + ordinal2;
	}
	addLayerWithFilter(paradas_ruta,filter,null);
	for (i=0; i<x_paradas.size(); i++){
		agregarMarcador(x_paradas[i],y_paradas[i],LAYER_PARADAS_NOTABLES,desc_paradas[i],null,i+1);
	}	
}

function loadResultadoCombinacion(layerTramoPieOrigen_GML_Url,layerTramoVariante1_GML_Url,layerTramoVariante2_GML_Url,layerTramoPieDestino_GML_Url,
		layerTramoPieTransbordo_GML_Url,cod_variante1,cod_variante1_circ,ordinal1,ordinal2,
		cod_variante2,cod_variante2_circ,ordinal3,ordinal4,limXLL,limYLL,limXUR,limYUR){
	console.log("Cargando resultado = " + layerTramoPieOrigen_GML_Url + ", " + layerTramoVariante1_GML_Url + ", " + layerTramoVariante2_GML_Url + ", " + layerTramoPieDestino_GML_Url + ", " + layerTramoPieTransbordo_GML_Url);
	gmlTramoPieOrigenURL = urlApp+"/"+layerTramoPieOrigen_GML_Url;
	gmlTramoVariante1URL = urlApp+"/"+layerTramoVariante1_GML_Url;
	gmlTramoVariante2URL = urlApp+"/"+layerTramoVariante2_GML_Url;
	gmlTramoPieDestinoURL = urlApp+"/"+layerTramoPieDestino_GML_Url;
	gmlTramoPieTransbordoURL = urlApp+"/"+layerTramoPieTransbordo_GML_Url;
	clearResultados(false);
	zoomOrigenDestino(limXLL,limYLL,limXUR,limYUR);
	loadGMLLayer(LAYER_RECORRIDO_A_PIE_ORIGEN,gmlTramoPieOrigenURL,LAYER_RECORRIDO_A_PIE_ORIGEN,false);
	loadGMLLayer(LAYER_RECORRIDO_A_PIE_DESTINO,gmlTramoPieDestinoURL,LAYER_RECORRIDO_A_PIE_DESTINO,false);
	loadGMLLayer(LAYER_RECORRIDO_A_PIE_TRANSBORDO,gmlTramoPieTransbordoURL,LAYER_RECORRIDO_A_PIE_TRANSBORDO,false);
	loadGMLLayer(LAYER_RECORRIDO_OMNIBUS_1,gmlTramoVariante1URL,LAYER_RECORRIDO_OMNIBUS_1,false);
	loadGMLLayer(LAYER_RECORRIDO_OMNIBUS_2,gmlTramoVariante2URL,LAYER_RECORRIDO_OMNIBUS_2,false);

	// Dejo las paradas arriba de las otras capas
	var filter1, filter2;
	if (cod_variante1_circ==0){
		filter1 = 'cod_variante = ' + cod_variante1 + ' and ordinal between ' + ordinal1 + ' and ' + ordinal2;
	}
	else{
		filter1 = 'cod_variante = ' + cod_variante1 + ' and ordinal >= ' + ordinal1 + ' or ' + 
				  'cod_variante = ' + cod_variante1_circ + ' and ordinal <= ' + ordinal2;
	}
	if (cod_variante2_circ==0){
		filter2 = 'cod_variante = ' + cod_variante2 + ' and ordinal between ' + ordinal3 + ' and ' + ordinal4;
	}
	else{
		filter2 = 'cod_variante = ' + cod_variante2 + ' and ordinal >= ' + ordinal3 + ' or ' + 
				  'cod_variante = ' + cod_variante2_circ + ' and ordinal <= ' + ordinal4;
	}
		
	addLayerWithFilter(paradas_ruta,filter1 + ' or ' + filter2,null);
	for (i=0; i<x_paradas.size(); i++){
		agregarMarcador(x_paradas[i],y_paradas[i],LAYER_PARADAS_NOTABLES,desc_paradas[i],null,i+1);
	}

}

function clearResultados(borrar_marcados){	
	limpiarRecorridos();
	if (borrar_marcados)
		limpiarLayersMarcados();
}

function removeFeatures(layerName){
	console.log("borrando features capa = "+ layerName);
	var layerArray = map.getLayersByName(layerName);
	if(layerArray.size()>0){
		layerArray[0].destroyFeatures();
	}
}

function addLayerWithFilter(layer,cqlFValue,newBbox){
	console.log("bbox = "+ newBbox);

	var filterParams = {
			cql_filter:cqlFValue //'cod_nombre_via='+filterInput.value

	};
	layer.mergeNewParams(filterParams);
	layerArray = map.getLayersByName(layer.name);

	if(layerArray.size()==0){
		map.addLayer(layer);
	}
	if (newBbox!=null){
		map.zoomToExtent(newBbox);
	}
	upMarkers();
}

function createBBox(lowerLeftX,lowerLeftY,upperRightX,upperRightY){
	// make new bounding box
	var ul = new Array();// x, y
	var br = new Array();// x, y
	ul[0] = lowerLeftX;
	ul[1] = lowerLeftY;
	br[0] = upperRightX;
	br[1] = upperRightY;
	return new OpenLayers.Bounds(ul[0],ul[1],br[0],br[1]);
}

function intercambiarExtremos(){
	zoomOrigenDestino();
	apagarPopups();
	limpiarRecorridos();
	limpiarLayer(LAYER_DESTINO);
	limpiarLayer(LAYER_LUGAR_DE_INTERES_DESTINO);
	limpiarLayer(LAYER_POLIGONO_LUGAR_DE_INTERES_DESTINO);
	limpiarLayer(LAYER_ORIGEN);
	limpiarLayer(LAYER_LUGAR_DE_INTERES_ORIGEN);
	limpiarLayer(LAYER_POLIGONO_LUGAR_DE_INTERES_ORIGEN);
	// Guardo x e y de origen porque en agregarMarcador los modifico
	var tmp_x = x_origen;
	var tmp_y = y_origen;

	var tmp_desc = desc_origen;
	desc_origen = desc_destino;
	desc_destino = tmp_desc;

	var tmp_dir = dir_origen;
	dir_origen = dir_destino;
	dir_destino = tmp_dir;

	var tmp_tipo = tipo_origen;
	tipo_origen = tipo_destino;
	tipo_destino = tmp_tipo;
	
	var tmp_gmlURL = gmlURL_lugar_de_interes_origen;
	gmlURL_lugar_de_interes_origen = gmlURL_lugar_de_interes_destino;
	gmlURL_lugar_de_interes_destino = tmp_gmlURL;
	
	agregarMarcador(x_destino,y_destino,LAYER_ORIGEN,desc_origen,dir_origen);
	agregarMarcador(tmp_x,tmp_y,LAYER_DESTINO,desc_destino,dir_destino);

	if (tipo_origen==TIPO_LUGAR_DE_INTERES){
		limpiarLayer(LAYER_POLIGONO_LUGAR_DE_INTERES_ORIGEN);
		loadGMLLayer(LAYER_POLIGONO_LUGAR_DE_INTERES_ORIGEN,gmlURL_lugar_de_interes_origen,LAYER_LUGAR_DE_INTERES_ORIGEN,false);
	}
	if (tipo_destino==TIPO_LUGAR_DE_INTERES){
		limpiarLayer(LAYER_POLIGONO_LUGAR_DE_INTERES_DESTINO);
		loadGMLLayer(LAYER_POLIGONO_LUGAR_DE_INTERES_DESTINO,gmlURL_lugar_de_interes_destino,LAYER_LUGAR_DE_INTERES_DESTINO,false);
	}
}

function irAlDestino(){
	if (esPunto(tipo_destino)){
		irAlPunto(x_destino,y_destino,null,null,null,null,null,300);
	}
	if (tipo_destino==TIPO_CALLE){
		marcar_via_destino(via_destino,lowerLeft_x_destino,lowerLeft_y_destino,upperRight_x_destino,upperRight_y_destino,false);
		agregarMarcador(x_esquina,y_esquina,LAYER_DESTINO,desc_esquina);
	}
	if (tipo_destino==TIPO_LINEA_DE_OMNIBUS){
		marcar_variante_destino(variante_destino,lowerLeft_x_destino,lowerLeft_y_destino,upperRight_x_destino,upperRight_y_destino,false);
		agregarMarcador(x_parada,y_parada,LAYER_PARADAS_NOTABLES,desc_parada,null,0);
	}
}

function marcar_via_sitio(codigo_via,xLl,yLl,xUr,yUr){
	apagarPopups();
	limpiarLayersMarcados();
	limpiarRecorridos();
	offset = 500;
	newBbox = createBBox(xLl-offset,yLl-offset,xUr+offset,yUr+offset);
	addLayerWithFilter(calle_marcada,'cod_nombre_via='+codigo_via,newBbox);
	
	// Dejo la capa de calle marcada abajo de la de calle destino
	limpiarLayer(LAYER_CALLE_DESTINO);
	if (tipo_destino==TIPO_CALLE){
		marcar_via_destino(via_destino,lowerLeft_x_destino,lowerLeft_y_destino,upperRight_x_destino,upperRight_y_destino,false);
		newBbox = createBBox(xLl-offset,yLl-offset,xUr+offset,yUr+offset);
		map.zoomToExtent(newBbox);
	}
} 

function marcar_variante_sitio(codigo_variante,xLl,yLl,xUr,yUr){
	apagarPopups();
	limpiarLayersMarcados();
	limpiarRecorridos();
	offset = 500;
	newBbox = createBBox(xLl-offset,yLl-offset,xUr+offset,yUr+offset);
	addLayerWithFilter(recorrido_marcado,'cod_variante='+codigo_variante,newBbox);
	addLayerWithFilter(paradas_recorrido_marcado,'cod_variante='+codigo_variante,newBbox);

	// Dejo la capa de recorrido marcado abajo de la de calle marcada
	limpiarLayer(LAYER_RECORRIDO_DESTINO);
	limpiarLayer(LAYER_PARADAS_RECORRIDO_DESTINO);
	if (tipo_destino==TIPO_LINEA_DE_OMNIBUS){
		marcar_variante_destino(variante_destino,lowerLeft_x_destino,lowerLeft_y_destino,upperRight_x_destino,upperRight_y_destino,false);
		newBbox = createBBox(xLl-offset,yLl-offset,xUr+offset,yUr+offset);
		map.zoomToExtent(newBbox);
	}
} 

function marcar_via_destino(codigo_via,xLl,yLl,xUr,yUr,borrar){
	apagarPopups();
	if (borrar){
		limpiarRecorridos();
	}
	limpiarLayer(LAYER_DESTINO);
	limpiarLayer(LAYER_LUGAR_DE_INTERES_DESTINO);	
	limpiarLayer(LAYER_POLIGONO_LUGAR_DE_INTERES_DESTINO);
	limpiarLayer(LAYER_RECORRIDO_DESTINO);
	limpiarLayer(LAYER_PARADAS_RECORRIDO_DESTINO);
	limpiarLayer(LAYER_CALLE_DESTINO);
	offset = 500;
	newBbox = createBBox(xLl-offset,yLl-offset,xUr+offset,yUr+offset);
	addLayerWithFilter(calle_destino,'cod_nombre_via='+codigo_via,newBbox);
	via_destino = codigo_via;
	tipo_destino = TIPO_CALLE;
	lowerLeft_x_destino = xLl;
	lowerLeft_y_destino = yLl;
	upperRight_x_destino = xUr;
	upperRight_y_destino = yUr;
} 

function marcar_variante_destino(codigo_variante,xLl,yLl,xUr,yUr,borrar){
	apagarPopups();
	if (borrar){
		limpiarRecorridos();
	}
	limpiarLayer(LAYER_DESTINO);
	limpiarLayer(LAYER_LUGAR_DE_INTERES_DESTINO);	
	limpiarLayer(LAYER_POLIGONO_LUGAR_DE_INTERES_DESTINO);
	limpiarLayer(LAYER_RECORRIDO_DESTINO);
	limpiarLayer(LAYER_PARADAS_RECORRIDO_DESTINO);
	limpiarLayer(LAYER_CALLE_DESTINO);
	offset = 500;
	newBbox = createBBox(xLl-offset,yLl-offset,xUr+offset,yUr+offset);
	addLayerWithFilter(recorrido_destino,'cod_variante='+codigo_variante,newBbox);
	addLayerWithFilter(paradas_recorrido_destino,'cod_variante='+codigo_variante,newBbox);
	variante_destino = codigo_variante;
	tipo_destino = TIPO_LINEA_DE_OMNIBUS;
	lowerLeft_x_destino = xLl;
	lowerLeft_y_destino = yLl;
	upperRight_x_destino = xUr;
	upperRight_y_destino = yUr;
} 

function irAlPunto(x1,y1,layer,tipo,gmlFileName,descripcion,direccion,offset){

	
	if (x1==undefined || y1==undefined)
		return;
	
	xLowerLeft= x1 - offset;	
	yLowerLeft= y1 - offset;
	xUpperRight= x1 + offset;
	yUpperRight= y1 + offset; 

	// make new bounding box
	var ul = new Array();// x, y
	var br = new Array();// x, y
	ul[0] = xLowerLeft;
	ul[1] = yLowerLeft;
	br[0] = xUpperRight;
	br[1] = yUpperRight;
	map.zoomToExtent(new OpenLayers.Bounds(ul[0],ul[1],br[0],br[1]));
	
	if (layer!=null){
		//agrego el marcador en el punto
		limpiarLayer(layer);
		if (esLayerOrigen(layer) || esLayerDestino(layer)){
			limpiarRecorridos();
			if (esLayerOrigen(layer)){
				limpiarLayer(LAYER_ORIGEN);
				limpiarLayer(LAYER_LUGAR_DE_INTERES_ORIGEN);
				limpiarLayer(LAYER_POLIGONO_LUGAR_DE_INTERES_ORIGEN);
				if (tipo_destino==TIPO_CALLE || tipo_destino==TIPO_LINEA_DE_OMNIBUS){
					limpiarLayer(LAYER_DESTINO);
					limpiarLayer(LAYER_PARADAS_NOTABLES);
				}
			}
			if (esLayerDestino(layer)){
				limpiarLayer(LAYER_CALLE_DESTINO);
				limpiarLayer(LAYER_RECORRIDO_DESTINO);
				limpiarLayer(LAYER_PARADAS_RECORRIDO_DESTINO);
				limpiarLayer(LAYER_DESTINO);
				limpiarLayer(LAYER_LUGAR_DE_INTERES_DESTINO);	
				limpiarLayer(LAYER_POLIGONO_LUGAR_DE_INTERES_DESTINO);
			}
		}
		else{
			limpiarLayer(LAYER_SITIO_MARCADO);
			limpiarLayer(LAYER_CALLE_MARCADA);
			limpiarLayer(LAYER_RECORRIDO_MARCADO);
			limpiarLayer(LAYER_PARADAS_RECORRIDO_MARCADO);
			limpiarLayer(LAYER_LUGAR_DE_INTERES_MARCADO);
			limpiarLayer(LAYER_POLIGONO_LUGAR_DE_INTERES_MARCADO);
			x_sitio_marcado = null;
			y_sitio_marcado = null;
		}
		agregarMarcador(x1,y1,layer,descripcion,direccion);
	}
	if (esLayerOrigen(layer)){
		if (esPunto(tipo)){
			tipo_origen = tipo;
		}
	}
	if (esLayerDestino(layer)){
		if (esPunto(tipo)){
			tipo_destino = tipo;
		}
	}
	
	if (layer==LAYER_SITIO_MARCADO && tipo==TIPO_LUGAR_DE_INTERES && gmlFileName!=null){
		console.log("Cargando resultado = " + gmlFileName);
		gmlURL = urlApp+"/"+gmlFileName;
		clearResultados(false);
		limpiarLayer(LAYER_POLIGONO_LUGAR_DE_INTERES_MARCADO);
		loadGMLLayer(LAYER_POLIGONO_LUGAR_DE_INTERES_MARCADO,gmlURL,LAYER_LUGAR_DE_INTERES_MARCADO,true);
		limpiarLayer(layer);
		agregarMarcador(x1,y1,layer,descripcion,direccion);
	}
	
	if (layer==LAYER_LUGAR_DE_INTERES_ORIGEN && gmlFileName!=null){
		console.log("Cargando resultado = " + gmlFileName);
		gmlURL = urlApp+"/"+gmlFileName;
		url_gml_origen = gmlURL; 
		clearResultados(false);
		limpiarLayer(LAYER_POLIGONO_LUGAR_DE_INTERES_ORIGEN);
		loadGMLLayer(LAYER_POLIGONO_LUGAR_DE_INTERES_ORIGEN,gmlURL,LAYER_LUGAR_DE_INTERES_ORIGEN,true);
		gmlURL_lugar_de_interes_origen = gmlURL;
		limpiarLayer(layer);
		agregarMarcador(x1,y1,layer,descripcion,direccion);
	}
	
	if (layer==LAYER_LUGAR_DE_INTERES_DESTINO && gmlFileName!=null){
		console.log("Cargando resultado = " + gmlFileName);
		gmlURL = urlApp+"/"+gmlFileName;
		url_gml_destino = gmlURL; 
		clearResultados(false);
		limpiarLayer(LAYER_POLIGONO_LUGAR_DE_INTERES_DESTINO);
		loadGMLLayer(LAYER_POLIGONO_LUGAR_DE_INTERES_DESTINO,gmlURL,LAYER_LUGAR_DE_INTERES_DESTINO,true);
		gmlURL_lugar_de_interes_destino = gmlURL;
		limpiarLayer(layer);
		agregarMarcador(x1,y1,layer,descripcion,direccion);
	}
	
	// Dejo la capa de sitios marcados abajo de las otras 
	if (esLayerLugarMarcado(layer)){
		//limpiarRecorridos();
		limpiarLayer(LAYER_ORIGEN);
		limpiarLayer(LAYER_DESTINO);
		limpiarLayer(LAYER_LUGAR_DE_INTERES_ORIGEN);
		limpiarLayer(LAYER_POLIGONO_LUGAR_DE_INTERES_ORIGEN);
		limpiarLayer(LAYER_LUGAR_DE_INTERES_DESTINO);
		limpiarLayer(LAYER_POLIGONO_LUGAR_DE_INTERES_DESTINO);
		if (tipo_origen==TIPO_LUGAR_DE_INTERES){
			loadGMLLayer(LAYER_POLIGONO_LUGAR_DE_INTERES_ORIGEN,url_gml_origen,LAYER_LUGAR_DE_INTERES_ORIGEN,false);
		}
		if (tipo_destino==TIPO_LUGAR_DE_INTERES){
			loadGMLLayer(LAYER_POLIGONO_LUGAR_DE_INTERES_DESTINO,url_gml_destino,LAYER_LUGAR_DE_INTERES_DESTINO,false);
		}
		if (esPunto(tipo_origen)){
			agregarMarcador(x_origen,y_origen,LAYER_ORIGEN,desc_origen,dir_origen);
		}
		if (esPunto(tipo_destino)){
			agregarMarcador(x_destino,y_destino,LAYER_DESTINO,desc_destino,dir_destino);
		}
	}
	upMarkers();
}

function agregarMarcador(x,y,layer,descripcion,direccion,index_parada){

	if (x==null || y==null)
		return;
	
	var size_marker = new OpenLayers.Size(31,25);
	var size_parada = new OpenLayers.Size(30,30);
	var offset = new OpenLayers.Pixel(-(size_marker.w/2)+16, -size_marker.h);
	var icon;
	apagarPopups();
	if (esLayerOrigen(layer)){
		icon_origen = new OpenLayers.Icon('resources/markers/marcador_origen.png',size_marker,offset);
		markers_origen = new OpenLayers.Layer.Markers(layer);
		desc_origen = descripcion;
		dir_origen = direccion;
		x_origen = x;
		y_origen = y;
		map.addLayer(markers_origen);
		markers_origen.setZIndex(map.Z_INDEX_BASE['Popup'] - 1);
		marker_origen = new OpenLayers.Marker(new OpenLayers.LonLat(x,y),icon_origen);
		markers_origen.addMarker(marker_origen);
		marker_origen.events.register("click",marker_origen,origenClick);
		return;
	}
	if (esLayerDestino(layer)){
		icon_destino = new OpenLayers.Icon('resources/markers/marcador_destino.png',size_marker,offset);
		markers_destino = new OpenLayers.Layer.Markers(layer);
		desc_destino = descripcion;
		dir_destino = direccion;
		x_destino = x;
		y_destino = y;
		map.addLayer(markers_destino);
		markers_destino.setZIndex(map.Z_INDEX_BASE['Popup'] - 1);
		marker_destino = new OpenLayers.Marker(new OpenLayers.LonLat(x,y),icon_destino);
		markers_destino.addMarker(marker_destino);
		marker_destino.events.register("click",marker_destino,destinoClick);
		return;
	}
	if (esLayerLugarMarcado(layer)){
		icon_lugar_marcado = new OpenLayers.Icon('resources/markers/marcador_ir_al_sitio.png',size_marker,offset);
		markers_lugar_marcado = new OpenLayers.Layer.Markers(layer);
		desc_sitio_marcado = descripcion;
		dir_sitio_marcado = direccion;
		x_sitio_marcado = x;
		y_sitio_marcado = y;
		map.addLayer(markers_lugar_marcado);
		markers_lugar_marcado.setZIndex(map.Z_INDEX_BASE['Popup'] - 1);
		marker_lugar_marcado = new OpenLayers.Marker(new OpenLayers.LonLat(x,y),icon_lugar_marcado);
		markers_lugar_marcado.addMarker(marker_lugar_marcado);
		marker_lugar_marcado.events.register("click",marker_lugar_marcado,lugarMarcadoClick);
		return;
	}
	if (layer==LAYER_PARADAS_NOTABLES){
		icon_parada = new OpenLayers.Icon('resources/markers/parada.png',size_parada,0);
		if (index_parada==0 || index_parada==1){
			markers_parada = new OpenLayers.Layer.Markers(layer);
			map.addLayer(markers_parada);
			markers_parada.setZIndex(map.Z_INDEX_BASE['Popup'] - 1);
		}
		desc_parada = descripcion;
		x_parada = x;
		y_parada = y;
		if (index_parada==0){
			marker_parada = new OpenLayers.Marker(new OpenLayers.LonLat(x,y),icon_parada);
			markers_parada.addMarker(marker_parada);
			marker_parada.events.register("click",marker_parada,paradaNotableClick0);
		}
		if (index_parada==1){
			marker_parada1 = new OpenLayers.Marker(new OpenLayers.LonLat(x,y),icon_parada);
			markers_parada.addMarker(marker_parada1);
			marker_parada1.events.register("click",marker_parada1,paradaNotableClick1);
		}
		if (index_parada==2){
			marker_parada2 = new OpenLayers.Marker(new OpenLayers.LonLat(x,y),icon_parada);
			markers_parada.addMarker(marker_parada2);
			marker_parada2.events.register("click",marker_parada2,paradaNotableClick2);
		}
		if (index_parada==3){
			marker_parada3 = new OpenLayers.Marker(new OpenLayers.LonLat(x,y),icon_parada);
			markers_parada.addMarker(marker_parada3);
			marker_parada3.events.register("click",marker_parada3,paradaNotableClick3);
		}
		if (index_parada==4){
			marker_parada4 = new OpenLayers.Marker(new OpenLayers.LonLat(x,y),icon_parada);
			marker_parada4.events.register("click",marker_parada4,paradaNotableClick4);
			markers_parada.addMarker(marker_parada4);
		}
		return;
	}
}

function apagarPopups(){
	if (popup_origen!=null && popup_origen.visible())
		popup_origen.toggle();
	if (popup_destino!=null && popup_destino.visible())
		popup_destino.toggle();
	if (popup_lugar_marcado!=null && popup_lugar_marcado.visible())
		popup_lugar_marcado.toggle();
	if (popup_parada!=null && popup_parada.visible())
		popup_parada.toggle();
	if (popup_parada1!=null && popup_parada1.visible())
		popup_parada1.toggle();
	if (popup_parada2!=null && popup_parada2.visible())
		popup_parada2.toggle();
	if (popup_parada3!=null && popup_parada3.visible())
		popup_parada3.toggle();
	if (popup_parada4!=null && popup_parada4.visible())
		popup_parada4.toggle();
}

function createHTML(tipo,descripcion,direccion){
	var html_tipo = '<li><b>' + tipo + ':</b><br/><br/></li>';
	var html_descripcion = '<li>' + descripcion + '<br/><br/></li>';
	var html_direccion = '';
	if (direccion!=null)
		html_direccion = '<li>' + direccion + '<br/><br/></li>';
	return '<div><ul>' + html_tipo  + html_descripcion + html_direccion + '</ul></div>';
}

function origenClick(evt) {
	if (popup_origen!=null && popup_origen.visible())
		popup_origen.toggle();
	else{
		tipo = 'Origen'
		html = createHTML(tipo,desc_origen,dir_origen);
		popup_origen = new OpenLayers.Popup.FramedCloud('',new OpenLayers.LonLat(x_origen,y_origen),null,html,icon_origen,true,null);
		markers_origen.map.addPopup(popup_origen,true);
		popup_origen.setOpacity(1.0);
		popup_origen.autoSize = true;
		popup_origen.keepInMap = true;
	}
    OpenLayers.Event.stop(evt);	
}  

function destinoClick(evt) {
	if (popup_destino!=null && popup_destino.visible())
		popup_destino.toggle();
	else{
		tipo = 'Destino';
		html = createHTML(tipo,desc_destino,dir_destino);
		popup_destino = new OpenLayers.Popup.FramedCloud('',new OpenLayers.LonLat(x_destino,y_destino),null,html,icon_destino,true,null);
		markers_destino.map.addPopup(popup_destino,true);
		popup_destino.setOpacity(1.0);
		popup_destino.autoSize = true;
		popup_destino.keepInMap = true;
	}
    OpenLayers.Event.stop(evt);	
}   

function lugarMarcadoClick(evt) {
	if (popup_lugar_marcado!=null && popup_lugar_marcado.visible())
		popup_lugar_marcado.toggle();
	else{
		tipo = 'Lugar marcado';
		html = createHTML(tipo,desc_sitio_marcado,dir_sitio_marcado);
		popup_lugar_marcado = new OpenLayers.Popup.FramedCloud('',new OpenLayers.LonLat(x_sitio_marcado,y_sitio_marcado),null,html,icon_lugar_marcado,true,null);
		markers_lugar_marcado.map.addPopup(popup_lugar_marcado,true);
		popup_lugar_marcado.setOpacity(1.0);
		popup_lugar_marcado.autoSize = true;
		popup_lugar_marcado.keepInMap = true;
	}
    OpenLayers.Event.stop(evt);	
}   

function paradaNotableClick0(evt) {
	if (popup_parada!=null && popup_parada.visible())
		popup_parada.toggle();
	else{
		tipo = 'Destino';
		html = createHTML(tipo,desc_parada);
		popup_parada = new OpenLayers.Popup.FramedCloud('',new OpenLayers.LonLat(x_parada,y_parada),null,html,icon_parada,true,null);
		markers_parada.map.addPopup(popup_parada,true);
		popup_parada.setOpacity(1.0);
		popup_parada.autoSize = true;
		popup_parada.keepInMap = true;
	}
    OpenLayers.Event.stop(evt);	
}   

function paradaNotableClick1(evt) {
	if (popup_parada1!=null && popup_parada1.visible())
		popup_parada1.toggle();
	else{
		tipo = 'Parada de ascenso al ' + desc_linea1;
		html = createHTML(tipo,desc_paradas[0]);
		popup_parada1 = new OpenLayers.Popup.FramedCloud('',new OpenLayers.LonLat(x_paradas[0],y_paradas[0]),null,html,icon_parada,true,null);
		markers_parada.map.addPopup(popup_parada1,true);
		popup_parada1.setOpacity(1.0);
		popup_parada1.autoSize = true;
		popup_parada1.keepInMap = true;
	}
    OpenLayers.Event.stop(evt);	
}   

function paradaNotableClick2(evt) {
	// Si hay una parada sola de transbordo, el popup lo dejo solo en marker3
	if (x_paradas.size()==4 && x_paradas[1]==x_paradas[2] && y_paradas[1]==y_paradas[2])
		return;
	if (popup_parada2!=null && popup_parada2.visible())
		popup_parada2.toggle();
	else{
		tipo = 'Parada de descenso del ' + desc_linea1;
		html = createHTML(tipo,desc_paradas[1]);
		popup_parada2 = new OpenLayers.Popup.FramedCloud('',new OpenLayers.LonLat(x_paradas[1],y_paradas[1]),null,html,icon_parada,true,null);
		markers_parada.map.addPopup(popup_parada2,true);
		popup_parada2.setOpacity(1.0);
		popup_parada2.autoSize = true;
		popup_parada2.keepInMap = true;
	}
    OpenLayers.Event.stop(evt);	
}   

function paradaNotableClick3(evt) {
	if (popup_parada3!=null && popup_parada3.visible())
		popup_parada3.toggle();
	else{
		if (x_paradas.size()==4 && x_paradas[1]==x_paradas[2] && y_paradas[1]==y_paradas[2])
			tipo = 'Parada de transbordo del ' + desc_linea1 + ' al ' + desc_linea2;
		else
			tipo = 'Parada de ascenso al ' + desc_linea2;
		html = createHTML(tipo,desc_paradas[2]);
		popup_parada3 = new OpenLayers.Popup.FramedCloud('',new OpenLayers.LonLat(x_paradas[2],y_paradas[2]),null,html,icon_parada,true,null);
		markers_parada.map.addPopup(popup_parada3,true);
		popup_parada3.setOpacity(1.0);
		popup_parada3.autoSize = true;
		popup_parada3.keepInMap = true;
	}
    OpenLayers.Event.stop(evt);	
}   

function paradaNotableClick4(evt) {
	if (popup_parada4!=null && popup_parada4.visible())
		popup_parada4.toggle();
	else{
		tipo = 'Parada de descenso del ' + desc_linea2;
		html = createHTML(tipo,desc_paradas[3]);
		popup_parada4 = new OpenLayers.Popup.FramedCloud('',new OpenLayers.LonLat(x_paradas[3],y_paradas[3]),null,html,icon_parada,true,null);
		markers_parada.map.addPopup(popup_parada4,true);
		popup_parada4.setOpacity(1.0);
		popup_parada4.autoSize = true;
		popup_parada4.keepInMap = true;
	}
    OpenLayers.Event.stop(evt);	
}   

function loadCaminoAPie(filename,parada,esquina,descripcion){
	if (filename==null)
		return;
	gmlURL = urlApp+"/"+filename;
	loadGMLLayer(LAYER_RUTA_A_PIE,gmlURL,LAYER_RUTA_A_PIE,false);
	if (parada!=null){
		limpiarLayer(LAYER_RECORRIDO_DESTINO);
		marcar_variante_destino(variante_destino,lowerLeft_x_destino,lowerLeft_y_destino,upperRight_x_destino,upperRight_y_destino,false);
		agregarMarcador(parada[0],parada[1],LAYER_PARADAS_NOTABLES,descripcion,null,0);
		x_parada = parada[0];
		y_parada = parada[1];
		desc_parada = descripcion;
	}
	if (esquina!=null){
		limpiarLayer(LAYER_DESTINO);
		agregarMarcador(esquina[0],esquina[1],LAYER_DESTINO,descripcion);
		x_esquina = esquina[0];
		y_esquina = esquina[1];
		desc_esquina = descripcion;
	}
	upMarkers();
}

function loadCaminoEnAuto(filename,parada,esquina,descripcion){
	if (filename==null)
		return;
	gmlURL = urlApp+"/"+filename;
	loadGMLLayer(LAYER_RUTA_EN_AUTO,gmlURL,LAYER_RECORRIDO_OMNIBUS,false);
	if (parada!=null){
		limpiarLayer(LAYER_RECORRIDO_DESTINO);
		marcar_variante_destino(variante_destino,lowerLeft_x_destino,lowerLeft_y_destino,upperRight_x_destino,upperRight_y_destino,false);
		agregarMarcador(parada[0],parada[1],LAYER_PARADAS_NOTABLES,descripcion,null,0);
		x_parada = parada[0];
		y_parada = parada[1];
		desc_parada = descripcion;
	}
	if (esquina!=null){
		limpiarLayer(LAYER_DESTINO);
		agregarMarcador(esquina[0],esquina[1],LAYER_DESTINO,descripcion);
		x_esquina = esquina[0];
		y_esquina = esquina[1];
		desc_esquina = descripcion;
	}
	upMarkers();
}

function clearListaRutas(id){
	document.getElementById(id).selectedIndex = -1;	
}
