//setting the POST destination page (current page)
var url_post;
var geoTimeOut;

//function wich send datas of position to php page with Ajax Jquery POST 
//input: coordLa,coordLn: coordinates of user's location
//output: POST datas: userLat, userLng, error
function ajaxReq(coorLa,coorLn,erro)
{	
	// ajax request with jquery
	//$.ajax({ type: "POST", url: url_post, data: "userLat=" + coorLa + "&userLng=" + coorLn + "&error=" + erro , success: function(){ window.location.reload();}});
	
	
	//ajax request 
			
			//create object xmlhttp object
			if (window.XMLHttpRequest)
			  {// code for IE7+, Firefox, Chrome, Opera, Safari
			  xmlhttp=new XMLHttpRequest();
			  }
			else
			  {// code for IE6, IE5
			  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			  }
			  
			//  display variables of coordinates
			xmlhttp.onreadystatechange=function()
			  {
				 if (xmlhttp.readyState==4 && xmlhttp.status==200)
				{
					window.location.reload();
				}
			  }
			
			//send variables of coordinates with xmlHttp request
			xmlhttp.open("POST", "index.php" , true);
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.send("userLat=" + escape(coorLa) + "&userLng=" + escape(coorLn) + "&error=" + escape(erro));
				  
	
	
}

//this callback function takes the coordinates of user's position if they are found and send them to ajaxRequest function
//input: position
//caution: use this callback function with the geolocation.getCurrentPosition function inside initGeoloc()
function getPositionSuccessCallback(position)
{
	if(geoTimeOut == true)
	{
		var ScoordLat = position.coords.latitude;
		var ScoordLng = position.coords.longitude;
		var Serror = "0";
		
		geoTimeOut = false;
		ajaxReq(ScoordLat,ScoordLng,Serror);
	}
}

//this callback function is triggered by non available gps coordinates
//input: error message
function getPositionErrorCallback(err) 
{
	if(geoTimeOut == true)
	{
		var EcoordLat = "0";
		var EcoordLng = "0";
		
		geoTimeOut = false;
		ajaxReq(EcoordLat,EcoordLng,err);
	}
}

//this function stops initGeoloc() when it's too long
function timerCallBack()
{
	if(geoTimeOut == true)
	{
		geoTimeOut = false;
		ajaxReq(0,0,2);
	}
}

//this function sets the type of method of geolocation with regard to user's type of geolocalization (iPhone W3c, W3c, Gears)
//we pass datas of geolocating through the callback function (success and error) which uses the ajax POST request function
//input: none
//output: none
function initGeoloc(url_pos)
{	
	//we affect parameter url_pos to the POST parameter
	url_post = url_pos;
	
	geoTimeOut = true;
	
	//trigger the timer
	setTimeout("timerCallBack()",10000);
	
	//if the mobile is an iPad/iPhone, or for geolocation with w3c
	if (!window.google || !google.gears || (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) 
	{
		try
		{
			//function which get postition of w3c mobile
			navigator.geolocation.getCurrentPosition(getPositionSuccessCallback,getPositionErrorCallback);
		}
		catch (e)
		{	
			getPositionErrorCallback(3);
			return;
		}
	}
	//geolocation with Google Gears
	else if(window.google || google.gears)
	{
		//call to Gears geolocation function with exception case
		try 
		{	
			var geolocation = google.gears.factory.create('beta.geolocation');
			geolocation.getCurrentPosition(getPositionSuccessCallback, getPositionErrorCallback);
		}
		catch (e) 
		{
			getPositionErrorCallback(3);
			return;
		}		
	}
	else
	{	
		getPositionErrorCallback(4);
		return;
	}
}
	  
