// this variable will collect the html which will eventually be placed in the side_bar
var sidebar_html = "";
    
// arrays to hold copies of the markers and html used by the side_bar
// because the function closure trick doesnt work there
var gmarkers = [];
var htmls = [];
var i = 0;
var themarker;
var hash = null;
var sidebar_locations = new Object();

// hold the current bounds of the map
var southWest;
var northEast;

// function to add a marker
function addMarker(id, lon,lat,icon,name,html, type) {
	var marker = createMarkerByPoint(id, new GLatLng(lat,lon),icon,name,html, type);
	map.addOverlay(marker);
}

// A function to create the marker and set up the event window
function createMarkerByPoint(id, point, icon, name, html, type) {
	var marker = new GMarker(point, icon);
	
	if(html) {
		GEvent.addListener(marker, "click", function() {
	 		marker.openInfoWindowHtml(html);
		});
	}
	
	if(typeof(gmarkers[id]) == 'undefined') {
		// save the info we need to use later for the side_bar
	    gmarkers[id] = marker;
	    htmls[id] = html;
    
	    if(name) {
	    	// add the sidebar object
	    	if(typeof(sidebar_locations[type]) == 'undefined') {
	    		sidebar_locations[type] = new Object();
	    	}
	    	
	    	sidebar_locations[type][id] = new Object();
	    	sidebar_locations[type][id]["icon"] = icon;
	    	sidebar_locations[type][id]["name"] = name;
	    }
	}
	
    return marker;
}

// the function generates the html for the sidebar by looping over all locations
function rebuildList() {
	temp_sidebar_html = "";
	for (var type in sidebar_locations) {
		temp_sidebar_html += "<h2>"+type+"s</h2>";
		for(id in sidebar_locations[type]) {
			temp_sidebar_html += '<a href="javascript:myclick(' + id + ')"><img height=\"12px\" src="'+sidebar_locations[type][id]["icon"].image+'" /> ' + sidebar_locations[type][id]["name"] + '</a><br />';
		}
	}
	
	sidebar_html = temp_sidebar_html;
}

function clearList() {
	gmarkers = [];
	sidebar_locations = new Object();
	hash = null;
}

function setStartMarker(lon,lat,icon){
	if(icon == undefined) {
		var icon = icon_default;
	}
	
	return newMarker(false, new GLatLng(lat,lon), icon);
}

function newMarker(marker, point, icon) {
	if(icon == undefined) {
		var icon = icon_default;
	}
	
	if (marker && marker == themarker) {
	    map.removeOverlay(marker);
	} else if(marker) {
	} else {
		if(themarker) {
			map.removeOverlay(themarker);
		}
		if(document.getElementById('cordLon')) {
			document.getElementById('cordLon').value = point.x;	
		}
		
		if(document.getElementById('cordLat')) {
			document.getElementById('cordLat').value = point.y;	
		}
		
		marker = new GMarker(point, icon);
		themarker = marker;
	    map.addOverlay(themarker);
	}
}

function addPolyline(points, color, zoom) {
	// set parameters to default if null
	if(color == null) {
		color = "#FF0000";
	}
	if(zoom == null) {
		zoom = 10;
	}
	
	// create polyline
	var polyline = new GPolyline(points, color, zoom);
	//
	map.addOverlay(polyline);
}

// This function picks up the click and opens the corresponding info window
function myclick(id) {
	gmarkers[id].openInfoWindowHtml(htmls[id]);
}

function updateBounds() {
	var bounds = map.getBounds();
	southWest = bounds.getSouthWest();
	northEast = bounds.getNorthEast();
}

function centerMapOnAddress(address, zoom) {
	toggleLoading();
	
	if(typeof(zoom) == 'undefined') {
		zoom = 13;
	}
	
  var geocoder = new GClientGeocoder();
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        $('geocode_message').innerHTML = 'the address' + address + ' could not be found';
        Effect.BlindDown('geocode_message');
        window.setTimeout("Effect.BlindUp('geocode_message')",3000);
      } else {
      	clearList();
      	$('geocode_message').innerHTML = 'address found. centering Map on Address';
      	Effect.BlindDown('geocode_message');
      	window.setTimeout("Effect.BlindUp('geocode_message')",3000);
        map.setCenter(point, zoom);
      }
      toggleLoading();
    }
  );
}

function toggleLoading() {
	if(Element.visible('loading')) {
		Effect.Fade('loading');
	} else {
		$('loading').style.display = 'block';
	}
}
