$('document').ready(function(){
  $('#geobutton').click(determineLocation);
});

function determineLocation(){
    //var container = Raphael(document.getElementById("spinner"), 125, 125);
    //var spinner = container.image("images/spinnerBW.svg", 0, 0, 125, 125);
    //var attrsToAnimate = { rotation: "720" }; 
    //spinner.animate(attrsToAnimate, 60000);
    //if (Modernizr.geolocation) {
    if (navigator.onLine) {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(displayOnMap, handleLocationError, {
				// Options for geolocation
				maximumAge: 10000,
				timeout: 10000,
				enableHighAccuracy: true
			});
        } else {
            // geolocation is not supported in this browser
            // we can use Google Gears as a fallback
            alert("HTML5 Geolocation is not supported in your browser.");
        }
    } else {
        alert("Upps! You are Offline");
    }
}
function displayOnMap(position){
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;
    
    //document.getElementById("spinner").style.visibility = "hidden";
	// var accuracy = position.coords.accuracy;
	// var timestamp = position.timestamp;
    
    // we use Google Maps to display the location 
    var myOptions = {
        zoom: 14,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    
    var map = new google.maps.Map(document.getElementById("mapDiv"), myOptions);
    var initialLocation = new google.maps.LatLng(latitude, longitude);
    
    var marker = new google.maps.Marker({
        position: initialLocation,
        map: map,
        title: "Hello World!"
    });
    map.setCenter(initialLocation);
}

function handleLocationError(error) {
	switch(error.code){
	case 0:
		updateStatus("There was an error while retrieving your location: " + error.message);
		break;
	case 1:
		updateStatus("The user prevented this page from retrieving a location.");
		break;
	case 2:
		updateStatus("The browser was unable to determine your location: " +
		error.message);
		break;
	case 3:
		updateStatus("The browser timed out before retrieving the location.");
		break;
	}
}

function loadDemo() {
	if(navigator.geolocation) {
		document.getElementById("support").innerHTML = "HTML5 Geolocation supported.";
	} else {
		document.getElementById("support").innerHTML = "HTML5 Geolocation is not supported in your browser.";
	}
}
/*
// ############# Example of a "one-shot" position request.

function showMap(position) {
      // Show a map centered at (position.coords.latitude, position.coords.longitude).
    }

 // One-shot position request.
 navigator.geolocation.getCurrentPosition(showMap);
 
 
// ############# Example of requesting repeated position updates.

    function scrollMap(position) {
      // Scrolls the map so that it is centered at (position.coords.latitude, position.coords.longitude).
    }

    // Request repeated updates.
    var watchId = navigator.geolocation.watchPosition(scrollMap);

    function buttonClickHandler() {
      // Cancel the updates when the user clicks a button.
      navigator.geolocation.clearWatch(watchId);
    }

// ############# Example of requesting repeated position updates and handling errors.

    function scrollMap(position) {
      // Scrolls the map so that it is centered at (position.coords.latitude, position.coords.longitude).
    }

    function handleError(error) {
      // Update a div element with error.message.
    }

    // Request repeated updates.
    var watchId = navigator.geolocation.watchPosition(scrollMap, handleError);

    function buttonClickHandler() {
      // Cancel the updates when the user clicks a button.
      navigator.geolocation.clearWatch(watchId);
    }
	
// ############# Forcing the user agent to return a fresh cached position.

    // Request a position. We only accept cached positions whose age is not
    // greater than 10 minutes. If the user agent does not have a fresh
    // enough cached position object, it will immediately invoke the error
    // callback.
    navigator.geolocation.getCurrentPosition(successCallback,
                                             errorCallback,
                                             {maximumAge:600000, timeout:0});

    function successCallback(position) {
      // By using the 'maximumAge' option above, the position
      // object is guaranteed to be at most 10 minutes old.
      // By using a 'timeout' of 0 milliseconds, if there is
      // no suitable cached position available, the user agent 
      // will aynchronously invoke the error callback with code
      // TIMEOUT and will not initiate a new position
      // acquisition process.
    }

    function errorCallback(error) {
      switch(error.code) {
        case error.TIMEOUT:
          // Quick fallback when no suitable cached position exists.
          doFallback();
          // Acquire a new position object.
          navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
          break;
        case ... // treat the other error cases.
      };
    }

    function doFallback() {
      // No fresh enough cached position available.
      // Fallback to a default position.
    
// ########### Forcing the user agent to return any available cached position.

    // Request a position. We only accept cached positions, no matter what 
    // their age is. If the user agent does not have a cached position at
    // all, it will immediately invoke the error callback.
    navigator.geolocation.getCurrentPosition(successCallback,
                                             errorCallback,
                                             {maximumAge:Infinity, timeout:0});

    function successCallback(position) {
      // By setting the 'maximumAge' to Infinity, the position
      // object is guaranteed to be a cached one.
      // By using a 'timeout' of 0 milliseconds, if there is
      // no cached position available at all, the user agent 
      // will immediately invoke the error callback with code
      // TIMEOUT and will not initiate a new position
      // acquisition process.
      if (position.timestamp < freshness_threshold && 
          position.coords.accuracy < accuracy_threshold) {
        // The position is relatively fresh and accurate.
      } else {
        // The position is quite old and/or inaccurate.
      }
    }

    function errorCallback(error) {
      switch(error.code) {
        case error.TIMEOUT:
          // Quick fallback when no cached position exists at all.
          doFallback();
          // Acquire a new position object.
          navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
          break;
        case ... // treat the other error cases.
      };
    }

    function doFallback() {
      // No cached position available at all.
      // Fallback to a default position.
    }
*/

