var map = null;
var ajax = new Ajax();
var mapContainer = null;
var statusDiv = null
var tripMarkers = null;
var messageWidth = 120;
var messageHeight = 20;
var activityId = "";
var maxResults = 100;

function addLoadingDiv(message) {
     
	removeLoadingDiv();
	statusDiv = document.createElement('div');
 	statusDiv.id = 'statusDiv';
	statusDiv.className = "map-status-container";

 	var mapWidth = mapContainer.style.width.replace('px','');
   	var mapHeight = mapContainer.style.height.replace('px','');

	mapWidth = 841;
	mapHeight = 489;
  	var leftPos = (mapWidth-messageWidth)/2;
   	var topPos = (mapHeight-messageHeight)/2;

  	statusDiv.style.left = leftPos+'px';
 	statusDiv.style.top = topPos+'px';
   	statusDiv.style.width = messageWidth + "px";
   	statusDiv.style.height = messageHeight + "px";
 	statusDiv.innerHTML = message;

  	mapContainer.appendChild(statusDiv);
 }
   
function removeLoadingDiv() {
 	if(statusDiv != null) mapContainer.removeChild(statusDiv);
  	statusDiv = null;
}

function changeMapType(mapName) {
      var mapType = getMapFromName(mapName);
      var mapId = getMapTypeId(mapName);
      var map_selector = document.getElementById('map_selector');
      map_selector.selectedIndex = mapId;
      map.setMapType(mapType);
}

function updateActivities() {
	activityId = "";
 	for(var i=0; i<numActivities; i++) {
 		if(document.getElementById("activityBox"+i) && document.getElementById("activityBox"+i).checked) {
    		activityId += document.getElementById("activityBox"+i).value + ",";
   		}
  	}
 	getAllTrips();
}
   
function changeChecks() {
	for(var i=0; i<numActivities; i++) {	
 		if(document.getElementById("activityBox"+i)) {
     		document.getElementById("activityBox"+i).checked = document.getElementById('checkAll').checked;
 		}
  	}
}


function getAllTrips() {
	var start = new Date();
   	start = start.getTime();
  	var bounds = map.getBounds();
	var ne = bounds.getNorthEast();
	var sw = bounds.getSouthWest();
      
	var minLat = sw.lat();
 	var minLon = sw.lng();
  	var maxLat = ne.lat();
 	var maxLon = ne.lng();

	var query = "&minLat="+minLat+"&minLon="+minLon+"&maxLat="+maxLat+"&maxLon="+maxLon+"&activityId="+activityId;
               
	// todo - ajax call
    ajax.abort();
	addLoadingDiv("Loading...");
	ajax.doGet('/get_trips_bbox.php?start='+start+query, showAllTrips,"xml");

}

function showAllTrips(xml) {
	removeLoadingDiv();  
 	map.clearOverlays();
	document.getElementById('results-container').innerHTML = "";

	var res = [];
	res = XMLParse.xml2ObjArray(xml,'trip');
	var trips = [];

	for(var i = 0; i < res.length; i++) {
 		var lat = res[i].centerLat;
 		var lon = res[i].centerLon;
 		var title = res[i].articleName;
 		var tripId = res[i].articleId;
 		var picture = res[i].img;

		// todo
		var marker = makeTripMarker(map, new GLatLng(lat,lon), title, tripId, picture);
		map.addOverlay(marker);
 	}

	var numTrips = Math.min(maxResults, res.length);
	document.getElementById('results-container').innerHTML = "Showing " + numTrips + " most popular trips in this area.  Zoom in to see more.";
}

function loadMap() {

	if (GBrowserIsCompatible()) {
		mapContainer = document.getElementById("map-container");
 		map = new GMap2(mapContainer);
   		map.addControl(new GLargeMapControl());
 		map.setCenter(new GLatLng(0,0), 2);
       
 		map.addControl(new GV_MapTypeControl());
 		changeMapType("Terrain");
  		map.enableDoubleClickZoom();
		map.enableContinuousZoom();
      

		GEvent.addListener(self.map,"moveend",function() {
  			getAllTrips();
 		});

 		getAllTrips();
	}
}

function centerMap(latLng) {
	if(latLng) {
		map.setCenter(latLng,13);
	}   
}

function searchKeyPress(e) {
	if(keyCode(e) == 13){
 		geocodeAddress(document.getElementById("address").value, centerMap);
  	}
}

function searchAddress(e) {
 	geocodeAddress(document.getElementById("address").value, centerMap);
}


jQuery(document).ready(function() {
	loadMap();
});