// this class needs google maps api to work.
// custom class for spec to map all locations on a map

var office = 
{
	markers: [],
			
	loadMap: function()
	{
		if(GBrowserIsCompatible())
		{
			// set height and width for div holding map (temp, should be in css)
			$('#officeGoogleMap').css('height','500px').css('width','500px');
			//blendDebugger.log('init map');
			
			// generate new google map and set to a preset x/y
			var map = new GMap2($('#officeGoogleMap').get(0));
			map.setCenter(new GLatLng(33.645492,-101.453247), 8);
			map.setMapType(G_NORMAL_MAP);
			
			// init geocoder
			var geocoder = new GClientGeocoder();						
		
			// get marker locations from dom
			$('.officeMap').each( function(i)
			{
				// get title and address, but remove <br /> from address, and lat/long
				var title = $(this).children('h2').html();
				var x = $(this).children('.point').children('.x').text();
				var y = $(this).children('.point').children('.y').text();
				
				// format address and remove any <br />
				var addressArray = $(this).children('address').children('.address').html().split(/<[bB][rR](.*?)+>/);
				var address = '';				
				
				// remove any whitespace from the address array and join into a single address
				$.each(addressArray, function(i)
				{
					var part = $.trim(this);
					address += (part != '') ? part+' ' : '';					
				});
				
				address = $.trim(address);
				// end get title and address
				
				// add marker to map
				var html = 
				'<h2>'+title+'</h2>'+
				'<p>'+address+'</p>'+
				'<p><a href="javascript:office.openMiniMapWindow('+i+')">View Mini Map</a> | <a href="http://maps.google.com/maps?f=d&daddr='+address+'&hl=en">Get Directions</a></p>';
				//office.addMarkerByAddress(map, geocoder, address, '<h2>'+title+'</h2><p>'+address+'</p>');
			 	office.markers[i] = office.addMarkerByPoint(map, x, y, html);
				
				// bind event to pop up marker info box.  map link relates to address.
				$(this).children('address').children('.officeMapLink').bind('click', function() { office.openMarkerInfoWindow(i); });
			});
			
			// add map controls
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());			
		}
	},
	
	openMiniMapWindow: function(i)
	{
		office.markers[i].showMapBlowup({zoomLevel:13, mapType:G_NORMAL_MAP});
	},
	
	openMarkerInfoWindow: function(i)
	{
		// using index parameter, open marker info window from office.markers array
		GEvent.trigger(office.markers[i], 'click');		
	},
	
	addMarkerByPoint: function(map, x, y, html)
	{
		// map is a google map object
		// lat and long are used to plot the marker
		// html is what text / html shows up when the marker is clicked
		
		var point = new GPoint(x, y);
		var marker = new GMarker(point);
		map.addOverlay(marker);
		marker.bindInfoWindowHtml(html);
		
		return marker;			
	},
	
	addMarkerByAddress: function(map, geocodeObject, address, title)
	{
		// geocode must be an object of type GClientGeocoder
		// address is a str address like 'lubbock tx'
		// title is what text / html shows up when the marker is clicked
		
		geocodeObject.getLatLng(address, function(point)
		{
			if(!point)
			{
		        // failed
			}
			else
			{
				var marker = new GMarker(point);
				map.addOverlay(marker);
				marker.bindInfoWindowHtml(title);
				
				return marker;
			}
		});				
	},
	
	// initalizer function
	init: function()
	{
		// load map
		this.loadMap();		
		
		// google maps api unload (for memory leaks)
		$('body').bind('unload', function() { GUnload(); });		
	}
	
}

// add to document after dom is loaded
$(document).ready(function() { office.init(); });