/**
 * @fileOverview NewHomes - Support for NewHomes.cfm
 * Creates a singleton object, LMS, which encapsulates the
 * functionality required to interact with the New Homes map.
 * 
 * @requires CustomMap.js (which requires GoogleMap.js)
 * @requires jquery.js
 * 
 * @author Larry Reinhard
 */
var LMS = function() {

	// A reference to a map manager
	var mapMgr;
	
	// Reference to jQuery objects
	var searchingMsg, resultMsg;

	// The listing data for all of the active markers
	var markerData = {};
	
	/**
	 * Formats the given number with commas separating groups of three digits
	 * 
	 * @param {Number}
	 *            value The number to be formatted
	 * @return {String} The formatted number
	 */
	function fmtNumberCommas(value) {
		value += '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(value)) {
			value = value.replace(rgx, '$1' + ',' + '$2');
		}
		return value;
	}

	/**
	 * Displays the result message and sets and timer to remove is from the screen
	 */
	function showResultMessage(msg) {
		resultMsg.text(msg).show().fadeOut(5000);
	}

	/**
	 * Stops any animation and hides the result message
	 */
	function hideResultMessage() {
		resultMsg.stop().hide();
	}

	/**
	 * Handle the click event on a marker; "this" is a reference
	 * to the marker object
	 * 
	 * @param {string} id The identifier we assigned to this marker
	 */
	function handleMarkerClick(id) {
		
		if (false) {
			// compute marker anchor point
			var ll = this.getLatLng();
			mapMgr.logMessage(id +' = ('+ll.lat()+','+ll.lng()+')');	
		
		} else {
			//searchingMsg.show();
			//document.location = markerData[id];
			return;
		}
	}
	
	// public interface
	return {

		showDebug : false,

		/**
		 * Initializes the New Homes Map Search object
		 * 
		 * @param {object} cfg Configuration parameters: 
		 *	map: An ID name or DOM element; the map container
		 *  zoom: (see CustomMap.js constructor for details)
		 *  pan: (see CustomMap.js constructor for details)
		 *  maptype: (see CustomMap.js constructor for details)
		 *  tags: (see CustomMap.js constructor for details)
		 *  start: (see CustomMap.js init method for details)
		 *  markers: a single marker or array of marker objects
		 *  	(see GoogleMap.js addMarkers method for details)
		 *  	also accepts a member called "link" that can contain
		 *  	a url for a destination when the marker is clicked
 		 *  message: An object with these members:
		 *  	wm: The ID of the element that is displayed while a search is occurring
		 *  	rm: The ID of the element that displays a search result message
		 *  sf: The ID of the form element that is submitted for searching
		 *  iw: The ID of the custom Info Window popup
		 *  
		 */
		init : function(cfg) {
			
			// get a new Map Manager object
			mapMgr = new CustomMap( {
				zoom   : cfg.zoom,
				pan    : cfg.pan
			});
			
			// draw initial map
			mapMgr.init(cfg.map, cfg.start);

			// add markers
			if (cfg.markers)
				mapMgr.addMarkers(LMS.augmentMarkers(cfg.markers));
			
			/*if (cfg.message) {
				// cache reference to messsage elements
				searchingMsg = $('#'+cfg.message.wm);
				resultMsg = $('#'+cfg.message.rm);				
			}*/

		},

		/**
		 * Calls the private marker click handler; this
		 * handler is attached to markers when they are
		 * added to the map (see augmentMarkers)
		 
		handleMarkerClickProxy : function (id) {
			handleMarkerClick.call(this,id);
		},
		*/
		
		/**
		 * Adds new markers to the custom map after initialization
		 */
		addMarkers : function (markers) {
			
			mapMgr.addMarkers(augmentMarkers(markers));

		},
		
		/**
		 * Adjust map center point and zoom to make
		 * all markers visible
		 */
		zoomToMarkers : function () {
			mapMgr.zoomToMarkers();
		},
		
		/**
		 * Adds common configuration data to each marker
		 */
		augmentMarkers : function(markers) {
	
			var icon = {
				image:'/Images/Maps/Interactive/Pin/Resale.png',
				shadow: '/Images/Maps/Interactive/Pin/Shadow.png',
				iconsize: '23,34',
				shadowsize: '41,34',
				iconanchor: '12,34',
				infowindowanchor: '12,12'
			};
			
			if (markers instanceof Array) {
				// an array of marker objects
				for (var i = 0; i < markers.length; i++) {
		
					//markers[i].click = LMS.handleMarkerClickProxy;
					markers[i].draggable = false;
					markers[i].icon = icon
	
					// store the html for the click handler
					//markerData[markers[i].id] = markers[i].link || null;
					markerData[markers[i].id] = markers[i].html || null;
				}
			} else {
				// a single marker object
				markers.click = LMS.handleMarkerClickProxy;
				markers.draggable = false; 
				markers.icon = icon
				// store the link for the click handler
				markerData[markers.id] = markers.link || null;
			}
			
			return markers;
		}
	}
}(); // create singleton

	

