var map = null;
var url;
function load() 
{
	if (GBrowserIsCompatible()) 
	{
		map = new GMap2(document.getElementById("map"));		
		GEvent.addListener(map,"moveend", function() 
    	{  
    		loadGeoDataFromDB()
    	});
    	map.setCenter(new GLatLng(45, 10), 2);
		map.addControl(new GLargeMapControl());
    	map.addControl(new GMapTypeControl());
	}
}

function loadGeoDataFromDB()
{
 		var bounds = map.getBounds();          
		var sw = bounds.getSouthWest();
     	var ne = bounds.getNorthEast();
  		var longitude_west = sw.lng();
    	var latitude_south = sw.lat();
    	var longitude_east = ne.lng();
     	var latitude_north = ne.lat();
          
    	//http://localhost:8080/DoBeGo1/app?lat_north=9.0&lat_south=11.0&lng_east=44.0&lng_west=46.0&service=geometry
       	url = 	basePath +
       			"/app?service=reports" +
         		"&lat_north=" + latitude_north.toString() +
          		"&lat_south=" + latitude_south.toString() +
          		"&lng_east=" + longitude_east.toString() +
          		"&lng_west=" + longitude_west.toString() + 
          		"&thisLangOnly=true";		    	  
   		var req = new GXmlHttp.create();
		req.open("GET",url , true);
    	req.onreadystatechange = function(){
        	if (req.readyState == 4)
        	{
 	           	var xmlDoc = req.responseXML;
        		parseXML(xmlDoc);
        	}
    	}
    	req.send(null);
}

function parseXML(xmlDoc)
{
	var roottag = xmlDoc.documentElement;
	var xmlReportArray = roottag.getElementsByTagName("Report");
	for (i=0;i<xmlReportArray.length;i++)
	{
		parseReport(xmlReportArray[i]);
	}
}

function parseReport(xmlReport)
{
	var reportTitle = xmlReport.getElementsByTagName("Title")[0].firstChild.nodeValue;
	var reportLink = xmlReport.getElementsByTagName("ReportLink")[0].firstChild.nodeValue;
	var locationElement = xmlReport.getElementsByTagName("Location")[0];
	var latitude = locationElement.getElementsByTagName("Latitude")[0].firstChild.nodeValue;
	var longitude = locationElement.getElementsByTagName("Longitude")[0].firstChild.nodeValue;
	var reportMarker = new ReportMarker(new GLatLng(parseFloat(latitude), parseFloat(longitude)));
	var availableIn = xmlReport.getElementsByTagName("AvailableIn")[0].firstChild.nodeValue;
	reportMarker.setTitle(reportTitle);
	reportMarker.setAvailableIn(availableIn);
	GEvent.addListener(reportMarker,"click", function(overlay)
	{
		reportMarker.openInfoWindowHtml("<h3>" + reportTitle + "</h3><p>" + availableIn + "<p><a href=" + reportLink + ">more...</a>");
    });  
	map.addOverlay(reportMarker);
}


/* Constructor */
function ReportMarker(latlng){
    this.latlng = latlng;    
    
    GMarker.apply(this, arguments);
}

/* It's a limitation of JavaScript inheritance that we can't conveniently
   extend GMarker without having to run its constructor. In order for the
   constructor to run, it requires some dummy GLatLng. */
ReportMarker.prototype = new GMarker(new GLatLng(0, 0));

ReportMarker.prototype._reportTitle;

ReportMarker.prototype.getTitle = function()
{
	return this._reportTitle;
}

ReportMarker.prototype.setTitle = function(title)
{
	this._reportTitle = title;
}

ReportMarker.prototype._reportId;

ReportMarker.prototype.getReportId = function()
{
	return this._reportId;
}

ReportMarker.prototype.setReportId = function(id)
{
	this._reportId = id;
}


ReportMarker.prototype._reportAvailableIn;

ReportMarker.prototype.getAvailableIn = function()
{
	return this._reportAvailableIn;
}

ReportMarker.prototype.setAvailableIn = function(available)
{
	this._reportAvailableIn = available;
}
