﻿// JScript File
var map;
var gDir;
var gSearch;
var directionsSummaryDiv;

function initialiseMap()
{
    setupMap();
    setupSearch();
    setupDirections();
}

function setupMap()
{
    if(map == null)
    {
        map = new GMap2(document.getElementById("devMap"), {size: new GSize(452, 370)});
//        map.setUIToDefault();
        map.setCenter(new GLatLng(devLatitude, devLongitude), zoomLevel);
    
        var mapUI = map.getDefaultUI();
        mapUI.maptypes.physical = false;
        
        map.setUI(mapUI);
    }
    
    addHouseIcon();
}

function addHouseIcon()
{
    var loc = new GLatLng(devLatitude, devLongitude);
    //var theme = getThemeFromHost(location.hostname);

    var houseIcon = new GIcon(G_DEFAULT_ICON);
    
    for(var i = 0; i < pinArray.length; i++)
    {
        var title = pinArray[i][2];
        var desc = pinArray[i][3];
        var icon = pinArray[i][4];
        
        houseIcon.image = icon;
        houseIcon.iconSize = new GSize(16, 16);
        houseIcon.iconAnchor = new GPoint(5,16);
        houseIcon.shadowSize = new GSize(0, 0);
            
        var pin = new GMarker(loc, {icon:houseIcon});
        
        
        var infoOverlay = new houseInfoOverlay(pin, generateInfoHtml(title, desc, icon));
        
        map.addOverlay(pin);
        
        GEvent.addListener(pin, "mouseover", function(){ displayHouseOverlay(pin, infoOverlay); } );
        GEvent.addListener(pin, "mouseout", function(){ hideHouseOverlay(this); } );
    }
}

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;
}

function generateInfoHtml(title, desc, icon)
{
    var container = document.createElement("div");
    var infoDiv = document.createElement("div");
    infoDiv.className = "pinInfo";
    
    var titleDiv = document.createElement("div");
    titleDiv.className = "VE_Pushpin_Popup_Title";
    
    titleDiv.innerHTML = "<p>"  + title + "</p>";
    
    var bodyDiv = document.createElement("div");
    bodyDiv.className = "VE_Pushpin_Popup_Body";
    
    bodyDiv.innerHTML = desc;
    
    infoDiv.appendChild(titleDiv);
    infoDiv.appendChild(bodyDiv);
    
    container.appendChild(infoDiv);
    
    return container.innerHTML;
}

function setupSearch()
{
    gSearch = new GlocalSearch();
    
    gSearch.setSearchCompleteCallback(null, function() {performDirectionsLoad(gSearch.results[0]);}); 
}

function setupDirections()
{
    directionsSummaryDiv = document.getElementById("drivingDirections");

    gDir = new GDirections(map, directionsSummaryDiv);
    
    GEvent.addListener(gDir, "load", onDirectionsLoad);
    GEvent.addListener(gDir, "error", onDirectionsError);
    GEvent.addListener(gDir, "addoverlay", onDirectionsOverlayComplete);
}

function getDirections()
{
    var val = document.getElementById("mapPostcode").value;
    
    if(val == null) return;
    
    gSearch.execute(val + ", UK");
}

function performDirectionsLoad(loc)
{
    var query = "from:" + loc.lat + "," + loc.lng + " to:" + devLatitude + "," + devLongitude;
    
    gDir.load(query);
}

function onDirectionsLoad()
{
    directionsSummaryDiv.style.display = "block";
}

function onDirectionsError()
{
    alert("There was a problem calculating this route, please ensure your input address is valid and try again.\nIf this error continues please contact the website administrator.\n" + gDir.getStatus().code + ": " + gDir.getStatus().request);
}

function onDirectionsOverlayComplete()
{
    map.removeOverlay(gDir.getMarker(gDir.getNumGeocodes() - 1));
}


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 left = (map.fromLatLngToDivPixel(this.marker.getLatLng()).x + 15);
                                            var top = (map.fromLatLngToDivPixel(this.marker.getLatLng()).y - 30);

                                            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()
                                    {
                                        var element = this;
                                        
                                        element._div.parentNode.removeChild(element._div);
                                    }
houseInfoOverlay.prototype.redraw = function(force)
                                    {
                                        var left = (this._map.fromLatLngToDivPixel(this.marker.getLatLng()).x + 15);
                                        var top = (this._map.fromLatLngToDivPixel(this.marker.getLatLng()).y - 30);
                                        
                                        this._div.style.top = top + "px";
                                        this._div.style.left = left + "px";
                                    }                                    
