﻿// Global to the page variables
var firing = false;
var map = null;
var g_map = null;
var init_width, init_height;
var currGMStyle, currVEStyle;
var g_geocoder = new GClientGeocoder();
var currGMarker = null;
var currVMarker = null;
var ggl_found = false;
var ve_found = false;


function SizeDivs() {
    init_width = Math.floor((document.body.offsetWidth - 10) / 2);
    init_height = document.body.offsetHeight - 165;
    document.getElementById('ve').style.width = init_width + "px";
    document.getElementById('ve').style.height = init_height + "px";
    document.getElementById('gm').style.width = init_width + "px";
    document.getElementById('gm').style.height = init_height + "px";
    document.getElementById('bottomDiv').style.width = document.body.offsetWidth-5 + "px";
}	

function PageLoad() {
    SizeDivs();

	document.getElementById('whatsnew').style.left = (document.body.offsetWidth - 100) + "px";
	document.getElementById('whatsnew').style.visibility = "visible";

    drawVE();
    drawGM();
}


function PageResize() {
    SizeDivs();
}

function drawVE() {
    map = new VEMap('ve');
    map.LoadMap(new VELatLong(lat, lon, 0, VEAltitudeMode.RelativeToGround), init_zoom, VEMapStyle.Hybrid, false, VEMapMode.Mode2D, false, 1);
    map.AttachEvent("onchangeview", updateGMap);
    currVEStyle = map.GetMapStyle();
}

function drawGM() {
    g_map = new GMap2(document.getElementById("gm"));
    g_map.setUIToDefault();
 //   if (init_height < 290) {
 //       current_gmap_control = new GSmallMapControl();
 //       g_map.addControl(current_gmap_control);
 //   } else {
 //       current_gmap_control = new GLargeMapControl();
 //       g_map.addControl(current_gmap_control);
 //   }
    g_map.addControl(new GMapTypeControl());
    g_map.setCenter(new GLatLng(lat, lon), map.GetZoomLevel());
    g_map.setMapType( G_HYBRID_MAP );
    g_map.removeMapType( G_PHYSICAL_MAP );
    currGMStyle = g_map.getCurrentMapType();
    GEvent.addListener(g_map, "moveend", updateVE);
	GEvent.addListener(g_map, "maptypechanged", updateVE);
}

function updateVE(){
    var v_lat, v_lon, v_zoom;
    debug("Updating VEMap");
    if (! firing) {
        if (g_map.getCurrentMapType() != currGMStyle) {
            map.SetMapStyle(ge2veXRef(g_map.getCurrentMapType()));
            currGMStyle = g_map.getCurrentMapType();
        }
        v_lat = g_map.getCenter().lat();
        v_lon = g_map.getCenter().lng();
        v_zoom = g_map.getZoom();
        debug(v_lat + "," + v_lon + "," + v_zoom);
        firing = true;
        if (map.GetZoomLevel() <= 17) {
            map.SetCenterAndZoom(new VELatLong(v_lat, v_lon), v_zoom);
        } else {
            map.SetCenter(new VELatLong(v_lat, v_lon));
        }

        updatePerma();
        firing = false;
			
    }
}

function updateGMap(v_lat, v_lon, v_zoom) {
    if (! firing) {
        if (map.GetMapStyle() != currVEStyle) {
            g_map.setMapType(ve2geXRef(map.GetMapStyle()));
            currVEStyle = map.GetMapStyle();
        }
        v_lat = map.GetCenter().Latitude;
        v_lon = map.GetCenter().Longitude;
        v_zoom = map.GetZoomLevel();
        firing = true;
        g_map.setCenter(new GLatLng(v_lat, v_lon), v_zoom);
        updatePerma();
 		firing = false;
    }
}

function debug(message) {
//    GLog.write(message);
}

function updatePerma() {
    var j_url;
    j_url = "http://www.jonasson.org/maps/?lat=" + g_map.getCenter().lat() + "&lon=" + g_map.getCenter().lng() + "&z=" + g_map.getZoom();
    document.getElementById("perma").innerHTML = "&nbsp;&nbsp;<a href=" + j_url + " target=_blank>Link to this page</a>";
}

function ge2veXRef(GMapType) {
    if (GMapType == G_NORMAL_MAP) {
        return VEMapStyle.Road;
    } else if (GMapType == G_SATELLITE_MAP) {
        return VEMapStyle.Aerial;
    } else if (GMapType == G_HYBRID_MAP) {
        return VEMapStyle.Hybrid;
    }
}

function ve2geXRef(VEMapType) {
    if (VEMapType == VEMapStyle.Road) {
        return G_NORMAL_MAP;
    } else if (VEMapType == VEMapStyle.Aerial) {
        return G_SATELLITE_MAP;
    } else if (VEMapType == VEMapStyle.Hybrid) {
        return G_HYBRID_MAP;
    }
}

function Geocode() {
    var address = document.getElementById("search").value;
    map.DeleteAllShapes();
    g_map.clearOverlays();
    ggl_found = false;
    ve_found = false;
    g_geocoder.getLatLng(address, GMResults); 
	try {
        map.Find(null, address, null, null, null, 1, true, true, false, false, VEResults);
	} catch(e) {
	    ve_found = true;
        recenterMap();
    }    
	return(false);
}

function VEResults(layer, resultsArray, places, hasMore, veErrorMessage) {
    if (places != null) {
        currVMarker = new VEShape(VEShapeType.Pushpin, places[0].LatLong);
        currVMarker.SetDescription(document.getElementById("search").value);
        map.AddShape(currVMarker);
    }
    ve_found = true;
    recenterMap();
}


function GMResults(point) {
    if (!point) {
        ggl_found = true;
        currGMarker = null;
    } else {
        ggl_found = true;
        currGMarker = new GMarker(point, {title: document.getElementById("search").value});
        g_map.addOverlay(currGMarker);
    }
    ggl_found = true;      
    recenterMap();
}

function recenterMap() {
    if (!ggl_found || !ve_found) {
        debug("one of them wasn't done yet");
        return;
    }
    debug("they're done, moving on");
    if (currGMarker == null && currVMarker != null) { // If only Virtual Earth found
        map.SetCenterAndZoom(currVMarker.GetPoints()[0], 13);
    } else if (currGMarker != null && currVMarker == null) {    // if only Google Maps found
        g_map.setCenter(currGMarker.getLatLon(), 13);
    } else if (currGMarker != null && currVMarker != null) {    // If both found
        var n = currGMarker.getLatLng().lat() > currVMarker.GetPoints()[0].Latitude  ? currGMarker.getLatLng().lat() : currVMarker.GetPoints()[0].Latitude;
        var s = currGMarker.getLatLng().lat() < currVMarker.GetPoints()[0].Latitude  ? currGMarker.getLatLng().lat() : currVMarker.GetPoints()[0].Latitude;
        var e = currGMarker.getLatLng().lng() > currVMarker.GetPoints()[0].Longitude ? currGMarker.getLatLng().lng() : currVMarker.GetPoints()[0].Longitude;
        var w = currGMarker.getLatLng().lng() < currVMarker.GetPoints()[0].Longitude ? currGMarker.getLatLng().lng() : currVMarker.GetPoints()[0].Longitude;
        var ne = new GLatLng(n,e);
        var sw = new GLatLng(s,w);
        var gbounds = new GLatLngBounds(sw,ne);
        var boundZoom = g_map.getBoundsZoomLevel(gbounds);
        if (boundZoom > 15) {
            boundZoom = 15;
        }
        g_map.setCenter(gbounds.getCenter(), boundZoom-1);
    } else {    // neither found it
        alert("The address " + document.getElementById("search").value + " could not be located");
    }
}    
