jQuery(document).ready(function(){

   function markLocationIsUnavailable(fSet) {
      var tViewAllLink = fSet.find('.view-all').find('a').attr('href');
      fSet.find('.view-all').html('Geolocation is not enabled on your device.<br /><a href="' + tViewAllLink + '">Tap here</a> to see the full list of locations.');
      return false;
   }

   function setupNearestLocations(position) {
         jQuery('.geo-locations').each(function() {
            var tRel = jQuery(this).attr('rel').split(':');
            var tSetId = parseInt(tRel[0]);
            var tCount = parseInt(tRel[1]);
            if(tSetId < 1 || tCount < 1)
               return;


            var tDiv = jQuery(this).find('.nearest-locations');
            if(tDiv.length < 1) {
               tDiv = jQuery('<div class="nearest-locations"></div>');
               jQuery(this).prepend(tDiv)
            }
            tDiv.load('/Files/Handlers/GetLocations.ashx?id=' + tSetId + '&lat=' + position.coords.latitude + '&lon=' + position.coords.longitude + '&count=' + tCount + ' .geo-locations ul', function() {
               tDiv.find('.view-map').click(function() {
                  var tMap = jQuery(this).parents('li:first').find('.location-map');
                  if(jQuery(this).html() == 'Hide Map') {
                     tMap.css('display', 'none');
                     jQuery(this).html('View Map');
                  } else{
                     tMap.css('display', 'block');
                     jQuery(this).html('Hide Map');
                  }
                  return false;
               });
            });
         });
   }

   if(jQuery('.geo-locations').length > 0) {
    if(navigator.userAgent.match(/^blackberry/i) && typeof blackberry != 'undefined' && blackberry.location && blackberry.location.GPSSupported) {
      var tUpdated = false;
      function setupNearestLocationsBlackBerry() {
         //blackberry.location.removeLocationUpdate();
         if(blackberry.location.latitude == 0 && blackberry.location.longitude == 0)
            jQuery('.geo-locations').each(function() { markLocationIsUnavailable(jQuery(this)) });
         else if(!tUpdated) {
            setupNearestLocations({ coords: { latitude:blackberry.location.latitude,
                                              longitude:blackberry.location.longitude } });
            tUpdated = true;
         }
      }
      blackberry.location.setAidMode(2);
      blackberry.location.onLocationUpdate(setupNearestLocationsBlackBerry);
      blackberry.location.refreshLocation();
    } else if(navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(setupNearestLocations,
              function (error)
                 {
                    jQuery('.geo-locations').each(function() { markIsUnavailable(jQuery(this)) });
                    /*switch(error.code)
                     {
                     case error.TIMEOUT:
                     alert ('Timeout');
                     break;
                     case error.POSITION_UNAVAILABLE:
                     alert ('Position unavailable');
                     break;
                     case error.PERMISSION_DENIED:
                     alert ('Permission denied');
                     break;
                     case error.UNKNOWN_ERROR:
                     alert ('Unknown error');
                     break;
                     }*/
                 }
              );
   } else {
      jQuery('.geo-locations').each(function() { markLocationIsUnavailable(jQuery(this)) });
    }
  }
});

