﻿/*
    Map plotting essentials
    To be used by /Map/Map.ascx (User Control)
    
    Dependencies: WebServiceCalls.js
    map_element and init_location variables inserted by the User Control
    onload = loadMap; <-- Inserted by User Control
*/
var map;
var map_element;
var init_location;
var centerPin = false;
var layer;
var initialLoad = true;
var zoomLevel = 0;
var da = "";
var dl;
var trackingDisabled = false;

function disableTracking(yes) {
    if (yes) {
        layer.Hide();
    }
    else
        layer.Show();
    trackingDisabled = yes;
    if (!yes) onViewChange();
}

function onViewChange(e) {
    if (trackingDisabled) return;
    view = map.GetMapView();
    topleft = view.TopLeftLatLong;
    bottomright = view.BottomRightLatLong;
    newZoomLevel = map.GetZoomLevel();

    var minLat = topleft.Latitude; 
    var maxLat = bottomright.Latitude;  
    if (minLat > maxLat) {
        minLat = bottomright.Latitude;
        maxLat = topleft.Latitude;
    }

    var minLon = topleft.Longitude;  
    var maxLon = bottomright.Longitude;  
    if (minLon > maxLon) {
        minLon = bottomright.Longitude;
        maxLon = topleft.Longitude;
    }

    if (layer) {
        if (newZoomLevel > 11 && zoomLevel <= 11) { //zoomed in
            layer.Show();
            map.HideControl(dl);
        }

        if (newZoomLevel <= 11 && zoomLevel > 11) {
            layer.Hide();
            map.ShowControl(dl);
        }
    }

    zoomLevel = newZoomLevel;

    if (zoomLevel > 11) {
        connect.GetGeoList(minLat, maxLat, minLon, maxLon, initialLoad, onResultList, onGeoError);
        initialLoad = false;
    }

    var c = map.GetCenter();
    da = "#," + c.Latitude + "," + c.Longitude + "," + zoomLevel;

    //alert(view.ZoomLevel);
}

function onGeoError(e) {
    initialLoad = true;
    alert(e.get_message());
    //map.ZoomIn();
}

function loadMap(noMiniMap) {
    var options = new VEMapOptions();
    if(zoomLevel == 0)
        zoomLevel = 12;
    options.EnableBirdseye = false;
    var mel = document.getElementById(map_element);
    //mel.style.cssText = map_style;
    map = new VEMap(map_element);
    var vll = null;
    if (init_location.charAt(0) == '(') //lat long
        vll = eval("new VELatLong" + init_location);

    map.AttachEvent("onchangeview", onViewChange);
    map.LoadMap(vll, zoomLevel, null, false, null, null, null, options);

    if (location.hash) {
        var mloc = location.hash.split(',');
        if (mloc.length == 4) {
            map.SetCenterAndZoom(new VELatLong(mloc[1], mloc[2]), mloc[3]);
        }
    }
    else if (vll == null && init_location != '') {
    map.Find(null, init_location, null, null, null, null, null, null, null, null, function() { setMapLocation = false; map.SetZoomLevel(13); });
        map.HideControl(dl);
    }

    dl = document.createElement("div");
    dl.className = "mapMessage";
    dl.style.width = "200px";
    dl.style.height = "20px";
    dl.style.background = "#f00";
    dl.style.color = "#fff";
    dl.style.position = "absolute";
    //dl.style.right = "5px";
    dl.style.top = "5px";
    dl.style.border = "solid 1px #f00";
    dl.style.left = mel.clientWidth - 205 + "px";
    dl.style.fontSize = ".8em";
    dl.innerHTML = "Please Zoom-in to view Properties";
    map.AddControl(dl);
    map.HideControl(dl);

    if(!noMiniMap)
        map.ShowMiniMap(5, mel.clientHeight - 195);

    if (centerPin) {
        var pin = new VEShape(VEShapeType.Pushpin, map.GetCenter());
        pin.SetZIndex(1500);
        if (pinCustomIcon)
            pin.SetCustomIcon(pinCustomIcon);
        map.AddShape(pin);
    }

}

function setFilter(name, value) {
    document.cookie = name + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;path=/";
    if (value != '') {
        document.cookie = name + "=" + escape(value) + ";path=/";
        //alert("Setting cookie: " + name + "=" + value);
    }
    //reloadMap();
}

function reloadMap() {initialLoad = true;  onViewChange(); }

var setMapLocation = false;
function onResultList(result) {
    //Better performance seen through loading off of a seperate thread
    setTimeout(function() { loadItems(result); }, 25);
    if (setMapLocation)
        location = da;
    else
        setMapLocation = true;
}

function loadItems(result) {

    var r = eval(result);
    if (r.newSession) {
        if (layer) //Lets remove the shape/all icons if they already existed
            map.DeleteShapeLayer(layer);

        layer = new VEShapeLayer();
        map.AddShapeLayer(layer);
    }

    loadList(r.list);
    loadList(r.featuredList, true );
}

function loadList(list, featured) {
    var icon = "<img alt='' src='/images/icons/mp.gif'/>";
    var featuredIcon = "<img alt='' src='/images/mfi.gif'/>";
    var i = 0;
    for (i = 0; i < list.length; i++) {
        var item = list[i];
        var lid = item[0] + item[1];
        var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(item[8], item[9]));
        if(featured)
            shape.SetCustomIcon(featuredIcon);
        else
            shape.SetCustomIcon(icon);
        var title;

        if (item[7])
            title = ("<div style='text-align:left'>" + item[6] + "</div>");
        else
            title = ("(Address Undisclosed)");

        if (featured)
            title = "<div style='margin:-10px;margin-bottom:5px;color:white;background:red;padding:4px'>Featured Property</div>"
               + title;
        shape.SetTitle(title);

        var thumb = item[12];
        if (featured) {
            if (thumb.length > 0)
                thumb = "/Thumb/" + item[1] + "-" + thumb;
            else
                thumb = "/Images/No-Thumb.gif";
        }
        var desc = "<div style='margin:0;min-height:50px;text-align:left'>"
                + "<a title='View Details' href='/Homes/" + lid + "/'>" + "<img style='max-height:75px;float:left;padding:2px;border:solid 1px #666;margin-right:7px;margin-bottom:10px' alt='' src='" + thumb + "'/></a>"
                + "<span style='color:red;font-weight:bold'>" + item[3] + "</span><br/>";
        if (item[4]) desc += item[4] + " Bed";
        if (item[4] && item[5]) desc += ", ";
        if (item[5]) desc += item[5] + " Bath";
        if (item[10]) desc += " (" + item[10] + "&nbsp;sqft" + ")";
        if (item[11]) desc += "<br />Year built: " + item[11];
        desc += "<br />";
        desc += "<a href='/Homes/" + lid + "/'>View Details</a> | ";
        desc += "<a href='#' onclick='saveListing(" + item[2] + "," + item[1] + ",this);return false'>Save</a>";
        desc += "</div>";
        shape.SetDescription(desc);
        layer.AddShape(shape);
    }
}
