﻿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());
    map.setZoom(map.getBoundsZoomLevel(bounds));
}

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()
{
    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.width = "150px";
                                            div.style.color = "#000";
                                            div.style.backgroundColor = "#FFF";
                                            div.style.padding = "3px";
                                            div.style.border = "solid 1px #676767";
                                            div.innerHTML = this.html;

                                            var overlayPos = getOverlayPositionRelativeToMapEdges(this.marker, div);

                                            var left = overlayPos.x;
                                            var top = overlayPos.y;
                                            
                                            div.style.top = top + "px";
                                            div.style.left = left + "px";
                                            
                                            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;
                                        
                                        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 ariel";
        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' />&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 = 150;
    var divHeight = element.offsetHeight;

    var height = map.getSize().height;
    var width = map.getSize().width;
    
    var markerPosX = map.fromLatLngToContainerPixel(_marker.getLatLng()).x;
    var markerPosY = map.fromLatLngToContainerPixel(_marker.getLatLng()).y;
    
    var xPos = map.fromLatLngToDivPixel(_marker.getLatLng()).x + 15;
    var yPos = map.fromLatLngToDivPixel(_marker.getLatLng()).y - 12;
    
    if((markerPosX + divWidth + 100) >= width)
    {
        xPos -= divWidth + 30;
    }
    
    if((markerPosY + divHeight) >= height)
    {
        yPos -= divHeight + 3;
    }
    
    return {x:xPos, y:yPos};
}