﻿function closeRefineBox(refinepanelid, sortpanelid) {
    document.getElementById(refinepanelid).style.visibility = "hidden";
    if (document.getElementById(sortpanelid) != null) {
        document.getElementById(sortpanelid).style.visibility = "visible";
    }
}
function openRefineBox(refinepanelid, sortpanelid) {
    trackEvent('/Find-An-Agent-App/SearchResults/RefineResults/', 'Refine Results');
    document.getElementById(refinepanelid).style.visibility = "visible";
    if (document.getElementById(sortpanelid) != null) {
        document.getElementById(sortpanelid).style.visibility = "hidden";
    }
}
var baseIcon;
var geocoder;
var side_bar_html = "";
/* arrays to hold copies of the markers and html used by the side_bar because the function closure trick doesnt work there*/
var gmarkers = [];
var htmls = [];
var i = 1;
/* arrays to hold variants of the info window html with get direction forms open*/
var to_htmls = [];
var from_htmls = [];
/* functions that open the directions forms*/
function tohere(i) {
    gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}
function fromhere(i) {
    gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}
function changezoom(level, id) {
    /*save the zoom level value in the zoomlevel hidden field*/
    var zoomlevelfld = document.getElementById("zoomlevel");
    if (zoomlevelfld != null)
        zoomlevelfld.value = level;
    initmap(lat, lng, level, xml, id);
    zoom_level = level;
    canvas = id;
}
function gridclick(i) {
    if (gmarkers[i] != null) {
        GEvent.trigger(gmarkers[i], "click");
    }
}
function createMarker(point, index, html) {
    var letter = String.fromCharCode("A".charCodeAt(0) + index);
    var letteredIcon = new GIcon(baseIcon);
    letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
    /*Set up our GMarkerOptions object*/
    markerOptions = {
        icon: letteredIcon
    };
    var marker = new GMarker(point, markerOptions);
    html = '<div class=\"bubbleStyle\"><span>' + html + '</span></div>';
    /* The info window version with the "to here" form open*/
    to_htmls[index] = html + '<div class="directionsThree"><span>Get directions: To here - <a href="javascript:fromhere(' + i + ')">From here</a>' + '<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' + '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' + '<INPUT value="Get Directions" TYPE="SUBMIT">' + '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + 
   '"/></span></div>';
    /* The info window version with the "to here" form open*/
    from_htmls[index] = html + '<div class="directionsTwo"><span>Get directions: <a href="javascript:tohere(' + i + ')">To here</a> - From here' + '<br>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' + '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' + '<INPUT value="Get Directions" TYPE="SUBMIT">' + '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() + 
   '"/></span></div>';
    /* The inactive version of the direction info*/
    html = html + '<div class="directionsOne"><span>Get directions: <a href="javascript:tohere(' + index + ')">To here</a> - <a href="javascript:fromhere(' + index + ')">From here</a></span></div>';
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
    }
   );
    /* save the info we need to use later for the side_bar*/
    gmarkers[index] = marker;
    htmls[index] = html;
    return marker;
}
function initmap(initlat, initlng, zoom, xml, canvas) {
  if (typeof window.GBrowserIsCompatible == 'function') { /*check for existence of this function which is in the script file downloaded from Google*/
    if (GBrowserIsCompatible()) {
        if (zoom == 13) {
            zoom_level = zoom + 2;
            top_link = '<img src="/imageserver/plumtree/portal/custom/SafecoCom/Image/icon_txt_view.gif" width="15" heigth="12" border="0"/>&nbsp;&nbsp;Text View ' + '<a href="javascript:changezoom(' + zoom_level + ',\'map-view-canvas\'); trackEvent(\'/Find-An-Agent-App/SearchResults/SwitchViews/Map\',\'Switch Views\');">' + '<img src="/imageserver/plumtree/portal/custom/SafecoCom/Image/icon_map_view.gif" width="15" heigth="12" border="0"/>&nbsp;&nbsp;Map View' + '</a><br>';
            document.getElementById("map-top-link").innerHTML = top_link;
            document.getElementById("map-map-view").style.display = "none";
            document.getElementById("map-text-view").style.display = "";
            document.getElementById("map-view-canvas").style.display = "none";
            document.getElementById("map-canvas").style.display = "";
            document.getElementById("ddl-sort-and-filter").style.display = "block";
        }
        else {
            top_link = '<a href="javascript:changezoom(' + 13 + ',\'map-canvas\'); trackEvent(\'/Find-An-Agent-App/SearchResults/SwitchViews/Text\', \'Switch Views\');">' + '<img src="/imageserver/plumtree/portal/custom/SafecoCom/Image/icon_txt_view.gif" width="15" heigth="12" border="0"/>&nbsp;&nbsp;Text View' + '</a>' + '<img class="iconMapView" src="/imageserver/plumtree/portal/custom/SafecoCom/Image/icon_map_view.gif" width="15" heigth="12" border="0"/>&nbsp;&nbsp;Map View';
            document.getElementById("map-top-link").innerHTML = top_link;
            document.getElementById("map-text-view").style.display = "none";
            document.getElementById("map-map-view").style.display = "";
            document.getElementById("map-view-canvas").style.display = "";
            document.getElementById("map-canvas").style.display = "none";
            document.getElementById("ddl-sort-and-filter").style.display = "none";
        }
        /* init base icon */
        baseIcon = new GIcon();
        baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new GSize(20, 34);
        baseIcon.shadowSize = new GSize(37, 34);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);
        baseIcon.infoShadowAnchor = new GPoint(18, 25);
        var map = new GMap2(document.getElementById(canvas));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(initlat, initlng), zoom);
        /* Start with an empty GLatLngBounds object*/
        var bounds = new GLatLngBounds();
        var xmlDoc = GXml.parse(xml);
        var markers = xmlDoc.documentElement.getElementsByTagName("marker");
        var markerlat, markerlng;
        for (var j = 0; j < markers.length; j++) {
            /* obtain the attribues of each marker*/
            markerlat = parseFloat(markers[j].getAttribute("lat"));
            markerlng = parseFloat(markers[j].getAttribute("lng"));
            if (markerlat != 0 && markerlng != 0) {
                var point = new GLatLng(markerlat, markerlng);
                var html = markers[j].getAttribute("html");
                var label = markers[j].getAttribute("label");
                var marker = createMarker(point, j, html);
                map.addOverlay(marker);
                /* Each time a point is found, extent the bounds to include it*/
                bounds.extend(point);
            }
        }
        /* determine the zoom level from the bounds*/
        map.setZoom(map.getBoundsZoomLevel(bounds));
        /* determine the centre from the bounds*/
        map.setCenter(bounds.getCenter());
        /*map.enableGoogleBar();*/
    }
    else {
        alert('Google Maps API is not compatible with this browser');
    }
   }
  else
  { 	if (document.getElementById("map-canvas") != null) {
		document.getElementById("map-canvas").innerHTML = '<div style="font-size:12px;padding:10px;">Google maps are temporarily unavailable.</div>';
		document.getElementById("map-canvas").style.display = "none";
	}
  }
}
var OnLoad = 'initmap(lat,lng,zoom_level,xml,canvas)';
/*hiding the map canvas if the XML is empty*/
if (typeof(xml) != "undefined") 
{
    if (xml != null && xml != "") {
        setTimeout(OnLoad, 500);
    }
    else {
        if (document.getElementById("map-map-view") != null) { document.getElementById("map-map-view").style.display = "none"; }
        if (document.getElementById("map-view-canvas") != null) { document.getElementById("map-view-canvas").style.display = "none"; }
        if (document.getElementById("map-canvas") != null) { document.getElementById("map-canvas").style.display = "none"; }
    }
}
else {
        if (document.getElementById("map-map-view") != null) { document.getElementById("map-map-view").style.display = "none"; }
        if (document.getElementById("map-view-canvas") != null) { document.getElementById("map-view-canvas").style.display = "none"; }
        if (document.getElementById("map-canvas") != null) { document.getElementById("map-canvas").style.display = "none"; }
}



function loadMapNew(lat, lng) {
    if (typeof (google) != "undefined") {
        /*variables aname, html, lat, lng set in contactanagent.aspx.cs*/
        var myLatlng = new google.maps.LatLng(lat, lng);
        var myOptions = {
            zoom: 14,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("caa-map-canvas"), myOptions);
        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: aname
        });

        var window_content = '<div style="margin-top:10px;">' + html + '<div style="margin:5px 0px 0px 0px;">Directions from your location:' +
                '<form action="http://maps.google.com/maps" method="get" target="_blank" style="margin:0px;">' +
                '<input type="text" SIZE=40 MAXLENGTH=60 name="saddr" id="saddr" value="" /><br />' +
                '<INPUT value="Get Directions" TYPE="SUBMIT" style="margin-top:6px;">' +
                '<input type="hidden" name="daddr" value="' + lat + ',' + lng +
                '"/></div></div>';

        var infowindow = new google.maps.InfoWindow(
      { content: window_content,
          size: new google.maps.Size(50, 50),
          position: myLatlng
      });
        infowindow.open(map);

        var infowindow2 = new google.maps.InfoWindow(
      { content: html,
          size: new google.maps.Size(50, 50),
          position: myLatlng
      });

        google.maps.event.addListener(marker, 'click', function() {
            infowindow2.open(map);
        });
    }
}

function loadMap(lat, lng) {
    if (GBrowserIsCompatible()) {

        function createMarker(point, html) {
            var marker = new GMarker(point);
            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(html);
            });

            return marker;
        }

        /* Display the map, with some controls and set the initial location */
        var map = new GMap2(document.getElementById("caa-map-canvas"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(lat, lng), 14);
        /*map.enableGoogleBar();*/
        var point = new GLatLng(lat, lng);      
        var info_window = '<div style="height:100px;">' + html + '<br>Directions from your location:' +
                '<form action="http://maps.google.com/maps" method="get" target="_blank" style="margin:0px;">' +
                '<input type="text" SIZE=30 MAXLENGTH=60 name="saddr" id="saddr" value="" /><br />' +
                '<INPUT value="Get Directions" TYPE="SUBMIT" style="margin-top:7px;">' +
                '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() +
                '"/></div>';

        var marker = createMarker(point, html);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(info_window, { maxWidth: 150 });
    }
    else {
        alert("Google Maps is not compatible with this browser. We apologize for the inconvenience.");
    }
}    
    
/*for PrintFriendly page -------------------------------------------------------------------------------------------------------------------------------------------------------------*/
function loadMapPrintVersion(lat, lng) {
        /*variables aname, html, lat, lng set in contactanagent.aspx.cs*/
        var myLatlng = new google.maps.LatLng(lat, lng);
        var myOptions = {
            zoom: 14,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("caa-map-canvas"), myOptions);
        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map
        });        
}
