(function(){
	// Credits to : http://code.google.com/p/geo-location-javascript/
	if (typeof(window.blackberry) != "undefined" && blackberry.GPSSupported ) {

		var bb_successCallback;
		var bb_errorCallback;
		var bb_blackberryTimeout_id=-1;
		
		function handleBlackBerryLocation()
		{
			clearTimeout(bb_blackberryTimeout_id);
			bb_blackberryTimeout_id=-1;
			if (bb_successCallback && bb_errorCallback)
			{
				if(blackberry.location.latitude==0 && blackberry.location.longitude==0)
				{
					//http://dev.w3.org/geo/api/spec-source.html#position_unavailable_error
					//POSITION_UNAVAILABLE (numeric value 2)
					bb_errorCallback({message:"Position unavailable", code:2});
				}
				else
				{  
					var timestamp=null;
					//only available with 4.6 and later
					//http://na.blackberry.com/eng/deliverables/8861/blackberry_location_568404_11.jsp
					if (blackberry.location.timestamp)
					{
						timestamp=new Date(blackberry.location.timestamp);
					}
					bb_successCallback({timestamp:timestamp, coords: {latitude:blackberry.location.latitude,longitude:blackberry.location.longitude}});
				}
				//since blackberry.location.removeLocationUpdate();
				//is not working as described http://na.blackberry.com/eng/deliverables/8861/blackberry_location_removeLocationUpdate_568409_11.jsp
				//the callback are set to null to indicate that the job is done

				bb_successCallback = null;
				bb_errorCallback = null;
			}
		}

		function handleBlackBerryLocationTimeout()
		{
			if(bb_blackberryTimeout_id!=-1)
			{
				bb_errorCallback({message:"Timeout error", code:3});
			}
		}

		// set to autonomous mode
		if(typeof(blackberry.location.setAidMode) == "undefined")
		{
			return false;									
		}
		blackberry.location.setAidMode(2);
		
		if (typeof(navigator.geolocation) == 'undefined') {
			navigator.geolocation = {
				getCurrentPosition : function(successCallback, errorCallback, options) {
					//passing over callbacks as parameter didn't work consistently
					//in the onLocationUpdate method, thats why they have to be set
					//outside
					bb_successCallback = successCallback;
					bb_errorCallback   = errorCallback;
					
					if ( successCallback ) {
						if(options['timeout'])  
						{
							bb_blackberryTimeout_id = setTimeout("handleBlackBerryLocationTimeout()",options['timeout']);
						}
						else
						//default timeout when none is given to prevent a hanging script
						{
							bb_blackberryTimeout_id = setTimeout("handleBlackBerryLocationTimeout()",60000);
						}
						blackberry.location.onLocationUpdate("handleBlackBerryLocation()");
						blackberry.location.refreshLocation(); 
					}
				}
			};
		}
	}
	/*
	 * Ancienne méthode : ne fonctionnait pas sur le Blackberry présent
	 if (typeof navigator.geolocation == "undefined" && typeof blackberry != "undefined") navigator.geolocation = {
		
		getCurrentPosition : function(successCallback, errorCallback, options){
			if(blackberry.location.GPSSupported){
				blackberry.location.setAidMode(2);
				if(successCallback)
					blackberry.location.onLocationUpdate(function(){ successCallback(blackberry.location); });
				if(!blackberry.location.refreshLocation() && errorCallback)
					errorCallback();
			}	else if(errorCallback){
				errorCallback();
			}
		}
	};*/
	
	if (typeof navigator.geolocation == "undefined" && typeof nokia != "undefined" && nokia.device && nokia.device.load && nokia.device.load("geolocation")) navigator.geolocation = {
			
		getCurrentPosition : function(successCallback, errorCallback, options){
			nokia.device.load("geolocation").getCurrentPosition(successCallback, "{timeout: 6000}", errorCallback);
		}
	};
	
	if (typeof navigator.geolocation == "undefined" && typeof Mojo != "undefined") navigator.geolocation = {
			
		getCurrentPosition : function(successCallback, errorCallback, options){
			var geo = new Mojo.Service.Request('palm://com.palm.location', {
				method : 'startTracking',
		        parameters: {
					subscribe: true
		        },
		        onSuccess: function(event) { 
		        	if(successCallback)
		        		successCallback({coords: event});
				},
		        onFailure: function() {
					if(errorCallback)
						errorCallback();
				}
		    });
		}
	};
	
	if (typeof navigator.geolocation == "undefined" && typeof google != "undefined" && typeof google.gears != "undefined" && google.gears != false) navigator.geolocation = {
			
		getCurrentPosition : function(successCallback, errorCallback, options){
			google.gears.factory.create("beta.geolocation").getCurrentPosition(successCallback, errorCallback, options);
		}
	};
	
	if (typeof navigator.geolocation == "undefined" && typeof google != "undefined") navigator.geolocation = {
			
		getCurrentPosition : function(successCallback, errorCallback, options){
			google.load("maps", "3", {other_params: "sensor=false", callback: function(){
				if (google.loader.ClientLocation) {
					if(successCallback)
						successCallback({coords: {latitude: google.loader.ClientLocation.latitude, longitude: google.loader.ClientLocation.longitude}});
				}else if(errorCallback){
					errorCallback();
				}
			}});
		}
	};
})();
