﻿var GMap = null;
var geocoder = null;
var houseIcon = null;
var vmapLong = null;
var vmapLats = null;
var vmapZoom = null;
var count = 0;
var countNot = 0;

function loadMap(map, vlong, vlats, zoom) 
{
    try{
        if (GBrowserIsCompatible() && map != null) 
        {
            // long and lats
            vmapLong = vlong;
            vmapLats = vlats;
            vmapZoom = zoom;
            GMap = new GMap2(map);
            GMap.setCenter(new GLatLng(vlats,vlong), zoom);
            GMap.addControl(new GSmallMapControl());
            GMap.addControl(new TextualCenterMapControl());
            // save position - so recenter() method works correctly
            GMap.savePosition();
            geocoder = new GClientGeocoder();
            houseIcon = new GIcon(); 
            houseIcon.image = 'Images/MapMarkers/houseIcon.gif';
            //houseIcon.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
            houseIcon.iconSize = new GSize(27, 45);
            //houseIcon.shadowSize = new GSize(22, 20);
            houseIcon.iconAnchor = new GPoint(14, 45);
            houseIcon.infoWindowAnchor = new GPoint(13, 1);
            
            // say map was loaded
            return true;
        }
    }                     
    catch(err){}
    
    // say map was not loaded
    return false;
}

function recenter()
{
    GMap.returnToSavedPosition();
}

function addLocation(address)
{
    geocoder.getLocations(address, addAddressToMap);
}

function getIndex(response)
{
    for (var y in addresses)
    {
        if (addresses[y] == response.name)
        {
            return y;
        }
    }
    return -1;
}

function addToMap(image, width, height, vLong, vLats)
{
    //create GIcon to place on map center
    tempIcon = new GIcon(); 
    tempIcon.image = image;
    //houseIcon.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    tempIcon.iconSize = new GSize(width, height);
    //houseIcon.shadowSize = new GSize(22, 20);
    tempIcon.iconAnchor = new GPoint(Math.ceil((width / 2)), height);
    tempIcon.infoWindowAnchor = new GPoint((Math.ceil((width / 2))-1), 1);
    //create marker and set to center of map
    var marker = new GMarker(new GLatLng(vLats,vLong), tempIcon);
    GMap.addOverlay(marker);
}

function addToMapCenter(image, width, height)
{
    //create GIcon to place on map center
    tempIcon = new GIcon(); 
    tempIcon.image = image;
    //houseIcon.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    tempIcon.iconSize = new GSize(width, height);
    //houseIcon.shadowSize = new GSize(22, 20);
    tempIcon.iconAnchor = new GPoint(Math.ceil((width / 2)), height);
    tempIcon.infoWindowAnchor = new GPoint((Math.ceil((width / 2))-1), 1);
    //create marker and set to center of map
    var marker = new GMarker(new GLatLng(vmapLats,vmapLong), tempIcon);
    GMap.addOverlay(marker);
}
function addAddressToMap(listingID, imageUrl, propertyName, propertyCoords, bCenterMap)
{
    try{
        // create property marker
        var marker = new GMarker(new GLatLng(propertyCoords[1], propertyCoords[0]), getPropertyIcon(listingID));
        // add marker to map
        GMap.addOverlay(marker);
            
        // created content to display in pop up window
        var html = "<table border='0' cellpadding='0' cellspacing='0'>";
        html += "<tr><td valign='top' align='left' style='width:114px'><img src='" + imageUrl + "' height='69px' width='114px' /></td>";
        html += "<td valign='top' align='left' style='padding-left:2px;width:250px;'><b>" + propertyName + "</b>";
        html += "</td>";
        html += "</tr></table>";
        GEvent.addListener(marker, 'click', function() {
            marker.openInfoWindowHtml(html);});
        
        // check if map should be centered on property    
        if (bCenterMap)
        {
            // set the center to be around property
            GMap.setCenter(new GLatLng(propertyCoords[1], propertyCoords[0]), 10);
            // save position - so recenter() method sends user this position
            GMap.savePosition();
        }
    }
    catch (err) {}
}

function getPropertyIcon(listingID)
{
    try{
        // create new Google map icon
        var propertyIcon = new GIcon();
        
        // used the listingID to determine which icon to use
        switch (listingID)
        {
            // Dolphin Springs
            case "123":
                propertyIcon.image = 'Images/MapMarkers/dolphinSprings.png';
                //houseIcon.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
                propertyIcon.iconSize = new GSize(39, 33);
                //houseIcon.shadowSize = new GSize(22, 20);
                propertyIcon.iconAnchor = new GPoint(20, 33);
                propertyIcon.infoWindowAnchor = new GPoint(19, 1);
                break;
            // Formosa Springs
            case "128":
                propertyIcon.image = 'Images/MapMarkers/formosaSprings.png';
                //houseIcon.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
                propertyIcon.iconSize = new GSize(37, 36);
                //houseIcon.shadowSize = new GSize(22, 20);
                propertyIcon.iconAnchor = new GPoint(19, 36);
                propertyIcon.infoWindowAnchor = new GPoint(18, 1);
                break;
            // Captain Nemo's
            case "129":
                propertyIcon.image = 'Images/MapMarkers/captainNemo.png';
                //houseIcon.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
                propertyIcon.iconSize = new GSize(39, 29);
                //houseIcon.shadowSize = new GSize(22, 20);
                propertyIcon.iconAnchor = new GPoint(20, 29);
                propertyIcon.infoWindowAnchor = new GPoint(19, 1);
                break;
            // Forest Springs
            case "130":
                propertyIcon.image = 'Images/MapMarkers/forestSprings.png';
                //houseIcon.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
                propertyIcon.iconSize = new GSize(26, 36);
                //houseIcon.shadowSize = new GSize(22, 20);
                propertyIcon.iconAnchor = new GPoint(13, 36);
                propertyIcon.infoWindowAnchor = new GPoint(12, 1);
                break;
            // Tigger's Treehouse
            case "134":
                propertyIcon.image = 'Images/MapMarkers/tiggerTreehouse.png';
                //houseIcon.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
                propertyIcon.iconSize = new GSize(38, 32);
                //houseIcon.shadowSize = new GSize(22, 20);
                propertyIcon.iconAnchor = new GPoint(19, 32);
                propertyIcon.infoWindowAnchor = new GPoint(18, 1);
                break;
            // Default - properties who have no icon yet
            default: 
                propertyIcon.image = 'Images/MapMarkers/houseIcon.gif';
                //houseIcon.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
                propertyIcon.iconSize = new GSize(27, 45);
                //houseIcon.shadowSize = new GSize(22, 20);
                propertyIcon.iconAnchor = new GPoint(14, 45);
                propertyIcon.infoWindowAnchor = new GPoint(13, 1);
        }
        
        // return the property icon
        return propertyIcon;
    }
    catch (err) {}
    
    // return the house icon
    return houseIcon
}

// A TextualCenterMapControl is a GControl that displays textual "Recenter Map" button.

// We define the function first
function TextualCenterMapControl() {
}

// To "subclass" the GControl, we set the prototype object to
// an instance of the GControl object
TextualCenterMapControl.prototype = new GControl();

// Creates a DIV element with the Text "Recenter Map".
TextualCenterMapControl.prototype.initialize = function(map) {
  var container = document.createElement("div");

  var zoomInDiv = document.createElement("div");
  this.setButtonStyle_(zoomInDiv);
  container.appendChild(zoomInDiv);
  zoomInDiv.appendChild(document.createTextNode("Recenter Map"));
  GEvent.addDomListener(zoomInDiv, "click", function() {
    map.returnToSavedPosition();
  });
  
  map.getContainer().appendChild(container);
  return container;
}

// By default, the control will appear in the top right corner of the
// map with 7 pixels of padding.
TextualCenterMapControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7));
}

// Sets the proper CSS for the given button element.
TextualCenterMapControl.prototype.setButtonStyle_ = function(button) {
  button.style.textDecoration = "underline";
  button.style.color = "#0000cc";
  button.style.backgroundColor = "white";
  button.style.font = "small Arial";
  button.style.border = "1px solid black";
  button.style.padding = "2px";
  button.style.marginBottom = "3px";
  button.style.textAlign = "center";
  button.style.width = "100px";
  button.style.cursor = "pointer";
}

