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

var officeMap = 
{
	markers: [],
			
	loadMap: function()
	{
		if(GBrowserIsCompatible())
		{			
			// set height and width for div holding map (temp, should be in css)
			$('#officeGoogleMap').css('height','425px').css('width','465px');
			
			// generate new google map and set to a preset x/y
			var map = new GMap2($('#officeGoogleMap').get(0));
			map.setCenter(new GLatLng(0, 0), 1);
			map.setMapType(G_HYBRID_MAP);		
			
			// init geocoder
			var geocoder = new GClientGeocoder();
			
			// ajax request to get all offices
			$.ajax(
			{
				type: 'post',
				url: 'ajaxOffice.php',
				data: 'action=getAllOffice',
				dataType: 'json',
				
				success: function(jsonObject)
				{
					// add markers to map
					$.each(jsonObject.officeArray, function(i, office)
					{
						var html = 
						'<div class="googleWindowContent">';
						
						// display flag
						if(office.country_flag != '')
						{
							html +=
							'<div class="countryFlag"><img src="images/flags/'+office.country_flag+'" alt="Country Flag"></div>';
						}
						
						html +=
						'<div class="googleTitle">'+office.office_name+'</div>';						
						
						// display address
						html +=
						'<div class="googleText">'+
							office.office_addr1+'<br />';
							
						html += (office.office_addr2 != '') ?  office.office_addr2+'<br />' : '';
						html += (office.office_addr3 != '') ?  office.office_addr3+'<br />' : '';
							
						html +=
							office.office_city+', '+office.country_name+' '+office.office_zip+
						'</div>'+
						'<div class="googleTitle">Phone</div>'+
						'<div class="googleText">'+
							office.office_phone1;
						
						// show phone number if it exists
						html += (office.office_phone2 != '') ? '<br />'+office.office_phone2 : '';
						html += (office.office_phone3 != '') ? '<br />'+office.office_phone3 : '';
						
						html +=
						'</div>';
						// end show phone
						
						// show fax number if exists						
						if(office.office_fax1 != '' || office.office_fax2 != '')
						{
							html +=
							'<div class="googleTitle">Fax</div>'+
							'<div class="googleText">';
						
							// show phone number if it exists
							html += (office.office_fax1 != '') ? office.office_fax1 : '';						
							html += (office.office_fax2 != '') ? '<br />'+office.office_fax2 : '';
						
							html +=
							'</div>';
						}
						// end show fax number
						
						// show email if exists
						if(office.office_email1 != '' || office.office_email2 != '')
						{
							html +=
							'<div class="googleTitle">Email</div>'+
							'<div class="googleText">';
						
							// show phone number if it exists
							html += (office.office_email1 != '') ? '<a class="inlineLink" href="mailto:'+office.office_email1+'">'+office.office_email1+'</a>' : '';
							html += (office.office_email2 != '') ? '<br /><a class="inlineLink" href="mailto:'+office.office_email2+'">'+office.office_email2+'</a>' : '';
													
							html +=
							'</div>';
						}
						// end show email						
						
						// show website if exists
						if(office.office_url != '')
						{
							html += 
							'<div class="googleTitle">Website</div>'+
							'<div class="googleText">'+
								'<a class="inlineLink" href="'+office.office_url+'">'+office.office_url+'</a>'+
							'</div>';
						}
						
						html +=
						'</div>';				
						
						//'<p><a href="javascript:officeMap.openMiniMapWindow('+i+')">View Mini Map</a> | <a href="http://maps.google.com/maps?f=d&daddr='+office.office_city+', '+office.country_name+'&hl=en">Get Directions</a></p>';
						
						// add marker and save to array
						officeMap.markers[i] = officeMap.addMarkerByPoint(map, office.office_google_x, office.office_google_y, html);
												
					});
					// end add markers to map				
				}
			});		
			// end ajax request
			
			// add map controls
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
						
		}
	},
	
	openMiniMapWindow: function(i)
	{
		officeMap.markers[i].showMapBlowup({zoomLevel:13, mapType:G_NORMAL_MAP});
	},
	
	openMarkerInfoWindow: function(i)
	{
		// using index parameter, open marker info window from officeMap.markers array
		GEvent.trigger(officeMap.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, {'maxWidth': 300});
		//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() { officeMap.init(); });
