var map;
var mapunitymap;
var hybridmap;
var cntr = new GLatLng(0,0);
var minzoom=0;
var maxzoom=20;
var startzoom = 4;
var basemap;
var satellite;
var hybrid;
var cpyrt;
var ctrl;
var infoText = "";
var tiledir = "/maps/tiles/indiaadmin1";
var indiacntrpnt = new GLatLng(23.62,81.78);

//<![CDATA[


// A infoTextControl is a GControl that displays textual "Zoom In"
// and "Zoom Out" buttons (as opposed to the iconic buttons used in
// Google Maps).

function infoTextControl() {
}

infoTextControl.prototype = new GControl();

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
infoTextControl.prototype.initialize = function(map) {
	infoDiv = document.createElement("div");
	infoDiv.id = "text";
	this.setButtonStyle_(infoDiv);
	infoDiv.appendChild(document.createTextNode(""));
	GEvent.addDomListener(infoDiv, "click", function() {
	  map.zoomIn();
	});
	map.getContainer().appendChild(infoDiv);
	return infoDiv;
}

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
infoTextControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(8,35));
}


// Sets the proper CSS for the given button element.
infoTextControl.prototype.setButtonStyle_ = function(button) {
  button.style.backgroundColor = "#FFFFB1";
  button.style.font = "10pt Arial";
  button.style.font.size = "1";
  //button.style.font.class = "contents";
  button.style.marginBottom = "5px";
  button.style.textAlign = "center";
  button.style.width = "17.3em";
}

/*
 * Create a point with the latitude and longitude values
 * and set the zoom level. 
 */
function showMap(lat,lon,zoom,name){

  cntr = new GLatLng(lon,lat);
  startzoom = zoom;

  if (name!=infoText)
  {
	    infoText = "Data : " + name;
  }
  
	
	loadMap();
}

/*
 * Load the map and set the required components 
 * like scalebar, zoom slider, etc.
 */
function loadMap(){

  // check if the browser can handle google maps api
	if (GBrowserIsCompatible()) {
		// set the size for the map div based on the window size.

		map = new GMap2(document.getElementById("map"));
		// Add scale bar
		map.addControl(new GScaleControl());
		// Add zoom slider 
	 	map.addControl(new GLargeMapControl());
    // specify the tile url
    CustomGetTileUrl=function(a,b)
    {
       return "http://www.mapunity.org/" + tiledir + "/tile_"+(a.x)+"_"+(a.y)+"_"+b+".png";
    }

    // specify another tile layer
    var tilelayers1 = [new GTileLayer(new GCopyrightCollection("Mapunity"),minzoom,maxzoom)];

    // custom map overlay
    tilelayers1[0].getTileUrl = CustomGetTileUrl;
    tilelayers1[0].isPng = function(a,b){return true;};
    tilelayers1[0].getCopyright = function(a,b) {
      return {prefix:"", copyrightTexts:[cpyrt]};
    }

    var custommap = new GMapType(tilelayers1, new GMercatorProjection(maxzoom+1), "Map", {maxResolution:maxzoom,minResolution:minzoom,errorMessage:_mMapError});

    // === Add it to the list of map types ===
    map.addMapType(custommap);
    map.removeMapType(G_HYBRID_MAP);
    map.removeMapType(G_NORMAL_MAP);

    // setup initial map display to have basemap, satellite and if enabled hybrid
    map.setCenter(cntr, startzoom, custommap);
    // Add map type control to switch between maps.
    map.addControl(new GMapTypeControl());
		map.addControl(new GOverviewMapControl());
		
		GEvent.bind(map, "dragend", this, onViewChange);
   		GEvent.bind(map, "zoomend", this, onViewChange);

    // Click listener to return the lat and lon values
    GEvent.addListener(map, 'click',
       function (overlay, cntr)
       {
          if (cntr)
          {
	           // alert("lat:" + cntr.lat());
	           // alert("lon:" + cntr.lng());
          }
       }
     );
		 onViewChange();
	}
	else {
		document.getElementByID("map").innerHTML='<b>Sorry. Your browser in not capable of displaying the map. Try Firefox Browser. <a href="http://www.getfirefox.com">http://getfirefox.com</a>';
	}
}


function onViewChange(){
	
	bounds = map.getBounds();
	zLevel = map.getZoom();
	sw = bounds.getSouthWest();
	ne = bounds.getNorthEast();
	upper = ne.lat();
	right = ne.lng();
	
	lower = sw.lat();
	left = sw.lng();
	
  // Request url can be a call to any server side scrip, which returns a
  // JSON object as response
	var reqURL = "../../servlet/CensusServlet?req=927&urlid=corp";
	//alert(reqURL);
    do_GET(reqURL,loadMarkers);


}

/*
 * Load the markers with lat, lon and location name as returned by
 * the server side script. 
 */

function loadMarkers(){

  map.clearOverlays();

  if(isResponseReady()) {
    var resp = getResponseAsText();  
    var json_data = eval(resp);
    //alert(json_data); 
  	for(var i = 0; i < json_data.length; i++)
    {
	        var lon  = json_data[i].longitude;
	        var lat  = json_data[i].latitude;
	        var name = json_data[i].name;
			map.addOverlay(createMarker(lon,lat,name));
    }

  }
}


// create a new icon
var icon = new GIcon();
icon.image = "../images/marker_aqua.png";
icon.iconAnchor = new GPoint(6, 20);
icon.infoWindowAnchor = new GPoint(5, 1);


/*
 * Create a marker and associate a click listener for the
 * marker.
 */
function createMarker(lon, lat, name)
{
  var pnt  = new GLatLng(lon, lat);
	// create the marker. 
	var marker = new GMarker(pnt,{icon:icon,title:name});
	
	GEvent.addListener(marker,"click", function(){
    marker.openInfoWindowHtml(name);
	});

	return marker;
}

function rm(){
	map.removeControl(ctrl);
}

function showMe(lat,lon){
	map.setCenter(new GLatLng(lon,lat), 9);
}

			    





