// JavaScript Document
var country ; 
var days ;
var geoLocated = false ;

var language ;

var locWait = new Array() ;
locWait["en"] = 'Waiting for location ...' ;
locWait["de"] = 'Waiting for location ...' ;
locWait["fr"] = 'Waiting for location ...' ;

var noLoc = new Array() ;
noLoc["en"] = 'Location not found, or unavailable' ;
noLoc["de"] = 'Location not found, or unavailable' ;
noLoc["fr"] = 'Location not found, or unavailable' ;


function venueJump(lang) {
	
	language = lang ;
	// filter the venu page by country or day open
	country = document.getElementById("countryMenu") ;
	days = document.getElementById("dayMenu") ;
	
	// this is for geolocation
	//alert("Checking with country =" + country.value) ;
	
	if (! isNaN(country.value)) {
		// we have a distance
		if ( !geoLocated) {
			if ( navigator.geolocation ) {
				notePara = document.getElementById('geoInfo') ;
				notePara.innerHTML = locWait[lang] ;
				navigator.geolocation.getCurrentPosition(foundLocation, noLocation) ; 
			} else {
				noLocation() ;
			}
		} else {
			window.location.href = 'venues.php?ctry=' + country.value + '&day=' + days.value ;
		}
	} else {
		
		window.location.href = 'venues.php?ctry=' + country.value + '&day=' + days.value ;
	}
}

function foundLocation(position) {

	var lat = position.coords.latitude; 
	var long = position.coords.longitude; 

	geoReq = new XMLHttpRequest () ;
	geoURL = self.location.protocol + '//' + self.location.hostname + '/common/geolocate.php?lat=' + lat + '&long=' + long  ; 

	geoReq.open("GET",geoURL,false);
	geoReq.send(null) ;
	
	
	window.location.href = 'venues.php?ctry=' + country.value + '&day=' + days.value ;

}

function noLocation() {
	// try to locate the user based on their profile information
	
	locReq = new XMLHttpRequest () ;
	locURL = self.location.protocol + '//' + self.location.hostname + '/common/autolocate.php' ;
	
	locReq.open("GET",locURL,false) ;
	locReq.send(null) ;
	
	if ( locReq.responseText == 'OK' ) {
		window.location.href = 'venues.php?ctry=' + country.value + '&day=' + days.value ;
	} else {
		// can't find the position
		alert(noLoc[language]) ;
	}
}

function setLocation() {
	geoLocated = true ;
}

/*function pageInit() {
	if ( navigator.geolocation ) {
		mOpt = document.getElementById('geoOpt') ;	
		mOpt.innerHTML = '<option value="geo">Use geolocation</option>' ;
	}
	startMsgCheck() ;
}*/

