﻿var map = null;
var Taiko = new VELatLong(44.455806, -73.217971);
var Rich = new VELatLong(44.40298124707744, -72.9951810562209);
var EastMont = new VELatLong(44.26464079316622, -72.50766);
var Location = null;
var pinPoint = null;
var pinPixel = null;
var driveFrom = 'from';
var driveTo = 'to';
var currentMap = null;
var currentDirection = null;
var currentAddress = null;

function GetMap(mapName) {
    map = new VEMap('myMap');

    if (mapName == '') {
        mapName = 'Burlington';
    }

    currentMap = mapName;

    switch (mapName) {
        case 'Burlington':
            Location = Taiko;
            document.getElementById('mapTitle').innerHTML = "Burlington Taiko Classes location";
            map.LoadMap(Taiko, 13, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 1);
            document.getElementById('classAddress').innerHTML = "BTG Dojo, 208 Flynn avenue, Burlington, VT 05401";
            break;
        case 'Rich':
            Location = Rich;
            document.getElementById('mapTitle').innerHTML = "Richmond Taiko Classes location";
            map.LoadMap(Rich, 13, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 1);
            document.getElementById('classAddress').innerHTML = "Richmond Free Library, 201 Bridge street, Richmond, VT 05477";
            break;
        case 'EastMont':
            Location = EastMont;
            document.getElementById('mapTitle').innerHTML = "East Montpelier Taiko Classes location";
            map.LoadMap(EastMont, 13, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 1);
            document.getElementById('classAddress').innerHTML = "AllTogetherNow! 170 Cherry Tree Hill Rd, East Montpelier, VT 05651";
            break;
        default:
            Location = Taiko;
            document.getElementById('mapTitle').innerHTML = "Burlington Taiko Classes location";
            map.LoadMap(Taiko, 13, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 1);
            document.getElementById('classAddress').innerHTML = "BTG Dojo, 208 Flynn avenue, Burlington, VT 05401";
    }

    //currentAddress = document.getElementById('classAddress').innerHTML;

    AddPin(mapName);
}


function AddPin(mapName) {
    
    var desc = null;
    var locationName = null;
    var locationAddress = null;
    
    switch (mapName) {
        case 'Burlington':
            Location = Taiko;
            locationName = "Burlington Taiko Group Dojo";
            locationAddress = "208 Flynn avenue, Burlington, VT 05401";
            break;
        case 'Rich':
            Location = Rich;
            locationName = "Richmond Free Library";
            locationAddress = "201 Bridge street, Richmond, VT 05477";
            break;
        case 'EastMont':
            Location = EastMont;
            locationName = "AllTogetherNow!";
            locationAddress = "170 Cherry Tree Hill Rd, East Montpelier, VT 05651";
            break;
        default:
            Location = Taiko;
            locationName = "Burlington Taiko Group Dojo";
            locationAddress = "208 Flynn avenue, Burlington, VT 05401";
    }

    currentAddress = locationAddress;
    
    // create the info box description
    desc = "<b>" + locationName + "</b>" + "<br/>";
    desc = desc + locationAddress + "<br/>" + "<br/>" + "<br/>";
    desc = desc + '<a href=' + "taiko_classes.aspx" + '>' + "Class information" + '</a>' + "<br/>";
    desc = desc + '<a href=' + "taiko_classes.aspx#schedule" + '>' + "Schedule" + '</a>' + "<br/>";
    desc = desc + '<a href=' + "teachers.aspx" + '>' + "Instructors" + '</a>' + "<br/>";
    desc = desc + '<a href=' + "pdfs/flyer.pdf" + '>' + "Registration" + '</a>';
//    desc = desc + "<br/>" + "<br/>" + "<br/>";
//    desc = desc + '<a href=' + "javascript:DriveFrom();" + '>' + "Drive from..." + '</a>';
//    desc = desc + " or " + '<a href=' + "javascript:DriveTo();" + '>' + "drive to..." + '</a>' + " " + locationAddress + ".";

    var pin = new VEShape(VEShapeType.Pushpin, Location);
    pin.SetDescription(desc);
    map.AddShape(pin);

}

function DriveFrom() {
    currentDirection = driveFrom;
    OpenDriving();
}

function DriveTo() {
    currentDirection = driveTo;
    OpenDriving();
}


function OpenDriving() {
    var urlDrive = 'DrivingDirections.htm' + "?location=" + currentMap + "&direction=" + currentDirection + "&addr=" + currentAddress;
    popupWin = window.open(urlDrive,
                       'open_window',
                       'menubar=yes, toolbar=yes, location=yes, directories=no, status=yes, scrollbars=yes, resizable=yes, dependent, width=640, height=640');
}


function loadDirectionMap() {
    var currentMap = getUrlParamValue('location');
    var driveDirection = getUrlParamValue('direction');
    var currentAddress = getUrlParamValue('addr');
    var showAddress = null;

    if (driveDirection == driveTo) {
        showAddress = document.getElementById('txtTo');
    }
    else {
        showAddress = document.getElementById('txtFrom');
    }

    showAddress.value = currentAddress;
    
    GetMap(currentMap);

}

function getUrlParamValue(strParamName) {

    var strReturn = "";
    var strHref = window.location.href;
    if (strHref.indexOf("?") > -1) {
        var strQueryString = strHref.substr(strHref.indexOf("?"));
        var aQueryString = strQueryString.split("&");
        for (var iParam = 0; iParam < aQueryString.length; iParam++) {
            if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1) {
                var aParam = aQueryString[iParam].split("=");
                strReturn = aParam[1];
                break;
            }
        }
    }
    return unescape(strReturn);
}

function getOnRoute() {

    var addressTo = document.getElementById('txtTo');
    var addressFrom = document.getElementById('txtFrom');
    var options = new VERouteOptions();
    options.RouteCallback = onGotRoute;
    map.GetDirections([addressFrom.value, addressTo.value], options);
}

function onGotRoute(route) {
    // Unroll route
    var legs = route.RouteLegs;
    var turns = "<h3>" + "Total distance: " + route.Distance.toFixed(1) + " mi" + "</h3></br></br>";
    var numTurns = 0;
    var leg = null;
    var numberText = null;
    
    // Get intermediate legs
    for (var i = 0; i < legs.length; i++) {
        // Get this leg so we don't have to derefernce multiple times
        leg = legs[i];  // Leg is a VERouteLeg object

        // Unroll each intermediate leg
        var turn = null;  // The itinerary leg

        for (var j = 0; j < leg.Itinerary.Items.length; j++) {
            turn = leg.Itinerary.Items[j];  // turn is a VERouteItineraryItem object
            switch(numTurns)  
            {
            case 0:
                numberText = "Start: ";
                break;
            case (leg.Itinerary.Items.length - 1):
                numberText = "End: ";
                break;
             default:
                 numberText = numTurns + ".\t";
            }
            turns += "<span class='routeNumber'>" + numberText + "</span>" + turn.Text + " (" + turn.Distance.toFixed(1) + " mi)\n" + "</br>";
            numTurns++;
        }
    }

    var showRoute = document.getElementById('route');
    showRoute.innerHTML = turns;
}


		
