/*
to initialise a search call    searchForCoordinates function with address as the only string parameter
(format=postcode, country)

if   searchForCoordinatesCallback function is defined
than it will be launched when the search is completed

getGoogleLatitude() and    getGoogleLongitude() could be called from this callback function
in order to get the appropriate Latitude and  Longitude
*/

var sPoint = null;
var callBackFunction = null;

function SearchPoint(address, latitude, longitude){
    this.address = address;
    this.latitude = latitude ? latitude : 0;
    this.longitude = longitude ? longitude : 0;
}

function searchForCoordinates(address,callBack) {
	callBackFunction=callBack;
    var cpLookup = new GlocalSearch();
    sPoint = null;
    cpLookup.setSearchCompleteCallback(null, cpLookupComplete, [cpLookup, address]);
    cpLookup.execute(address);
}
function getGoogleLatitude(){
    if (sPoint && sPoint!=null){
        return sPoint.latitude;
    }
    return 0;
}
function getGoogleLongitude(){
    if (sPoint && sPoint!=null){
        return sPoint.longitude;
    }
    return 0;
}
function cpLookupComplete(cpLookup, args){
    if ( cpLookup.results && cpLookup.results.length > 0 ) {
      sPoint = new SearchPoint(args, parseFloat(cpLookup.results[0].lat),
                        parseFloat(cpLookup.results[0].lng));
    }
	if(callBackFunction){
		callBackFunction();
	}else    if (searchForCoordinatesCallback){
        searchForCoordinatesCallback();
    }
}