﻿var g_veBEMap;
var g_veBEIgnoreZoomEnd = true;
var g_hostInCustomMapTab = false;

function ShowVEPluginMap(hostInCustomMapTab) {

	var onPointSetting = GetOnPointSettings(null);
	if(onPointSetting != null)
    {    
		var mapCtrlId = onPointSetting.id;
		var mapDiv = onPointSetting.divId;
		
		if (window.g_veBEMap != null)
		{
			//dispose.
			try	{
				window.g_veBEMap.Dispose();
			}
			catch (e) {
			    Console.error(String.format('{0}::{1}| window.g_veBEMap.Dispose error - {2}', 'VEBirdseye.js', 'ShowVEPluginMap', e.message));				
			}
		}
		
		window.g_veBEMap = new VEMap(mapDiv);
				
        // disable the scale bar.  It is causing JS error when switching from Birdeyes to 2D VE in NuVi/Arabic setting
        window.g_veBEMap.HideScalebar();

		var center = new VELatLong(onPointSetting.curCenterLat,onPointSetting.curCenterLon);

		window.g_veBEMap.AttachEvent("onendpan", VEOnEndPan);
		window.g_veBEMap.AttachEvent("onendzoom", VEOnEndZoom);		
				
		//load map
		var options = new VEMapOptions();
		options.EnableDashboardLabels = false;
		window.g_veBEMap.LoadMap(center, 1, VEMapStyle.BirdseyeHybrid, false, VEMapMode.Mode2D, false, 0, options);

		//refresh layermanager.		
		InitializeLyrMgr('lmTree', onPointSetting.mapSrcId, mapCtrlId, '');

		if (onPointSetting.dynamicMapResizingEnabled) {
		    doResize();
		}

		g_hostInCustomMapTab = hostInCustomMapTab;
		if (hostInCustomMapTab) {
		    // Add button to switch back to VE 2D sub maptab
		    var holderDiv = document.getElementById('ctmMapTabUseDiv');		    

		    if (holderDiv != null) {
		        var btn2D = document.createElement('input');
		        btn2D.id = 'btn2D';
		        btn2D.setAttribute('type', 'button');
		        btn2D.setAttribute('name', 'btn2D');
		        btn2D.setAttribute('class', 'btnGel');
		        btn2D.setAttribute('className', 'btnGel');
		        btn2D.value = "Roads";

		        btn2D.onclick = function() {
		            ToggleVESubMapTab();
		        }

		        holderDiv.appendChild(btn2D);
		    }
		    else {
		        Console.error(String.format('{0}::{1}| holderDiv not found', 'VEBirdsEye.js', 'ShowVEPluginMap'));
		    }

		    // Hack to remove style switch control
		    try {
		        var navCtrl = document.getElementById('MSVE_navAction_topBar');		        
		        navCtrl.style.display = "none";

		        var navContainer = document.getElementById('MSVE_navAction_container');
		        navContainer.style.width = '100px';
		    } catch (e) {
		        Console.error(String.format('{0}::ShowVEPluginMap| VE Nav Ctrl hack failed', 'VEBirdseye.js'));
		    }
		}
		
	}
}

function VEOnEndPan(me)
{
	UpdateBEExtents(me);	
}

function VEOnEndZoom(me)
{	
	if (g_veBEIgnoreZoomEnd)
	{
		g_veBEIgnoreZoomEnd = false;
	}
	else
	{
		UpdateBEExtents(me);
	}
}

function SetVEBirdseyeMapWindowSize(mapSize) {

    try {
        if (window.g_veBEMap) {
            var onPointSetting = GetOnPointSettings(null);

            if (onPointSetting != null) {
                if (onPointSetting.useEmbeddedMapContainer) {
                    EmbeddedMapContainerResize(mapSize.width, onPointSetting.embeddedMapContainerHeight);
                }

                var mapWidth = mapSize.width;
                var mapHeight = mapSize.height;

                if (g_hostInCustomMapTab) {
                    mapHeight = mapHeight - 25; // adj for status bar below map
                }
                else {
                    mapHeight = mapHeight - 5; // adj for border 
                }

                // use the map div size to check is size changed?
                var outerMapDivReference = getObject("outer_map_" + onPointSetting.id);
                if (outerMapDivReference != null) {
                    if ((parseInt(outerMapDivReference.style.width) != mapWidth)
                    || (parseInt(outerMapDivReference.style.height) != mapHeight)) {
                        outerMapDivReference.style.width = mapWidth + "px";
                        outerMapDivReference.style.height = mapHeight + "px";

                        var mapDivReference = getObject("map_" + onPointSetting.id);
                        if (mapDivReference != null) {
                            if ((parseInt(mapDivReference.style.width) != mapWidth)
                            || (parseInt(mapDivReference.style.height) != mapHeight)) {
                                mapDivReference.style.width = mapWidth + "px";
                                mapDivReference.style.height = mapHeight + "px";
                            }
                        }

                        try {
                            window.g_veBEMap.Resize(mapWidth, mapHeight);
                        }
                        catch (e) {
                            Console.error(String.format('{0}::{1}| window.g_veBEMap.Resize error - {2}', 'VEBirdseye.js', 'SetVEBirdseyeMapWindowSize', e.message));
                        }
                    }
                }                    
            }
        }
    } catch (e) {
        Console.error(String.format('{0}::{1}| Unknown error - {2}', 'VEBirdseye.js', 'SetVEBirdseyeMapWindowSize', e.message));
    }
}

function UpdateBEExtents(me)
{
	if (window.g_veBEMap)
	{
		var env = null;
		if (g_veBEMap.GetMapStyle() == VEMapStyle.Birdseye || g_veBEMap.GetMapStyle() == VEMapStyle.BirdseyeHybrid)
		{
			env = window.g_veBEMap.GetBirdseyeScene().GetBoundingRectangle();
		}
		else
		{
			env = window.g_veBEMap.GetMapView();
		}
		
		var tl = env.TopLeftLatLong;
		var br = env.BottomRightLatLong;
		var xMin = tl.Longitude;
		var yMin = br.Latitude;
		var xMax = br.Longitude;
		var yMax = tl.Latitude;	
		try
		{
			if (_Onpoint.MapHistory)
			{
				var onPointSetting = GetOnPointSettings(null);
				_Onpoint.MapHistory.UpdateMapTabExtentsFromLatLong(onPointSetting.mapSrcId, xMin, yMin, xMax, yMax);
			}
		}
		catch(e) {
		    Console.error(String.format('{0}::{1}| Unknown error - {2}', 'VEBirdseye.js', 'UpdateBEExtents', e.message));
		}	
	}
}
