﻿var showLayer;
//var coffeeLayer = new VEShapeLayer();
//var restaurantLayer = new VEShapeLayer();
//var supermarketLayer = new VEShapeLayer();
//var petstoreLayer = new VEShapeLayer();
//var barLayer = new VEShapeLayer();

var resultDiv;

var zoom = 13;

function Map() 
{

    map = new VEMap('map-category');
    var mapOptions = new VEMapOptions();
    mapOptions.EnableBirdseye = false;
    map.LoadMap(new VELatLong(property.Latitude, property.Longitude), zoom, VEMapStyle.Road, false, VEMapMode.Mode2D, false, 0, mapOptions); // var 'zoom','mapData' is set in taxonomy-corporate-housing.php
    map.ClearInfoBoxStyles();
    map.AttachEvent("onclick", ShowInfo);
    map.AttachEvent('onmouseover', function(e) { if(e.elementID) return true; });
    CreateHomePin();
}

function findNearBy(what)
{
    try {
        //showLayer = getLayer(what);
        results = map.Find(what,
                            new VELatLong(property.Latitude, property.Longitude),
                            VEFindType.Businesses,
                            showLayer,
                            1,
                            20,
                            true,
                            true,
                            true,
                            true,
                            ShowResults);
        //index = parseInt(index) + 9;
    }
    catch (e) {
        alert(e.message);
    }

}

function ShowResults(layer, resultsArray, places, hasMore, veErrorMessage) {
    //var resultList = "<ul>";
    for (var i = 0; i < resultsArray.length; i++) {
        resultsArray[i].Shape.SetTitle('<h2 class="map-title">' + resultsArray[i].Name + '</h2>');    
        resultsArray[i].Shape.SetCustomIcon('<span><img src="/images/search-pin-green.png" /><label class="pushpin">' + (i + 1).toString() + '</label></span>');
        resultsArray[i].Shape.SetDescription(GetResultHTML(resultsArray[i]));
        //resultList += "<li>" + resultsArray[i].Name + "<br />" + resultsArray[i].Phone + "</li>";
    }
    //resultList += "</ul>";
    //resultDiv.html(resultList);
    //map.AddShapeLayer(layer);
}

function CreateHomePin()
 {
    var pin = new VEShape(VEShapeType.Pushpin, new VELatLong(property.Latitude, property.Longitude));
    pin.SetTitle('<h2 class="map-title">' + property.Name + '</h2>');
    
    pin.SetDescription(GetPinHTML());
    pin.SetCustomIcon('<span><img src="/images/search-pin-blue.png" /><label class="pushpin"></label></span>');
    map.AddShape(pin);
 }
             
function GetPinHTML()
{
    var html = '<div class="search-hover-box">';
    html+='<img class="map-thumb" src="/images/fetch.aspx?id=' + property.ImageId + '&width=108&height=91" width="" height="" alt="" />';
    
    html+='<p class="map-description">' + property.Address + '<br />';
    html+=property.City + ', ' + property.State + ' ' + property.Zip + '<br />';
     if(property.RentalPhone != null && property.RentalPhone.length > 0)
        html+=FormatPhone(property.RentalPhone) + '<br /><br />';
        
    html+='<a href="/property/?id=' + property.Id + '" class="blue">see details</a></p>';
    
    html+='<a href="#" class="map-schedule-tour">Schedule a tour</a>';
    
    html+='</div>';
    return html;
}

function GetResultHTML(result)
{
    var html = '<div class="search-hover-box">';
    
    html+='<p class="map-description2">' + result.Description + '<br />';
     if(result.Phone != null && result.Phone.length > 0)
        html+=result.Phone + '<br /><br />';
    
    html+='</div>';
    return html;
}
            
function FormatPhone(number)
{
    if(number != null)
    {
        return '(' + number.substring(0, 3) + ') ' + number.substring(3,6) + '-' + number.substring(6,10);
    }
}

function ShowInfo(e)
            {
                if(e.elementID != null)
                {
                    var shape = map.GetShapeByID(e.elementID); 
                    if(shape) 
                    {
                        var pixel = new VEPixel(e.mapX, e.mapY);
                        var ll = map.PixelToLatLong(pixel);
                        map.ShowInfoBox(shape, ll, new VEPixel(20, 20));
                        $(".customInfoBox-body").attr('style', 'display:block');
                    }


                }
            }

function showNeighborhood()
{
    Map();
}

$(document).ready(function () {
    $("#results input").live('click', function (e) {
        //e.preventDefault();
        map.DeleteAllShapeLayers();
        CreateHomePin()
        //if(resultDiv != null)
        //    resultDiv.html("");
        //resultDiv = $(this).parent().siblings(".search-results")
        findNearBy($(this).attr("search-param"));
    });
});



