/**
 * jQuery overview plugin. Attach it to *-overview.html pages.
 */
(function($){
	$.fn.overview = function(){
		var self = this;
		self.map = $('#map',self);
		self.marker = null;
		self.content = $('#infoWindowContent',self);
		self.address = $('#infoWindowContent .address',self).text();
		// get the 2-letter development designation from the uri
		self.development = document.location.href.split('/').pop().split('-')[0];
		// grab the html markup to use in the map, wrapped in a unique id
		var markup = '<div id="ginfo">' + $('#infoWindowContent',self).html() + '</div>';

/**
 *	Replace the jmap addMarker function with one that returns the new marker object.
 */
$.jmap.addMarker = function(options, callback) {
	// Create options
	var options = $.extend({}, $.jmap.JMarkerDefaults, options);
	var markerOptions = {}

	if (typeof options.pointIcon == 'object')
		$.extend(markerOptions, {icon: options.pointIcon});

	if (options.pointIsDraggable)
		$.extend(markerOptions, {draggable: options.pointIsDraggable});

	// Create marker, optional parameter to make it draggable
	var marker = new GMarker(new GLatLng(options.pointLatLng[0],options.pointLatLng[1]), markerOptions);

	// If it has HTML to pass in, add an event listner for a click
	if(options.pointHTML)
		GEvent.addListener(marker, options.pointOpenHTMLEvent, function(){
			marker.openInfoWindowHtml(options.pointHTML, {maxContent: options.pointMaxContent, maxTitle: options.pointMaxTitle});
		});

	// If it is removable, add dblclick event
	if(options.pointIsRemovable)
		GEvent.addListener(marker, options.pointRemoveEvent, function(){
			$.jmap.GMap2.removeOverlay(marker);
		});

	// If the marker manager exists, add it
	if($.jmap.GMarkerManager) {
		$.jmap.GMarkerManager.addMarker(marker, options.pointMinZoom, options.pointMaxZoom);
	} else {
		// Direct rendering to map
		$.jmap.GMap2.addOverlay(marker);
	}

	if (typeof callback == 'function') return callback( marker, options );
}



		// initialize the map
		self.mapgo = function(){
			var opts = {
				mapControlSize:		'small',
				mapCenter:			[34.0522,-118.242798],
				mapZoom:			12,
				language:			'en',
				mapShowjMapIcon:	false,
				mapEnableType:		false
			}
			self.map.jmap( 'init', opts, self.mapinit );
		}

		// initialization callback
		self.mapinit = function( el, options ){
			self.el = el;
			$(self.el).jmap('searchAddress', {address: self.address}, function( options, point ){
				// add the marker, store it, then trigger the info window
				$(self.el).jmap('addMarker', {pointLatLng:[point.y, point.x], addMarker: true, pointHTML: markup}, function( marker, options ){
					// store the marker
					self.marker = marker;
					// center the point in the map
					$(self.el).jmap('moveTo',{mapCenter:[point.y, point.x]}, function(){
						GEvent.trigger( self.marker, 'click' );
					});
					// wait for a bit before enabling the form
					setTimeout( function(){
						// enable directions
						$('#ginfo button:not(.info-more)').click( function(){
							pageTracker._trackPageview( self.development + '/directions' );
							var from = $('#ginfo .From').val();
							if( !from ){
								alert('You must enter an address to start from');
								return false;
							}
							//self.triggerMarker(self.marker);
							self.getDirections(from);
							return false;
						});

						// show the "more info" phone number and track it with analytics
						$('#ginfo button.info-more').click( function(){
							pageTracker._trackPageview( self.development + '/call' );
							$('span',this).show();
							return false;
						});
					}, 500 );
				});
			});
		}

		// fire inline directions
		self.getDirections = function(from){
			$(self.map).animate({height:'200px'},function(){
				$.jmap.GMap2.checkResize();
				$(self.el).jmap("searchDirections", {fromAddress: from, toAddress: self.address, directionsPanel:"directions"},function(){
					$('#directions',self).show();
					$(self.content).show();
				});
			});
		}

		//time to run
		self.mapgo();
	}
})(jQuery);
