﻿function hondaMapControl (mapID) {
    //properties
    this.id = mapID;
    this.map = null;
    this.points = new Array;
    this.pushpinLayer = null;
    this.directionsId = null;
    this.mapLoaded = false;
    
    //methods & functions
    this.load = LoadMap;
    this.addPoint = addPoint;
    this.setPoint = setPoint;
    this.clearPoints = clearPoints;
    this.getDirections = getDirections;
    this.onGotRoute = onGotRoute;
    
    //events
    this.pinCall = pinCall;
    this.onOut = MousedOut;
    this.onOver = MousedOver;
    this.onClick = MouseClick;
}

function mapPoint (id,name,address,city,state,zip,pushpin,layerContent) {
    // Values
    this.id = id;
    this.name = name;
    this.address = address;
    this.city = city;
    this.state = state;
    this.zip = zip;
    this.pushpin = pushpin;
    this.layerContent = layerContent;
    this.returnAddress = this.address + ", " + this.city + ", " + this.state + ", " + this.zip;
    this.Lat = '';
    this.Long = '';
}

// ------------------- Functions ------------------- //
function LoadMap() {
    try {
        this.map = new VEMap(this.id);
        this.map.LoadMap(null, null, null, null, VEMapMode.Mode2D, false);
        this.mapLoaded = true;
    }
    catch(VEException) {
       callError(VEException);
    }
}

function setPoint(point,callback, showResult) {
    try {
        var armen = this;
        result = this.map.Find(null,point.returnAddress,null, null,0,1,false,false,false,true,function(layer, resultsArray, places, hasMore, veErrorMessage) { armen.pinCall(layer, resultsArray, places, hasMore, veErrorMessage) });
    }
    catch(VEException) {
        callError(VEException);
    } 
    return null;
}

function pinCall(layer, resultsArray, places, hasMore, veErrorMessage) {   
    for ( var i = 0 ; i < this.points.length ; i++ ) {
        if( String(this.points[i].Lat).slice(0,5) == String(places[0].LatLong.Latitude).slice(0,5) && String(this.points[i].Long).slice(0,5) == String(places[0].LatLong.Longitude).slice(0,5) ) {
            var pushpin = new VEShape(VEShapeType.Pushpin, places[0].LatLong);
            pushpin.SetCustomIcon(this.points[i].pushpin);
            this.map.AddShape(pushpin);
        }
    }
}
function addPoint(name,address,city,state,zip,pushpin,layerContent,nsetPoint,Lat,Long) {
    var index = this.points.length;
    // If point name exists overwrite
    for (var i = 0; i < this.points.length; i++) {
        if (this.points[i].name == name) {
            index = i;
        }
    }
    var myPoint = new mapPoint(index,name, address, city, state, zip, pushpin, layerContent);
    myPoint.Long = Long;
    myPoint.Lat = Lat;
    this.points[index] = myPoint
    if (nsetPoint == true) { return this.setPoint(myPoint,null,true); }
    return this.points.length;
}

function clearPoints() {
    if(this.directionsId) { document.getElementById(this.directionsId).innerHTML = ''; }
    this.map.DeleteRoute();
    this.map.Clear();
    this.points = [];
    
}

function getDirections() {
    armen = this;
    var options = new VERouteOptions();
    options.RouteCallback = function(route) { armen.onGotRoute(route) };
    options.ShowDisambiguation = false;
    var directions = new Array();
    for (var i = 0; i < this.points.length; i++) {
        directions[i] = this.points[i].returnAddress
    }
    this.map.GetDirections(directions, options);
}

function onGotRoute(route) {
    // Unroll route
    var legs     = route.RouteLegs;
    var turns    = "<tr><td colspan='3'>Your Total Distance will be: " + route.Distance.toFixed(1) + " miles</td></tr>";
    var numTurns = 0;
    var leg      = 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 - 1; j ++) {
            turn = leg.Itinerary.Items[j];  // turn is a VERouteItineraryItem object
            numTurns++;

            turns += "<tr>";
            turns += "<td width='20'><b>" + numTurns + "</b></td>";
            turns += "<td width='388'>" + turn.Text + "</td>";
            turns += "<td width='45'>" + turn.Distance.toFixed(1) + " mi</td>";
            turns += "</tr>"
        }
    }
    var start = '';
            
    start += '<table width="452" border="0" cellpadding="0" cellspacing="0" class="tstyle">';
    start += '<tr>';
    start += '<td height="46" align="center" bgcolor="#e6e6e6"><img src="/images/certified-used/inventory-search/sicon.jpg" /></td>';
    start += '<td height="46" bgcolor="#e6e6e6">Start : <b>' + this.points[0].returnAddress + '</b></td>';
    start += '<td height="46" bgcolor="#e6e6e6">&nbsp;</td>';
    start += '</tr>';
    
    var end = '';
    
    end += '<tr>';
    end += '<td height="46" align="center" bgcolor="#e6e6e6"><img src="/images/certified-used/inventory-search/ricon.jpg" width="16" height="16" /></td>'
    end += '<td height="46" bgcolor="#e6e6e6">End : ' + this.points[this.points.length -1].name + '<br />';
    end += '<b>' + this.points[this.points.length -1].returnAddress + '</b></td>';
    end += '<td height="46" bgcolor="#e6e6e6">&nbsp;</td>';
    end += '</tr>';
    end += '</table>';
    document.getElementById(this.directionsId).innerHTML = start + turns + end;
    document.getElementById(this.directionsId).style.display = "block";
}

// ------------------- Errors ------------------- //
function callError(error) {
       alert(error.message + " : " + error.name + " : " + error.source);    
}

// ------------------- Event Triggers ------------------- //
function MousedOut() {
    alert('moused Off');           
}

function MousedOver() {
    alert('moused Over');
}

function MouseClick() {
    alert('Mouse Click');
}
