var _aqa_ = _aqa_ || {};

/**
 * container : map element or id
 * options : {lat:number, lng:number, GLatLng:GLatLng obj., address:string, information:html string}
 * address : string
 * success : function (call back)
 * failure : function (call back)
 * index   : marker index
 * _*      : option
 *
 * see Google Maps API (http://code.google.com/intl/ja/apis/maps/documentation/reference.html)
 * GMapOptions, GMarkerOptions, 
 */
_aqa_.map = {
	/**
	 * methods for map and/or location
	 */
	init : function (container, _GMapOptions){
		if(typeof(GBrowserIsCompatible) != 'undefined' && GBrowserIsCompatible() && !this.map){
			container = typeof(container) == 'string' ? document.getElementById(container) : container;
			this.map = new GMap2(container, _GMapOptions);
			this.map.enableContinuousZoom();
			//this.map.enableScrollWheelZoom();
			this.map.addControl(new GLargeMapControl());
			this.map.addControl(new GMenuMapTypeControl());
			this.map.addControl(new GOverviewMapControl());
		}
		return this;
	},
	setCenter : function (_options){
		if(!this.map) return this;
		var slf = this;
		this.getLatLng(
			_options,
			function (loc){slf.loc = loc; slf.map.setCenter(slf.loc, 13);}
		);
		return this;
	},
	getCenter : function (){
		if(!this.map) return this;
		this.loc = this.map.getCenter();
		return this;
	},
	getLatLng : function (options, success, _failure){
		var loc = false;
		if(this.map && options){
			if(options.lat && options.lng) loc = new GLatLng(options.lat, options.lng);
			else if(options.GLatLng) loc = options.GLatLng;
			else if(options.address){
				(new GClientGeocoder()).getLatLng(
					options.address,
					function(p){if(p) success(p); else if(_failure) _failure();}
				);
				return this;
			}
			if(loc) success(loc); else if(_failure) _failure();
		}else if(this.map){
			if(this.loc) success(this.loc); else if(_failure) _failure();
		}
		return this;
	},
	/**
	 * methods for marker
	 */
	setMarker : function (index, _options, _GMarkerOptions){
		if(!this.map || (!index && index !== 0)) return this;
		var slf = this;
		this.getLatLng(
			_options,
			function (loc){
				if(!slf.markers[index]){
					slf.markers[index] = new GMarker(loc, _GMarkerOptions);
					slf.map.addOverlay(slf.markers[index]);
				}else{
					slf.markers[index].setPoint(loc);
				}
				if(_options.information){
					slf.markers[index].openInfoWindowHtml(_options.information);
				}
			}
		);
		return this;
	},
	/**
	 * other methods
	 */
	getMarker : function (index){
		if(!this.map || !this.markers[index]) return null;
		return this.markers[index].getPoint();
	},
	/**
	 * properties
	 */
	clear : function (){
		for(var i in this) if(this.hasOwnProperty(i)) this[i] = null;
		return this;
	},
	map : null,
	loc : null,
	markers : {}
}


