/********************************************************
 * Google map utility functions for Great Real Estate
 * by Roger Theriault, version 0.1.0 2008-06-15
 *
 * Requires a function gre_setupmap() defined to set the position
 * and marker overlay
 * and also requires the page needing this to invoke the google loader
 */

/*
 * Changelog:
 * [2008-06-27] added functions to handle XML population of featured map
 * [2008-06-27] changed id of map canvas from map_canvas to gre_map_canvas
 *              to reduce conflicts
 *
 */

	
var gre_map;
var gre_multi_map;
var gre_customIcons = [];

function gre_setup_custom_icons() {
    var iconForSale;
    var iconForRent;
    var iconGLTActive;
    iconForSale = new GIcon();
    iconForSale.image = 'http://labs.google.com/ridefinder/images/mm_20_green.png';
    iconForSale.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    iconForSale.iconSize = new GSize(12, 20);
    iconForSale.shadowSize = new GSize(22, 20);
    iconForSale.iconAnchor = new GPoint(6, 20);
    iconForSale.infoWindowAnchor = new GPoint(5, 1);

    iconForRent = new GIcon(); 
    iconForRent.image = 'http://labs.google.com/ridefinder/images/mm_20_blue.png';
    iconForRent.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    iconForRent.iconSize = new GSize(12, 20);
    iconForRent.shadowSize = new GSize(22, 20);
    iconForRent.iconAnchor = new GPoint(6, 20);
    iconForRent.infoWindowAnchor = new GPoint(5, 1);

    iconSold = new GIcon(); 
    iconSold.image = 'http://labs.google.com/ridefinder/images/mm_20_red.png';
    iconSold.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    iconSold.iconSize = new GSize(12, 20);
    iconSold.shadowSize = new GSize(22, 20);
    iconSold.iconAnchor = new GPoint(6, 20);
    iconSold.infoWindowAnchor = new GPoint(5, 1);
    
    iconGLTActive = new GIcon(); 
    iconGLTActive.image = 'http://www.goodlifeteam.com/wp-content/themes/goodlifeteam/images/marker-glt.png';
    iconGLTActive.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    iconGLTActive.iconSize = new GSize(20, 34);
    iconGLTActive.shadowSize = new GSize(22, 20);
    iconGLTActive.iconAnchor = new GPoint(10, 34);
    iconGLTActive.infoWindowAnchor = new GPoint(5, 1);

    gre_customIcons["sold"] = iconSold;
    gre_customIcons["forrent"] = iconForRent;
    gre_customIcons["forsale"] = iconForSale;
    gre_customIcons["gltactive"] = iconGLTActive
}

function gre_createMarker(point,html) {
       	var marker = new GMarker(point);
       	GEvent.addListener(marker, "click", function() {
       		marker.openInfoWindowHtml(html);
       	});
       	return marker;
}

function gre_createMarkerCustom(point, name, address, type, ref, img) {
      var marker = new GMarker(point, gre_customIcons[type]);
      var html = img + '<strong>' + name + "</a></strong> <br />" + address +
	'<br />' + '<a href="' + ref + '">listing info</a>' 
      GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
      });
      return marker;
    }



function gre_mapinitialize() {
	jQuery(window).bind("unload", GUnload); // guard against leaks

	gre_map = new google.maps.Map2(document.getElementById("gre_map_canvas"));
	gre_map.addControl(new google.maps.LargeMapControl());
	gre_map.addControl(new google.maps.MapTypeControl());
	gre_setupmap();
}

// set up map of all properties in feed
function gre_initmultimap() {
	jQuery(window).bind("unload", GUnload); // guard against leaks

	gre_setup_custom_icons();

	gre_multi_map = new google.maps.Map2(document.getElementById("gre_map_multi"));
	gre_multi_map.addControl(new google.maps.LargeMapControl());
	gre_multi_map.addControl(new google.maps.MapTypeControl());
	var temp_point = new google.maps.LatLng(0,0);
	gre_multi_map.setCenter(temp_point, 5);
	GDownloadUrl("/feed/googlemaps/", gre_addmarkers );
}


// set up map of all properties in south austin
function gre_init_socomap() {
	jQuery(window).bind("unload", GUnload); // guard against leaks

	gre_setup_custom_icons();

	gre_multi_map = new google.maps.Map2(document.getElementById("gre_map_multi"));
	var temp_point = new google.maps.LatLng(0,0);
	gre_multi_map.setCenter(temp_point, 5);
	gre_multi_map.disableDoubleClickZoom();
	gre_multi_map.disableScrollWheelZoom();
	gre_multi_map.setCenter(new GLatLng(30.243571, -97.765703), 14);
	
	var boundaries = new GLatLngBounds(new GLatLng(30.221473,-97.794371), new GLatLng(30.265665,-97.737637));
    var socomap = new GGroundOverlay("http://www.goodlifeteam.com/wp-content/themes/goodlifeteam/images/socosofisola-bg.gif", boundaries);
	
	GDownloadUrl("/feed/googlemaps/", gre_addmarkersnomove );
	
	gre_multi_map.addOverlay(socomap);
	
	// Set up the copyright information
	// Each image used should indicate its copyright permissions
	var myCopyright = new GCopyrightCollection("© ");
	myCopyright.addCopyright(new GCopyright('Demo',
  	new GLatLngBounds(new GLatLng(-90,-180), new GLatLng(90,180)),
  	0,'©2007 Google'));

	// Create the tile layer overlay and 
	// implement the three abstract methods                 
	var tilelayer = new GTileLayer(myCopyright);
	tilelayer.getTileUrl = function() { return "http://www.goodlifeteam.com/wp-content/themes/goodlifeteam/images/tilegreen.gif"; };
	tilelayer.isPng = function() { return true;};
	tilelayer.getOpacity = function() { return 1.0; }

	var myTileLayer = new GTileLayerOverlay(tilelayer);
	
	gre_multi_map.addOverlay(myTileLayer);
	
}

// set up map of all properties in east austin
function gre_init_eastsidemap() {
	jQuery(window).bind("unload", GUnload); // guard against leaks

	gre_setup_custom_icons();

	gre_multi_map = new google.maps.Map2(document.getElementById("gre_map_multi"));
	var temp_point = new google.maps.LatLng(0,0);
	gre_multi_map.setCenter(temp_point, 5);
	gre_multi_map.disableDoubleClickZoom();
	gre_multi_map.disableScrollWheelZoom();
	gre_multi_map.setCenter(new GLatLng(30.261662, -97.708111), 14);
	
	// 30.283974,-97.679529
	// 30.239716,-97.736435
	
	var boundaries = new GLatLngBounds(new GLatLng(30.239716,-97.736435), new GLatLng(30.283974,-97.679529));
    var eastmap = new GGroundOverlay("http://www.goodlifeteam.com/wp-content/themes/goodlifeteam/images/east-bg.gif", boundaries);
	
	GDownloadUrl("/feed/googlemaps/", gre_addmarkersnomove );
	
	gre_multi_map.addOverlay(eastmap);
	
	
	// Set up the copyright information
	// Each image used should indicate its copyright permissions
	var myCopyright = new GCopyrightCollection("© ");
	myCopyright.addCopyright(new GCopyright('Demo',
  	new GLatLngBounds(new GLatLng(-90,-180), new GLatLng(90,180)),
  	0,'©2007 Google'));

	// Create the tile layer overlay and 
	// implement the three abstract methods                 
	var tilelayer = new GTileLayer(myCopyright);
	tilelayer.getTileUrl = function() { return "http://www.goodlifeteam.com/wp-content/themes/goodlifeteam/images/tilegreen.gif"; };
	tilelayer.isPng = function() { return true;};
	tilelayer.getOpacity = function() { return 1.0; }

	var myTileLayer = new GTileLayerOverlay(tilelayer);
	
	gre_multi_map.addOverlay(myTileLayer);
	
}

// set up map of all properties in downtown austin
function gre_init_downtownmap() {
	jQuery(window).bind("unload", GUnload); // guard against leaks

	gre_setup_custom_icons();

	gre_multi_map = new google.maps.Map2(document.getElementById("gre_map_multi"));
	var temp_point = new google.maps.LatLng(0,0);
	gre_multi_map.setCenter(temp_point, 5);
	gre_multi_map.disableDoubleClickZoom();
	gre_multi_map.disableScrollWheelZoom();
	gre_multi_map.setCenter(new GLatLng(30.272448, -97.74725), 15);
	
	var boundaries = new GLatLngBounds(new GLatLng(30.261514,-97.761712), new GLatLng(30.283307,-97.733045));
    var downmap = new GGroundOverlay("http://www.goodlifeteam.com/wp-content/themes/goodlifeteam/images/downtown-bg.gif", boundaries);
	
	GDownloadUrl("/feed/googlemaps/", gre_addmarkersnomove );
	
	gre_multi_map.addOverlay(downmap);
	
	
	// Set up the copyright information
	// Each image used should indicate its copyright permissions
	var myCopyright = new GCopyrightCollection("© ");
	myCopyright.addCopyright(new GCopyright('Demo',
  	new GLatLngBounds(new GLatLng(-90,-180), new GLatLng(90,180)),
  	0,'©2007 Google'));

	// Create the tile layer overlay and 
	// implement the three abstract methods                 
	var tilelayer = new GTileLayer(myCopyright);
	tilelayer.getTileUrl = function() { return "http://www.goodlifeteam.com/wp-content/themes/goodlifeteam/images/tilegreen.gif"; };
	tilelayer.isPng = function() { return true;};
	tilelayer.getOpacity = function() { return 1.0; }

	var myTileLayer = new GTileLayerOverlay(tilelayer);
	
	gre_multi_map.addOverlay(myTileLayer);
	
}

function gre_addmarkers( data ) {
	var xml = GXml.parse(data);
	var markers = xml.documentElement.getElementsByTagName("marker");
	var maxLat = 0;
	var minLat = 91;
	var maxLng = -181;
	var minLng = 181;
	for (var i = 0; i < markers.length; i++) {
		var name = markers[i].getAttribute("name");
		var address = markers[i].getAttribute("address");
		address = address.replace(/,/,'<br />');
		var type = markers[i].getAttribute("type");
		var ref = markers[i].getAttribute("ref");
		var info = markers[i].getElementsByTagName("info")[0].childNodes[0].nodeValue;
		var lat = parseFloat(markers[i].getAttribute("lat"));
		var lng = parseFloat(markers[i].getAttribute("lng"));
		var point = new GLatLng(lat,lng);
		if (lat > maxLat) maxLat = lat;
		if (lat < minLat) minLat = lat;
		if (lng > maxLng) maxLng = lng;
		if (lng < minLng) minLng = lng;
		var marker = gre_createMarkerCustom(point, name, address, type, ref, info);
		gre_multi_map.addOverlay(marker);
	}
	var ctrLat = minLat + (maxLat - minLat)/2;
	var ctrLng = minLng + (maxLng - minLng)/2;
	var new_point = new google.maps.LatLng(ctrLat,ctrLng);
	gre_multi_map.setCenter(new_point, 10);
}

function gre_addmarkersnomove( data ) {
	var xml = GXml.parse(data);
	var markers = xml.documentElement.getElementsByTagName("marker");
	var maxLat = 0;
	var minLat = 91;
	var maxLng = -181;
	var minLng = 181;
	for (var i = 0; i < markers.length; i++) {
		var name = markers[i].getAttribute("name");
		var address = markers[i].getAttribute("address");
		address = address.replace(/,/,'<br />');
		var type = markers[i].getAttribute("type");
		var ref = markers[i].getAttribute("ref");
		var info = markers[i].getElementsByTagName("info")[0].childNodes[0].nodeValue;
		var lat = parseFloat(markers[i].getAttribute("lat"));
		var lng = parseFloat(markers[i].getAttribute("lng"));
		var point = new GLatLng(lat,lng);
		if (lat > maxLat) maxLat = lat;
		if (lat < minLat) minLat = lat;
		if (lng > maxLng) maxLng = lng;
		if (lng < minLng) minLng = lng;
		var marker = gre_createMarkerCustom(point, name, address, type, ref, info);
		gre_multi_map.addOverlay(marker);
	}
}
