﻿// JScript File

var map = null;

// San Francisco
// var initialX = -122.388581;
// var initialY = 37.789536;

// London (trafalgar Square)
// var initialX = -0.127481;
// var initialY = 51.507258;

// London (Tottenham Court Road)
// var initialX = -0.130349;
// var initialY = 51.516407;

// Bournemouth
var initialX = -1.8761569;
var initialY = 50.7166576;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// UI Util functions

function e(id) 
{
    return document.getElementById(id); 
}

function g_formatFloat(pFloat, pDp)
{
    var m = Math.pow(10, pDp);
    return parseInt(pFloat * m, 10) / m;
}


function locKeyPress(p_event)
{
    var l_keyCode = p_event.keyCode;
    
    if (l_keyCode == 13)
    {
        gotoLocation();
        return false;
    }
    return true;
}   

function gotoLocation()
{
    var x = parseFloat(e("xLoc").value);
    var y = parseFloat(e("yLoc").value);
    var z = parseFloat(e("zLoc").value);
    
    // update google map
    map.setCenter(new GLatLng(y,x), z);
}

 
 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Main UI Page Functions


function initPage()
{
    // Create location setting map
    g_createLocationMap();
    
    // Create default preview map and script
    createMap();
}

function closePage()
{
    GUnload();
}

function g_createLocationMap()
{
    var l_mapDiv = e("mapDiv");
    var l_latLong = new GLatLng(initialY,initialX);
    
    var l_zoom = 16;
    
    
    var l_mapOptions = 
            {
                draggableCursor:"crosshair", 
                draggingCursor:"move", 
                googleBarOptions : { showOnLoad : true } 
            };
    
    
    map = new GMap2(l_mapDiv, l_mapOptions);

    map.addControl(new GLargeMapControl3D());
    map.addControl(new GScaleControl());
    map.addControl(new GMenuMapTypeControl());
    map.addMapType(G_PHYSICAL_MAP);        
    
    map.enableDoubleClickZoom();
    map.enableContinuousZoom();

    map.enableGoogleBar();

    map.setCenter(l_latLong, l_zoom);    
    map.setMapType(G_NORMAL_MAP); 
    
    // Add listener for map center changing
    GEvent.addListener(map, "moveend", function()
    {
        g_locationViewChanged(true);
    });

    g_locationViewChanged(false);
}

function g_locationViewChanged(p_user)
{
    var l_point = map.getCenter();
    var x = l_point.lng();
    var y = l_point.lat();    
    var z = map.getZoom();
    
    e("xMap").value = g_formatFloat(x, 7);
    e("yMap").value = g_formatFloat(y, 7);
    e("zMap").value = z;
}



function widthClick(a)
{
    if (a.checked)
    {
        e("pxWidth").disabled = "disabled";
    }
    else
    {
        e("pxWidth").disabled = false;
    }
}

function markerSelectClick()
{
    var l_gmSelected = e("gmMarkerShow").selectedIndex;
    var l_veSelected = e("veMarkerShow").selectedIndex;
    
    if (l_gmSelected || l_veSelected)
    {
        e("markerImage").disabled = false;
        e("markerTitle").disabled = false;
        e("markerDescription").disabled = false;
    }
    else
    {
        e("markerImage").disabled = "disabled";
        e("markerTitle").disabled = "disabled";
        e("markerDescription").disabled = "disabled";
    }
}


function xyzView()
{
    var x = parseFloat(e("xMap").value);
    var y = parseFloat(e("yMap").value);
    var z = parseInt(e("zMap").value);
    
    if (isNaN(x))
    {
        alert("invalid x value : " + e("xMap").value);
    }
    else if (isNaN(y))
    {
        alert("invalid y value : " + e("yMap").value);
    }
    else if (isNaN(z))
    {
        alert("invalid z value : " + e("zMap").value);
    }
    else
    {    
        map.setCenter(new GLatLng(y,x), z);
    }
}


function setGoogleLink()
{
    var url = e("googleLink").value;
    
    var l_fields = url.split("&");
    
    var x = null;
    var y = null;
    var z = 16;
    
    var l_bearing = null;
    var l_pitch = null;
    var l_zoom = null;
    
    for (var i=0; i<l_fields.length; i++)
    {
        var l_field = l_fields[i];
        
        // cbp=1,283.24,,2,6.98
        if (l_field.length > 3 && l_field.substr(0,3) == "cbp")
        {
            var l_cmd = l_field.substr(4);                
            var l_vals = l_cmd.split(",");
            
            l_bearing = parseFloat(l_vals[1]);
            l_zoom = parseInt(l_vals[3]);
            l_pitch = parseFloat(l_vals[4]);
        }
        
        
        if (l_field.length > 2 && l_field.substr(0,2) == "ll")
        {
            var l_cmd = l_field.substr(3);
            var l_vals = l_cmd.split(",");
            
            y = parseFloat(l_vals[0]);
            x = parseFloat(l_vals[1]);
        }

        if (l_field.length > 4 && l_field.substr(0,4) == "cbll")
        {
            var l_cmd = l_field.substr(5);
            var l_vals = l_cmd.split(",");
            
            y = parseFloat(l_vals[0]);
            x = parseFloat(l_vals[1]);
        }
        
        if (x != null)
        {
            e("xMap").value = x;
        }
        if (y != null)
        {
            e("yMap").value = y;
        }
        if (l_bearing != null)
        {
            e("streetviewBearing").value = l_bearing;
        }
        if (l_pitch != null)
        {
            e("streetviewPitch").value = l_pitch;
        }
        if (l_zoom != null)
        {
            e("streetviewZoom").value = l_zoom;
        }
        
        map.setCenter(new GLatLng(y,x), z);
        
        createMap();
    }

    
}


// set up IFramed dual map
function createMap()
{
    // First get map input properties and validate each
    var x = parseFloat(e("xMap").value);
    var y = parseFloat(e("yMap").value);
    var z = parseInt(e("zMap").value);  

    
    // birdseye zoom
    var zb = e("zBirdsEye").selectedIndex;
    var l_birdseyeDirection = 0;    // e("birdseyeDirection").selectedIndex;
    
    var l_gmType = e("gmType").selectedIndex;  
    var l_veType = e("veType").selectedIndex;
    
    var l_gmControls = e("gmControls").selectedIndex;  
    
    var l_fullWidth = e("fullUnits").checked;
    var l_pxWidth = parseInt(e("pxWidth").value);  
    var l_pxHeight = parseInt(e("pxHeight").value);  
    
    var l_border = e("borderSelect").selectedIndex == 0 ? 0 : 1;
    var l_mouseWheel = e("mousewheelSelect").selectedIndex == 0 ? 0 : 1;
    
    var l_streetviewEnable = e("streetviewEnable").selectedIndex == 0 ? 0 : 1;
    var l_streetviewBearing = parseFloat(e("streetviewBearing").value);
    var l_streetviewPitch = parseFloat(e("streetviewPitch").value);
    var l_streetviewZoom = parseInt(e("streetviewZoom").value);
    var l_streetviewMarker = parseInt(e("streetviewMarkerSelect").value);
    var l_streetviewFOV = e("streetviewFOV").selectedIndex == 0 ? 0 : 1;
    
    var l_gmMarkerOn = e("gmMarkerShow").selectedIndex;
    var l_veMarkerOn = e("veMarkerShow").selectedIndex;
    
    var l_markerImage = e("markerImage").selectedIndex;
    var l_markerTitle = e("markerTitle").value;
    var l_markerDescription = e("markerDescription").value;
    
    var l_geocodeAddress = e("geocodeAddress").value;

    if (isNaN(x))
    {
        alert("invalid x value : " + e("xMap").value);
        return;
    }
    if (isNaN(y))
    {
        alert("invalid y value : " + e("yMap").value);
        return;
    }
    if (isNaN(z))
    {
        alert("invalid z value : " + e("zMap").value);
        return;
    }

    if (!l_fullWidth && isNaN(l_pxWidth))
    {
        alert("invalid width value : " + e("pxWidth").value);
        return;
    }
    if (isNaN(l_pxHeight))
    {
        alert("invalid height value : " + e("pxHeight").value);
        return;
    }
    if (isNaN(l_streetviewBearing))
    {
        alert("invalid street view bearing value : " + e("streetviewBearing").value);
        return;
    }
    if (isNaN(l_streetviewPitch))
    {
        alert("invalid street view pitch value : " + e("streetviewPitch").value);
        return;
    }
    if (isNaN(l_streetviewZoom))
    {
        alert("invalid street view zoom value : " + e("streetviewZoom").value);
        return;
    }
    
    // Generate script now ....

    var l_domain = "http://data.mashedworld.com/dualmaps/";
    
    /* if (typeof(localScript) != "undefined")
    {
        l_domain = "http://localhost:2150/150 Mashed World 2/dualmaps/";
    }
    else
    {
        l_domain = "http://data.mashedworld.com/dualmaps/";
    } */
    
    var l_scriptHtml = "<iframe ";
    
    l_scriptHtml += "style=\"width:";
        
    if (l_fullWidth)
    {
        l_scriptHtml += "100%";
    }
    else
    {
        l_scriptHtml += l_pxWidth + "px";
    }
        
    l_scriptHtml += ";height:" + l_pxHeight + "px";     
    l_scriptHtml += ";padding:0";

    if (l_border)
    {
        l_scriptHtml += ";border:solid 1px black";        
    }
        
    l_scriptHtml += "\" ";

    l_scriptHtml += "src=\"" + l_domain + "map.htm";

    l_scriptHtml += "?x=" + g_formatFloat(x,7) + "&y=" + g_formatFloat(y,7) + "&z=" + z;
        
    // map types
    l_scriptHtml += "&gm=" + l_gmType;
    l_scriptHtml += "&ve=" + l_veType;
    l_scriptHtml += "&gc=" + l_gmControls;
    
    // Birdseye mode options
    if (l_veType >= 4)
    {        
        l_scriptHtml += "&bz=" + zb;
        l_scriptHtml += "&bd=" + l_birdseyeDirection;        
    }
    
    // style options
    l_scriptHtml += "&mw=" + l_mouseWheel;
    
    // streetview
    if (l_streetviewEnable)
    {
        l_scriptHtml += "&sv=" + l_streetviewEnable;
        l_scriptHtml += "&svb=" + l_streetviewBearing;
        l_scriptHtml += "&svp=" + l_streetviewPitch;
        l_scriptHtml += "&svz=" + l_streetviewZoom;
        l_scriptHtml += "&svm=" + l_streetviewMarker;
        l_scriptHtml += "&svf=" + l_streetviewFOV;
    }
    
    // marker options
    if (l_gmMarkerOn || l_veMarkerOn)
    {
        // _TODO: ascii to html char mapping fns ... 
        l_scriptHtml += "&mi=" + l_markerImage;
        l_scriptHtml += "&mg=" + l_gmMarkerOn;
        l_scriptHtml += "&mv=" + l_veMarkerOn;
        if (l_markerTitle.length > 0)
        {
            l_scriptHtml += "&mt=" + l_markerTitle;
        }
        if (l_markerDescription.length > 0)
        {
            l_scriptHtml += "&md=" + l_markerDescription;
        }
        
    }
    
    if (l_geocodeAddress)
    {
        l_scriptHtml += "&addr=" + l_geocodeAddress;
    }
      
    // Additional params  
    l_scriptHtml += "\" marginwidth=\"0\" marginheight=\"0\" frameborder=\"0\" scrolling=\"no\"></iframe>";
    
    

    e("previewDiv").innerHTML = l_scriptHtml;
    e("scriptText").value = l_scriptHtml;

}



