/*

		YahooMaps.js
		
		Mootools-based class to provide Yahoo! Maps functionality
		
		Written by:		Ken Kolodziej
		Created on:		April 23, 2009
		
*/

var YahooMaps = new Class({
	options: {
		mapContainerID: '',
		points: [],
		defaultZoom: 3,
		disableKeys: true,
		mapType: 'YAHOO_MAP_REG',
		width: 0,
		height: 0
	},
	initialize: function(options) {
		this.setOptions(options);
		
		this.dirty = false;
		
		this.geoPoints = [];
	},
	injectMap: function() {
		if(this.options.width > 0 && this.options.height > 0)
			var map = new YMap(document.getElementById(this.options.mapContainerID), null, new YSize(this.options.width, this.options.height));
		else
			var map = new YMap(document.getElementById(this.options.mapContainerID));
		
         	// Add the ability to change between Sat, Hybrid, and Regular Maps  
         	map.addTypeControl();
         	
		// Add the zoom control. Long specifies a Slider versus a "+" and "-" zoom control  
         	map.addZoomLong();
         	
		// Add the Pan control to have North, South, East and West directional control  
         	map.addPanControl();
		
		// Enable dragging
		map.enableDragMap();
         	
		// Specifying the Map starting location and zoom level
		if(this.options.points.length >= 1)
			map.drawZoomAndCenter(this.options.points[0].streetAddress + ', ' + this.options.points[0].city + ', ' + this.options.points[0].state + ' ' + this.options.points[0].zip, this.options.defaultZoom);
		else
         		map.drawZoomAndCenter('', this.options.defaultZoom);
		
		// Draw points
		YEvent.Capture(map, EventsList.endMapDraw, function() {
			if(this.dirty == false) {
				
				if(this.options.points.length > 1) {
					for(var i = 0; i < this.options.points.length; i++) {
						var geoPoint = new YGeoPoint(this.options.points[i].latitude, this.options.points[i].longitude);
						
						if(this.options.points[i].latitude != null && this.options.points[i].longitude != null
						   && this.options.points[i].latitude != 0 && this.options.points[i].longitude != 0)
							this.options.points[i].marker = new YMarker(geoPoint);
						else
							this.options.points[i].marker = new YMarker(map.getCenterLatLon());
						
						// Add labels, etc.
						this.options.points[i].marker.addAutoExpand(this.options.points[i].label);
						this.options.points[i].marker.openAutoExpand();
						//var markerMarkup = this.options.points[i].labelText;
						
						YEvent.Capture(this.options.points[i].marker, EventsList.MouseClick,
							function(){ this.marker.openSmartWindow(this.labelText); }.bind(this.options.points[i])
						);
						
						map.addOverlay(this.options.points[i].marker);
						
						this.geoPoints.push(geoPoint);
					}
					
					var details = map.getBestZoomAndCenter(this.geoPoints);
					
					map.setZoomLevel(details.zoomLevel);
					
					if(this.dirty == false) {
						var theFunc = function() { map.panToLatLon(details.YGeoPoint); };
						theFunc.delay(1000, this);
					}
				}
				else if(this.options.points.length == 1) {
					this.options.points[0].marker = new YMarker(map.getCenterLatLon());
					this.options.points[0].marker.addAutoExpand(this.options.points[0].labelText);
					this.options.points[0].marker.openAutoExpand();
					
					map.addOverlay(this.options.points[0].marker);
					
					/*if(this.dirty == false) {
						var theFunc = function() { map.panToLatLon(map.getCenterLatLon()); };
						theFunc.delay(1000, this);
					}*/
					map.panToLatLon(map.getCenterLatLon());
					//this.options.points[i].marker.openSmartWindow(this.options.points[i].labelText);
				}
				
				this.dirty = true;
			}
		}.bind(this));
		
		if(this.options.disableKeys == true)
			map.disableKeyControls();
	}
});
YahooMaps.implement(new Options, new Events);