﻿var propertyMap = null;

//Display the map on the page
function GetMap()
    {
        propertyMap = new VEMap('propertyMap');
        propertyMap.LoadMap();
        onGeocodeLoad();
        //propertyMap.ClearInfoBoxStyles();
     }

function UnloadMap()
{
if (propertyMap != null)
    {
        propertyMap.Dispose();
    }
}

//Find a location address
function StartGeocoding(what, address)
{
    propertyMap.Find(what,  //what
                      address,    //where
                      null,    // VEFindType (always VEFindType.Businesses)
                      null,    // VEShapeLayer (base by default)
                      null,    // start index for results (0 by default)
                      null,    // max number of results (default is 10)
                      null,    // show results? (default is true)
                      null,    // create pushpin for what results? (ignored since what is null)
                      null,    // use default disambiguation? (default is true)
                      null,    // set best map view? (default is true)
                      GeocodeCallBack); // call back function
}
 
function onGeocodeLoad()
{
   propertyMap.Clear();
   address = document.getElementById("ctl00_primaryContent_lblPropertyAddress").innerText + " " + document.getElementById("ctl00_primaryContent_lblPropertyCity").innerText;
    
   StartGeocoding(null, address);
}

function onGeocodeNearby(what)
{   
    propertyMap.Clear();
    address = document.getElementById("ctl00_primaryContent_lblPropertyAddress").innerText + " " + document.getElementById("ctl00_primaryContent_lblPropertyCity").innerText;
  
    StartGeocoding(what, address);
}

function onGeocodeOther()
{   
    propertyMap.Clear();
    address = document.getElementById("ctl00_primaryContent_lblPropertyAddress").innerText + " " + document.getElementById("ctl00_primaryContent_lblPropertyCity").innerText;
    what = document.getElementById("ctl00$secondaryContent$txtMapOther").value;
  
    StartGeocoding(what, address);
}

//Return GeocodeCallback information
function GeocodeCallBack(shapeLayer, findResults, places, moreResults, errorMsg)
{   
    //if no results show an error
    if(places == null)
    {
        alert((errorMsg == null) ? "There were no results" : errorMsg );
        return;
    }
    
    propertyMap.SetZoomLevel(15);

    var bestPlace = places[0];

    // Add pushpin to the *best* place
    var location = bestPlace.LatLong;

    var shape = new VEShape(VEShapeType.Pushpin, location);

    var icon = new VECustomIconSpecification();
    icon.Image = "Images/MapIcons/MapPropertyIcon.png";
    icon.ImageOffset= "Images/MapIcons/MapPropertyIcon.png";
    icon.ImageOffset = new VEPixel(0, -20);

    var photo = "<img src='Images/tempApartmentThumbnail.jpg' alt='Property Preview' border='1' />";
    address = document.getElementById("ctl00_primaryContent_lblPropertyAddress").innerText + "<br />" + document.getElementById("ctl00_primaryContent_lblPropertyCity").innerText + "<br />" + document.getElementById("ctl00_primaryContent_lblPropertyPhone").innerText + "<br /><br />" + photo;

    shape.SetTitle("<h5>" + document.getElementById("ctl00_primaryContent_lblPropertyName").innerText + "</h5>");
    shape.SetDescription(address);
    shape.SetCustomIcon(icon);

    propertyMap.AddShape(shape);
}
 
//Function to display the traffic on the site
 function ShowTraffic()
 {    
    propertyMap.LoadTraffic(true);
    propertyMap.ShowTrafficLegend(150,50);
    propertyMap.SetTrafficLegendText("The traffic legend");
 }
 
 function ShowLocation()
 {
     //Add a pushpin to the new layer
    shape = new VEShape(VEShapeType.Pushpin, propertyMap.GetCenter());
    shape.SetTitle('<h5>My pushpin</h5>');
    shape.SetDescription('This is a pushpin.');
    propertyMap.AddShape(shape);
 }  