var geo = null;
var request = null;
var isGear = google != null && google.gears != null;
var isHtml5 = navigator != null && navigator.geolocation != null;
var progressbar;

	
if(isGear) 
{
	geo = google.gears.factory.create('beta.geolocation');
	request = google.gears.factory.create('beta.httprequest');	
}

if(isHtml5) 
{
	geo = navigator.geolocation;
	request = new XMLHttpRequest();
}


var geoloc = function() {
		if(GBrowserIsCompatible()) {
			initialize();
			if(geo != null && false) {
				geo.getCurrentPosition(updatePosition, handleError, {maximumAge:60000});
				getProxBars();
			} else {
//				alert("Impossible à geoloc: fake values");

			//

			var la = 48.856666;
			var lo = 2.350987;

			var p = new GLatLng(la, lo);
        	        map.setCenter(p, 15);
	                addPoint(p, "Votre position actuelle", "red");

			$.post('/display/prox-bars.php', { lat: la, long: lo }, getProxBarsCallback, 'json');
			//

			}
		} else {
			alert("Votre browser n'est pas compatible avec Google Maps");
		}
	};


var updatePosition = function(position) {
		var coords = position.coords;
		var p = new GLatLng(coords.latitude, coords.longitude);
		map.setCenter(p, 15);
		addPoint(p, "Votre position actuelle", "red");
};
	
var handleError = function(positionError) {
	  alert('Attempt to get location failed: ' + positionError.message);
};
	
var initialize = function() {	  

	var mapCanvas = $('.current .map_canvas')[0];
        map = new GMap2(mapCanvas);

	map.setUIToDefault();
//	var c = new GLatLng(48.8567, 2.3510);
//	map.setCenter(c, 3);
	
	progressbar = new ProgressbarControl(map);
};

var addPoint = function(point, text, color) {
	  var i = new GIcon();
	  i.image = 'http://labs.google.com/ridefinder/images/mm_20_' + color + '.png';
	  i.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
	  i.iconSize = new GSize(12, 20);
	  i.shadowSize = new GSize(22, 20);
	  i.iconAnchor = new GPoint(6, 20);
	  i.infoWindowAnchor = new GPoint(5, 1);

	  var marker = new GMarker(point,{clickable: true, icon: i});

	if (text)
	{
	  GEvent.addListener(marker, "click", function() {
	    var myHtml = text;
	    map.openInfoWindowHtml(point, myHtml);
	  });
	}  
	map.addOverlay(marker);
};

var glBars;
var addBarsWithProgress = function() {

	if (glBars.length > 0)
	{
		var bar = glBars.pop();
                var p = new GLatLng(bar['lat'], bar['long']);
                addPoint(p, bar['infos'], 'blue');
		progressbar.updateLoader(1);
		setTimeout('addBarsWithProgress()', 10);
	}
	else
	{
		progressbar.remove();
		$('.current #geo_progress_container').remove();
		$('.current .lblLoadBars').hide();
		$('.current .lkLoadedBars').show();
	}

}

var getProxBars = function() {
	geo.getCurrentPosition(function(position){
				        var coords = position.coords;
					$.post('/display/prox-bars.php', { lat: coords.latitude, long: coords.longitude }, getProxBarsCallback, 'json');
				});

}


var getProxBarsCallback = function(res) {
	if (res['success'])
        {
		glBars = res['bars'];
		progressbar.start(res['bars'].length);
		addBarsWithProgress();
//		$('.current .map_canvas').parent().after(res['listBars']);
	}
}



///utils

var dxMap = 
{
	map: null,
	progressbar: null,
	mapClassName: '.current .map_canvas',

	initialize:function() 
	{
        	var mapCanvas = $(this.mapClassName)[0];
        	this.map = new GMap2(mapCanvas);

        	this.map.setUIToDefault();
        	this.progressbar = new ProgressbarControl(map);
	}
	
}

/*

var account =
{
        formLogin : '#frmLogin',
        message : 'mess',
        reqInputsLogin : ['#login', '#password'],
        ajaxLoginPage : '/data/login.php',
	ajaxToggleFav : '/data/toggleFav.php',
	accountPage : '/display/display-account.php',

        login:function()
        {
		$('.current .mess').hide();
		var data = $(this.formLogin+'.current').serialize();
		$.post(this.ajaxLoginPage, data, this.loginCallBack, 'json');
	},
	loginCallBack:function(res)
	{
		if (res['success'])
	        {
			$('#aAccount').click()
		}
		else
		{
			$('.current .mess li').text(res['mess']);			
			$('.current .mess').show();
		}
	},
	toggleFav:function(pId,pType)
	{
		$.post(this.ajaxToggleFav, { idObject: pId, typeObject: pType }, this.toggleFavCallback, 'json');
	},
	toggleFavCallback:function(res)
	{
		if (res['success'])
		{
			$('.current .lkFav div').text(res['mess']);
		}
	},
	toggleRecipeFav:function(pId)
	{
		this.toggleFav(pId, 'RECIPE');
	},
	toggleBarFav:function(pId)
	{	
		this.toggleFav(pId, 'BAR');
	}
}

*/

var gui =
{
	ajaxBarMapPage: '/data/bar-map.php',
	ajaxCommentsPage : '/data/comments.php',
	ajaxPhotosPage : '/data/photos.php',
	ajaxSearchPage : '/data/search.php',

	getBarMap:function(pIdBar) 
	{
		$.post(this.ajaxBarMapPage, { idBar: pIdBar }, this.getBarMapCallback, 'json');
	},
	getBarMapCallback:function(res) 
	{
		if (res['success'])
        	{
			$('.current .lk_get_map').hide();
			$('.current .map_canvas').show();
			initialize();

			var p = new GLatLng(res['lat'], res['long']);
			map.setCenter(p, 15);
                	addPoint(p, res['infos'], 'blue');
		}
	},

	getComments:function(pId, pType)
	{
		$.post(this.ajaxCommentsPage, { idObject: pId, typeObject: pType }, this.getCommentsCallback, 'json');
	},
 	getCommentsCallback:function(res)
        {
		if (res['success'])
		{
			$('.current .lkShowComments').parent().replaceWith(res['comments']);
		}
	},
	getBarComments:function(pId)
	{
		this.getComments(pId, 'bar');
	},
	getCocktailComments:function(pId)
	{
		this.getComments(pId, 'recipe');
	},

	getPhotos:function(pId, pType)
	{
		$.post(this.ajaxPhotosPage, { idObject: pId, typeObject: pType }, this.getPhotosCallback, 'json');
	},
	getPhotosCallback:function(res)
	{
                if (res['success'])
                {
                        $('.current .lkShowPhotos').parent().replaceWith(res['photos']);
                }

	},
	getCocktailPhotos:function(pId)
	{
		this.getPhotos(pId, 'recipe');
	},
	getBarPhotos:function(pId)
	{
		this.getPhotos(pId, 'bar');
	},
	
	getNextItems:function(pUri)
	{
		$.get(pUri, this.getNextItemsCallback);		
	},
	getNextItemsCallback:function(res)
	{
		$('.current .nextItems').replaceWith(res);
	},
	
	searchRecipes:function()
	{

	},
	searchBars:function()
        {

        },
	search:function(pType)
        {
		$.post(this.ajaxSearchPage, { idObject: pId, typeObject: pType }, this.getPhotosCallback, 'json');
        }
}
