var mapclick;
var FFM = {
	dom_loaded: function() {
		GEvent.addListener(map, "click", function(overlay, latlng){
			mapclick = latlng;
		});
		
		GEvent.addListener(map, "zoomend", function(){
			if(this.getZoom() > 2){
				$$("div.elabel").invoke("hide");
			} else {
				$$("div.elabel").invoke("show");
			}
		});
		
		//GEvent.addListener(map, "dragend", function(){
		//	FFM.refresh_markers(this);
		//});
		
		//GEvent.addListener(map, "zoomend", function(){
		//	FFM.refresh_markers(this);
		//});
		
		// Initialize sidebar navigation
		$$("a.sidebar_control").invoke("observe","click",function(){
			sidebar_tabs(this);
		});
		
		$$("a.markerlink").invoke("observe","click",function(){
			mid = this.id.substring(3);
			pop_marker(mid);
		});
		
		// Setup region marker observers
		FFM.setup_observers();
	},
	
	show_markers: function(item_id, scope){
		switch(scope){
			case "area":
				url = "/areas/find_by_country?id=" + item_id;
			break;
			case "country":
				url = "/countries/find_by_region?id=" + item_id;
			break;
			default:
				url = "/markers/find_by_area?id=" + item_id;
		}
		//new Ajax.Request(url, {
		//	method: 'get'
		//});
	},
	setup_observers: function(){
		$$("div.elabel").invoke("observe","click",function(){
			map.zoomIn();map.setCenter(mapclick);
			ffm_cluster.hide();
			//FFM.refresh_markers(map);
		});
	},
	
	refresh_markers: function(map){
		swlat = map.getBounds().getSouthWest().lat();
		swlng = map.getBounds().getSouthWest().lng();
		nelat = map.getBounds().getNorthEast().lat();
		nelng = map.getBounds().getNorthEast().lng();
		zoom = map.getZoom();
		
		url = "/markers/refresh?zoom="+zoom;
		url+= "&bounds[sw][lat]="+swlat;
		url+= "&bounds[sw][lng]="+swlng;
		url+= "&bounds[ne][lat]="+nelat;
		url+= "&bounds[ne][lng]="+nelng;
		
		new Ajax.Request(url, {
			method: 'get'
		});
	}
};
Event.observe(window, "load", FFM.dom_loaded);
