﻿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());
            // save position - so recenter() method works correctly
            GMap.savePosition();
            geocoder = new GClientGeocoder();
            houseIcon = new GIcon(); 
            houseIcon.image = "03_images/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 loadMapFromKML(map, fileLoc)
{
    try{
        if (GBrowserIsCompatible() && map != null) 
        {        
            GMap = new GMap2(map);
            //center map, in order to use GeoXML auto centering method
            GMap.setCenter(new GLatLng(49.496675,-102.65625), 3);
            //create coloured by getting KML file
            var geoXml = new GGeoXml(fileLoc,function() {
                if (geoXml.loadedCorrectly()) {
                    //auto center and zoom map
                    geoXml.gotoDefaultViewport(GMap);
                    //save position
                    GMap.savePosition();
                }});
            //zoom control and show coloured areas
            GMap.addControl(new GSmallMapControl());
            GMap.addOverlay(geoXml);
            //used to get address GLatLng
            geocoder = new GClientGeocoder();
            //icon used to show properties on the map
            houseIcon = new GIcon(); 
            houseIcon.image = '03_images/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.setCenter(new GLatLng(vmapLats,vmapLong), vmapZoom);
    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 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)
{
    try{
        // create property marker
        var marker = new GMarker(new GLatLng(propertyCoords[1], propertyCoords[0]), houseIcon);
        // add marker to map
        GMap.addOverlay(marker);
            
        // created content to display in pop up window
        var html = "<table border='0' cellpadding='0' cellspacing='0' width='400px'>";
        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'>" + propertyName + "<br />";
        html += "<div style='text-align:right;font-size:10pt;'><a href='" + listingID + "'>View Listing</a></div></td>";
        html += "</tr></table>";
        GEvent.addListener(marker, 'click', function() {
            marker.openInfoWindowHtml(html);});
    }
    catch (err) {}
}
