﻿function setMapView(pins)
{      
    if(pins.length == 0) return;
    
    var n, e, s, w;
    
    for(var i = 0; i < pins.length; i++)
    {
        var pinLat, pinLng;
        
        pinLat = pins[i].lat();
        pinLng = pins[i].lng();
    
        if(!n) n = pinLat;
        if(!s) s = pinLat;
        if(!e) e = pinLng;
        if(!w) w = pinLng;
        
        if(pinLat > n) n = pinLat;        
        if(pinLat < s) s = pinLat;        
        if(pinLng > e) e = pinLng;        
        if(pinLng < w) w = pinLng;    
    }
    
    var bounds = new GLatLngBounds(new GLatLng(s, w), new GLatLng(n, e));
    map.setCenter(bounds.getCenter());
    
    var maxZoom = 14;        
    map.setZoom(Math.min(map.getBoundsZoomLevel(bounds), maxZoom));        
}

var houseOverlayMarker = null;
var houseOverlayMarkerTimer = null;

function displayHouseOverlay(pin, overlay)
{
    if(houseOverlayMarkerTimer)
    {
        clearTimeout(houseOverlayMarkerTimer);
        hideHouseOverlayNow();
    }
    
    if(!pin.overlay)
    {
        pin.overlay = overlay;
        map.addOverlay(overlay);
    }
}

function hideHouseOverlay(marker)
{
    houseOverlayMarker = marker;
    houseOverlayMarkerTimer = setTimeout('hideHouseOverlayNow()', 3000);
}

function hideHouseOverlayNow()
{
    if (houseOverlayMarker.overlay != null)
    {
        map.removeOverlay(houseOverlayMarker.overlay);
        houseOverlayMarker.overlay = null;
        houseOverlayMarkerTimer = null;
    }
}

///Custom overlays
function houseInfoOverlay(_marker, _html) 
{
    this.marker = _marker;
    this.html = _html;
}

houseInfoOverlay.prototype = new GOverlay();
houseInfoOverlay.prototype.initialize = function(map)
                                        {
                                            var div = document.createElement("div");
                                            div.style.position = "absolute";
                                            div.style.height = "auto";
                                            div.style.color = "#000";

                                            var overlayPos = getOverlayPositionRelativeToMapEdges(this.marker, div);

                                            var left = overlayPos.x;
                                            var top = overlayPos.y;
                                            
                                            var arrowClass = "dmr";
                                            
                                            arrowClass += overlayPos.yClass;
                                            arrowClass += overlayPos.xClass;
                                                                                        
                                            div.style.top = top + "px";
                                            div.style.left = left + "px";
                                            
                                            div.innerHTML = this.html.replace("#CLSS#", arrowClass);
                                            
                                            map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
                                            
                                            this._map = map;
                                            this._div = div;                                                                            
                                        }
                                        
houseInfoOverlay.prototype.remove = function()
                                    {
                                        this._div.parentNode.removeChild(this._div);
                                    }
                                    
houseInfoOverlay.prototype.redraw = function(force)
                                    {
                                        var overlayPos = getOverlayPositionRelativeToMapEdges(this.marker, this._div);
                                        
                                        var left = overlayPos.x;
                                        var top = overlayPos.y;
                                        
                                        var arrowClass = "dmr";
                                            
                                        arrowClass += overlayPos.yClass;
                                        arrowClass += overlayPos.xClass;
                                        
                                        this._div.firstChild.className = arrowClass;
                                            
                                        this._div.style.top = top + "px";
                                        this._div.style.left = left + "px";
                                    }                                    
function getLoadingElement(container)
{
    if(document.getElementById("VELoading") == null)
    {
        var el = document.createElement("div");
        el.setAttribute("id", "VELoading");
        
        var currWidth = container.offsetWidth;
        var currHeight = container.offsetHeight;
        
        el.style.position = "absolute";
        el.style.top = ((currHeight - 25) / 2) + "px";
        el.style.left = ((currWidth - 105) / 2) + "px";
        el.style.border = "1px solid gray";
        el.style.font = "12px Verdana";
        el.style.background = "white";
        el.style.padding = "2px";
        el.style.verticalAlign = "middle";
        el.style.zIndex = "1000";
        el.style.width = "195px";
        el.innerHTML = "<img src='/gw/images/spinner.gif' alt='Please wait' />&nbsp;Please Wait. Loading data...";
        
        return el;
    }
}

///Loading
function showLoading()
{
    var mapContainer = $("#myMap")[0];
    
    $("#myMap").append(getLoadingElement(mapContainer));
}

function hideLoading()
{
    if(document.getElementById("VELoading"))
    {
        $("#VELoading").remove();
    }
}

function getOverlayPositionRelativeToMapEdges(_marker, element)
{
    var divWidth = 184;
    var divHeight = element.offsetHeight;

    var height = map.getSize().height;
    var width = map.getSize().width;
    
    var pinY = map.fromLatLngToContainerPixel(_marker.getLatLng()).y;
    var pinX = map.fromLatLngToContainerPixel(_marker.getLatLng()).x;
    
    var markerPosX = map.fromLatLngToContainerPixel(_marker.getLatLng()).x;
    var markerPosY = map.fromLatLngToContainerPixel(_marker.getLatLng()).y;
    
    var xPos = map.fromLatLngToDivPixel(_marker.getLatLng()).x;
    var yPos = map.fromLatLngToDivPixel(_marker.getLatLng()).y;
    
    var yPlace = "top";
    var xPlace = "left";
    
    var lowerMiddle = (height / 2) - 40;
    var upperMiddle = (height / 2) + 40;
    
    if(pinY < lowerMiddle)
    {
        yPos -= 10;
        yPlace = "top";
    }
    else if(pinY >= lowerMiddle && pinY <= upperMiddle)
    {   
        yPos -= (divHeight / 2) - (_marker.getIcon().iconSize.height / 4) + 10;
        yPlace = "mid";
    }
    else if(pinY > upperMiddle)
    {
        yPos -= (divHeight - 10);
        yPlace = "bottom";
    }
    
    //alert("Y: " + markerPosY + " and X: " + markerPosX);
    
    if(pinX < width / 2)
    {           
        xPos += (_marker.getIcon().iconSize.width / 2) + 5;
        xPlace = "left";
    }
    else
    {
        xPos -= divWidth + 20;
        xPlace = "right";
    }
    
    return {x:xPos, y:yPos, xClass:xPlace, yClass:yPlace};
}

function centerAndZoom(lat, lng)
{
    var curLevel = map.getZoom();
        
    map.setCenter(new GLatLng(lat,lng), curLevel + 2);
}
