// let's control the map

var gmap;
var gmarkers = [];
var htmls = [];

var icon = new GIcon();
icon.image = "http://recycletorrance.org/images/marker.png";
icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
icon.iconSize = new GSize(20, 34);
icon.shadowSize = new GSize(20, 36);
icon.iconAnchor = new GPoint(6, 20);
icon.infoWindowAnchor = new GPoint(5, 1);

function createMarker(latitude, longitude, tab1, tab2, i, address) {
	tab1 = tab1 + '<br /><form method="get" name="directions" action="http://maps.google.com/maps" target="_blank"><label>Get directions to here from:</label><br /><input name="daddr" type="hidden" value="' + address + '" /><input class="directions" name="saddr" type="text" /><input class="submit" type="submit" value="Go" /></form>';
	var infoTabs = [
		new GInfoWindowTab("Location", tab1),
		new GInfoWindowTab("Items", tab2)
	];		


	var marker = new GMarker(new GLatLng(latitude, longitude), icon)
	openInfo = function () {		
		marker.openInfoWindowTabsHtml(infoTabs);	
		for(z=0; z < 6; z++){
			id = "location_" + z;
			if (z != i){
				if (document.getElementById(id)){
					document.getElementById(id).className = "";
				}	
			}
		}
		id = "location_" + i;
		if (document.getElementById(id)){
			document.getElementById(id).className = "active";
		}
	}
	closeInfo = function () {						
		id = "location_" + i;
		if (document.getElementById(id)){
			document.getElementById(id).className = "";
		}
	}
	GEvent.addListener(marker, "click", openInfo);
	GEvent.addListener(marker, "infowindowclose", closeInfo);
	
	htmls[i] = infoTabs;
	
	return marker;	

}


function setLocation(latitude,longitude,i) {
	gmap.panTo(new GLatLng(latitude, longitude));		
	gmarkers[i].openInfoWindowTabsHtml(htmls[i]);
	for(z=0; z < 6; z++){
		id = "location_" + z;	
		if (z != i){
			if (document.getElementById(id)){
				document.getElementById(id).className = "";
			}	
		}	
	}
	id = "location_" + i;	
	if (document.getElementById(id)){
		document.getElementById(id).className = "active";
	}
}

function load(jsonObject) {

	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		
		//map.setCenter(new GLatLng(33.827479, -118.351826), 13);
	
		gmap = map;
	
		// loop through object
		for (i=0; i < jsonObject.locations.length; i++){		
		
			if(i==0){
				map.setCenter(new GLatLng(jsonObject.locations[i].latitude, jsonObject.locations[i].longitude), 11);
			}
		
			var marker = createMarker(jsonObject.locations[i].latitude,jsonObject.locations[i].longitude,jsonObject.locations[i].locationTab,jsonObject.locations[i].recycleTab,i,jsonObject.locations[i].addressString);
			map.addOverlay(marker);
			//marker.openInfoWindowTabsHtml(infoTabs);
			gmarkers[i] = marker;		
		}
		
			
	} // end if (GBrowserIsCompatible())
	
}
