// Globals	

 
var serverUrl = 'http://www.residoo.com/Residoo/restful/residoos';
//var serverUrl = 'http://localhost/Residoo/restful/residoos';
var map;
var residoos = new Array();
var selectedResidoo;
var geocoder = new GClientGeocoder();

var zoomLevelForSelectedResidoo = 16;

function Residoo(id,latitude, longitude,title, author, numVisits)
{	
	var id;
	var latitude;
	var longitude;
	var title;
	var author;
	var numVisits;
	var marker;
	
	this.id = id;
	this.latitude = latitude;
	this.longitude = longitude;
	this.title = title;
	this.author = author;
	this.numVisits = numVisits;
	
	

}

function initialize() {
  if (GBrowserIsCompatible()) {
     map = new GMap2(document.getElementById("map_canvas"));
	
	resetMapToOriginalView();
    map.setUIToDefault();
 	
	loadResidoos();
  }


}

function resetMapToOriginalView()
{
	var initialLatitude 	= 45.556342;
	var initialLongitude 	= -73.550806;
	var zoomLevel = 1; // 0 is world, max is 20 I think. 0 gives a double world.
			
    map.setCenter(new GLatLng(initialLatitude, initialLongitude), zoomLevel);

	if(selectedResidoo!=null)
	{
		selectedResidoo.marker.closeInfoWindow();
	}
}


function populateSelectionBox()
{
	
	var options;
	// Go from latest to first
	for(var count=residoos.length-1;count >=0;count--)
   {
		var residoo = residoos[count];
		if(residoo!=null)
		{
			var display;
			
			if(residoo.numVisits==0)
			{
			 	display = residoo.title + " by " + residoo.author + " hasn't been visited yet.";	
			}
			else if (residoo.numVisits==1)
			{
				display = residoo.title + " by " + residoo.author + " was visited just once so far.";
			}
			else{
				display = residoo.title + " by " + residoo.author + " was visited (" + residoo.numVisits + ") times so far.";	
			}
			
			

			options += '<option value="' + residoo.id + '">' + display + '</option>'; 
		}
   }

  $('#residooSelectionBox').append(options);	

	 $('#residooSelectionBox').change(
		function() {
			var selectedResidooId =  $(this).val();
			onSelectedResidoo(selectedResidooId);
		}
	);
 
 
}

function onSelectedResidoo(selectedResidooId)
{
	if(selectedResidooId!=null && selectedResidooId.length>0)
	{
		selectedResidoo = getResidooWithId(selectedResidooId);
		if(selectedResidoo!=null)
		{
 			var coordinates =  new GLatLng(selectedResidoo.latitude, selectedResidoo.longitude);
			map.setCenter(coordinates, zoomLevelForSelectedResidoo);
			geocoder.getLocations(coordinates, showResidooLocationInfo);
			
	 
		}
		else{
			alert("Residoo couldn't be found for:" + selectedResidooId)
		}
	
		 
	}
	else{
		resetMapToOriginalView();
	}
 
}

function showResidooLocationInfo(geocoderResponse)
{
	var location = geocoderResponse.Placemark[0];

	var address = location.address;

	//map.addOverlay(marker);
    selectedResidoo.marker.openInfoWindowHtml(address);
 	
}

function getResidooWithId(targetResidooId)
{
	for(var count=0;count < residoos.length;count++)
	{
		var residoo = residoos[count];
		if(residoo.id == targetResidooId)
		{
			return residoo;
		}
	}
	
	return null;
}


function loadResidoos()
{	
	$.get(serverUrl,{},function(xml) {extractAndMarkResidoos(xml);});
}

function extractAndMarkResidoos(xmlDocument)
{
 
      $(xmlDocument).find("residoo").each(function() {

     	var residooNode = $(this);
 		residoos.push( new Residoo( residooNode.attr('id'),residooNode.attr('latitude'),residooNode.attr('longitude'),residooNode.attr('title'),residooNode.attr('member_username'),residooNode.attr('num_visits')));
      });
 
 
	markResidoos();
	updateMapTitle();
	populateSelectionBox();
}

function markResidoos(){
 
 
  for(var count=0;count < residoos.length;count++)
	{
 
		markResidoo(residoos[count]);
	}
 
}

function updateMapTitle()
{
	var title = "There are now " + residoos.length + " Residoos dropped in the world.";
	$('#map_title').html(title);
	
}

function markResidoo(residoo)
{	
	var title= residoo.title + "(" + residoo.author + ") (visits:" + residoo.numVisits + ")";
	var options = {title:title};
	var marker = new GMarker(new GLatLng(residoo.latitude,residoo.longitude),options);
	map.addOverlay(marker);
	residoo.marker = marker;

}

