﻿
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 NetismMapLocation(mid, lid)
{
    this.LocationID = "";
    this.Title = "";
    this.Link = "";
    this.Description = "";
    this.Address = "";
    this.City = "";
    this.State = "";
    this.Zip = "";
    this.Country = "";
    this.Longitude = "";
    this.Latitude = "";
    this.SortOrder = "";
    this.CreatedDate = "";
    this.IsActive = "";
    
    var fieldDiv = null;
    
    for ( var field in this )
    {
        fieldDiv = document.getElementById("NetismMap_" + mid + "_" + lid + "_" + field);
        if ( fieldDiv == null )
            continue;
        this[ field ] = fieldDiv.innerHTML;
    }
}

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.1';
        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 != "MapSettings" && window.Netism.ModuleName != "EditLocation" )
                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, "/DesktopModules/NetismMapExp/images/youarehere.png", "", ""));

            vem.AttachEvent("onchangeview", function(e) {
            
                vem.DeleteAllPushpins();
                var mapCenter = vem.GetCenter();
                if ( !mapCenter ) return;

                vem.AddPushpin(new VEPushpin(1, mapCenter,
                        "/DesktopModules/NetismMapExp/images/youarehere.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);
                
                var latlongArr = [];
                this.allLatLong = latlongArr;
                
                var mapLocations = document.getElementById("NetismMap_" + mapSetting.ModuleID + "_Locations");
                if ( !mapLocations )
                    continue;

                var locations = mapLocations.childNodes;
                var location = null;
                var locCount = 0;
                
                for ( var locIdx = 0; locIdx < locations.length; locIdx ++ )
                {
                    if ( !locations[locIdx].id || locations[locIdx].id.indexOf("NetismMap_") != 0 )
                        continue;
                    locCount ++;
                    location = new NetismMapLocation( mapSetting.ModuleID, locCount );

                    if ( isNaN(location.Latitude) || isNaN(location.Longitude) || 
                         location.Latitude == 0 || location.Longitude == 0 || location.IsActive != "True" )
                        continue;
                    
                    var boxHtml = GetBoxHtml(location);
                    latlongArr.push( new VELatLong(location.Latitude, location.Longitude) );
                    vem.AddPushpin( new VEPushpin("NetismMapPP_" + mapSetting.ModuleID + "_" + locCount, 
                                    new VELatLong(location.Latitude, location.Longitude), 
                                    "/DesktopModules/NetismMapExp/pushpin.aspx?id="+locCount, location.Title, boxHtml));
                }
                
                if ( !isNaN(mapSetting.Longitude) && !isNaN(mapSetting.Latitude) && !isNaN(mapSetting.Zoom)
                     && mapSetting.Longitude != 0 && mapSetting.Latitude != 0 && mapSetting.Zoom != 0 )
                    vem.SetCenterAndZoom(new VELatLong(mapSetting.Latitude, mapSetting.Longitude), mapSetting.Zoom);
                else
                    vem.SetMapView(latlongArr);
            }
        }
    }
    
    function GetBoxHtml(loc)
    {
        var html = loc.Description;
        return html;
    }
}

function NetismMap_PlotAddressOnMap()
{
    if ( !window.Netism || !window.Netism.Map || !window.Netism.Map.mapControl )
        return;

    var baseElementId = "dnn_ctr" + window.Netism.ModuleId + "_EditLocation_";
    var address = document.getElementById(baseElementId+"txtAddress").value;
    var city = document.getElementById(baseElementId+"txtCity").value;
    var state = document.getElementById(baseElementId+"txtState").value;
    var zip = document.getElementById(baseElementId+"txtZip").value;
    var country = document.getElementById(baseElementId+"txtCountry").value;
    var useAddress = address + ", " + city + ", " + state + " " + zip + ", " + country;
    
    window.Netism.Map.mapControl.Find(null, useAddress);
    return false;
}

function NetismMap_ShowPushpinBox(pinID)
{
    var pinAnchor = document.getElementById(pinID.toString());
    if ( !pinAnchor )
        return;
    
    var pinImg = pinAnchor.getElementsByTagName("img");
    if ( !pinImg || !pinImg.length || pinImg.length == 0 )
        return;

    pinImg[0].src += "&selected=1";
    var pinImgMouseOver = pinImg[0].onmouseover;
    pinImgMouseOver();
}

function NetismMap_HidePushpinBox(pinID)
{
    var pinAnchor = document.getElementById(pinID.toString());
    if ( !pinAnchor )
        return;
    
    var pinImg = pinAnchor.getElementsByTagName("img");
    if ( !pinImg || !pinImg.length || pinImg.length == 0 )
        return;
    
    pinImg[0].src = pinImg[0].src.replace("&selected=1", "");
    var pinImgMouseOut = pinImg[0].onmouseout;
    pinImgMouseOut();
}

function NetismMap_ZoomView(ModuleID, Latitude, Longitude, Zoom, Style)
{
    if ( !Netism || !Netism.Map || !Netism.Map.mapControls )
        return false;
    
    var map = Netism.Map.mapControls[ModuleID];
    
    if ( !map )
        return false;

    if ( map.GetMapStyle() != Style )
        map.SetMapStyle(Style);

    if ( map.GetZoomLevel() == Zoom )
        map.SetCenter( new VELatLong(Latitude, Longitude) );
    else
        map.SetCenterAndZoom(new VELatLong(Latitude, Longitude), Zoom);
            
    return false;
}