﻿
var map = null;
var layer01=null;
var pinID = 0;
var endPanAction=0;
var changeViewAction=0;
var mapPopupID=0;
var iLoadError=0;
var tmpShapeToPopup=null;
var objCoords=null;


function GetMap()
{

	try{
		map = new VEMap("mapVE");
		map.onLoadMap = onMapLoaded;
		map.LoadMap(new VELatLong(15, 0), 2 ,'h' , false);//h for hybrid and r for road
		map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
		}
	catch(err)
    {
        if (iLoadError < 2)
        {
            iLoadError++;
            //alert(err.description);
            //setTimeout("GetMap()",2000);
        }else
        {
            //alert("The Mapping Control failed to load.");
        }
    }	
}   

 function GetTiles()
 {
    var bounds = [new VELatLongRectangle(new VELatLong(60,-175),new VELatLong(0,-67))];

    var tileSourceSpec = new VETileSourceSpecification("lidar", 
    "http://cimss.ssec.wisc.edu/tropic/real-time/atlpac/winds/wg810vor.GIF");
    tileSourceSpec.NumServers = 1;
    tileSourceSpec.Bounds = bounds;
    tileSourceSpec.MinZoomLevel = 4;
    tileSourceSpec.MaxZoomLevel = 18;
    tileSourceSpec.Opacity = 0.4;
    tileSourceSpec.ZIndex = 100;
		
    map.AddTileLayer(tileSourceSpec, true);
 } 

function MapZoomIn(){
    map.ZoomIn();
}
function MapZoomOut(){
    map.ZoomOut();
}

function SetCenterAndZoom(lngLat,zoomLevel) {
    var point = lngLat.split(' ');
    var coords = new VELatLong(parseFloat(point[1]), parseFloat(point[0]));
    map.SetCenterAndZoom(coords, zoomLevel);
}

function MapDispose(){
    if (map!=null)
    {
        map.Dispose();
        map = null;
    }
}
function HandleEndPan(e)
{
   EndPanManager();
} 
function HandleChangeView(e)
{
   ChangeViewManager();
}


function DrawLine(objLayer, lngLat, lineWidth, lineColor, sTitle, sDesc, sIcon) {

    var coords = new Array();
    var tmpLngLat = new Array();

    tmpLngLat = LngLat.toString().split(',');

    for (i = 0; i < tmpLatLng.length - 1; i++) {
        try {
            var point = tmpLngLat[i].split(' ');
            var p2 = new VELatLong(0, 0);
            p2.Latitude = parseFloat(point[1]);
            p2.Longitude = parseFloat(point[0]);
            coords.push(p2);
        }
        catch (ex) {
            alert(ex.description);
        }
    }
    shape = new VEShape(VEShapeType.Polyline, coords);
    shape.HideIcon();
    shape.SetLineWidth(width);
    shape.SetLineColor(color);
    shape.SetFillColor(fillcolor);
    if (sIcon.length > 0) {
        shape.SetCustomIcon(sIcon);
    }
    if (sTitle.length > 0 || sDesc.length > 0) {
        shape.ShowIcon();
        shape.SetTitle(sTitle);
        shape.SetDescription(sDesc);
    }
    objLayer.AddShape(shape);
}

function DrawPolygon(objLayer,lngLat,lineWidth,lineColor,fillColor,sTitle,sDesc,sIcon) {

    var coords = new Array();
    var tmpLngLat = new Array();

    tmpLngLat = lngLat.toString().split(',');

    for (i = 0; i < tmpLngLat.length - 1; i++) {
        try {
            var point = tmpLngLat[i].split(' ');
            var p2 = new VELatLong(0, 0);
            p2.Latitude = parseFloat(point[1]);
            p2.Longitude = parseFloat(point[0]);
            coords.push(p2);
        }
        catch (ex) {
            alert(ex.description);
        }
    }
    
    shape = new VEShape(VEShapeType.Polygon, coords);
    
    shape.HideIcon();
    shape.SetLineWidth(lineWidth);
    shape.SetLineColor(lineColor);
    shape.SetFillColor(fillColor);

    if (sIcon.length > 0) {
        shape.SetCustomIcon(sIcon);
    }
    if(sTitle.length>0 || sDesc.length>0){
		shape.ShowIcon();
		shape.SetTitle(sTitle);
		shape.SetDescription(sDesc);
	}
	objLayer.AddShape(shape);
	
}


function DrawCircle(id, latin, lonin, radius, lineWidth, lineColor,fillColor,sTitle,sDesc,sIcon)
{
    var R = 6371; // earth's mean radius in km
    
    var lat = (parseFloat(latin)+0.1) * Math.PI/180.0; 
    var lon = (parseFloat(lonin)-0.1) * Math.PI/180.0;
    var d = parseFloat(radius)/R;  // d = angular distance covered on earth's surface
    var locs = new Array();
    
    for (x = 0; x <= 360; x+=10) 
    { 
        var p2 = new VELatLong(0,0)            
        brng = x * Math.PI / 180; //rad
        p2.Latitude = Math.asin(Math.sin(lat)*Math.cos(d) + Math.cos(lat)*Math.sin(d)*Math.cos(brng));
        p2.Longitude = ((lon + Math.atan2(Math.sin(brng)*Math.sin(d)*Math.cos(lat), Math.cos(d)-Math.sin(lat)*Math.sin(p2.Latitude))) * 180) / Math.PI;
        p2.Latitude = (p2.Latitude * 180) / Math.PI;
        locs.push(p2);
    }
    shape = new VEShape(VEShapeType.Polygon, locs);
    shape.HideIcon();
    shape.SetLineWidth(lineWidth);
    shape.SetLineColor(lineColor);
    shape.SetFillColor(fillColor);
    if(sIcon.length>0) {shape.SetCustomIcon(sIcon);}
    if(sTitle.length>0 || sDesc.length>0){
		shape.ShowIcon();
		shape.SetIconAnchor(new VELatLong(latin,lonin));
		shape.SetTitle(sTitle);
		shape.SetDescription(sDesc);
	}
    map.AddShape(shape);
}


function DrawPoint(objLayer, lngLat, sTitle, sDesc, sIcon) {

    var point = lngLat.split(' ');
    var coords = new VELatLong(parseFloat(point[1]), parseFloat(point[0]));
    var shape = new VEShape(VEShapeType.Pushpin, coords);
	shape.SetTitle(sTitle);
	shape.SetDescription(sDesc);
	if (sIcon.length > 0) {
	    shape.SetCustomIcon(sIcon);
	}
	if (sTitle.length > 0 || sDesc.length > 0) {
	    shape.ShowIcon();
	    shape.SetTitle(sTitle);
	    shape.SetDescription(sDesc);
	}
	objLayer.AddShape(shape)         
}

function ShowControl()
{
	map.ShowDashboard();
}
function HideControl()
{
	map.HideDashboard();
}
