﻿


function $$(id, context) {
    var el = $("#" + id, context);
    if (el.length < 1)
        el = $("[id$=_" + id + "]", context);
    return el;
}
var stsFlag = "pause";
var d = document;
function switchPurpose() {
    //alert($$("btnPlay").attr("id"));
    // var elmnt = d.getElementById("<%= btnPlay.ClientID %>");
    if (stsFlag == "pause") {
        stsFlag = "play";
        //$('#<%= btnPlay.ClientID %>').removeClass('btn_pause').addClass('btn_play');
        $$("btnPlay").removeClass('btn_pause').addClass('btn_play');
    }
    else if (stsFlag == "play") {
        stsFlag = "pause";
        //$('#<%= btnPlay.ClientID %>').removeClass('btn_play').addClass('btn_pause');
        $$("btnPlay").removeClass('btn_play').addClass('btn_pause');
    }
}
//
// this is for course description
//

function onGetHoleSuccess(result) {
    $.each(result, function (index, value) {
        $$("lblHoleYdg" + $.trim(value.TeeColor)).html(value.Yardage);
    });
    $$("divHoleNumber").text(result[0].HoleNumber);
    $$("divHolePar").text(String.format('Par {0} Handicap {1}', result[0].Par, result[0].Hcp));
    //$$("lblHole").html(String.format('<h3>{0}</h3>Par {1} Handicap {2}', result[0].HoleNumber, result[0].Par, result[0].Hcp));
    //$$("lblHoleYdg").html(result[0].Yardage + "yards");
    //alert("inside onSuccess.  Yardage=" + result[0].Yardage);
    $$("pnlHoles").removeClass('hide').addClass('show');
}
function showHoleDetail(arg) {

    PageMethods.GetProTip(arg, function (result) { $$("divProTip").text(result[0].ProTip); }, function () { alert('GetTip Failed!'); });
    PageMethods.GetHole(arg, onGetHoleSuccess, function () { alert('GetHole Failed!'); });
}
function hideHoleDetail() {
    $$("pnlHoles").removeClass('show').addClass('hide');
}
//
//
// All this is to get a map
//
//
var map = null;

function GetMap() {
    // Initialize the map
    map = new Microsoft.Maps.Map(document.getElementById("mapDiv"),
              { credentials: "ArftDjDpRtSXV8OAsSCkU3XAjc_gCLKHQz1pSEE4SgtqbI5baM0Rvx9TviUBLugP", mapTypeId: "r" }
              );

}

function LoadMapClick() {
    alert("Inside LoadMapClick()");
    GetMap();
    map.getCredentials(MakeGeocodeRequest);
}


function MakeGeocodeRequest(credentials) {

    var geocodeRequest = "http://dev.virtualearth.net/REST/v1/Locations/" + "257 South Smith Road, LaGrange, GA" + "?output=json&jsonp=GeocodeCallback&key=" + credentials;

    CallRestService(geocodeRequest);
}

function GeocodeCallback(result) {
    //alert("Found location: " + result.resourceSets[0].resources[0].name);

    if (result &&
                   result.resourceSets &&
                   result.resourceSets.length > 0 &&
                   result.resourceSets[0].resources &&
                   result.resourceSets[0].resources.length > 0) {
        // Set the map view using the returned bounding box
        var bbox = result.resourceSets[0].resources[0].bbox;
        var viewBoundaries = Microsoft.Maps.LocationRect.fromLocations(new Microsoft.Maps.Location(bbox[0], bbox[1]), new Microsoft.Maps.Location(bbox[2], bbox[3]));
        map.setView({ bounds: viewBoundaries });

        // Add a pushpin at the found location
        var location = new Microsoft.Maps.Location(result.resourceSets[0].resources[0].point.coordinates[0], result.resourceSets[0].resources[0].point.coordinates[1]);
        var pushpin = new Microsoft.Maps.Pushpin(location);
        map.entities.push(pushpin);
    }
}

function ClickRoute(credentials) {
    map.getCredentials(MakeRouteRequest);
}


function MakeRouteRequest(credentials) {
    var routeStart = "156 Maple Trace, Pine Mountain, GA";
    var routeEnd = "257 South Smith Road, LaGrange, GA";
    if ($$("txtMapStart").val() != null) {
        routeStart = $$("txtMapStart").val();
        //alert("txtMapStart.Val: " + routeStart);
    }
    var routeRequest = "http://dev.virtualearth.net/REST/v1/Routes?wp.0=" + routeStart + "&wp.1=" + routeEnd + "&routePathOutput=Points&output=json&jsonp=RouteCallback&key=" + credentials;

    CallRestService(routeRequest);

}


function RouteCallback(result) {


    if (result &&
                   result.resourceSets &&
                   result.resourceSets.length > 0 &&
                   result.resourceSets[0].resources &&
                   result.resourceSets[0].resources.length > 0) {

        // Set the map view
        var bbox = result.resourceSets[0].resources[0].bbox;
        var viewBoundaries = Microsoft.Maps.LocationRect.fromLocations(new Microsoft.Maps.Location(bbox[0], bbox[1]), new Microsoft.Maps.Location(bbox[2], bbox[3]));
        map.setView({ bounds: viewBoundaries });


        // Draw the route
        var routeline = result.resourceSets[0].resources[0].routePath.line;
        var routepoints = new Array();

        for (var i = 0; i < routeline.coordinates.length; i++) {

            routepoints[i] = new Microsoft.Maps.Location(routeline.coordinates[i][0], routeline.coordinates[i][1]);
        }


        // Draw the route on the map
        var routeshape = new Microsoft.Maps.Polyline(routepoints, { strokeColor: new Microsoft.Maps.Color(200, 0, 0, 200) });
        map.entities.push(routeshape);

    }
}


function CallRestService(request) {
    var script = document.createElement("script");
    script.setAttribute("type", "text/javascript");
    script.setAttribute("src", request);
    document.body.appendChild(script);
}

//
// no more map stuff
//
