var Locations = {
	map:null,
	cache:null,
	geocoder:null,
	markers:null,
	search:null,
	timer:null,
	handlers:{},
	init:function(map_id){		
      	//init map
		if (map_id && GBrowserIsCompatible()) {	
			Locations.map = new GMap2(document.getElementById(map_id));
        	Locations.map.setCenter(new GLatLng(44.40, -73.2), 9);
      		var keys = new GKeyboardHandler(Locations.map);
		
		}else if(map_id){
			document.getElementById(map_id).innerHTML = "<p>Please upgrade your browser to make use of this map.</p>";
		}
		
		Locations.cache = new GGeocodeCache();
		Locations.geocoder = new GClientGeocoder(Locations.cache); 
	
		//set up custom controls?
		
	},
	//display a point from the locations 
	display:function(location, marker_img){	
			var point = new GLatLng(location.lat, location.lng);
			var marker = new GMarker(point);
				marker.show();
			   	
			 Locations.map.addOverlay(marker);	
			 var addy = location.address == "" ? "" : location.address + ", ";
			 marker.openInfoWindowHtml("<p>" + addy + location.city + ", " + location.state + ", " + location.zip + "</p>");
					
			Locations.map.setCenter(point);	
			Locations.map.setZoom(14);
	},
	get_all:function(){
			
	},
	only_category:function(){
		
	},
	lookup:function(address){
		 if(address == "") return false;
		 if(Locations.handlers.onLoading) Locations.handlers.onLoading();
		 Locations.geocoder.getLocations( address ,
			function(location) {
				if(location)
					if(Locations.handlers.onData) Locations.handlers.onData(location);		
				else
					if(Locations.handlers.onError) Locations.handlers.onError();		
					
			});
	},
	from_json:function(json){
		var result;
		var str = 'result = ' + json + ';';
		try{
			eval(str);
		}catch(e){
			alert("Request Runtime error: " + e+ "\nReturned Response:\n\n" + response);
		}
		return result;
	},
	json:function(location){
		
		var ad = location.Placemark[0].AddressDetails;
		
		
		var string_info    = location.Placemark[0].address.split(",");
		
		address = string_info[0];
				
		var lat 		= location.Placemark[0].Point.coordinates[1];
		var lng 		= location.Placemark[0].Point.coordinates[0];
		
		
		//parse from comma delmited address because structure info is apparently iffy as of 10/27/2008..  
		var info = location.Placemark[0].address.split(", ");
		
		var country = info[info.length-1];
		var state = info.length > 1 ? info[info.length-2] : "";
		if(state.indexOf(" ") > 0){
			var spl = state.split(" ");
			state = spl[0];
			zip = spl[1];
		} else {
			zip = "";	
		}
		var city = info.length > 2 ? info[info.length-3] : "";
		var address = info.length > 3 ? info[info.length-4] : "";
		
		/*return false;
		var state 		=  ad.Country.AdministrativeArea.AdministrativeAreaName ? ad.Country.AdministrativeArea.AdministrativeAreaName : "";		
		var country		=  ad.Country.CountryNameCode ? ad.Country.CountryNameCode : "";
		var address = "";
		var city = "";
		var zip = "";
		
		alert(ad.Country.AdministrativeArea.SubAdministrativeArea);
		
		if(ad.Country.AdministrativeArea.SubAdministrativeArea){
			city =	ad.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName;
			alert("found=" + city);
		}
		
		if(ad.Country.AdministrativeArea.SubAdministrativeArea.Locality){		
			city 		=  ad.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
			alert("locality=" + city);
			zip 		=  ad.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode ? ad.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber : "";
			if(ad.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare){				
				var address 	=  ad.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
			} else {
				var address = "";	
			}					
		} 
	
		*/
		
		var object = '{';
			object += '"lat":"'+lat+'",';
			object += '"lng":"'+lng+'",';
			object += '"address":"'+address+'",';
			object += '"state":"'+state+'",';
			object += '"city":"'+city+'",';
			object += '"zip":"'+zip+'",';
			object += '"country":"'+country+'"';		
			object += '}';
			
		return object;
		
	}
}


/*
function Calculator(){
	//check for compatible browsers
	if(!GBrowserIsCompatible()){ 
		document.getElementById("map").innerHTML = "<p style='margin:15px'>Sorry, your browser will not work with Google Maps!<br /><br />  For a better browsing experience: <a href='http://www.mozilla.com/en-US/firefox/'>Download Firefox</a>'";		
	}

	var This = this;
	
	this.map = new GMap2(document.getElementById("map"));
	var control = new GSmallMapControl();
	var pos = new GControlPosition(G_ANCHOR_TOP_RIGHT, 0);

	this.map.addControl(control, pos);
	//this.map.addControl(new GMapTypeControl());
	this.map.setCenter(new GLatLng(36, -95), 3);
	
	this.geocoder = new GClientGeocoder();
	

	this.enter_point = function(method, object){
		if(object.value == "") return false;
		var input = object;
		var to = object.getAttribute("name").indexOf("_to") > 0; 
		if(to){
			document.getElementById("not_found_to_" + method).style.display = 'none';
			document.getElementById("not_found_to_" + method).innerHTML = "";
		} else {		
			document.getElementById("not_found_from_" + method).style.display = 'none';
			document.getElementById("not_found_from_" + method).innerHTML = "";		
		}
		if(to &&  This.cur_to_mark != null){   This.map.removeOverlay(This.cur_to_mark);  } 
		if(!to && This.cur_from_mark != null) { This.map.removeOverlay(This.cur_from_mark); }
		if(This.cur_line != null){ This.map.removeOverlay(This.cur_line); }
			
		//grab point from geocoder...
		if(object.value != "") This.geocoder.getLatLng( object.value,
			function(point) {							 
				  if(!point){
					 if(to){ 
						document.getElementById("not_found_to_" + method).style.display = 'block';
						document.getElementById("not_found_to_" + method).innerHTML = "Location could not found! Please try again";
					} else{
					   document.getElementById("not_found_from_" + method).style.display = 'block';
					   document.getElementById("not_found_from_" + method).innerHTML = "Location could not found! Please try again";					 
					}
					 This.distance = 0;	
					 input.focus();
					 return false;
				   } else {						  
					  var marker = new GMarker(point);
					  if(to){
						This.cur_to_mark = marker;
						This.cur_to_point = point;						
					  } else {
						This.cur_from_mark = marker;
					  	This.cur_from_point = point;
					  }
					  if(This.cur_from_point != null && This.cur_to_point != null){							
							//center map over route
							var bounds = new GLatLngBounds();
								bounds.extend(This.cur_from_point);	
							    bounds.extend(This.cur_to_point);								
								This.map.setZoom(This.map.getBoundsZoomLevel(bounds));
								This.map.setCenter(bounds.getCenter());
							 	//draw line between two points
						 		This.draw_line(method, This.cur_from_point, This.cur_to_point);
								//track distance
								This.distance =  Math.ceil( (This.cur_from_point.distanceFrom(This.cur_to_point) * 0.000621371192) * 1000 ) / 1000;  // / 621.371192) / 2;														
								This.activate_button(method); 
					 } else {
					 	This.map.setCenter(point, 5);
					  }
					  This.map.addOverlay(marker);					
				  }
			   }          
			);      
	}

	this.enter_distance = function(method, object){
			if(object && object.value != ""){ 
			//remove commas  
				object.value = This.unformat(object.value);
				This.distance = object.value;
				This.map.removeOverlay(This.cur_to_mark);
				This.map.removeOverlay(This.cur_from_mark);
				This.map.removeOverlay(This.cur_line);
				This.cur_to_point = null;
				This.cur_to_mark = null;
				This.cur_from_point = null;
				This.cur_point_mark = null;
				eval('This.form.'+method+'_to.value=""');	
				eval('This.form.'+method+'_from.value=""');	
				This.activate_button(method); 
			} else if(object.value == ""){
				This.deactivate_button(method); 
			}	 	
	}	
	
	this.timer = false;
	this.add_trip = function(method, object){
		if(This.timer != null) window.clearTimeout(This.timer);
		if(This.distance == 0){ 			
			if( eval('This.form.'+method+'_to.value!=""') && eval('This.form.'+method+'_from.value!=""')) {
					This.form.elements[method + "_to"].focus();
					This.timer = window.setTimeout(This.add_trip, 1000, method, object);
					return false;
			} else {
				alert('Please enter From and To locations, or enter a distance manually.');
		 		This.form.elements[method + "_to"].focus();
			}
			return false; 
		} 

		//save data
		if(method=='fly'){ 
			var mult = This.form.fly_mult[1].checked ? 2 : 1;
		 	This.distance *= mult;
		}
	
		if(method=='drive'){ 			
		 	This.distance *= 1.15;
		}
		
		var tid = "trip_" + method + "_" + (This.ids[method].length).toString();
		var rb = (This.form.railbus[1].checked) ? "rail" : "bus";
		if(method=='rail_bus') This.distance += "|" + rb;

		This.markers[method].push(This.cur_to_mark);
		This.markers[method].push(This.cur_from_mark); 
		This.points[method].push(This.cur_to_point);
		This.points[method].push(This.cur_from_point);
		This.lines[method].push(This.cur_line); 
		This.trips[method].push(This.distance);		
		This.ids[method].push(tid);
		
		var input = object;		
		This.total[method] += parseInt(This.distance);
		
		var total = This.total[method];			
		document.getElementById("total_from"). innerHTML = "CO<sub>2</sub> from "+ This.inflect[method] +" " + This.num_format(total) + " miles";				
		
		var carbon = Math.round(This.carbon_total(method)*1000)/1000;
		document.getElementById("total_carbon").innerHTML = "TOTAL: " + carbon + " tons";
		document.getElementById("total_from_" + method ).innerHTML = carbon + " tons CO<sub>2</sub> from " + This.inflect[method];
		
		   

		if(!This.bounds[method]) This.bounds[method] = new GLatLngBounds();
		if(This.cur_to_point != null)   This.bounds[method].extend(This.cur_to_point);
		if(This.cur_from_point != null) This.bounds[method].extend(This.cur_from_point);		

		var dist = method != "rail_bus" ? This.distance : This.distance.substr(0,This.distance.indexOf("|"));

		//add trip to the list so it can be removed...
		var summary	= document.getElementById("summary_" + method);
			summary.style.display = 'block';
		var trip = document.createElement("p");
	    var labl = document.createElement("strong");
			labl.innerHTML = eval('This.form.' + method + '_dist.value==""') ? "From " + eval('This.form.' + method + '_from.value') + " to " + eval('This.form.'+method+'_to.value') + "<br />" : "Unspecified Origin and Destination <br />"; 
			labl.innerHTML += This.num_format(dist) + " miles ";

		var remove = document.createElement("a");	
			remove.style.cursor = 'pointer';			
			remove.setAttribute("tid",tid);
			remove.onclick = function(){ This.remove(method, this.getAttribute("tid"), this); };
			//trip.innerHTML += " <a href=\"javascript:_calc.edit('"+method+"'," + This.lines[method].length + ");\">[Edit]</a>"; 
			
			remove.innerHTML += "[Remove]"; 
			trip.appendChild(labl);
			trip.appendChild(remove);

			summary.appendChild(trip);

		//reset currents
		This.cur_to_mark = null;
		This.cur_from_mark = null;
		This.cur_to_point = null;
		This.cur_from_point = null;
		This.cur_line = null;	
		This.distance = 0;
		
		This.set_view(method);
		
		//clear form
		eval('This.form.'+method+'_to.value=""');	
		eval('This.form.'+method+'_from.value=""');
		eval('This.form.'+method+'_dist.value=""');	

		This.deactivate_button(method); 	
	}

	this.remove = function(method, id, linkobj){
		var index = -1;
		for(i=0; i < This.ids[method].length; i++){
			if(This.ids[method][i] == id)
				index = i;
		}				
	
		This.total[method] -= This.trips[method][index];	
		
		document.getElementById("total_from"). innerHTML = "CO<sub>2</sub> from "+ This.inflect[method] +" " + This.num_format(This.total[method]) + " miles";				
		
		var carbon = Math.round(This.carbon_total(method)*1000)/1000;
		document.getElementById("total_carbon").innerHTML = "TOTAL: " + carbon + " tons";
		document.getElementById("total_from_" + method ).innerHTML = carbon + " tons CO<sub>2</sub> from " + This.inflect[method];
		
	
		//remove from map
		This.map.removeOverlay(This.lines[method][index]);
		This.map.removeOverlay(This.markers[method][index*2]);
		This.map.removeOverlay(This.markers[method][(index*2)+1]);
		
		//remove from storage
		This.markers[method].splice(index*2,2);
		This.points[method].splice(index*2,2);
		This.lines[method].splice(index,1);
		This.trips[method].splice(index,1);
		This.ids[method].splice(index,1);

		//remove link object		
		linkobj.parentNode.parentNode.removeChild(linkobj.parentNode);
	
	}

	this.selected_method = "drive";
	this.switch_view = function(method){
		This.map.clearOverlays();		
		document.images['tab_' + This.selected_method].src = 'Media/Trav/NE_Trav_' + This.selected_method + '.gif';
		document.images['tab_' + method].src = 'Media/Trav/NE_Trav_' + method + '_on.gif';
		document.getElementById(This.selected_method).style.display = 'none';
		document.getElementById(method).style.display = 'block';
		if(document.getElementById("summary_" + This.selected_method)) 
			document.getElementById("summary_" + This.selected_method).style.display = 'none';
		
		This.selected_method = method;	
	
		if(method != "total"){ 
				document.getElementById("map").style.display = 'block';
				document.getElementById("order_form").style.display = 'none';
				document.getElementById("summary_" + method).style.display = 'block';
				document.getElementById("done").style.display = 'block';
		} else {
				document.getElementById("map").style.display = 'none';
				document.getElementById("order_form").style.display = 'block';
				document.getElementById("done").style.display = 'none';
				This.draw_total();
				return;
		}
	
		document.getElementById("total_from"). innerHTML = "CO<sub>2</sub> from "+ This.inflect[method] +" " + This.num_format(This.total[method]) + " miles";				
		
		var carbon = Math.round(This.carbon_total(method)*1000)/1000;
		document.getElementById("total_carbon").innerHTML = "TOTAL: " + carbon + " tons";
		document.getElementById("total_from_" + method ).innerHTML = carbon + " tons CO<sub>2</sub> from " + This.inflect[method];
		This.draw_view(method);
		This.deactivate_button(method);
	}

	this.next_tab = function(){
	   if(This.selected_method == "drive"){
			This.switch_view('fly');
			return;
		}
		if(This.selected_method == "fly"){
			This.switch_view('rail_bus');
			return;
		}
		if(This.selected_method == "rail_bus"){
			This.switch_view('total');		
			return;
		}
	}

	this.activate_button = function(method){
		var btn = new Element(method + '_btn', {alpha:100} );
	}

	this.deactivate_button = function(method){
		if(!method) method = This.current_method;		
		var btn = new Element(method + '_btn', {alpha:10} );
	} 
	this.deactivate_button('drive');

	this.draw_view = function(method){
		for(var d = 0; d < This.markers[method].length; d++){			
			if(This.markers[method][d] != null) This.map.addOverlay(This.markers[method][d]);			
		}
		for(var d = 0; d < This.lines[method].length; d++){			
			if(This.lines[method][d] != null) This.map.addOverlay(This.lines[method][d]);			
		}
		This.set_view(method);
	}		

	this.set_view = function(method){
		if(This.bounds[method]) {
			// all markers for method to bounds and recenter map...
			This.map.setZoom(This.map.getBoundsZoomLevel(This.bounds[method]));
			This.map.setCenter(This.bounds[method].getCenter());
			document.getElementById("more_to_calc").style.display = 'inline';

		} else if(method=='total'){
			
			var bounds =  new GLatLngBounds();
			for(var d = 0; d < This.points['drive'].length; d++){	
				if(This.points['drive'][d] != null) bounds.extend(This.points['drive'][d]);
			}
			for(var d = 0; d < This.points['fly'].length; d++){	
				if(This.points['fly'][d] != null) bounds.extend(This.points['fly'][d]);
			}
			for(var d = 0; d < This.points['rail_bus'].length; d++){	
				if(This.points['rail_bus'][d] != null) bounds.extend(This.points['rail_bus'][d]);
			}
			This.map.setZoom(This.map.getBoundsZoomLevel(bounds));
			This.map.setCenter(bounds.getCenter());
			//hide buttons and show form selection.
			document.getElementById("more_to_calc").style.display = 'none';

 		} else {
			document.getElementById("more_to_calc").style.display = 'inline';
			This.map.setCenter(new GLatLng(36, -95), 3);
		}	
	}

	this.draw_total = function(){
		var total = This.total['drive'] + This.total['fly'] + This.total['rail_bus'];
		document.getElementById("total_from"). innerHTML = "CO<sub>2</sub> from " + This.num_format(total) + " miles of travel";				
		
		var carbon =  This.carbon_total('drive');
			carbon += This.carbon_total('fly');
			carbon += This.carbon_total('rail_bus');
		document.getElementById("total_carbon").innerHTML = "TOTAL: " + Math.round(carbon*1000)/1000 + " tons";
		This.draw_view('drive');
		This.draw_view('fly');
		This.draw_view('rail_bus');	
		This.set_view('total');	
		
	var carbon_rounded = Math.round(carbon*1000)/1000;
	//UPDATE YAHOO FORM FOR SUBMISSION - 
	updateTotalForms(carbon_rounded);

	}

	this.carbon_total = function(method){
		if(method=="drive"){
				if(This.form.drive_mpg_man.value == ""){
					for(m = 0; m < This.form.drive_mpg.length; m++){
						if(This.form.drive_mpg[m].checked){
							return (This.total[method] / This.form.drive_mpg[m].value) * (19.56/2000);
						}
					}
					return (This.total[method] / 20) * (19.56/2000); 
				} else {
					return (This.total[method] / This.form.drive_mpg_man.value) * (19.56/2000);
				}		
		} else if (method=="fly"){
				//loop through trips and find distances because of varrying
				var total = 0;
				for(var t = 0; t < This.trips['fly'].length; t++){   
						if(This.trips['fly'][t] < 280){  
							total += (This.trips['fly'][t] * 1.28) / 2000; 
						} else if(This.trips['fly'][t] < 994){
							total += (This.trips['fly'][t] * 0.88) / 2000;
						} else {
							total += (This.trips['fly'][t] * 0.80) / 2000;
						}					
				}
			return total;					
		} else {
			//calculate for rail or bus
				var total = 0;
				for(var t = 0; t < This.trips['rail_bus'].length; t++){
					var rlbs = This.trips['rail_bus'][t].toString().split("|");		
					if(rlbs[1]=='rail'){
						total += rlbs[0] * (0.37/2000);
					} else {
						total += rlbs[0] * (0.66/2000);
					}					
				}
				return total;				
		}
		return 0;
	}
	
	this.draw_line = function(method, from, to){
		
		var line = new GPolyline([
									from,
									to
								  ], This.line_colors[method] , 5); //last param is line weight
		
		if(line) This.map.addOverlay(line);	
		This.cur_line = line;
	}

	this.num_format = function(nStr){
			nStr += '';
			x = nStr.split('.');
			x1 = x[0];
			x2 = x.length > 1 ? '.' + x[1] : '';
			var rgx = /(\d+)(\d{3})/;
			while (rgx.test(x1)) {
				x1 = x1.replace(rgx, '$1' + ',' + '$2');
			}
			return x1 + x2;
	}

	this.unformat = function(str){		
		return str.replace(/,/, "");	
	}

}
*/