var map;
var ATLayer;
var ATVLayer;

sm = function(u, o){
	var extent		= new OpenLayers.Bounds(-80.5, 1.6, -72.6, 9.8);
 	         
    map = new OpenLayers.Map('map',
		{
   			maxExtent: extent,
   			restrictedExtent: extent,
   			maxResolution: 'auto',
			units:"dd",
			projection: new OpenLayers.Projection("EPSG:4326"),
			numZoomLevels: 5,
			controls: [new OpenLayers.Control.Navigation({'zoomWheelEnabled': false}), 
                       new OpenLayers.Control.ZoomPanel()]
		}
	);
	ATLayer = new OpenLayers.Layer.WMS( "Anthrotect WMS", u,
		{	
			layers: 'country',
			format: 'image/png',
			request: 'getmap',		
			transparent: "false",
			transitionEffect: 'resize'	
		}, {displayInLayerSwitcher: false,  isBaseLayer: true}
	); 

	var style  =new OpenLayers.Style({
        pointRadius: "${radius}", // sized according to type attribute
        fillColor: "${color}",
        fillOpacity: "0.35",
        strokeColor: "${color}",
        strokeWidth: 1,
        label: "${total}",
        fontColor: "#FFFFFF",
        fontSize: "10px",
        fontWeight: "bold"
    });
	
    var ATStyles = new OpenLayers.StyleMap({
        "default": style,
        "select": style
    });
    	

	ATVLayer = new OpenLayers.Layer.Vector( "Anthrotect Search", {styleMap: ATStyles, format: OpenLayers.Format.GeoJSON}); 	
	
    map.addLayer(ATLayer);
    $j.each(o, function(i, l){ map.addLayer(l);});
    map.addLayer(ATVLayer);

	map.addControl(new OpenLayers.Control.LayerSwitcher({baseLayers: ATLayer }) );
	map.addControl(new OpenLayers.Control.LoadingPanel());
	map.setCenter(new OpenLayers.LonLat(3.6, -76), 5);
}

sc = function(options){
	$j('#filter').addClass('hide');
	$j('#content').removeClass('hide');
	
	$j.get(options.url, function(data) { $j('#content').html(data);});
}

ss = function(theme){
	$j('#theme').val(theme);
	$j('#search').val('');
	$j('#filter').removeClass('hide');
	$j('#content').addClass('hide');
	$j('#search').focus();
	srch('');
}

srch = function(){
	var theme = $j('#theme').val();
	var param = $j('#search').val();

	$j('#results').html('');
	$j('#results').addClass('loading');
	if(ATVLayer)
		ATVLayer.destroyFeatures();
	map.getControlsByClass('OpenLayers.Control.LoadingPanel')[0].toggle();
	$j('.olControlLoadingPanel:first').css({width: '450px', height: '470px', display: 'block'});
	$j.get('/search/'+theme+'/'+param, function(data) {
		$j('#results').html(data);
		$j('#results').removeClass('loading');
		sgf('/map/vector/'+theme+'/'+param);
	});

}

sgf = function(u){
	$j.getJSON(u, function(json){
		var geojson = new OpenLayers.Format.GeoJSON(
			{ 'internalProjection': map.baseLayer.projection, 'externalProjection':  map.baseLayer.projection});
		var features =  geojson.read(json);
		$j('.olControlLoadingPanel:first').css({width: '0px', height: '0px', display: 'none'});
		ATVLayer.addFeatures(features);
	});
}

