/**
 * mapcmsAddress.js, Creates a standard Address for MapCms
 * 
 * Creates Address from string, The MapCMS address has
 * 4 elements .address .lat .lng and .latlng
 * @author ASA-henworx <programming@henworx.de>
 * @version 0.9
 * @package henworx
 */


/**
 * henworx.MapCms.Address 
 *
 * Converts the string address into standard Address.
 * If only _address is provided attempts to resolve the address 
 * two callback functions can be assigned as 3rd ad 4th arguments
 * __beforeAddressResolve(_address) is given the parameter _address
 * __afterAddressResolve(_latlng) is given latlng on successfull resolution else null
 *
 * @param string _address  oneline address of the location
 * @param float _lat  latitude [optional]
 * @param float _lng  latitude [optional]
 * @param function __beforeAddressResolve function to be executed prior to resolving address [optional]
 * @param function __afterAddressResolve  function to be executed after resolving address. [optional]
 */
henworx.MapCms.Address=function(_address,_lat,_lng,__beforeAddressResolve,__afterAddressResolve){


	if(typeof _address.latlng != 'undefined' ){ //address is already resolved so jsut return the same
		return _address;
	}
	
	//property address
	this.address=_address
	
	//if there is no latlng in parameters resolve it
	if(_lat==false || _lng==false || (typeof _lat=='undefined' || typeof _lng=='undefined')){
		
		//raise the event beforeAddressResolve callback
		if(typeof __beforeAddressResolve !='undefined'){
			__beforeAddressResolve(_address)
		}
		
		//resolve it
		this.latlng=null;
		//alert(_address);
		_output=requestURL(gPluginRelativeUrl+"/includes/resolve-address.php?key="+gKey+"&q="+_address);
		
			//alert(this.address+":"+_output);
		//if got reply
		if(_output!='0'){
			_outputSlices=_output.split(",");
			this.latlng= new GLatLng(_outputSlices[2],_outputSlices[3])
		}
		
		//raise the event afterAddressResolve callback
		if(typeof __afterAddressResolve !='undefined'){
			__afterAddressResolve(this.latlng)
		}
		
	}else{ //already have latlng in arguments
		this.latlng= new GLatLng(_lat,_lng)
	}
	//store properties lat lng
	this.lat= this.latlng.lat();
	this.lng= this.latlng.lng();
	//alert("final"+this.address+":"+this.lat);
}