/**
  * jquery google map plugin
  * by Matt, Ming @ Toll
  * version 0.1 beta
  * 2010-06-01
  * 
  * dependancy: 
		$(".officeDetails")
		$("#officeSelect")
		contact.json
  *
  *
  **/

(function($) {
	/**
	  * Options:
	  * 	autozoom - 0 | 1
	  */
	$.fn.rendermaps = function(options) {
		$obj = $(this);
	
		// Default options 
		var defaults = { 
			autozoom : 0
		}; 		
		
		// Overwrite defaults
		var options = $.extend({}, defaults, options); 

		// An array to hold all the markers we create					
		var gMarkers = [];

		var gMapOptions = {
			zoom: 3,
			center: new google.maps.LatLng(-25.335448, 135.745076),
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			navigationControl: true,
			navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
			mapTypeControl: true,
			mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}
		};
		
		// Create Google Maps object
		var gMap = new google.maps.Map($obj[0], gMapOptions); 
		
		// Load Branch Markers
		$.ajax({
			type: 'GET',
			url: 'contact.json',
			cache: false,
			dataType: 'json',
			success: function (data) {
				
				var infowindow = new google.maps.InfoWindow({});
				
				// Loop officeDetails from page
				$(".officeDetails").each(function(i, officeDiv) {

					// Get address from div on page
					var address = $(officeDiv).find(".address").text();
					var officeId = $(officeDiv).attr("id");
					
					if(data[officeId]) {
						var gLatLng = new google.maps.LatLng(data[officeId].latlng[0], data[officeId].latlng[1]);
						var gMarker = new google.maps.Marker({
							position: gLatLng
						});
						
						gMarker.setTitle(data[officeId].name);
						gMarker.setMap(gMap);
						
						google.maps.event.addListener(gMarker, 'click', function() {
							if(options["autozoom"]) {
								// set zoom
								gMap.setZoom(13);
								gMap.setCenter(gMarker.getPosition());
								
								// handle dropdown
								$("#officeSelect").val(officeId);
								
							}
							
							infowindow.close();
							infowindow.setContent($(officeDiv).html());
							infowindow.open(gMap,gMarker);
						});
						
						// Add marker to array
						gMarkers[officeId] = gMarker;									
					}
				});

				// Store objects on Canvas for later use
				$obj.data("google", { "map" : gMap, "markers" : gMarkers, "json" : data, "infowindow" : infowindow });
				
				return $obj;
			}
		});
    };  	
	
	$.fn.initBranch = function(options) {
		$obj = $(this);

		// Default options 
		var defaults = { 
			closeOnDefault : true
		}; 		
		
		// Overwrite defaults
		var options = $.extend({}, defaults, options); 
				
		// Hide all offices and select first option on list
		$(".officeDetails").hide(); 
		$("#officeSelect option:selected").attr('selected', ''); 
		$("#officeSelect option:first").attr('selected', 'selected'); 
		
		var changeAddress = function() {
			
			// Hide all office details
			$(".officeDetails").hide();
			
			// Get ID of selected office 
			var officeId = $(this).val();
			
			// Display selected office div
			var officeSelector = "#" + $(this).val();
			$(officeSelector).show();
			
			// Determine if google maps is initialised
			if($obj.data("google")) {
				
				// Close all infowindow when returning to default
				if(options.closeOnDefault == true && $(this).val() == "default") 
				{
					// Close any Infowindows
					$obj.data("google").infowindow.close();
				}
				
				// Get office marker for selected office and map object
				var gMarker = $obj.data("google").markers[officeId];
				var gMap = $obj.data("google").map;
									
				// Show selected office on map
				if(gMarker) {
					gMap.setCenter(gMarker.getPosition());
					gMap.setZoom(13);
				} else {
					gMap.setCenter(new google.maps.LatLng(-25.335448, 135.745076));
					gMap.setZoom(3);
				}
			}
		};
		
		// Bind function to change officeDetails when key press or click.
		$("#officeSelect").keyup(changeAddress);
		$("#officeSelect").click(changeAddress);		
		
		return $obj;
	};
})(jQuery);
