var currentLayerId;
var map;
var po = org.polymaps;
function init() {


var map_div = document.getElementById("map");

map = po.map()
    .container(map_div.appendChild(po.svg("svg")))
    .zoom(6)
    .center({"lat":54, "lon":-3})
    .add(po.interact())

map.add(po.image()
    .url(po.url("http://acetate.geoiq.com/tiles/terrain/{Z}/{X}/{Y}.png")))


map.add(po.compass()
    .pan("none"));
currentLayer = 1;

addLayer("green man");

jQuery("#pub_button").click(function () {
	addLayer(jQuery("#pub_input").val());
});


} // init
function setFeatures(e){
	for (var i = 0; i < e.features.length; i++) {
		var feature = e.features[i];
		feature.element.setAttribute("feat_name", feature.data.name); //give the element an id
		feature.element.setAttribute("class", "pub_point"); //set css class for colours
		feature.element.setAttribute("r", "5"); //radius of svg circle.
		feature.element.addEventListener("mousedown", function(e){  
				clickFeature(this, e);	
		}, false); 
	}
}
function clickFeature(f, evt){
      var blurb = "<div class='info_blurb'>" + f.getAttribute("feat_name") + "</div>";

      var infowin = document.getElementById('infowin')
      infowin.style.width = "200px";
      infowin.style.maxHeight = "200px";
      infowin.style.overflow = "auto";
      infowin.style.left = evt.clientX + "px";
      infowin.style.top = evt.clientY + "px";
      infowin.style.position = 'absolute';
      infowin.style.display = 'block';
      
      infowin.innerHTML = blurb; 
}

function addLayer(filterText){
if (currentLayerId){

element = document.getElementById(currentLayerId);
if (element) {
	var parent    = element.parentNode;
        parent.removeChild(element);
}
}
var guid = Math.floor(Math.random()*3000);
currentLayerId = guid;



var url =   "http://geocommons.com/overlays/136549/features.json?limit=10000"+
	    "&filter[name][][like]="+escape(filterText);
url = "/cgi-bin/proxy.cgi?url=" + escape(url);
j = jQuery.getJSON(url,
		function(data){
 		    map.add(po.geoJson().id(guid).features(data).on("load", setFeatures))
		});


}

function removeLayer(){

}

	

