/**
 * Модуль для работы с Geolocation API
 */
Vite.geo = function ($) {

	var provider;

	if ("geolocation" in navigator) {
		provider = navigator.geolocation;
	} else {
		try {
			provider = google.gears.factory.create("beta.geolocation");
		} catch (error) {
		}
	}

	// iWidget.geo
	return {

		UNAVAILABLE: 666,
		UNKNOWN_ERROR: 0,
		PERMISSION_DENIED: 1,
		POSITION_UNAVAILABLE: 2,
		TIMEOUT: 3,

		availiable: !!provider,
		enableHighAccuracy: true,
		maximumAge: 0,

		provider: provider,
		params: ["latitude", "longitude", "accuracy"],

		getCurrentPosition: function (successCallback, errorCallback, options) {

			if (this.availiable) {
				try {
					this.provider.getCurrentPosition(successCallback, errorCallback, $.extend({
						enableHighAccuracy: this.enableHighAccuracy,
						maximumAge: this.maximumAge
					}, options));
				} catch (error) {
					errorCallback({code: this.UNKNOWN_ERROR, message: "Something wrong, try again."});
				}
			} else {
				errorCallback({code: this.UNAVAILABLE, message: "Geolocation services are not supported by your browser."});
			}
		},

		getCurPos: function (successCallback, errorCallback, options) {

			var params = this.params;
			var _this = this;

			_this.getCurrentPosition(
				function (position) {
					var i, param, pos = {}, coords = position.coords;
					for (i = 0; i < params.length; i++) {
						param = params[i];
						pos[param] = coords[param];
					}
					successCallback.call(_this, pos);
				},
				errorCallback,
				options
			);

		}

	};

}(jQuery);
