window.addEvent('domready', function() {
	// section for property.asp mapping.
	if ($('google-map-canvas-property')) {			
		var my_slide = new Fx.Slide('google-map-canvas-property',{transition:Fx.Transitions.Cubic.easeOut}).hide();
		var map_status = 0;
		var map_loaded = false;
		
		$('show_map').addEvent('click', function(el) {
			toggle_map_property();
		});
	}
	
	function toggle_map_property() {
		if (map_status == 0) {
			if (map_loaded == false) {
				map_loaded = true;
				
				var latitude = $('hid_latitude').value;
				var longitude = $('hid_longitude').value;
				var map = null;
										
				if (GBrowserIsCompatible()) { 
					map = new GMap2(document.getElementById("google-map-canvas-property"), { size: new GSize(537,400) });
					map.addControl(new GLargeMapControl());
					map.addControl(new GMapTypeControl());
											
					// declaring new GLatLng point
					var point = new GLatLng(latitude, longitude);
					// centering map
					map.setCenter(point, 15);
					// declaring marker and original marker for reference.
					var marker = new GMarker(point);
					// adding marker as overlay to map
					map.addOverlay(marker);
				}
				
				// handles the click event of the markere.
				GEvent.addListener(marker, "click", function() {
					info = '<h2>Get Directions</h2>';
					info += '<form action="http://maps.google.com/maps" method="get" class="notables google_info_box">';
					info += '<label for="saddr">Starting Address';
					info += '<input type="text" name="saddr" id="saddr" value="" /></label>';
					info += '<input type="submit" value="Go" class="submit" />'
					info += '<input type="hidden" name="daddr" value="' + latitude + ',' + longitude + '" class="hidden" />';
					info += '<input type="hidden" name="hl" value="en" class="hidden" />';
					info += '</form>';						 
																	
					marker.openInfoWindowHtml(info);
				});
			}
			
			my_slide.slideIn();
			$('show_map').set('html','Hide');
			map_status = 1;
			$('show_map_header').removeClass('noprint');
		} else {
			my_slide.slideOut();
			$('show_map').set('html','Show');
			map_status = 0;
			$('show_map_header').addClass('noprint');
		}
	}
	
	// section for results.asp mapping.
	if ($('google-map-canvas-results')) {					 
		var map = null;
		var bounds; // holds all of the latitude/longitude for map centering.
		var gmarkers = [];
	
		// setting opacity to .1 so users know that it is loading
		new Fx.Tween($('google-map-canvas-results'), {duration: 400, wait: false}).set('opacity', .1);
		new Request({
			url: 'results_list.asp?action=original&layout=join&map=true', 
			method: 'get', 
			onComplete: 
			build_map 
		}).send();
	}
	
	// adding click event to each property type checkbox
	$$('input[id^=chkPropertyType]').each(function(element) {
		$(element).addEvent('click', function(){
			// setting opacity to .1 so users know that it is loading
			new Fx.Tween($('google-map-canvas-results'), {duration: 400, wait: false}).set('opacity', .1);
			$('updating').setStyle("background", "url(../images/loading.gif) no-repeat center center");
			
			// clearing existing overlays
			map.clearOverlays();
			// clearing property list
			$('property_list').set('html','');
			
			// looping through each property type. if selected add it to array.
			var property_types = new Array();
			$$('input[id^=chkPropertyType]').each(function(element) {
				if ($(element).getProperty('checked') == true) {
					property_types.push($(element).getProperty('value'));
				}
			});
			
			// making ajax call
			new Request({
				url: 'results_list.asp?action=rebuild&layout=join&map=true&propertytypes=' + property_types,
				method: 'get', 
				onComplete: build_map
			}).send();
		});
	});
	
	function build_map(responseText, responseXML) {	
		// grabbing error in response if there is one.
		var error = responseXML.getElementsByTagName('error');
		
		if (error.length == 0) {
			// clearing any errors
			$('map_error').set('html', '');
			
			var property = responseXML.getElementsByTagName('property');
		
			// making sure the browser is compatible with google maps.
			if (GBrowserIsCompatible()) { 
				map = new GMap2(document.getElementById("google-map-canvas-results"), { size: new GSize(540,550) });
				map.addControl(new GLargeMapControl());
				map.addControl(new GMapTypeControl());
				map.setCenter(new GLatLng(0,0),0);
			}
			
			bounds = new GLatLngBounds()
					
			for (var i = 0; i < property.length; i++) {
				// getting values from xml response.
				var property_id = (property.item(i).getElementsByTagName("propertyid")[0].firstChild) ? property.item(i).getElementsByTagName("propertyid")[0].firstChild.nodeValue:"";
				var fldid = (property.item(i).getElementsByTagName("fldid")[0].firstChild) ? property.item(i).getElementsByTagName("fldid")[0].firstChild.nodeValue:"";
				var property_type_number = (property.item(i).getElementsByTagName("propertytypenumber")[0].firstChild) ? property.item(i).getElementsByTagName("propertytypenumber")[0].firstChild.nodeValue:"";
				var property_name = (property.item(i).getElementsByTagName("name")[0].firstChild) ? property.item(i).getElementsByTagName("name")[0].firstChild.nodeValue:"";
				var latitude = (property.item(i).getElementsByTagName("latitude")[0].firstChild) ? property.item(i).getElementsByTagName("latitude")[0].firstChild.nodeValue:"";
				var longitude = (property.item(i).getElementsByTagName("longitude")[0].firstChild) ? property.item(i).getElementsByTagName("longitude")[0].firstChild.nodeValue:"";
				var address = (property.item(i).getElementsByTagName("address")[0].firstChild) ? property.item(i).getElementsByTagName("address")[0].firstChild.nodeValue:"";	
				var city = (property.item(i).getElementsByTagName("city")[0].firstChild) ? property.item(i).getElementsByTagName("city")[0].firstChild.nodeValue:"";
				var state = (property.item(i).getElementsByTagName("state")[0].firstChild) ? property.item(i).getElementsByTagName("state")[0].firstChild.nodeValue:"";
				var zip = (property.item(i).getElementsByTagName("zip")[0].firstChild) ? property.item(i).getElementsByTagName("zip")[0].firstChild.nodeValue:"";
				var custom_field_1 = (property.item(i).getElementsByTagName("customfield1")[0].firstChild) ? property.item(i).getElementsByTagName("customfield1")[0].firstChild.nodeValue:"";
				var custom_field_2 = (property.item(i).getElementsByTagName("customfield2")[0].firstChild) ? property.item(i).getElementsByTagName("customfield2")[0].firstChild.nodeValue:"";
				var custom_field_3 = (property.item(i).getElementsByTagName("customfield3")[0].firstChild) ? property.item(i).getElementsByTagName("customfield3")[0].firstChild.nodeValue:"";
				var custom_field_4 = (property.item(i).getElementsByTagName("customfield4")[0].firstChild) ? property.item(i).getElementsByTagName("customfield4")[0].firstChild.nodeValue:"";
				var agent_1 = (property.item(i).getElementsByTagName("agent1")[0].firstChild) ? property.item(i).getElementsByTagName("agent1")[0].firstChild.nodeValue:"";
				var agent_2 = (property.item(i).getElementsByTagName("agent2")[0].firstChild) ? property.item(i).getElementsByTagName("agent2")[0].firstChild.nodeValue:"";
				var businesstype = (property.item(i).getElementsByTagName("businesstype")[0].firstChild) ? property.item(i).getElementsByTagName("businesstype")[0].firstChild.nodeValue:"";
				var property_type = (property.item(i).getElementsByTagName("type")[0].firstChild) ? property.item(i).getElementsByTagName("type")[0].firstChild.nodeValue:"";
				var property_desc = (property.item(i).getElementsByTagName("propertydescription")[0].firstChild) ? property.item(i).getElementsByTagName("propertydescription")[0].firstChild.nodeValue:"";
				var location = (property.item(i).getElementsByTagName("location")[0].firstChild) ? property.item(i).getElementsByTagName("location")[0].firstChild.nodeValue:"";
				var comments = (property.item(i).getElementsByTagName("comments")[0].firstChild) ? property.item(i).getElementsByTagName("comments")[0].firstChild.nodeValue:"";
				var image = (property.item(i).getElementsByTagName("image")[0].firstChild) ? property.item(i).getElementsByTagName("image")[0].firstChild.nodeValue:"";
						
				// html for the info popup box for each marker.
				var info = '<h3>' + property_type + '</h3><br /><span class="small">';
				if (property_name != '') { info += property_name + '<br />'; }
				if (address != '') { info += address + ' | <a href="property.asp?proptype=' + property_type_number + '&propid=' + fldid + '">View Listing</a><br />'; }
				if (city != '' || state != '' || zip != '') { info += city + ' ' + state + ' ' + zip + '<br />'; }
				if (custom_field_1 != '') { info += custom_field_1 + '<br />'; }
				if (custom_field_2 != '') { info += custom_field_2 + '<br />'; }
				if ((property_type_number == '1' || property_type_number == '2') && property_desc != '') { info += 'Prop. Type: ' + property_desc + '<br />'; }
				if (image != '' && image != 'noimage') { info += '<div class="map_popup_pic"><img src="../' + image + '" alt="Property Picture" /></div>'; }
				info += '</span>'
				
				// html for the property info for the property list below the map.
				var listinfo = '<div class="pdresults">';
				listinfo += '<div id="prop' + property_id + '" class="pdprop">';
				listinfo += '<h2>' + property_type + ': ' + property_name + ' ' + address + ' ' + city + ' ' + state + ' ' + zip + '<br />';
				listinfo += '<a href="javascript:void(0);" id="property-open-marker-' + (i+1) + '" title="' + (i+1) + '">View on Map</a> | ';
				listinfo += '<a href="property.asp?proptype=' + property_type_number + '&propid=' + fldid + '">View Listing</a></h2>';
				listinfo += '<div class="pd3col">';
				listinfo += '<p><img src="' + image + '" /></p>';
				listinfo += '<p><a id="hypAddToList' + property_id + '" class="listadd" name="' + property_id + '" href="">Add to List</a>';
				listinfo += '<input id="hidAddToList' + property_id + '" class="hidden" type="hidden" value="Add to List: ' + property_name + ' ' + address + ' ' + city + ' ' + state + ' ' + zip + '" name="hidAddToList' + property_id + '" /></p>';
				listinfo += '</div>';
				listinfo += '<div class="pd3col">' + custom_field_1 + '<br />' + custom_field_2 + '<br />' + custom_field_3 + '<br />' + custom_field_4;
				listinfo += '</div>';
				listinfo += '<div class="pd3col"><p>';
				listinfo += agent_1 + '<br />';
				if (agent_2 != '') { listinfo += agent_2 + '<br />'; }
				listinfo += 'ID# ' + property_id + '<br />';
				listinfo += 'Location: ' + location + '<br />';
				listinfo += 'Prop. Type: ' + property_desc;
				listinfo += '</p></div>';
				listinfo += '<p class="pdclear">' + comments.replace(/linebreak/g, "<br />") + '</p>';
				listinfo += '</div></div>';
			
				// injecting property into list.
				var map_list_container = new Element('div').setProperties({'id': i}).set('html', listinfo).inject($('property_list'));
				
				// mapping property
				map_property(latitude,longitude,info,property_type_number,i,property_id);
			}
			
			// adding event to each property in the list. when clicked, it will pop open the 
			// info tab for that marker.
			$$('a[id^=property-open-marker]').each(function(element) {
				$(element).addEvent('click', function() {
					GEvent.trigger(gmarkers[($(element).getProperty('title'))-1], "click");
					
					var scroll = new Fx.Scroll(window);
					scroll.toElement('google-map-canvas-results');
				});
			});
					
			// centering map and setting zoom level based on points in bounds
			map.setCenter(bounds.getCenter(),map.getBoundsZoomLevel(bounds));								
			
			listbox(); // calling function in modal.js so that the add to list links will work.
			
			// clearing updating overlay if there is one.
			new Fx.Tween($('google-map-canvas-results'), {duration: 400, wait: true}).set('opacity',1);

			
		} else {
			// some error occurred.
			error_message = (error.item(0).getElementsByTagName("message")[0].firstChild) ? error.item(0).getElementsByTagName("message")[0].firstChild.nodeValue:"";
			$('map_error').set('html', '<h3 class="alert">' + error_message + '</h3>');
			$('google-map-canvas-results').empty();
			$('google-map-canvas-results').removeProperty('style');
		}
	}
	
	// mapping the property.
	function map_property(latitude,longitude,info,propertytypenumber,index,property_id) {
		// Create custom icon.
		var Icon = new GIcon();
		Icon.image = "../images/maps/icon-map-"+(index+1)+".png"
		
		if ((index+1) < 10) {
			Icon.shadow = "../images/maps/icon-map-shadow-1.png";
			Icon.iconSize = new GSize(12, 24);
			Icon.shadowSize = new GSize(25, 24);
			Icon.iconAnchor = new GPoint(6, 24);
			Icon.infoWindowAnchor = new GPoint(6, 2);
		} else if ((index+1) < 100) {
			Icon.shadow = "../images/maps/icon-map-shadow-1.png";
			Icon.iconSize = new GSize(18, 24);
			Icon.shadowSize = new GSize(25, 24);
			Icon.iconAnchor = new GPoint(9, 24);
			Icon.infoWindowAnchor = new GPoint(9, 2);
		} else {
			Icon.shadow = "../images/maps/icon-map-shadow-1.png";
			Icon.iconSize = new GSize(24, 24);
			Icon.shadowSize = new GSize(25, 24);
			Icon.iconAnchor = new GPoint(12, 24);
			Icon.infoWindowAnchor = new GPoint(12, 2);
		}
	
		// declaring new GLatLng point
		var point = new GLatLng(latitude, longitude);
		
		// adding point to bounds for map centering
		bounds.extend(point);
		
		// declaring marker and original marker for reference.
		var marker = new GMarker(point, {icon:Icon});
		// adding marker as overlay to map
		map.addOverlay(marker);
		// adding marker to gmarkers array. used in click events later on.
		gmarkers[index] = marker;
		
		// handles the click event of the markere.
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(info);
			$('prop' + property_id).addClass('on');	
		});
		
		// handles the close event of the marker info window. just removes the background style.
		GEvent.addListener(marker, "infowindowclose", function() {
			$('prop' + property_id).removeClass('on');
		});
	}
});