
// Check in the browser supports native geolocation and make a call to the geolocation service
function findLocation() {
	if(navigator.geolocation) {
		Element.show('loading_alt');
		navigator.geolocation.getCurrentPosition(successCallback,errorCallback);
	} else {
		var displayLocation = document.getElementById("location");
		displayLocation.innerHTML = "Your browser doesn't support geolocation services";
	}
}

// Extract the latitude and longitude from the response
function successCallback(position) {
  
  var latitude = position.coords.latitude;
  var longitude = position.coords.longitude;
  //var displayLocation = document.getElementById("location");
  //displayLocation.innerHTML = "latitude: " + latitude + ", longitude:" + longitude;
  //alert("latitude: " + latitude + ", longitude:" + longitude);
  
  new Ajax.Request('/geolocation', {asynchronous:true, evalScripts:true,
				onComplete:function(request){Element.hide('loading_alt'); self.location.href = "/"+request.responseText;}, 
				//onLoading:function(request){Element.show('loading');}, 
				//onException: function(req,exception) {maploading=0;return true;},
				//onFailure: function(request) {maploading=0;return true;},
				parameters:'latitude=' + latitude+'&longitude='+longitude + '&authenticity_token=' + encodeURIComponent(token)});


	
//<%#= remote_function( :url => {:controller => 'iphone',  :action => 'iphoneweather' }, :with =>"'latitude=' + latitude+'&longitude='+longitude", :complete   =>  "alert(latitude); ") %>

  //data(request.responseText);
  
}

// There was an error
function errorCallback() {
  //var displayLocation = document.getElementById("location");
  alert("Sorry, we couldn't find your location");
}

//function location(data){
	//alert("hej");
	//self.location.href = "/"+data; 
//}
//window.onload = findLocation;

