
var map;
var mouseLoc;
var popup_info;
var popup;

OpenLayers.ProxyHost = "http://maps.loggier.net/cgi-bin/proxy.cgi?url=";
OpenLayers.IMAGE_RELOAD_ATTEMPTS = 5;
// make OL compute scale according to WMS spec
OpenLayers.DOTS_PER_INCH = 25.4 / 0.28;

OpenLayers.Util.onImageLoadErrorColor = 'transparent';

function init() {
	var mapOptions = {
					resolutions : [3.4332275390625E-4, 1.71661376953125E-4, 8.58306884765625E-5, 4.291534423828125E-5, 2.1457672119140625E-5, 1.0728836059570312E-5, 5.364418029785156E-6, 2.682209014892578E-6],
					projection : new OpenLayers.Projection('EPSG:4326'),
					maxExtent : new OpenLayers.Bounds(-180.0, -90.0, 180.0, 90.0),
					units : "degrees",
					controls : []
				};
	map = new OpenLayers.Map('map', mapOptions);
	map.addControl(new OpenLayers.Control.PanZoomBar());

	map.addControl(new OpenLayers.Control.Navigation());
	// map.addControl(new OpenLayers.Control.Scale());
	map.addControl(new OpenLayers.Control.MousePosition());
	
	
	// map.zoomToMaxExtent();
	var lsdGuayana = new OpenLayers.Layer.WMS("loggisGuayana",
			"http://200.44.188.238:8080/geoserver/gwc/service/wms", {
				layers : 'loggis',
				format : 'image/png',
			
			}, {
				tileSize : new OpenLayers.Size(256, 256)
			}, 
			{buffer: 0}
			);
	/*
	 * Puntos
	 */
	
	// Crear un objeto estilo
    var point_style = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
    point_style.fillOpacity = 0.8;
    point_style.graphicOpacity = 0.8;
    point_style.externalGraphic="img/point-orange.png";
    point_style.graphicWidth = 25;
    point_style.graphicHeight = 25;
    
	 var point = new OpenLayers.Geometry.Point(lat,lon);
	    var pointFeature = new OpenLayers.Feature.Vector(point,null,point_style);
     
  	// Crear una capa vectorial
     var vectorLayer = new OpenLayers.Layer.Vector("Simple Geometry", {style: point_style});
     
     // A�adir las features a la capa vectorial
     vectorLayer.addFeatures([pointFeature]); 


    
	/*
	 * Fin Puntos
	 */
	var lsdPoint = new OpenLayers.Layer.Vector("lsdPoints", {
        projection: map.displayProjection,
        strategies: [new OpenLayers.Strategy.Fixed()],
        protocol: new OpenLayers.Protocol.HTTP({
            url: "xml/vector.twitter.php",
            format: new OpenLayers.Format.KML({
                extractStyles: true,
                extractAttributes: true
            })
        })
    });
	
	map.addLayers([lsdGuayana,lsdPoint]);

	
	map.zoomToExtent(new OpenLayers.Bounds(-62.878, 8.202, -62.596, 8.442));

	map.setCenter(new OpenLayers.LonLat(lat,lon), zoom);
	
	 geocrome();
	 if (ico==1){
		map.addLayer(vectorLayer);
		
	}
	/*
	 * Mostrar Ubicacion
	 */

	
	/*
	 * 
	 * map.events.register('click', map, function(e) { var params = { REQUEST :
	 * "GetFeatureInfo", EXCEPTIONS : "application/vnd.ogc.se_xml", BBOX :
	 * map.getExtent().toBBOX(), X : e.xy.x, Y : e.xy.y, INFO_FORMAT :
	 * 'text/plain', QUERY_LAYERS : map.layers[0].params.LAYERS, FEATURE_COUNT :
	 * 50, Srs : 'EPSG:4326', Styles : '', Layers : 'loggisGuayana', WIDTH :
	 * map.size.w, HEIGHT : map.size.h, format : 'image/png' };
	 * 
	 * OpenLayers.loadURL("http://lsdca.no-ip.org:8080/geoserver/wms", params,
	 * this, setHTML, setHTML); mouseLoc = map.getLonLatFromPixel(e.xy);
	 * 
	 * OpenLayers.Event.stop(e); });
	 */
	
	// Interaction; not needed for initial display.
    selectControl = new OpenLayers.Control.SelectFeature(lsdPoint);
    map.addControl(selectControl);
    selectControl.activate();
    lsdPoint.events.on({
        'featureselected': onFeatureSelect,
        'featureunselected': onFeatureUnselect
    });
    
    
/*
 map.events.register('click', map, function (e) {
                    document.getElementById('nodelist').innerHTML = "Cargando... por favor espere...";
                    var params = {
                        REQUEST: "GetFeatureInfo",
                        EXCEPTIONS: "application/vnd.ogc.se_xml",
                        BBOX: map.getExtent().toBBOX(),
                        SERVICE: "WMS",
                        VERSION: "1.1.1",
                        X: e.xy.x,
                        Y: e.xy.y,
                        INFO_FORMAT: 'text/plain',
                        QUERY_LAYERS: map.layers[0].params.LAYERS,
                        FEATURE_COUNT: 50,
                        Layers: 'puntos',
                        WIDTH: map.size.w,
                        HEIGHT: map.size.h,
                        srs: map.layers[0].params.SRS};
                    // merge filters
                    if(map.layers[0].params.CQL_FILTER != null) {
                        params.cql_filter = map.layers[0].params.CQL_FILTER;
                    } 
                    if(map.layers[0].params.FILTER != null) {
                        params.filter = map.layers[0].params.FILTER;
                    }
                    if(map.layers[0].params.FEATUREID) {
                        params.featureid = map.layers[0].params.FEATUREID;
                    }
                    OpenLayers.loadURL("http://200.44.188.238:8080/geoserver/wms", params, this, setHTML, setHTML);
                    OpenLayers.Event.stop(e);
                });*/
             //  alert(listTwitter)
               cargarTwitte();
            }
function setHTML(response){
	
      	if (response.responseText.indexOf('no results') == -1) {
		var cat = "Unknown", src = "Unknown", leg = "Unknown", linkinfo = "";
		var lines = response.responseText.split('\n');

		for (lcv = 0; lcv < (lines.length); lcv++) {
			var vals = lines[lcv].replace(/^\s*/, '').replace(/\s*$/, '')
					.replace(/ = /, "=").replace(/'/g, '').split('=');

			if (vals[1] == "") {
				vals[1] = "Unknown";
			}
			if (vals[0].indexOf('NAME') != -1) {
				nombre = vals[1];
				if (nombre=="null"){
					nombre="";
				}
			} else if (vals[0].indexOf('alias') != -1) {
				alia = vals[1];
			} else if (vals[0].indexOf('tipo') != -1) {
				tipo = vals[1];
			} else if (vals[0].indexOf('the_geom') != -1) {
				linkinfo = vals[1];
			}
		}
        var popup_info = nombre;

    }
    
     document.getElementById('nodelist').innerHTML =popup_info;

};
function onPopupClose(evt) {
    // 'this' is the popup.
    selectControl.unselect(this.feature);
	
}
function onFeatureSelect(evt) {
    feature = evt.feature;
    popup = new OpenLayers.Popup.FramedCloud("featurePopup",
                             feature.geometry.getBounds().getCenterLonLat(),
                             new OpenLayers.Size(100,100),
                             "<h2>"+feature.attributes.title+"</h2>" +
                             feature.attributes.description,
                             null, true, onPopupClose);
    feature.popup = popup;
    popup.feature = feature;
    map.addPopup(popup);
}
function onFeatureUnselect(evt) {
    feature = evt.feature;
    if (feature.popup) {
        popup.feature = null;
        map.removePopup(feature.popup);
        feature.popup.destroy();
        feature.popup = null;
    }
}

function popupDestroy(e) {
	map.removePopup(popup)
	popup = null;
	OpenLayers.Util.safeStopPropagation(e);
}
