// HD Map output javascript
var hd_map_object;
var hd_map_directions_service = new google.maps.DirectionsService();
var hd_map_directions_display;// = new google.maps.DirectionsRenderer();
var hd_map_settings = new Array;

function hd_map_display(d){
	// Define default settings
	var fieldname = (d.fieldname == null) ? 'fieldname' : d.fieldname;
	hd_map_settings[fieldname] = d;
	var lat = (d.lat == null) ? 32.343041 : d.lat;
	var long = (d.long == null) ? -86.171629 : d.long;
	var zoom = (d.zoom == null) ? 16 : d.zoom;
	var kml = (d.kml == null) ? false : d.kml;
	var address = (d.address == null) ? false : d.address;
	var bubble = (d.bubble == null) ? false : d.bubble;
	var myhue = (d.myhue == null) ? false : d.hue;
	var mysaturation = (d.saturation == null) ? false : d.saturation;
	var traffic = (d.traffic == null) ? true : d.traffic;
	var customMap = (d.customMap == null) ? 'Map' : d.customMap;
	var add_to_bubbles = (d.add_to_bubbles == null) ? 'directions <a href="javascript:hd_map_hide_div(\''+fieldname+'_directions_from\');hd_map_show_div(\''+fieldname+'_directions_to\');document.getElementById(\''+fieldname+'_source\').focus();">to here</a>&nbsp;&nbsp;<a href="javascript:hd_map_hide_div(\''+fieldname+'_directions_to\');hd_map_show_div(\''+fieldname+'_directions_from\');document.getElementById(\''+fieldname+'_destination\').focus();">from here</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<div id="'+fieldname+'_directions_to" style="display: none;">from: <input type="text" id="'+fieldname+'_source" name="'+fieldname+'_source" onkeypress="{if (event.keyCode==13) hd_map_find_route_from(\''+fieldname+'\');}"><input type="button" value="go" onClick="hd_map_find_route_from(\''+fieldname+'\');"></div><div id="'+fieldname+'_directions_from" style="display: none;">to: <input type="text" id="'+fieldname+'_destination" name="'+fieldname+'_destination" onkeypress="{if (event.keyCode==13) hd_map_find_route_to(\''+fieldname+'\');}"><input type="button" value="go" onClick="hd_map_find_route_to(\''+fieldname+'\');"></div><div style="height:10px;"></div>' : d.add_to_bubbles;
	var mypos = new google.maps.LatLng(lat,long);
	var mapOptions = (d.mapOptions == null) ? {
		mapTypeControl: true, 
		mapTypeControlOptions: {
			mapTypeIds: (myhue || d.mapStyles) ? [customMap, google.maps.MapTypeId.HYBRID, google.maps.MapTypeId.TERRAIN] : [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.HYBRID, google.maps.MapTypeId.TERRAIN],
			style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
		},
		navigationControl: true,
		streetViewControl: true,
		navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
		zoom: zoom,
		mapTypeId: (myhue || d.mapStyles) ? customMap : google.maps.MapTypeId.ROADMAP,
		center: mypos
	} : d.mapOptions;
	var mapStyles = (d.mapStyles == null) ? [{featureType:"all",elementType:"all",stylers:[{hue: myhue, saturation: mysaturation}]}] : d.mapStyles;
	
	// Initialize the map
	hd_map_object = new google.maps.Map(document.getElementById(fieldname), mapOptions);
	if (bubble != false){
		var infowindow = new google.maps.InfoWindow({
			content: bubble + '<br><span style="font-size: 80%;">'+add_to_bubbles+'</span>',
			/*size: new google.maps.Size(50,50),*/  
			position: mypos
		});
		infowindow.open(hd_map_object);
		//hd_map_settings[fieldname]['open_bubble'] = infowindow;
	}
	
	// Add directions handling
	hd_map_directions_display = new google.maps.DirectionsRenderer();
	hd_map_directions_display.setOptions({
		map: hd_map_object,
		panel: document.getElementById(fieldname+"_directions")		
	});

	// Define starting address
	hd_map_settings[fieldname]['last_clicked'] = hd_map_settings[fieldname]['address']; //+' ('+kmlEvent.featureData.name+')';
	
	// KML layer (multi_mode)
	if (kml != false) {
		var kmloverlay = new google.maps.KmlLayer(kml, {preserveViewport: true, map: hd_map_object});

		google.maps.event.addListener(kmloverlay, 'click', function(kmlEvent) {
			if (kmlEvent.featureData.description.indexOf(add_to_bubbles) < 0) kmlEvent.featureData.description += '&nbsp;|&nbsp;'+add_to_bubbles;
			var fc = kmlEvent.featureData.snippet.indexOf(',',0);
			var sc = kmlEvent.featureData.snippet.indexOf(',',fc+1)
			var address = kmlEvent.featureData.snippet.substr(0, sc); // For some reason, the direction finder does NOT like zip codes
			hd_map_settings[fieldname]['last_clicked'] = address; //+' ('+kmlEvent.featureData.name+')';
			infowindow.close();
//			hd_map_settings[fieldname]['open_bubble'] = kmlEvent;
		});
	}
	
	// Traffic layer (shows only on the non-colored map types)
	if (traffic != false) {
		var trafficLayer = new google.maps.TrafficLayer();
		trafficLayer.setMap(hd_map_object);
	}
	
	// Simple hue/saturation coloring
	if (myhue != false || mysaturation != false){
		var stylized =  new google.maps.StyledMapType(mapStyles,{name: customMap, title: 'Road Map'}); // title isn't working, need to find what changes the hover title (not documented)
		hd_map_object.mapTypes.set(customMap, stylized);
	}
}

function print_r(theObj){ // my little function to debug with
	if(theObj.constructor == Array || theObj.constructor == Object){
		var out = "<ul>"
		for(var p in theObj){
			if(theObj[p].constructor == Array || theObj[p].constructor == Object){
				out += "<li>["+p+"] => "+typeof(theObj)+"</li>";
				out += "<ul>...";
				out += print_r(theObj[p]); // recursion recursion recursion recursion recursion
				out += "</ul>";
			} else {
				out += "<li>["+p+"] => "+theObj[p]+"</li>";
			}
		}
		out += "</ul>";
	}
	return out;
}

function hd_map_find_route_from(fieldname){
	var s = document.getElementById(fieldname+'_source');
	var start = s.value;
	hd_map_find_route(fieldname, start, hd_map_settings[fieldname]['last_clicked']);
}

function hd_map_find_route_to(fieldname){
	var d = document.getElementById(fieldname+'_destination');
	var destination = d.value;
	hd_map_find_route(fieldname, hd_map_settings[fieldname]['last_clicked'], destination);
}

function hd_map_find_route(fieldname, start, end) {
	//if (hd_map_settings[fieldname]['open_bubble']) hd_map_settings[fieldname]['open_bubble'].close();
	var request = {
		origin: start, 
		destination: end,
		provideRouteAlternatives: true,
		travelMode: google.maps.DirectionsTravelMode.DRIVING
	};
	hd_map_directions_service.route(request, function(response, status){
		if (status == google.maps.DirectionsStatus.OK){
			// close the marker
			
			hd_map_directions_display.setDirections(response);
			hd_map_show_div(fieldname+'_close_directions');
			hd_map_show_div(fieldname+'_directions');
			document.getElementById(fieldname).scrollIntoView(true);
		}
	});
}

function hd_map_close_directions(fieldname){
	hd_map_hide_div(fieldname+'_close_directions');
	hd_map_hide_div(fieldname+'_directions');
	hd_map_directions_display.setMap(null);
	hd_map_display(hd_map_settings[fieldname]);
	document.getElementById(fieldname).scrollIntoView(true);
	document.getElementById(fieldname+'_directions').innerHTML = '';
}

/*function hd_map_show_div(layer_ref) {
	state = 'block';
	if (document.all) eval("document.all." + layer_ref + ".style.display = state"); 
	if (document.layers) document.layers[layer_ref].display = state; 
	if (document.getElementById &&!document.all) { 
		a = document.getElementById(layer_ref); 
		a.style.display = state; 
	}
}*/

/*function hd_map_hide_div(layer_ref) {
	state = 'none';
	if (document.all) eval("document.all." + layer_ref + ".style.display = state"); 
	if (document.layers) document.layers[layer_ref].display = state; 
	if (document.getElementById &&!document.all) { 
		a = document.getElementById(layer_ref); 
		a.style.display = state; 
	}
}*/

function hd_map_show_div(div){
	if (document.getElementById) { // DOM3 = IE5, NS6 
		document.getElementById(div).style.display = 'block'; 
	} else { 
		if (document.layers) { // Netscape 4 
			document.layers[div].display = 'block'; 
		} else { // IE 4 
			document.all.eval(div).style.display = 'block'; 
		}
	}
}

function hd_map_hide_div(div){
	if (document.getElementById) { // DOM3 = IE5, NS6 
		document.getElementById(div).style.display = 'none'; 
	} else { 
		if (document.layers) { // Netscape 4 
			document.layers[div].display = 'none';  
		} else { // IE 4 
			document.all.eval(div).style.display = 'none'; 
		}
	}
}
