﻿
var map;
var marker;
var centre;

var placeTitle = "London, United Kingdom";
   
function e(id)
{
    return document.getElementById(id); 
}

function formatFloat(v, p)
{
    var m = Math.pow(10, p);
    return parseInt(v * m, 10) / m;
}

function initPage()
{
    var mapDiv = e("mapDiv");
    
    map = new GMap(mapDiv);
    
    centre = new GLatLng(51.500152,-0.126236);
    map.setCenter(centre, 1);
    
    map.addMapType(G_PHYSICAL_MAP);    
    map.addControl(new GLargeMapControl3D()); 
    map.addControl(new GMenuMapTypeControl()); 
    map.addControl(new GScaleControl());     
    
    GEvent.addListener(map, "zoomend", function()
    {
        e("zoomText").value = map.getZoom();
    });    
    
    marker = new GMarker(centre, {"draggable":true});
    map.addOverlay(marker);
    
    GEvent.addListener(marker, "dragend", function()
       {            
            centre = marker.getPoint();
            map.setCenter(centre);
            
            e("latText").value = formatFloat(centre.lat(), 6);
            e("lngText").value = formatFloat(centre.lng(), 6);
       }
    );     

    GEvent.addListener(marker, "click", function()
       {            
            placeTitle = formatTitle(e("titleText").value);
            marker.openInfoWindowHtml(placeTitle);
       }
    );     

    e("latText").value = formatFloat(centre.lat(), 6);
    e("lngText").value = formatFloat(centre.lng(), 6);
    e("zoomText").value = map.getZoom();
    
    e("titleText").value = placeTitle;
    
    createMapScript();
}

   
function closePage()
{
    GUnload();
}


function applyLocation()
{
    var x = parseFloat(e("lngText").value);
    var y = parseFloat(e("latText").value);
    var z = parseFloat(e("zoomText").value);
    
    centre = new GLatLng(y,x);
    map.setCenter(centre, z);
}

function findAddress()
{
    var address = e("addressText").value;
    var geocoder = new GClientGeocoder(new GGeocodeCache()); 

    geocoder.getLatLng(address, function(point)
    { 
        if (point)
        {
            centre = point;
            marker.setPoint(centre);
            map.setCenter(centre);
            
            e("latText").value = formatFloat(centre.lat(), 6);
            e("lngText").value = formatFloat(centre.lng(), 6);
            e("zoomText").value = map.getZoom();            
        }
        else
        {
            alert("unable to find address");            
        }
    });
}


function createMapScript()
{
    var width = parseInt(e("widthText").value);
    var height = parseInt(e("heightText").value);
    
    if (height < 400)
    {
        height = 400;
    }
    
    if (width <= 0)
    {
        width = "100%";
    }
    else
    {
        if (width < 600)
        {
            width = 600;
        }
        width = width + "px";
    }
    
    height = height + "px";


    var x = formatFloat(centre.lng(), 6);
    var y = formatFloat(centre.lat(), 6);
    var z = map.getZoom();
    
    var t = 0;
    switch (map.getCurrentMapType())
    {
        case G_SATELLITE_MAP : t = 1; break;
        case G_HYBRID_MAP : t = 2; break;
        case G_PHYSICAL_MAP : t = 3; break;
    }
                
    placeTitle = formatTitle(e("titleText").value);
    
    var url = "http://data.mapchannels.com/instant/map.htm?x=" + x + "&y=" + y + "&z=" + z + "&t=" + t + "&name=" + placeTitle;
    var html = 
        "<iframe src='" + url + "'  style='width:" + width + ";height:" + height + ";border:solid 1px black' frameborder='0' marginwidth='0' marginheight='0' scrolling='off' ></iframe>";
    
    e("preview").innerHTML = html;
    e("scriptText").value = html;
}

function formatTitle(str)
{
    var title = "";
    for (var i=0; i<str.length; i++)
    {
        var c = str.substr(i,1);
        if (c != "/r" && c != "\n")
        {
            title += c;
        }
    }
    
    return title;
}

