﻿//VirtualEarth map class
function RDPLVirtualEarthMap() {
	//this.Version = '1.0';
	this.HTMLLayer = 'VirtualEarthMap';
      this.LocationsString = null;
	this.Locations = new Array();
	this.ReadyToDraw = false;
 }

//Disables the click on the "povered by" logo
RDPLVirtualEarthMap.prototype.DisableLogoClick = function() {
    var logo = document.getElementByClassName("PoweredByLogo");
    if(logo) {
        logo.onclick = null;
    }
}

//Returns the pin location points
RDPLVirtualEarthMap.prototype.GetLocationPoints = function() {
	var locations = new Array();
	for(var i=0; i<this.Locations.length; i++) {
		locations.push(new VELatLong(this.Locations[i].Latitude,this.Locations[i].Longitude));
	}
	return locations;
}

//Centers the map view to make all pins visible
RDPLVirtualEarthMap.prototype.CenterMap = function() {
	this.Map.SetMapView(this.GetLocationPoints());
}

//Displays the pins storedin the Locations array
RDPLVirtualEarthMap.prototype.PlotPoints = function() {
    if(this.LocationsString != ""){
        this.ParseMapPoints();
        this.Map.DeleteAllShapes();
	    for(var i=0; i<this.Locations.length; i++) {
		   this.Map.AddShape(this.Locations[i]);
	    }
    }
    else{
        this.Map.DeleteAllShapes();
    }
}

//Loads map with no pin on it. Center map with 10-zoom
RDPLVirtualEarthMap.prototype.NoPinMap = function() {
    this.Map.LoadMap(new VELatLong(this.CenterLatitude, this.CenterLongitude), 10);
}

//Loads the map, plots the pins and centers it for display
RDPLVirtualEarthMap.prototype.LoadMap = function() {
	if(!this.ValidateState()) {
		return;
	} else {
	    if(this.Map) {
	        return;
	    } else {
	        this.Map = new VEMap(this.HTMLLayer);
	        
	        if(this.LocationsString != "|"){
	        	this.Map.SetDashboardSize(VEDashboardSize.Tiny);
	            this.Map.LoadMap();
	        	this.Map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
	            this.DisableLogoClick();
	            this.PlotPoints();
	            this.CenterMap();
	            if(this.Locations.length < 3)
	                this.Map.SetZoomLevel(12);
            }else{
                this.NoPinMap();
	            this.DisableLogoClick();
	            this.DisablePageScroll();
            }
	    }
    }

}

//Centers Back Map - Enables to get back to initial positioning
RDPLVirtualEarthMap.prototype.CenterBackMap = function(){
    if(this.LocationsString != "|"){
        this.CenterMap();
    }
    else{
        this.NoPinMap();
    }
}

//Checks is it is possible to display the map; i.e, if we have switched to map view
RDPLVirtualEarthMap.prototype.ValidateState = function() {

    var returnValue = true

    try {
        if(!this.ReadyToDraw) {
            returnValue = false;
        } else {
            var layer = document.getElementById(this.HTMLLayer);
            var layerWidth = layer.offsetWidth;
            var layerHeight = layer.offsetHeight;
            if(layerWidth == 0 || layerHeight == 0) {
                throw('Layer not displayed');
            }
        }
    } catch(e) {
        returnValue = false;
    } finally {
        return returnValue;
    }

}

RDPLVirtualEarthMap.prototype.PageLoaded = function() {
    this.ReadyToDraw = true;
    this.LoadMap();
}

RDPLVirtualEarthMap.prototype.Initialize = function() {
    addEvent(document, 'click', 'LoadMap', 'VirtualEarthMap');
    addEvent(window, 'load', 'PageLoaded', 'VirtualEarthMap');
}

var VirtualEarthMap = new RDPLVirtualEarthMap();

VirtualEarthMap.LocationsString = document.getElementById("hdnMappingLabels").value;
//center point details
VirtualEarthMap.CenterLongitude = document.getElementById("hdnCenterLongitude").value;
VirtualEarthMap.CenterLatitude = document.getElementById("hdnCenterLatitude").value;

VirtualEarthMap.Initialize();
