﻿/// <reference path="global.js" />

function AddTileLayer(tileSourceId, tileSource, numServers, bounds, minZoom, maxZoom, getTilePath, opacity, zindex) {
    ///<summary>If layer doesn't already exist, Add tile layer.</summary>
    ///<param name="tileSourceId">The ID of the tileSource</param>
    ///<param name="tileSource">The URL source</param>
    ///<param name="numServers">The number of servers to load balance</param>
    ///<param name="bounds">Top-Left and Bottom-Right corners of the layer</param>
    ///<param name="minZoom">The minimum zoom level available</param>
    ///<param name="maxZoom">The minimum zoom level available</param>
    ///<param name="getTilePath">The javascript function defining tilenames</param>
    ///<param name="opacity">0-1 opacity with 0 being transparent and 1 being opaque</param>
    ///<param name="zindex">The z-index of the layer</param>
    ///<returns>null</returns>
    if (GetTileLayerByID(tileSourceId) == null) {
        map.AddTileLayer(new VETileSourceSpecification(tileSourceId, tileSource, numServers, bounds, minZoom, maxZoom, getTilePath, opacity, zindex), true);
    } else {
        map.ShowTileLayer(tileSourceId);
    }
}
function RemoveTileLayer(tileSourceId) {
    ///<summary>If layer exists, Remove layer</summary>
    ///<returns>null</returns>
    if (GetTileLayerByID(tileSourceId) != null) {
        map.DeleteTileLayer(tileSourceId);
    }
}
function ShowTileLayer(tileSourceId) {
    ///<summary>If layer exists, Show layer</summary>
    ///<returns>null</returns>
    if (GetTileLayerByID(tileSourceId) != null) {
        map.ShowTileLayer(tileSourceId);
    }
}
function HideTileLayer(tileSourceId) {
    ///<summary>If layer exists, Hide layer</summary>
    ///<returns>null</returns>
    if (GetTileLayerByID(tileSourceId) != null) {
        map.HideTileLayer(tileSourceId);
    }
}

function GetTileLayerByID(tileSourceId) {
    ///<summary>Get tile layer by ID</summary>
    ///<returns>Tile layer</returns>
    
    return map.GetTileLayerByID(tileSourceId);
}
function AddLayer(title, description) {
    ///<summary>Add a Layer to the map.</summary>
    ///<returns>Tile layer</returns>
    var layer = new VEShapeLayer();
    layer.SetTitle(title);
    layer.SetDescription(description);
    map.AddShapeLayer(layer);
    return layer;
}
function RemoveLayer(layer) {
    ///<summary>Remove a Layer from the map.</summary>
    ///<returns>null</returns>
    if (layer != null) {
        layer.DeleteAllShapes();
        map.DeleteShapeLayer(layer);
    }
}
function FormatDescription(description) {
    ///<summary>Format the description of the pushpins/polygons/polylines. Not fully implemented returns description that was passed in.</summary>
    ///<returns>Not fully implemented returns description that was passed in.</returns>
    return description;
}
function AddPushpinToLayer(layer, latLong, icon, draggable, title, description) {
    ///<summary>Add a Pushpin to the layer.</summary>
    ///<returns>Pushpin</returns>
    var shape = new VEShape(VEShapeType.Pushpin, latLong);
    shape.Draggable = draggable;
    shape.SetTitle(title);
    shape.SetDescription(FormatDescription(description));
    shape.SetCustomIcon(icon);
    shape.ShowIcon();
    layer.AddShape(shape);
    return shape;
}
function MovePushpin(shape, latLon) {
    ///<summary>Moves the Pushpin </summary>
    ///<returns>null</returns>
    shape.SetPoints(latLon);
}
function SetPushpinCustomIcon(shape, icon) {
    ///<summary>Sets the Pushpin custom icon</summary>
    ///<returns>null</returns>
    shape.SetCustomIcon(icon);
}
function ShowPushpin(shape, value) {
    ///<summary>Shows the Pushpin</summary>
    ///<returns>null</returns>
    if (value) { shape.Show(); }
    else { shape.Hide(); }
}
function AddPolyLine(layer, points, color) {
    ///<summary>Add a polyline to the layer.</summary>
    ///<returns>polyline</returns>
    var line = new VEShape(VEShapeType.Polyline, points);
    line.HideIcon();
    line.SetLineColor(color);
    layer.AddShape(line);
    return line;
}
function AddPoint(line, latLon, index) {
    ///<summary>Add a point to the PolyLine at index "index" or the end if index==null or index<0.</summary>
    ///<returns>null</returns>
    var points = line.GetPoints();
    if (index == null || index < 0) {
        points[points.length] = latLon;
    } else {
        points.splice(index, 0, latLon);
    }
    line.SetPoints(points);
}
function RemovePoint(line, index) {
    ///<summary>Remove a point from the PolyLine at index "index" if index!=null and index>0.</summary>
    ///<returns>Boolean indicating success</returns>
    if (line == null) { return false; }
    if (index == null) { return false; }
    if (index < 0) { return false; }
    var points = line.GetPoints();
    points.splice(index, 1);
    if (points.length > 1) {
        line.SetPoints(points);
        return true;
    }
    return false;
}
function MovePoint(line, latLon, index) {
    ///<summary>Move a point from the PolyLine at index "index".</summary>
    ///<returns>Boolean indicating success</returns>
    if (line == null) { return false; }
    if (index == null) { return false; }
    if (index < 0) { return false; }
    var points = line.GetPoints();
    points[index] = latLon;
    if (points.length > 1) {
        line.SetPoints(points);
        return true;
    }
    return false;
}
function AddCircleToLayer(layer, lat, lon, radius, lineColor, fillColor) {
    ///<summary>Add a circle to the layer.</summary>
    ///<returns>shape</returns>
    if (lineColor == null) { lineColor = new VEColor(0, 0, 0, 0) }
    if (fillColor == null) { fillColor = new VEColor(255, 128, 128, 0.5) }
    var shape = GetCircle(lat, lon, radius);
    shape.HideIcon();
    shape.SetLineColor(lineColor);
    shape.SetFillColor(fillColor);
    shape.Draggable = false;
    layer.AddShape(shape);
    return shape;
}
function GetCircle(latin, lonin, radius) {
    ///<summary>Returns a VEShape polygon with 36 points arranged in a circular pattern.</summary>
    ///<returns>VEShape circle</returns>
    var locs = new Array();
    var lat1 = latin * Math.PI / 180.0;
    var lon1 = lonin * Math.PI / 180.0;
    var d = radius / 3956;
    var x;
    for (x = 0; x <= 360; x += 10) {
        var tc = (x / 90) * Math.PI / 2;
        var lat = Math.asin(Math.sin(lat1) * Math.cos(d) + Math.cos(lat1) * Math.sin(d) * Math.cos(tc));
        lat = 180.0 * lat / Math.PI;
        var lon;
        if (Math.cos(lat1) == 0) {
            lon = lonin;
        }
        else {
            lon = ((lon1 - Math.asin(Math.sin(tc) * Math.sin(d) / Math.cos(lat1)) + Math.PI) % (2 * Math.PI)) - Math.PI;
        }
        lon = 180.0 * lon / Math.PI;
        var loc = new VELatLong(lat, lon);
        locs.push(loc);
    }
    var circle = new VEShape(VEShapeType.Polygon, locs);
    return circle;
}
function TBtoColor(tbID) {
    ///<summary>Returns a VEColor from a textbox with the ID tbID. textbox should contain 6 digit hex value for the color.</summary>
    ///<returns>VEColor</returns>
    return HextoColor(document.getElementById(tbID).value);
}
function HextoColor(hex) {
    ///<summary>Converts a 6 digit hex string to a VEColor</summary>
    ///<returns>VEColor</returns>
    return new VEColor(parseInt("0x" + hex.substring(0, 2)),
                       parseInt("0x" + hex.substring(2, 4)),
                       parseInt("0x" + hex.substring(4, 6)),
                       0.5);
}
function Rectangle(maxLat, minLon, minLat, maxLon) {
    ///<summary>Creates a VELatLongRectangle</summary>
    ///<returns>VELatLongRectangle</returns>
    return [new VELatLongRectangle(new VELatLong(maxLat, minLon), new VELatLong(minLat, maxLon))];
}
