﻿	
//get the url
var url = "http://www.leicester.gov.uk/your-council-services/lc/sports-services/";

if (url.indexOf("?", 0) != -1) {
    var queryStringIndex = url.indexOf("?", 0);
    url = url.substring(0, queryStringIndex);
}
var map = null;
var points = [];
var shapes = [];
var center = null;

function LoadMap(latitude, longitude, onMapLoaded) {
    map = new VEMap("theMap");
    options = new VEMapOptions();
    options.EnableBirdseye = false;
    
    // Makes the control bar less obtrusize.
    map.SetDashboardSize(VEDashboardSize.Tiny);
    
    if (onMapLoaded != null)
        map.onLoadMap = onMapLoaded;

    if (latitude != null && longitude != null) {
        center = new VELatLong(latitude, longitude);
    }

    map.LoadMap(center, null, null, null, null, null, null, options);
}

function LoadPin(LL, name, description) {
    var shape = new VEShape(VEShapeType.Pushpin, LL);

    //Make a nice Pushpin shape with a title and description
    shape.SetTitle("<span class=\"pinTitle\"> " + escape(name) + "</span>");
    if (description !== undefined) {
        shape.SetDescription("<p class=\"pinDetails\">" + 
        escape(description) + "</p>");
    }
    map.AddShape(shape);
    points.push(LL);
    shapes.push(shape);
	
}

function FindCentresGivenLocation(where, radius) {
    map.Find("", where, null, null, null, null, null, false, null, null, callbackUpdateMapCentres);
}

function callbackUpdateMapCentres(layer, resultsArray, places, hasMore, VEErrorMessage) {

    clearMap();
    var center = map.GetCenter();
    var postData = "{ latitude:'" + center.Latitude + "',longitude:'" + center.Longitude + "',searchRadius:'50'}";

    $.ajax({
        contentType: "application/json; charset=utf-8",
        dataType: "jsonp",
        jsonp:"onJsonPLoad",
        url: "http://webdev-vm/SportsAdmin2010/WebServices/SportsWebServices.asmx/GetCentresNear"
    });
}

function onJsonPLoad(msg){
    alert("hi");
}

function clearMap() {
    map.Clear();
    points = [];
    shapes = [];
}

function showCentreMarkers(centres) {
    $.each(centres, function (i, centre) {
        var LL = new VELatLong(centre.latitude,
                                   centre.longitude, 0, null);

        // Add Pin to Map
        var centreDescription = "";
        centreDescription = "<ul><li>" + centre.address + "</li>";
        centreDescription += "<li>" + centre.postcode + "</li>";
        centreDescription += "<li>Telephone: " + centre.telephone + "</li>";
        centreDescription += "<li><a class=\"contactCentreLink\" href=\"http://www.leicester.gov.uk/EasySiteWeb/EasySite/StyleData/Style-sports2010/contactCentre.aspx?centreID=" + centre.centreID + "\">contact the centre</a></li>";
        centreDescription += "<li><a href=\"" + url + "centre-details/?centreID=" + centre.centreID + "\">view centre details</a></li></ul>";

        LoadPin(LL, "<a href=\"" + url + "centre-details/?centreID=" + centre.centreID + "\">" + centre.centreName + "</a>", centreDescription);

        //Add a centre to the drop down list
        $('#dd_CentresList').append($("<option value=\"" + centre.centreID + "\">" + centre.centreName + "</option>"));

    });

    // Adjust zoom to display all the pins we just added.
    map.SetMapView(points);
    map.SetZoomLevel(11);

    // Display the event's pin-bubble on hover.
    $("#dd_CentresList option").each(function (i, centre) {
        $(centre).hover(
            function () { map.ShowInfoBox(shapes[i]); },
            function () { map.HideInfoBox(shapes[i]); }
        );
    });
}



