var SE_PreLoadTest_SEMap = 1;

//public global variables
var SEMaps = [];

function SEPoint(myLatitude, myLongitude) {
    // defaults to middle of US
    if (myLatitude)
        this.latitude = parseFloat(myLatitude);
    else
        this.latitude = 31.5;

    if (myLongitude)
        this.longitude = parseFloat(myLongitude);
    else
        this.longitude = -94.7;
}

function SEIcon(iconSrc, iconWidth, iconHeight) {
    this.src = iconSrc;
    this.width = iconWidth;
    this.height = iconHeight;
}

function SEBounds(northwest, southeast) {
    this.northWest = northwest;
    this.southEast = southeast;
}

function SEMarker(obj, map) {
    this.price = '';
    this.bedsBaths = '';
    this.livingArea = '';
    this.lotSizeAcreString = '';
    this.displayStatusString = '';
    this.courtesyOf = '';
    this.showMoreDetail = '';
    this.propertyDetailUrl = '';
    this.photoPathBase = '/common/images/';
    this.photoFiles = null;
    this.photoMapPath = '';
    
    if (obj) {
        this.location = new SEPoint(obj.location.Latitude ? obj.location.Latitude : obj.location.latitude,
            obj.location.Longitude ? obj.location.Longitude : obj.location.longitude);
        this.title = obj.title;
        this.listingID = obj.listingID;
        this.price = obj.price;
		this.bedsBaths = obj.bedsBaths;
		this.livingArea = obj.livingArea;
        this.lotSizeAcreString = obj.lotSizeAcreString;
        this.displayStatusString = obj.displayStatusString;
        this.markerIndex = map.markers.length;
        this.map = map;
        this.courtesyOf = obj.courtesyOf;
        this.isSoldListing = obj.isSoldListing;
        this.showMoreDetail = this.map.sourceApp != 'AgentAchieve';
        this.propertyDetailUrl = "javascript:SE_VL(\"" + this.listingID + "\")";
        this.photoPathBase = obj.photoPathBase;
        this.photoFiles = obj.photoFiles;
        this.photoMapPath = obj.photoMapPath;
        
        if (obj.icon == null)
            this.icon = map.markerIcon
        else
            this.icon = obj.icon;
        if (obj.selectedIcon == null)
            this.selectedIcon = map.markerIconSelected
        else
            this.selectedIcon = obj.selectedIcon;
    }
    else 
    {
        this.location = new SEPoint();
        this.setDefaultIcons()
        this.title = "";
    }
}

function SEMap($mapDiv) {

    //private
    this._imagePath = "/common/semapping/images/";

    //public properties
    this.centerPoint;
    this.mapType = "auto"; // street, aerial, hybrid or oblique
    this.zoomLevel = 14; // zooms - 0 is world view, 20 is max zoom)
    this.controlSize = "normal"; // none, small, normal, large
    this.markers = [];
    this.markerIcon = new SEIcon(this._imagePath + "icon_home20x20.gif", 20, 20);
    this.markerIconSelected = new SEIcon(this._imagePath + "icon_home20x20_on.gif", 20, 20);

    this.mouseOverAction = "tooltip";
    this.enableClickPopup = true;
    this.enableMouseOverHighlight = true;
    this.popupFormat = "resultspopup";
    this.map;
    this.clientID = "";
    this.isOpeningWindow = false;
    this.isLoading = true;
    this.sourceApp = "";
    this.isPublic = false;
    this.mapBoundaryPoints = new Array();
    this.mapBoundariesTempGUID = "";
    this.drawIsDrawing = false;
    this.hasUnsavedMapBoundaries = false;
    this.bounds;
    this.isLoading = true;
    this.showScalebar = true;
    this.tilesLoaded = false;
    this.$mapDiv = $mapDiv;    
}

// all private global variables
SEMap.NoScrollDiv = null;
SEMap.ScrollTimeoutId;
SEMap.BaseZIndex = 10000;
SEMap.TooltipZIndex = 10001;
SEMap.PopupZIndex = 10002;
SEMap.NoMarkerIndex = -1;
SEMap.ChangeViewEvents = [];
SEMap.MinZoom = 0;
SEMap.MaxZoom = 20;

SEMap.MapType = {};
SEMap.MapType.road = "road";
SEMap.MapType.birdseye = "birdseye";
SEMap.MapType.auto = "auto";

SEMap.DashboardSize = {};
SEMap.DashboardSize.none = "none";
SEMap.DashboardSize.tiny = "tiny";
SEMap.DashboardSize.tiny = "small";
SEMap.DashboardSize.normal = "normal";
SEMap.DashboardSize.large = "large";
SEMap.InfoBox = {};
SEMap.InfoBox.imageWidth = 229;

SEMap.prototype.init = function () {
   
    this.$mapDiv.parent().unbind();
    this.$container = this.$mapDiv.parent();
    this.setupDrawButtons();
    
    this.fitHeight();
    SEMaps.push(this);
   
    this.initMapControl();
 
    var thisMap = this;
    if (!this._hasResizeEvent && thisMap.onResize) {        
        thisMap.$mapDiv.parent().resize(function () { thisMap.onResize(); });
        thisMap._hasResizeEvent = true;
    }

    this.enhanceMarkers();
    this.drawLoadBoundaryPoints();

    this.saveBounds();

    this.makeMapTooltip();
    this.$infoBox = $("<div></div>");

    if (SEMap.ChangeViewEvents[this.clientID]) this.onChangeView = SEMap.ChangeViewEvents[this.clientID];

    this.isLoading = false;

}

SEMap.prototype.fitHeight = function () {
    if (this.$mapFilterControls) {
        var controlsHeight = this.$mapFilterControls.outerHeight();
        if (!controlsHeight)
            controlsHeight = this.$mapControls.outerHeight();

        this.$container.height(this.$container.availableHeight() - controlsHeight);
    }
    else
        this.$container.height(this.$container.availableHeight());
    this.$mapDiv.height(this.$mapDiv.availableHeight() );
}

SEMap.prototype.setupDrawButtons = function () {
    var thisMap = this;

    var $parent = this.$container.parent();
    while ($parent.length > 0) {
        var $mapControls = $parent.find(".sr_map_position_controls");
        if ($mapControls.length > 0) {
            this.$mapControls = $mapControls;
            break;
        }
        $parent = $parent.parent();
    }
    if (typeof (this.$mapControls) == 'undefined')
        this.$mapControls = $(".sr_map_position_controls");
    if (!thisMap.showDrawUI) {
        if (this.$mapControls.text().trim() == '')
            this.$mapControls.hide();
        return;
    }
    this.$mapFilterControls = this.$mapControls.find(".sr_map_filter_controls");
    var $btnContainer = this.$container;
    if (this.$mapControls.length > 0 && this.$mapFilterControls.find("[ id $='divDrawUIButtons']").length > 0) {
        $btnContainer = this.$mapFilterControls;
    }
    this.$drawUIButtons = $btnContainer.find("[ id $='divDrawUIButtons']");
    this.$drawButton = this.$drawUIButtons.find("[ id $='btnDraw']");

    this.$drawClearButton = this.$drawUIButtons.find("[ id $='btnClear']");
    this.$drawButton.button();
    this.$drawClearButton.button();
    this.$drawHelp = this.$container.find("[ id $='divDrawHelp']")
    this.$drawButton.val(SEMap.Draw.DrawCaption);
    this.$drawClearButton.val(SEMap.Draw.ClearCaption);
    this.$drawButton.unbind('click');
    this.$drawClearButton.unbind('click');
    this.$drawButton.click(function () { thisMap.drawButtonClick(); });
    this.$drawClearButton.click(function () { thisMap.drawClear(); })
    this.$drawUIButtons.show();

    if (this.$drawUIButtons.length > 0 && this.$mapControls.length > 0
        && this.$mapFilterControls.find("[ id $='divDrawUIButtons']").length == 0) {
        this.$mapFilterControls.append(this.$drawUIButtons);
        this.$mapFilterControls.append("<div class='clearfix'></div>");
        if (thisMap.$mapControls.height() < thisMap.$mapFilterControls.height()) {
            var padding = thisMap.$mapControls.innerHeight() - thisMap.$mapControls.height();
            thisMap.$mapControls.height(thisMap.$mapFilterControls.height() - padding);
        }
    }
}

SEMap.prototype.makeMapTooltip = function () {
    this.$tooltip = $("<div "
        + "id='" + this.clientID + "_tooltip' "
        + "style='position:absolute;display:none;z-index:" + SEMap.TooltipZIndex + ";' "
        + "class='tooltip'></div>");
    this.$container.append(this.$tooltip);
}

SEMap.prototype.hideTooltip = function () {
    if (this.$tooltip) {
        this.$tooltip.html("");
        this.$tooltip.hide();
        clearTimeout(this.$tooltip.closeTimeoutId);
        this.$tooltip.closeTimeoutId = null;
    }
}

SEMap.prototype.hideInfoBox = function () {
    if (this.$infoBox) {
        this.$infoBox.dialog("destroy");
        clearTimeout(this.$infoBox.closeTimeoutId);
        this.$infoBox.closeTimeoutId = null;
        this.$infoBox.marker = null;
    }
}

SEMap.prototype.enhanceMarkers = function() {

    this.clearMapMarkers();
    for (var i = 0; i < this.markers.length; i++) {
        this.markers[i].enableMarker(i);
    }
}

SEMap.prototype.createMarker = function (objMarker) {
    // this happens before the map is created so we can use the markers to determine best view
    var marker = new SEMarker(objMarker, this);
    this.markers.push(marker);
}

SEMarker.prototype.enableMarker = function(markerIndex) {   
    
    // this needs to happen after the map control is created
    this.pin = this.addPushpin()
    this.addHighlightActions();
    this.addTooltipActions();
    this.addInfoBoxActions();
}

SEMarker.prototype.addTooltipActions = function () {

    if (this.map.mouseOverAction == "tooltip") {
        var thisMap = this.map;
        var thisPin = this.pin;
        var thisMarker = this;
        this.mouseOverTooltipHandler = Microsoft.Maps.Events.addHandler(thisPin, 'mouseover', function () {
            thisMarker.showTooltip();
        });        
    }
}

SEMarker.prototype.addInfoBoxActions = function () {

    if (this.map.enableClickPopup) {
        var thisMap = this.map;
        var thisPin = this.pin;
        var thisMarker = this;
        this.mouseClickInfoBoxHandler = Microsoft.Maps.Events.addHandler(thisPin, 'click', function () {
            thisMarker.showInfoBox();
        });       
    }
}

SEMarker.prototype.showTooltip = function () {
    if (this.map.drawIsDrawing) return;
    this.map.$tooltip.html(this.title);
    if (!this.map.mapDimensions)
        this.map.loadMapDimensions();
    // realtive to center of map
    var xy = this.map.getXY(this.location);

    // if on bottom, show on top
    if (xy.y > 0)
        xy.y = xy.y - this.icon.height - this.map.$tooltip.outerHeight();

    // if on right side - show on left
    if (xy.x > 0)
        xy.x = xy.x - this.map.$tooltip.outerWidth();
    xy.x = this.map.mapDimensions.width / 2 + xy.x
    xy.y = this.map.mapDimensions.height / 2 + xy.y

    this.map.$tooltip.css({ left: xy.x, top: xy.y });
    this.map.$tooltip.marker = this;
    this.map.$tooltip.show();
    var thisMarker = this;
    this.map.$tooltip.click(function () { thisMarker.showInfoBox(); });
}

SEMarker.prototype.showInfoBox = function () {
    if (this.map.drawIsDrawing) return;
    this.map.hideTooltip();
    this.map.hideInfoBox();

    this.map.$infoBox = $(this.getListingInfoBoxHtml());
    this.setupPhotoChanger()
    var infoBoxWidth = 250;
    var xy = this.positionInfoBox(infoBoxWidth);
    this.map.$infoBox.dialog({
        position: [xy.x, xy.y],
        title: "<a href='" + this.propertyDetailUrl + "'>" + this.title + "</a>",
        zIndex: SEMap.PopupZIndex,
        dialogClass: "mapInfoBox",
        width: infoBoxWidth
    });
    this.map.$infoBox.marker = this;
}

SEMarker.prototype.setupPhotoChanger = function () {
    this.$infoBoxImages = this.map.$infoBox.find(".map_infobox_img");
    if (this.$infoBoxImages.length > 0) {
        var thisMarker = this;
        this.map.$infoBox.find("#mapInfoBox_photos_prev").click(function () {
            if (thisMarker.currentInfoBoxImage > 0)
                thisMarker.currentInfoBoxImage--;
            else
                thisMarker.currentInfoBoxImage = thisMarker.$infoBoxImages.length - 1;
            thisMarker.changePhoto();
        });
        this.map.$infoBox.find("#mapInfoBox_photos_next").click(function () {
            if (thisMarker.currentInfoBoxImage < thisMarker.$infoBoxImages.length - 1)
                thisMarker.currentInfoBoxImage++;
            else
                thisMarker.currentInfoBoxImage = 0;
            thisMarker.changePhoto();
        });
        this.currentInfoBoxImage = 0;
    }

}
SEMarker.prototype.changePhoto = function () {
    this.map.$infoBox.find(".map_infobox_image").hide().fadeIn(500).attr("src", this.$infoBoxImages[this.currentInfoBoxImage].src);
}

SEMarker.prototype.getListingInfoBoxHtml = function () {

    var firstPhotoPath = "";
    var photoControl = "";
    if (this.photoPathBase && this.photoPathBase != "" && this.photoFiles && this.photoFiles.length > 0) {
        firstPhotoPath = this.photoPathBase + this.photoFiles[0] + (this.photoFiles[0].indexOf(".jpg") == -1 ? ".jpg" : "");
        if (this.photoFiles.length > 1)
        {
            photoControl = "<div class='ui-widget-header mapInfoBox_photos' >"
            + "<div class='mapInfoBox_photos__link' style='float:left;cursor:pointer'><a id='mapInfoBox_photos_prev' >&lt;&lt; Previous</a></div>"
            + "<div class='mapInfoBox_photos__link' style='float:right;cursor:pointer'><a id='mapInfoBox_photos_next'>Next >></a></div>"
            + "<div class='clearfix'></div>"
            + "</div>"
        }
    }
    else if (this.photoMapPath)
        firstPhotoPath = this.photoMapPath;
    else
        firstPhotoPath = "/common/images/no_photo_listing_medium.gif";
    
    var summaryItemsOnThisLine = 0;
    var summaryDiv = ""
    _buildSummaryDiv(this.price, "<strong>" + this.price + "</strong>&nbsp; ");
    _buildSummaryDiv(this.bedsBaths, "<strong>" + this.bedsBaths + "</strong>&nbsp;beds/baths ");
    _buildSummaryDiv(this.livingArea, "<strong>" + this.livingArea + "</strong>&nbsp;sqft ");
    _buildSummaryDiv(this.lotSizeAcreString, "<strong>" + this.lotSizeAcreString + "</strong>&nbsp;acres ");
    _buildSummaryDiv(this.displayStatusString, "<strong>" + this.displayStatusString + "</strong>&nbsp; ");
    if (summaryDiv != "")
        summaryDiv += "</div>"

    var infobox = "<div style='width:270px;'>"
    + summaryDiv
	+ "<div style='padding:5px;text-align:center'>"
		+ "<a href='" + this.propertyDetailUrl + "'><img src='" + firstPhotoPath + "' width='229' height='172' class='map_infobox_image' /></a>"
	+ photoControl
	+ "</div>"
    + (this.courtesyOf != "" ? "<div style='padding:5px;text-align:center' >" + this.courtesyOf + "</div>" : "")
    + (this.showMoreDetail ? "<div  style='text-align:center;height:20px;' ><a href='" + this.propertyDetailUrl + "'>View More Detail</a>" : "")
	+ (this.showMoreDetail ? "</div>" : "");
    
    var images = "";
    
    if (this.photoFiles) {
        for (var iPhoto = 0; iPhoto < this.photoFiles.length; iPhoto++) {
            images += "<img src='" + this.photoPathBase + this.photoFiles[iPhoto] 
                + (this.photoFiles[iPhoto].indexOf(".jpg") == -1 ? ".jpg" : "")
                + "' style='display:none' class='map_infobox_img' />"
        }
    }

    return infobox + images;

    function _buildSummaryDiv(field, displayText) {
        if (field != "") {
            if (summaryItemsOnThisLine % 2 == 0) {
                if (summaryItemsOnThisLine > 0) summaryDiv += "</div>";
                summaryDiv += "<div style='text-align:center;margin:2px;'>";
            }
            summaryItemsOnThisLine++;
            summaryDiv += displayText
        }
    }
}



SEMarker.prototype.positionInfoBox = function (infoBoxWidth) {
    this.map.$infoBox.dialog();
    if (!this.map.mapDimensions)
        this.map.loadMapDimensions();
    var xy = this.map.getXY(this.location);
    
    // if on bottom, show on top   
    if (xy.y > this.map.mapDimensions.height / 2)
        xy.y = xy.y - this.icon.height - this.map.$infoBox.parent().outerHeight();

    // if on right side - show on left
    if (xy.x > this.map.mapDimensions.width / 2)
        xy.x = xy.x - infoBoxWidth;
        
    // dialog is absolute compared to page

    xy.x += this.map.mapDimensions.pageLeft + this.map.mapDimensions.width / 2 - $(window).scrollLeft();
    xy.y += this.map.mapDimensions.pageTop + this.map.mapDimensions.height/2 - $(window).scrollTop();
    return xy;
}

SEMarker.prototype.setDefaultIcons = function () {
    if (this.map) {
        this.icon = this.map.markerIcon
        this.selectedIcon = this.map.markerIconSelected
    }
}

SEMap.prototype.getMarkerBounds = function (padding) {
    var bounds = null;
    var markers = this.markers;
    if (markers.length > 1) {
        var minLat = markers[0].location.latitude;
        var maxLat = markers[0].location.latitude;
        var minLon = markers[0].location.longitude;
        var maxLon = markers[0].location.longitude;
        for (var i = 0; i < markers.length; i++) {
            if (markers[i].location.latitude < minLat)
                minLat = markers[i].location.latitude;
            if (markers[i].location.latitude > maxLat)
                maxLat = markers[i].location.latitude;
            if (markers[i].location.longitude < minLon)
                minLon = markers[i].location.longitude;
            if (markers[i].location.longitude > maxLon)
                maxLon = markers[i].location.longitude;
        }
        bounds = new SEBounds(new SEPoint(maxLat, minLon), new SEPoint(minLat, maxLon));
        this.addBoundsPadding(padding, bounds.northWest, bounds.southEast);
    }
    return bounds;
}

SEMap.prototype.getBoundsFromPoints = function (points, padding) {
    var bounds = null;
    if (points.length > 1) {
        var minLat = points[0].latitude;
        var maxLat = points[0].latitude;
        var minLon = points[0].longitude;
        var maxLon = points[0].longitude;
        for (var i = 0; i < points.length; i++) {
            if (points[i].latitude < minLat)
                minLat = points[i].latitude;
            if (points[i].latitude > maxLat)
                maxLat = points[i].latitude;
            if (points[i].longitude < minLon)
                minLon = points[i].longitude;
            if (points[i].longitude > maxLon)
                maxLon = points[i].longitude;
        }
        bounds = new SEBounds(new SEPoint(maxLat, minLon), new SEPoint(minLat, maxLon));
        this.addBoundsPadding(padding, bounds.northWest, bounds.southEast);
    }
    return bounds;
}

SEMap.prototype.saveBounds = function () {
    var bounds = this.getBounds();
    if (bounds) {
    
        if (bounds.northWest && this.getHiddenField("hidNWLat").length> 0) {
            this.getHiddenField("hidNWLat").val(bounds.northWest.latitude);
            this.getHiddenField("hidNWLong").val(bounds.northWest.longitude);
            this.getHiddenField("hidSELat").val(bounds.southEast.latitude);
            this.getHiddenField("hidSELong").val(bounds.southEast.longitude);

            var center = this.getCenter();
            this.getHiddenField("hidCenterLat").val(center.latitude);
            this.getHiddenField("hidCenterLong").val(center.longitude);
            this.zoomLevel = this.getZoomLevel();
            this.getHiddenField("hidZoomLevel").val(this.zoomLevel);
            this.mapType = this.getMapType();
            this.getHiddenField("hidMapType").val(this.mapType);
        }
    }
}

SEMap.prototype.getSavedBounds = function() {
    return new SEBounds(new SEPoint(this.getHiddenField("hidNWLat").val(), this.getHiddenField("hidNWLong").val()),
	    new SEPoint(this.getHiddenField("hidSELat").val(), this.getHiddenField("hidSELong").val()));
}

SEMap.prototype.isBigMove = function() {
    if (!this.isLoading && this.map) {
        var currentBounds = this.getSavedBounds();
        var newBounds = this.getBounds();
        var deltaLat = currentBounds.northWest.latitude - currentBounds.southEast.latitude;
        var deltaLong = currentBounds.southEast.longitude - currentBounds.northWest.longitude;
        return (
			(Math.abs(currentBounds.northWest.latitude - newBounds.northWest.latitude) / deltaLat > .1)
			|| (Math.abs(currentBounds.southEast.latitude - newBounds.southEast.latitude) / deltaLat > .1)
			|| (Math.abs(currentBounds.northWest.longitude - newBounds.northWest.longitude) / deltaLong > .1)
			|| (Math.abs(currentBounds.southEast.longitude - newBounds.southEast.longitude) / deltaLong > .1)
		);
    }
    return true;
}

SEMap.prototype.onStartViewChange = function () {
    _SEMap_ClosePopups()
}
SEMap.prototype.mapTypeChanged = function () {
    if (this.map) {
        this.mapType = this.getMapType();
        this.getHiddenField("hidMapType").val(this.mapType);
    }
}
// public external function
function _SEMap_ClosePopups() {

    for (var iMap =0; iMap < SEMaps.length; iMap++)
    {
        if (SEMaps[iMap].hideTooltip)
            SEMaps[iMap].hideTooltip();
        if (SEMaps[iMap].hideInfoBox)
            SEMaps[iMap].hideInfoBox();
    }    
}

function SEMap_OnScroll(mapClientID, isScrolling) {

    var map = null;
    for (var iMap = 0; iMap < SEMaps.length; iMap++) {
        if (SEMaps[iMap].clientID == mapClientID) {
            map = SEMaps[iMap];
        }
    }
    
    if (!map) {
        return;
    }
    
    if (SE_UserAgent('opera') || SE_UserAgent('firefox'))
        return;

    var mapElement = map.$mapDiv;        
        
    if (SEMap.NoScrollDiv == null) {
        SEMap.NoScrollDiv = $("<div "
            + "id='SEMap_NoScrollDiv' "
            + "style='position:absolute;top:0;left:0;"
            + "width:" + mapElement.outerWidth() + "px;"
            + "height:" + mapElement.outerHeight() + "px;"
            + "background-color: white;"
            + "filter:alpha(opacity=.01);"
            + "MozOpacity:0.1;"
            + "opacity:.01;"
            + "z-index:1999;"
            + "'></div>");

        mapElement.append(SEMap.NoScrollDiv);
        SEMap.ScrollTimeoutId = setTimeout('SEMap_OnScroll("' + mapClientID + '",false)', 1000);
    }
    else if (isScrolling == false) {
        SEMap.NoScrollDiv.remove();
        SEMap.NoScrollDiv = null;
        map.mapDimensions = null;
        
    }
    else {
        clearTimeout(SEMap.ScrollTimeoutId);
        SEMap.ScrollTimeoutId = setTimeout('SEMap_OnScroll("' + mapClientID + '",false)', 1000);
    }
}

SEMap.prototype.fitMarkers = function () {
    if (this.markers && this.markers.length == 1) {
        this.isOpeningWindow = true;
        this.setCenterZoom(this.markers[0].location, this.zoomLevel);
        this.isOpeningWindow = false;
    }
    else {
        var bounds = this.getMarkerBounds(.05);
        if (bounds != null) {
            this.isOpeningWindow = true;
            this.setBestMapView(bounds);
            this.zoomLevel = this.getZoomLevel()
            this.isOpeningWindow = false;
        }
    }
    this.saveBounds();
}

SEMap.prototype.handleChangeViewCallback = function () {

    if (this.onChangeView) {
        var isBigMove = this.isBigMove();
        if (this.map) {
            this.saveBounds();
            if (!this.isOpeningWindow
                && !this._isDrawMode
                && !this.isLoading
                && isBigMove) {
                this.onChangeView();
            }
        }
    }
}

SEMap.prototype.highlightAndTooltip = function (markerIndex, isHighlight) {

    this.highlightPushpin(markerIndex, isHighlight);
    if (isHighlight && this.markers[markerIndex])
        this.markers[markerIndex].showTooltip()
    else
        this.hideTooltip()
}

SEMap.prototype.highlightPushpin = function (markerIndex, isSelected) {

    for (iMarker = 0; iMarker < this.markers.length; iMarker++) {
        this.markers[iMarker].unHighlightIcon()
    }
    if (isSelected && this.markers[markerIndex]) 
        this.markers[markerIndex].highlightIcon();
}

SEMarker.prototype.isVisible = function() {
    bounds = this.map.getBounds();
    return (!(this.location.latitude > bounds.northWest.latitude
    	        || this.location.latitude < bounds.southEast.latitude
    	        || this.location.longitude < bounds.northWest.longitude
    	        || this.location.longitude > bounds.southEast.longitude))
}

SEMap.prototype.dispose = function () {
    if (this.map && this.map.dispose)
        this.map.dispose();
}

SEMap.CleanUp = function () {
    _SEMap_ClosePopups();
    for (var iMap = 0; iMap < SEMaps.length; iMap++) {
        if (!SEMaps[iMap].$mapDiv.is(":visible")) {
            var inVisibleMap = SEMaps[iMap];
            SEMaps.splice(iMap);
            inVisibleMap.$mapDiv.find(".MicrosoftMap").remove()
            if (inVisibleMap && inVisibleMap.map && inVisibleMap.map.blur)
                inVisibleMap.map.blur = function () { }
            inVisibleMap.map = null;
        }
    }
}

SEMap.prototype.killMe = function () {

    for (var iMap = 0; iMap < SEMaps.length; iMap++) {
        if (SEMaps[iMap].clientID == this.clientID)
            SEMaps.splice(iMap);
    }

    if (this.map) {
        this.map.blur = function () { }
        this.$mapDiv.find(".MicrosoftMap").remove()
        this.map = null;
    }
}

SEMap.prototype.hasBoundarySegments = function() {
    return this.getHiddenField("hidMapBoundaryPoints").val() !=  "";
}

SEMap.prototype.drawResizeToFitSelection = function(points, padding) {
    var northWest = new SEPoint(-90, 180);
    var southEast = new SEPoint(90, -180);
    for (i = 0; i < points.length; i++) {
        if (points[i].latitude < southEast.latitude)
            southEast.latitude = points[i].latitude;
        if (points[i].longitude > southEast.longitude)
            southEast.longitude = points[i].longitude;
        if (points[i].latitude > northWest.latitude)
            northWest.latitude = points[i].latitude;
        if (points[i].longitude < northWest.longitude)
            northWest.longitude = points[i].longitude;
    }
    
    this.addBoundsPadding(padding, northWest, southEast);
    
    var bounds = new SEBounds(northWest, southEast);
    this.setBestMapView(bounds);
    this.isLoading = false;
    this.saveBounds();
}

SEMap.prototype.addBoundsPadding = function(padding, northWest, southEast) {
    if (padding)
    {
        var deltaLat = northWest.latitude - southEast.latitude;
        northWest.latitude += deltaLat * padding;
        southEast.latitude -= deltaLat * padding;
        var deltaLong = southEast.longitude - northWest.longitude;
        northWest.longitude -= deltaLong * padding;
        southEast.longitude += deltaLong * padding;
    }
}

function SE_Map_ShowSimple(mapDivID, nwLat, nwLon, seLat, seLon) {
    // container div needs  style="width:250px;height:250px;position:relative;
    var bounds = Microsoft.Maps.LocationRect.fromCorners(
        new Microsoft.Maps.Location(nwLat, nwLon),
        new Microsoft.Maps.Location(seLat, seLon));
    
    var mapOptions = {
        credentials: SEMap.credentials,
        mapTypeId: Microsoft.Maps.MapTypeId.road,
        bounds: bounds,
        enableClickableLogo: false,
        enableSearchLogo: false,
        showCopyright: false
   }    
   var map = new Microsoft.Maps.Map($("#" + mapDivID)[0], mapOptions);
   
}

SEMap.prototype.getHiddenField = function (fieldName) {
    return $("#" + this.clientID.replace("_map", "_" + fieldName));
}

// -----------------------------------------
// ---------- begin drawing ----------------

SEMap.Draw = {};
SEMap.Draw.polygon = "polygon";
SEMap.Draw.rectangle = "rectangle";
SEMap.Draw.polyline = "polyline";
SEMap.Draw.crosshair = "crosshair";

SEMap.Draw.DrawCaption = 'Draw Boundaries';
SEMap.Draw.ClearCaption = 'Clear Boundaries';
SEMap.Draw.CancelCaption = 'Cancel';

SEMap.Draw.polygonStartIcon = new SEIcon("/common/semapping/images/polygon_start.png", 16, 16);
SEMap.Draw.polygonStartIconOver = new SEIcon("/common/semapping/images/polygon_end.png", 16, 16);

SEMap.prototype.drawClear = function () {
    var oldBoundaries = this.mapBoundaryPoints;
    
    this.drawDone(true);
    this._drawClear();
    this.$drawClearButton.hide();
    this.hasUnsavedMapBoundaries = oldBoundaries.length > 0;

    if (this.drawIsDrawing) {
         if (oldBoundaries && oldBoundaries.length > 0) {
            this.mapBoundaryPoints = oldBoundaries;
            this.drawLoadBoundaryPoints()
        }
        else { }
    }
    else if (this.onChangeView) {
        this.getHiddenField("hidMapBoundaryPoints").val("");
        this.onChangeView(true)
    }
}

SEMap.prototype.drawCancel = function () {
    var oldBoundaries = this.mapBoundaryPoints;

    this.drawDone(true);
    this._drawClear();
    this.$drawClearButton.hide();
    this.$drawButton.val(SEMap.Draw.DrawCaption);
    this.hasUnsavedMapBoundaries = oldBoundaries.length > 0;

    if (oldBoundaries && oldBoundaries.length > 0) {
        this.mapBoundaryPoints = oldBoundaries;
        this.drawLoadBoundaryPoints()
    }    
}

SEMap.prototype.showDrawHelp = function (drawType) {

    var thisMap = this;

    this.$drawHelp.show();
    
    var $rdoPolygon = this.$drawHelp.find("#rdoDrawPolygon");
    var $rdoRectangle = this.$drawHelp.find("#rdoDrawRectangle");

    
    if (drawType == SEMap.Draw.polygon)
        $rdoPolygon.attr("checked", "checked");
    else
        $rdoRectangle.attr("checked", "checked");
    this.$drawPolygonHelp = this.$drawHelp.find("[ id $= 'divDrawInstructionsPolygon']").click(thisMap.drawSetHelpText);
    this.$drawRectangleHelp = this.$drawHelp.find("[ id $= 'divDrawInstructionsRectangle']").click(thisMap.drawSetHelpText);
    this.drawSetHelpText();
    this.setMapTypeSelectorVisibility(false);
}

SEMap.prototype.drawSetHelpText = function () {
    if (this.$drawHelp.find("#rdoDrawRectangle").is(":checked")) {
        this.$drawRectangleHelp.show();
        this.$drawPolygonHelp.hide();
    }
    else {
        this.$drawRectangleHelp.hide();
        this.$drawPolygonHelp.show();
    }
}

SEMap.prototype.hideDrawHelp = function() {
    this.$drawHelp.hide();
}

SEMap.prototype.drawButtonClick = function () {

    if (this.$drawButton.val() == SEMap.Draw.CancelCaption) {
        this.drawCancel(true);
    }
    else {
        this.$drawButton.val(SEMap.Draw.CancelCaption);
        this.showDrawHelp(SEMap.Draw.rectangle);
        this.drawStart();
    }
}

SEMap.prototype.drawDone = function (isClear) {
    _SEMap_ClosePopups()
    this.drawIsDrawing = false;
    this._drawDone(isClear);
    this.setMapTypeSelectorVisibility(true);

    this.$drawButton.val(SEMap.Draw.DrawCaption);
    this.hideDrawHelp();

    if (!isClear) {
        var drawPoints = this.drawGetPointsString();
        this.getHiddenField("hidMapBoundaryPoints").val(drawPoints);

        this.hasUnsavedMapBoundaries = true;
        if (drawPoints != "")
            this.$drawClearButton.show();
        else
            this.$drawClearButton.hide();

        if (this.onChangeView && !isClear)
            this.onChangeView(true);
    }
}

// -----------------------------------------
// ---------- end drawing ----------------

SEMap.IsValidLatLong = function (latitude, longitude) {
    // also accepts single argument
    var isValidLat;
    var isValidLong = true;
    if (latitude)
        isValidLat = Math.abs(latitude) > .01 && Math.abs(latitude) < 180;
    if (longitude)
        isValidLong = Math.abs(longitude) < 180 && Math.abs(longitude) > .01;
    return isValidLat && isValidLong;
}

//earlier versions of ie don't reliable get the namespace added by the ms code
if (SE_MSIEVersion() > 0 && SE_MSIEVersion() < 9
    && document.namespaces && document.namespaces.add)
    document.namespaces.add("v", "urn:schemas-microsoft-com:vml")

