var map = null;
var overlays = new Array();
var selectedMarker = null;

var URL_ICON_DOT_BLUE = "static/imagesNew/dot_blue.gif";
var URL_ICON_DOT_GREEN = "static/imagesNew/dot_green.gif";
var URL_ICON_DOT_ORANGE = "static/imagesNew/dot_orange.gif";
var URL_ICON_DOT_YELLOW = "static/imagesNew/dot_yellow.gif";

var iconBlue = null;
var iconGreen = null;
var iconOrange = null;
var iconYellow = null;

var iconPostDefault = null;
var iconPostDefaultSelected = null;

var showMap = true;
var showList = true;

function initIcons(){
	// create blue icon
	iconBlue = new GIcon();
	iconBlue.image = URL_ICON_DOT_BLUE;
	iconBlue.iconSize = new GSize(8, 8);
	iconBlue.shadowSize = new GSize(8, 8);
	iconBlue.iconAnchor = new GPoint(4, 4);
	iconBlue.infoWindowAnchor = new GPoint(4, 4);
	
	// create green icon
	iconGreen = new GIcon();
	iconGreen.image = URL_ICON_DOT_GREEN;
	iconGreen.iconSize = new GSize(8, 8);
	iconGreen.shadowSize = new GSize(8, 8);
	iconGreen.iconAnchor = new GPoint(4, 4);
	iconGreen.infoWindowAnchor = new GPoint(4, 4);
	
	// create orange icon
	iconOrange = new GIcon();
	iconOrange.image = URL_ICON_DOT_ORANGE;
	iconOrange.iconSize = new GSize(8, 8);
	iconOrange.shadowSize = new GSize(8, 8);
	iconOrange.iconAnchor = new GPoint(4, 4);
	iconOrange.infoWindowAnchor = new GPoint(4, 4);
	
	// create yellow icon
	iconYellow = new GIcon();
	iconYellow.image = URL_ICON_DOT_YELLOW;
	iconYellow.iconSize = new GSize(8, 8);
	iconYellow.shadowSize = new GSize(8, 8);
	iconYellow.iconAnchor = new GPoint(4, 4);
	iconYellow.infoWindowAnchor = new GPoint(4, 4);
}

function loadGoogleMap(){
	
	// setup the icons
	initIcons();
	
	if(GBrowserIsCompatible()){
		// setup the map
    	map = new GMap2(document.getElementById("map"));
    	map.setCenter(new GLatLng(30, 0), 2);
	    map.addControl(new GSmallMapControl());
	    
	    // add map listeners
	    //GEvent.addListener(map,"zoomend",function() {updateMap();});
	    //GEvent.addListener(map,"moveend",function() {updateMap();});
	    
		// add a double click event listener
		//GEvent.addListener( map, "dblclick", function(p){
  		//	map.addOverlay( new GMarker( p ) );
		//});
		
		GEvent.addListener(map, "click", function(marker, point) {
  			//alert("You clicked the map. "+point);
  			marker= new GMarker(point );
  			
  			GEvent.addListener(marker, "click", function() {
  				//alert("You  clicked the point. ");
				//clickCallback(marker.meadanPosts);
				overlays[this.getPoint()]=null;
				map.removeOverlay(this);
  			});
  			
  			//overlays[overlays.length] = marker;
  			overlays[marker.getPoint()]=marker;
  			map.addOverlay( marker );
		});
		
		// a right click event listener
		//GEvent.addListener( map, "rightclick", function(p){
  		//	alert( "Right!" );
		//});
		
		// mouse wheel listener
		//GEvent.addListener( map, "middleclick", function(p){
  		//	alert( "Middle!" );
		//});
	}
	else{
		// need to go into non map mode
		// TODO Switch to list here?
	}
}



function listMarkers()
{
	//alert( overlays[index].getPoint().x);
    var wkt = "POLYGON ((";
	var i=0;
	var firstmarker=null;
	for( var index in overlays ) {
    		
    	// check for null markers that have been removed by the user	   	
    	if(overlays[index]!=null)
    	{
    		if(i!=0)
    			wkt +=",";
    		else
    			firstmarker=overlays[index];
    			
    		wkt += " " +overlays[index].getPoint().x + " " +overlays[index].getPoint().y;
    		i++;
    	}  
    		   	
	} 
	
	// add the first point to the end of the poly to complete it
	wkt += " ," +firstmarker.getPoint().x + " " +firstmarker.getPoint().y;
	
	wkt +="))";
   document.getElementById("polygon").value=wkt;
}

function clearmap()
{
	for( var index in overlays ) {
    	
    	map.removeOverlay(overlays[index]);	  
    	overlays[index]=null;
	} 

	overlays=new Array();
}

