/****************
 * this object is a wrapper class
 * @param {Object} ele - an ID of the element in which Google Maps will populate
 */
var mooGooMaps = { 
	customMarker: null,
	showAddresses: false,
	zoom: 3,
	width: 572,
	height: 339,
	centerX: null,
	centerY: null,
	centerAddress: "MO, Us",
	showAddresses: [],
	containerEle: null,
	map: null,
	customIcon: null,
	geocoder: null,
	CollectionOfCurrentPoints: new Array(),
	addAddress: function( plot ) {
		this.showAddresses.push( plot );
		if( !this.showingAddresses ) {
			this.showPassedAddresses();
		}
	},
	plotAddress: function( plot, center, callback ) {
		if( $type(plot) == "object" ) {
			var info = null;
			if( $type(plot.info) == "array" ) {
				info = [];
				for( var i = 0; i < plot.info.length; i++ ) {
					info.push(
						new GInfoWindowTab( plot.info[i].tab, plot.info[i].content )
					);
				}
			} else if( $type(plot.info) == "string" ) {
				info = plot.info;
			}
			if( plot.x && plot.y ) {
	            if(!plot.key) {
	                //console.log("you should really specify a key with your new marker otherwise your key is '"+plot.y+","+plot.x+"'");
	                plot.key = plot.y + "," + plot.x;
	            }
				this.showMarker( new GLatLng( plot.y, plot.x ), info, plot.key);
				if( $type(callback) == "function" ) {
					callback();
				}
			} else if( plot.address ) {
			    if(!plot.key) {
	                plot.key = plot.address;
	            }
				this.geocoder.getLatLng( plot.address, function( point ) {
					if( center ) {
						this.centerMap( point, this.zoom );
					}
					this.showMarker( point, info, plot.key);
					if( $type(callback) == "function" ) {
						callback();
					}
				}.bind( this ));
			} else {
				//alert("bad object"); 
			}
		}
	},
	showPassedAddresses: function() {
		if( this.map.isLoaded() ) {
			if( this.showAddresses.length > 0 ) {
				this.showingAddresses = true;
				var item = this.showAddresses.pop();
				this.plotAddress( item, false, this.showPassedAddresses.bind(this) );
			} else {
				this.showingAddresses = false;
			}
		} else {
			// the map hasn't loaded yet, wait a little bit and try again
			setTimeout( this.showPassedAddresses.bind( this ), 10 );	
		}
	},
	showMarker: function( point, info, key ) {
		var marker = new GMarker( point, this.customIcon );
		if( info ) {
			if( $type(info) == "string" ) {
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(info);
				});
			} else if( $type(info) == "array" ) {
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowTabsHtml(info);
				});
			}
		}
		this.CollectionOfCurrentPoints.push({"key": key.toLowerCase(), "marker": marker});
		this.map.addOverlay(marker);
	},
	getMarker: function(key) {
	    for(var i=0;i < this.CollectionOfCurrentPoints.length;i++) {
	        if(this.CollectionOfCurrentPoints[i].key == key.toLowerCase()) {
	            return this.CollectionOfCurrentPoints[i].marker;
	        }
	    }
	},
	addMarker: function(marker, key) {
	    this.CollectionOfCurrentPoints.push({"key": key.toLowerCase(), "marker": marker});
		this.map.addOverlay(marker);
	},
	initialize: function( ele ) {
		if( location.href.indexOf("http://localhost") > -1 ) {
			//alert("Google Maps will not work with 'localhost'.") 
			return;
		}
		this.geocoder = new GClientGeocoder();
		this.containerEle = $(ele);

		// set the height/width of the container		
		this.map = new GMap2( this.containerEle );

		if( arguments.length > 1 ) {
			options = arguments[1];
						
			this.setOption( "markCenter", options.markCenter );
			this.setOption( "centerX", options.centerX );
			this.setOption( "centerY", options.centerY );
			this.setOption( "zoom", options.zoom );
			this.setOption( "centerAddress", options.centerAddress );
			this.setOption( "showAddresses", options.showAddresses );
			if( options.customIcon ) {
				this.setCustomIcon( options.customIcon );	
			}	
		}
				
		if( this.centerX && this.centerY ) {
			this.map.setCenter( new GLatLng( options.centerX, options.centerY ), options.zoom  );
		} else if( this.centerAddress ) {
			this.plotAddress( this.centerAddress, true );
		} else {
			//alert( "expect something bad to happen, no default lat/long or address was defined" );
		}
		
		if( options && options.showAddresses ) {			
			this.showPassedAddresses();
		}
	},
	clearCustomMarker: function() {
		this.customMarker = null;
	},
	clearAllMarkers: function() {
	    this.CollectionOfCurrentPoints.each(function(MapMarkerItem) {
	        if(MapMarkerItem.marker)
            { 
	            this.map.removeOverlay(MapMarkerItem.marker);
	        }
	    }.bind(this));
	    this.CollectionOfCurrentPoints = new Array();
	},
	removeMarker: function(item) {
	    if( $type(item) == "string" ) {
	        item = item.toLowerCase();
			this.CollectionOfCurrentPoints.each(function(MapMarkerItem) {
			    if(MapMarkerItem.key == item) {
	                this.map.removeOverlay(MapMarkerItem.marker);
	                this.CollectionOfCurrentPoints.remove(MapMarkerItem);
	            }
	        }.bind(this));
	        
		} else if( $type(item) == "object" ) {
			this.map.removeOverlay(item);
		}
	},	
	setCustomIcon: function( options ) {
		this.customIcon = new GIcon();
		
		this.customIcon.image = options.image.url;
		this.customIcon.iconSize = new GSize(options.image.width, options.image.height);
		this.customIcon.iconAnchor = new GPoint( options.image.anchorX, options.image.anchorY );
		
		if( options.shadow ) {
			this.customMarker.shadow = options.shadow.image;
			this.customMarker.shadowSize = new GSize(options.shadow.height, options.shadow.width);
			this.customMarker.iconAnchor = new GPoint(options.shadow.y, options.shadow.x);
		}
		
		if( options.info ) {
			if( options.info.shadowAnchor ) {
				this.customIcon.infoShadowAnchor = new GPoint(options.info.shadowAnchor.x, options.info.shadowAnchor.y);
			}	
			if( options.info.windowAnchor ) {
				this.customIcon.infoWindowAnchor = new GPoint(options.info.windowAnchor.x, options.info.windowAnchor.y);
			}
		} else {
			//alert("not in info");
		}
	},
	panTo: function(obj) {
	    if( $type(obj) == "string" ) {
			
		} else if( $type(obj) == "array" ) {
			this.centerMap( new GPoint( obj[0], obj[1] ), this.zoom );
		}
    },
	centerMap: function(gLatLng, zoom) {
		this.map.setCenter(gLatLng, zoom);
	},
	showLargeZoom: function() {
		this.map.addControl(new GLargeMapControl());
	},
	showSmallZoom: function() {
		this.map.addControl(new GSmallMapControl());
	},
	setOption: function( param, newOption ) {
		if( newOption ) {
			this[param] = newOption;
		}
	}
};