﻿/// <reference path="~/VOMaps.aspx" />

var points = new Array();
var isMapLoaded = false;
var pushpinShapes = new Array();

//<mapSettings idxImg="" lat="39.595591" lon="-104.769623733333" minLat="39.565933" minLon="-104.977515" maxLat="39.638226" maxLon="-104.680416" />
/* DEFINE LISTING CLASS/FUNCTION */
function listingInfo(markerIndex, address, ln, lat, lon, beds, baths, price, pic, remarks, link) {
    // Set the local vars
    this.markerIndex = markerIndex;
    this.address = address;
    this.ln = ln;
    this.lat = lat;
    this.lon = lon;
    this.beds = beds;
    this.baths = baths;
    this.price = price;
    this.pic = pic;
    this.remarks = remarks;
    this.link = link;
}
function mapSettings(idxImg, lat, lon, minLat, minLon, maxLat, maxLon) {
    this.idxImg = idxImg;
    this.lat = lat;
    this.lon = lon;
    this.minLat = minLat;
    this.minLon = minLon;
    this.maxLat = maxLat;
    this.maxLon = maxLon;
}

function AddPushpin(ID, Lat, Long, IconURL, Title, Notes, Link) {
    var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(Lat, Long));
    var myIcon = new VECustomIconSpecification();
    var pinIcon = "";
    //Set the icon
    shape.SetCustomIcon(IconURL);
    myIcon.Image = IconURL;
    myIcon.TextContent = (pushpinShapes.length + 1);
    myIcon.TextSize = 5;
    myIcon.TextBold = true;

    //Set the info box
    shape.SetTitle(Title);
    shape.SetDescription(Notes);
    shape.SetCustomIcon("<div style=\"cursor:hand; position:relative;background-color:width: 32px; height: 25px;color:#990000; font-size: 14px; font-weight: bolder;text-align: left; vertical-align:middle;top:-20px\"><img src=images/home_green_32.png\><div style=\"position: relative;font-size: 14px; font-weight: bolder;top: -19px;padding-right: 5px;\">" + (pushpinShapes.length + 1) + "</div></div>");
    pushpinShapes[pushpinShapes.length] = shape;

    //Add the shape the the map
    map.AddShape(shape);
}

function checkBirdseye() {
    //Change the map style to Birdseye
    if (map.IsBirdseyeAvailable()) {
        map.SetMapStyle(VEMapStyle.Birdseye);
    } else {
        //Change the map style to Raod (Other Options: VEMapStyle.Road, VEMapStyle.Aerial, VEMapStyle.Hybrid, VEMapStyle.Oblique, and VEMapStyle.Birdseye)
        map.SetMapStyle(VEMapStyle.Road);
    }
}

/* Returns a VEFindResult Object */
function GeoCode(Address, City, State, Zip) {
    var nextPage = false;
    try {
        map.Find(null, Address + ", " + City + ", " + State + " " + Zip, 1,
			fnCallback);
    } catch (err) {
        alert(err.source + ": " + err.message);
    }
}

function fnCallback(findResults) {
    try {
        /* The Find Method returns a collection of VEFindResult Objects - http://dev.live.com/virtualearth/sdk/Ref/HTML/T_Namespace_VEFindResult.htm */
        if (findResults.length > 0) {
            /* return the first result */
            return findResults[0];
        }
    } catch (err) {
        alert(err.source + ": " + err.message);
    }
}

function loadMapMarkers() {
    objMapArr = new Array()
    var marker;
    var point;
    var endDate;
    var endTime;
    var lastPropertyRowIndex = 0;
    var mapSettingsRowIndex = 0;
    var fields = new Array();


    objDate = new Date();
    startTime = objDate.getTime();

    //Load property data into the objMarkers array
    if (!objMarkers || objMarkers.length < 1) {
        objMarkers = new Array();

        // Set the default mapSettingsRowIndex
        mapSettingsRowIndex = strRows.length - 1;
        // The lastPropertyRowIndex is "one up" from the map settings
        lastPropertyRowIndex = mapSettingsRowIndex - 1;

        // Load the settings - FROM THE mapSettings ROW
        strFields = strRows[mapSettingsRowIndex].split("|");
        try {
            objMapSettings = new mapSettings(strFields[0], strFields[1], strFields[2], strFields[3], strFields[4], strFields[5], strFields[6]);
        } catch (ex) {
            if (isDebug) { debugger; throw ex; }
            // Do Nothing!
        }
        // Add 1 to the index to get the item count
        listingCount = lastPropertyRowIndex + 1;
        var tempCount = 0.0;
        for (var i = 0; i <= lastPropertyRowIndex; i++) {
            strFields = strRows[i].split("|");
            if (strFields.length > 8) {
                averageLat += parseFloat(strFields[0]);
                averageLon += parseFloat(strFields[1]);

                /* Added By: Dan L. on 2007-09-11 */
                points[i] = new VELatLong(parseFloat(strFields[0]), parseFloat(strFields[1]));
                objMarkers[i] = new listingInfo(i, strFields[9], strFields[2], parseFloat(strFields[0]), parseFloat(strFields[1]), strFields[3], strFields[4], strFields[5], strFields[7], strFields[8], strFields[6]);
                tempCount += 1;
            }
        }
        averageLat = (averageLat / tempCount);
        averageLon = (averageLon / tempCount);
        // Set the map view to the same points used by the shape.
        map.SetMapView(points);
        // NB: objMarkers will be reset below - based on the current page
        strRows = null;
        strFields = null;
    }

    if (objMarkers && objMarkers.length) {
        for (var i = 0; i < objMarkers.length; i++) {
            AddMarker(i, objMarkers[i].address, objMarkers[i].ln, parseFloat(objMarkers[i].lat), parseFloat(objMarkers[i].lon), objMarkers[i].beds, objMarkers[i].baths, objMarkers[i].price, objMarkers[i].pic, objMarkers[i].remarks, objMarkers[i].link);
            objMapArr[i] = new DirectionList(i, objMarkers[i].address, objMarkers[i].lon, objMarkers[i].lat);
        }
    } else {
        //		if (isDebug) {debugger; alert("No Pushpin Data Could Be Loaded!");}
        //		alert("Error: Could not load results!");
    }
    markers = null;
    strRows = null;
    // Make sure we only initialize the map, etc. one time
    isMapLoaded = true;
    endDate = new Date();
    endTime = endDate.getTime();
    // Run time +5000ms
    // Run time with out adding overlays +4700ms
    if (isDebug) {
        //window.document.title = "(Load Time: " + (endTime - startTime) + "ms)";
        window.status = "(Load Time: " + (endTime - startTime) + "ms)";
    }
}

/* Old Code: function GetMarker(point, addToListingList, currentMarkerIndex, addr, ln, lat, lon, beds, baths, price, pic, remarks, link) */
function AddMarker(currentMarkerIndex, addr, ln, lat, lon, beds, baths, price, pic, remarks, link) {
    /* Override value for link (for VO only) */
    link = "";
    var notes = "<div style='width: 240px;'>";
    if (pic && pic.length > 0) {
        notes += "<img src=\"" + pic + "\" alt=\"Photo For " + addr + "\" width=\"100\" /><br />";
    }
    notes += "<b>" + addr + "</b>" +
		"<br /><b>MLS #:</b> " + ln +
		"<br /><b>Beds:</b> " + beds +
		"&#160;<b>Baths:</b>&#160;" + baths +
		"<br /><b>Price:</b>&#160;" + price + "<br />" +
		"<b>Remarks:</b>&#160;" + remarks + "<br />" +
		"" + IDXImagePath;
    var strIconURL = "images/home_green_32.png";

    notes += "</div>";
    AddPushpin("Pin_" + ln, lat, lon, strIconURL/*Was: null*/, addr, notes, link);

}


//Set DirectionList values to be used to access values throughout
function DirectionList(currindex, address, lon, lat) {
    this.currindex = currindex;
    this.address = address;
    this.lon = lon;
    this.lat = lat;
    this.listMapDir = function() {
    }

}