/*
File:	findagroup.js
This file is used to create the MOPS find a group functionality for the site.

Section:	Version History
28/05/2009 (DJO) - Created File
*/

MOPS.groupfinder = {
	// initialise defaults
	DEFAULTS: {
		LOCATEOPTS: {
			pin: false,
			callback: null,
			centerMap: true,
			popInfo: false
		}
	},

	// define variables
	activeRegion: null,
	map: null,
	geocoder: null,
	uiOptions: null,
	mumIcon: null,
	groupMarkerOpts: null,
	
	gotoLocation: function(bounds, title) {
		// TODO: if the title is set, update the location title
		this.windowClose();
		
		// create the upper and lower bounds objects
		var sw = new google.maps.LatLng(bounds.swlat, bounds.swlng);
		var ne = new google.maps.LatLng(bounds.nelat, bounds.nelng);
		
		// iterate through each of the maps and set the bounds
		this.map.setCenter(new google.maps.LatLng(
							(bounds.swlat + bounds.nelat) / 2,
							(bounds.swlng + bounds.nelng) / 2));
							
		// now set the map zoom level
		this.map.setZoom(this.map.getBoundsZoomLevel(new google.maps.LatLngBounds(sw, ne)));
	}, // gotoLocation
	
	gotoZone: function(zoneId) {
		// look for the statedata
		var zone = this.activeRegion.zones[zoneId];
		
		// if we have the state data, then set the map bounds
		if (zone && zone.loaded) {
			this.gotoLocation(zone.bounds, zone.title);
				
			// if we have results for that state, then pin those results
			if (zone.groups.length > 0) {
				// iterate through the groups in the list
				for (var ii = 0; ii < zone.groups.length; ii ++) {
					// initilaise variables
					var group = zone.groups[ii];
				
					// if the latlng is not defined, then geocode
					if (group.mapPin === null) {
						group.locate();
					} // if
				} // for
			} // if
		} // if
	}, // gotoState
	
	
	/*
	Method:	initGraphics
	This method is used to initialise the graphic objects associated with the map
	*/
	initGraphics: function() {
		// create the mum pin icon
		this.mumIcon = new google.maps.Icon(G_DEFAULT_ICON);
		this.mumIcon.image = IMAGEPATH_MAPS + "pin-mumsgroup.png";
		this.mumIcon.shadow = IMAGEPATH_MAPS + "pin-shadow.png";
		this.mumIcon.iconSize = new google.maps.Size(36, 42);
		this.mumIcon.shadowSize = new google.maps.Size(37, 24);
		
		// initialise the ui options
		this.uiOptions = new google.maps.MapUIOptions(new google.maps.Size(450, 400));
	}, // initGraphics
		
	/*
	Method:	loadMap
	This method is used to load a map into the specified selector elements
	*/
	loadMap: function(selector, region) {
		// initialise variables
		var container = $(selector).get(0);	
	
		// if we don't have the container display an error
		if (! container) {
			alert("could not initialise map");
			return;
		} // if
		
		// initialise the geocoder
		this.geocoder = new google.maps.ClientGeocoder();		
		this.geocoder.setBaseCountryCode("AU");
		
		// initialise graphics
		this.initGraphics();

		// create the new map instance
		this.map = new google.maps.Map2(container);		

		// center the map
		this.map.setUI(MOPS.groupfinder.uiOptions);	
		
		// goto the location
		this.gotoLocation(region.bounds, "Australia");
	},
	
	/*
	Method:	locateAddress
	This metohd is used to geocode the requested address
	*/
	locateAddress: function(address, opts) {
		// set options to default
		jQuery.extend(this.DEFAULTS.LOCATEOPTS, opts ? opts : {} );
		
		jQuery.status.push("Locating address: " + address);
		this.geocoder.getLatLng(address, function(point) {
			jQuery.status.pop();		
		
			if (!point) {
				jQuery.feedback.log("COULD NOT FIND ADDRESS: " + address);
			} 
			else {
				if (opts.pin) {
					MOPS.groupfinder.pinMap(point, address, address, opts.centerMap, opts.popInfo);
				} // if
				
				if (opts.callback) {
					opts.callback(point);
				} // if
			} // if..else
		});
	},	 // locateAddress
	
	locateSuburb: function(suburb) {
		jQuery.status.push("Locating suburb: " + suburb);
		this.geocoder.getLatLng(suburb, function(point) {
			jQuery.status.pop();
			if (!point) {
			  alert("could not locate suburb");
			} 
			else {
				MOPS.groupfinder.pinMap(point, suburb, suburb, true, false);
			} // if..else
		});
	}, // locateSuburb
	
	pinMap: function(point, pinTitle, infoBoxHtml, centerMap, popInfo) {
		// initialise the options
		var opts = { title: pinTitle, clickable: true, bouncy: true};		
	
		// create the new marker
		var fnresult = new google.maps.Marker(point, opts);
	
		// add the pin
		this.map.addOverlay(fnresult);
		
		// if we are centering, then center the map
		if (centerMap === null || centerMap) {
			this.map.setCenter(point, 13);
		} // 
		
		// if we should pop the info, then do that now
		if (popInfo === null || popInfo) {
			fnresult.openInfoWindowHtml(infoBoxHtml);
		} // if
		
		// add the marker to the list of markers
		return fnresult;		
	}, // pinMap
	
	windowClose: function() {
		this.map.disableInfoWindow();
		this.map.enableInfoWindow();
	}, // pinsClear
	
	/* debug functions */
	
	getBounds: function() {
		return this.map.getBounds();
	}, // getBounds
	
	dummy: function() {
	}
} // MOPS.groupfinder
		
google.load("maps", "2.x");
google.setOnLoadCallback(function() {
	// attach the status area
	jQuery("#statusmessages").statusArea();

	MOPS.groupfinder.activeRegion = MOPS.regions.AU;
	MOPS.groupfinder.loadMap("#group-map", MOPS.regions.AU);
});

$(document).ready(function() {
	jQuery("#group-states").regionAccordion(MOPS.regions.AU);
	
	// initialise the dialogs
	jQuery(".userDialog").dialog({ 
		autoOpen: false,
		width: 500,
		resizable: true,
		buttons: { 
			"Ok": function() {
				$(this).dialog("close");
			}
		}
	});
});