﻿
if ( window.Netism == undefined )
{
    window.Netism = {};
}

window.Netism.OnLoad = window.onload;

window.onload = function()
{
    window.Netism.OnLoad();
    
    window.Netism.Map = new NetismMap();    
    window.Netism.Map.Download();
    window.Netism.Map.Render();
}

function NetismMap()
{
    this.mapControl = null;
    this.mapControls = {};
    this.allLatLong = null;
    
    this.Download = function()
    {
        var e = document.createElement("script");
        e.src = 'http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2';
        e.type="text/javascript";
        document.getElementsByTagName("body")[0].appendChild(e);
    }
    
    this.Render = function(OnlyModuleID)
    {
        var me = this;

        if( window.VEMap === undefined )
        {
            setTimeout(function() { me.Render(); }, 500);
        }
        else if ( window.Netism.ModuleId != undefined && window.Netism.ModuleName != undefined )
        {
            if ( window.Netism.ModuleName != "edit" )
                return;

            var baseElementId = "dnn_ctr" + window.Netism.ModuleId + "_" + window.Netism.ModuleName + "_";
            var mapDivID = "NetismMap_" + window.Netism.ModuleId;;
            var mapDiv = document.getElementById(mapDivID);
            if ( !mapDiv )
                return;
            
            var vem = new VEMap(mapDivID);
            if ( !vem ) return;
            this.mapControl = vem;
            
            var mapStyle = document.getElementById( baseElementId + "ddlStyle" );
            var mapControl = document.getElementById( baseElementId + "ddlControls" );
            
            if ( mapControl )
            {
                mapControl = mapControl.options[ mapControl.selectedIndex ].value;
                if ( mapControl == "-" )
                    vem.HideDashboard();
                else
                    vem.SetDashboardSize(mapControl);
            }

            vem.LoadMap();

            if ( mapStyle )
            {
                mapStyle = mapStyle.options[ mapStyle.selectedIndex ].value;
                vem.SetMapStyle(mapStyle);
            }

            var glat = document.getElementById( baseElementId + "txtLatitude" ).value;
            var glong = document.getElementById( baseElementId + "txtLongitude" ).value;
            var gzoom = document.getElementById( baseElementId + "txtZoom" ).value;
            
            if ( !gzoom || gzoom == "" || gzoom == "0" || isNaN(gzoom) ) gzoom = 10;

            if ( glat != "" && glat != "0" && !isNaN(glat) )
                if ( glong != "" && glong != "0" && !isNaN(glong) )
                    vem.SetCenterAndZoom(new VELatLong(glat, glong), gzoom);
                
            var mapCenter = vem.GetCenter();
            vem.AddPushpin(new VEPushpin(1, mapCenter, window.Netism.ModulePath + "pin.png", "", ""));

            vem.AttachEvent("onchangeview", function(e) {
            
                vem.DeleteAllPushpins();
                var mapCenter = vem.GetCenter();
                if ( !mapCenter ) return;

                vem.AddPushpin(new VEPushpin(1, mapCenter, window.Netism.ModulePath + "pin.png", "", ""));

                glat = document.getElementById( baseElementId + "txtLatitude" );
                if ( glat ) glat.value = mapCenter.Latitude;

                glong = document.getElementById( baseElementId + "txtLongitude" );
                if ( glong ) glong.value = mapCenter.Longitude;

                gzoom = document.getElementById( baseElementId + "txtZoom" );
                if ( gzoom ) gzoom.value = vem.GetZoomLevel();
            });
        }
        else if ( window.Netism != undefined && window.Netism &&
                 window.Netism.MapSettings != undefined && window.Netism.MapSettings &&
                 window.Netism.MapSettings.length )
        {
            for ( var mapIdx = 0; mapIdx < window.Netism.MapSettings.length; mapIdx ++ )
            {
                var mapSetting = window.Netism.MapSettings[mapIdx];
                
                if ( OnlyModuleID && !isNaN(OnlyModuleID) && OnlyModuleID != mapSetting.ModuleID )
                    continue;
                
                var mapDivID = "NetismMap_" + mapSetting.ModuleID;
                var mapDiv = document.getElementById(mapDivID);
                if ( !mapDiv )
                    continue;
                
                var vem = new VEMap(mapDivID);
                this.mapControls[mapSetting.ModuleID] = vem;
                
                if ( mapSetting.Control == "-" )
                    vem.HideDashboard();
                else
                    vem.SetDashboardSize(mapSetting.Control);
     
                vem.LoadMap();
                vem.SetMapStyle(mapSetting.Style);
                vem.AddPushpin( new VEPushpin("NetismMapPP_" + mapSetting.ModuleID, 
                                new VELatLong(mapSetting.Latitude, mapSetting.Longitude), 
                                window.Netism.ModulePath + "pin.png", "", ""));
                vem.SetCenterAndZoom(new VELatLong(mapSetting.Latitude, mapSetting.Longitude), mapSetting.Zoom);
            }
        }
    }
}
