﻿/// <reference path="jquery-1.2.6.js" />

//______________Used for CustomerForm to find their postcode can delete when we used real postcode system.___________
function FindLongLat(where) {
    var numberOfResults = 20;
    var setBestMapView = true;
    var showResults = true;
    
    if (map == null) {
        map = new VEMap('theMap');
        map.LoadMap();
    }
    
    map.Find("", where, null, null, null,
           numberOfResults, showResults, true, true,
           setBestMapView, callbackForLongLat);
}

function callbackForLongLat(layer, resultsArray, places, hasMore, VEErrorMessage) {
    clearMap();
    if (places == null)
        return;
    //Make a pushpin for each place we find
    $.each(places, function(i, item) {
        var LL = new VELatLong(item.LatLong.Latitude, item.LatLong.Longitude);
        points.push(LL);
    });
    //If we've found exactly one place, that's our addresses.
    if (points.length === 1) {
        $("#Latitude").val(points[0].Latitude);
        $("#Longitude").val(points[0].Longitude);
        
    }
}

//_______________________________Used for EngineerDetails and edit and HomePage_________________________________
function FindAddressOnMap(where) {
    var numberOfResults = 20;
    var setBestMapView = true;
    var showResults = true;

    map.Find("", where, null, null, null,
           numberOfResults, showResults, true, true,
           setBestMapView, callbackForLocation);
}

function callbackForLocation(layer, resultsArray, places, hasMore, VEErrorMessage) {
    
    clearMap();
    if (places == null)
        return;

    //Make a pushpin for each place we find
    $.each(places, function(i, item) {
        var description = "";
        if (item.Description !== undefined) {
            description = item.Description;
        }
        var LL = new VELatLong(item.LatLong.Latitude, item.LatLong.Longitude);
        LoadPin(LL, item.Name);
    });
    //This updates the vlue on the webpage for HomePage and EngineerDetails otherwise the blur events don't work
    if (points.length === 1) {
        $("#Latitude").val(points[0].Latitude);
        $("#Longitude").val(points[0].Longitude);

        $("#EngineersProfile_Latitude").val(points[0].Latitude);
        $("#EngineersProfile_Longitude").val(points[0].Longitude);

    }
}


//_____________________________________________________________________________________________________________
var map = null;
var points = [];
var shapes = [];
var center = null;

function findFuelEngineers(fuel) {
    clearMap();
    //var center = map.GetCenter();
    $.post("/Admin/FindFuelEngineers", { fuel : fuel },
    function(dinners) {
        $.each(dinners, function(i, dinner) {
            var LL = new VELatLong(dinner.Latitude, dinner.Longitude, 0, null);
            LoadPin(LL, '<a href="/Admin/EngineerDetails/' + dinner.EngineerID + '">' + dinner.CompanyName + '</a>');
            DrawRadius(LL, dinner.ServiceRadius);
        });
        // Adjust zoom to display all the radius we just added.
        if (points.length > 1) {
            map.SetMapView(shapes);
        }
        // Display the event's pin-bubble on hover.
        $(".dinnerItem").each(function(i, dinner) {
            $(dinner).hover(
                function() { map.ShowInfoBox(shapes[i]); },
                function() { map.HideInfoBox(shapes[i]); }
            );
        });
    }, "json");
    
}

function DrawRadius(origin, radius) {
    var earthRadius = 6371;
    //latitude in radians
    var lat = (origin.Latitude * Math.PI) / 180;
    //longitude in radians
    var lon = (origin.Longitude * Math.PI) / 180;
    //angular distance covered on earth's surface
    var d = parseFloat(radius) / earthRadius;
    var points = new Array();
    for (i = 0; i <= 360; i++) {
        var point = new VELatLong(0, 0)
        var bearing = i * Math.PI / 180; //rad
        point.Latitude = Math.asin(Math.sin(lat) * Math.cos(d) +
      Math.cos(lat) * Math.sin(d) * Math.cos(bearing));
        point.Longitude = ((lon + Math.atan2(Math.sin(bearing) * Math.sin(d) * Math.cos(lat),
      Math.cos(d) - Math.sin(lat) * Math.sin(point.Latitude))) * 180) / Math.PI;
        point.Latitude = (point.Latitude * 180) / Math.PI;
        points.push(point);
    }
    var circle = new VEShape(VEShapeType.Polygon, points);
    circle.SetFillColor(new VEColor(250, 230, 65, 0.3));
    circle.SetLineColor(new VEColor(135, 135, 135, 0.8));
    circle.SetLineWidth(1);
    circle.HideIcon();
    map.AddShape(circle);
    shapes.push(circle);
}

function LoadPin(LL, name) {
    var shape = new VEShape(VEShapeType.Pushpin, LL);
    shape.SetCustomIcon("<div class='PinStyleOne'><div class='text'></div></div>");
    shape.SetTitle("<span class=\"pinTitle\"> " + escape(name) + "</span>");
    map.AddShape(shape);
    points.push(LL);
}

function clearMap() {
    //map.Clear();
    map.DeleteAllShapes();
    map.DeleteRoute();
    map.DeleteAllShapeLayers();

    points = [];
    shapes = [];
}

function ShowPinAndRaduis(center, title, radius) {
    clearMap();
    LoadPin(center, title);
    DrawRadius(center, radius);
    map.SetMapView(shapes);
}

function LoadMap(latitude, longitude, onMapLoaded) {

    map = new VEMap('theMap');
    options = new VEMapOptions();
    options.EnableBirdseye = false;
    zoom = 6;
    map.SetDashboardSize(VEDashboardSize.Small);

    if (onMapLoaded != null)
        map.onLoadMap = onMapLoaded;

    if (latitude != null && longitude != null) {
        center = new VELatLong(latitude, longitude);

    }

    map.LoadMap(center, zoom, null, null, null, null, null, options);
}