var CFG = { URL : '/ajax/ajax.php' }

window.dhtmlHistory.create({
    toJSON: function(o) {
        return JSON.stringify(o); 
    } , fromJSON: function(s) {
        return JSON.parse(s); 
    } 
}); 

var yourListener = function(newLocation, historyData) {
	iniciar();
}

window.onload = function() {
	dhtmlHistory.initialize();
	dhtmlHistory.addListener(yourListener);
	iniciar();
}




function iniciar()
{
	var h = window.location.hash;
	
	if( h != '' )
	{
		var long = h.length;
		var hh = h.substr( 1, long)
		canviar_imatge(hh);
	}
}
function canviar_imatge( id_imatge )
{
 	if( id_imatge != '' )
	{
		$("#galeria_mapa").show();
		$("#galeria_fotografia").hide();
		$("#mapat_galeria").hide();
		$("#imatge_ampliada").fadeIn("slow");
		
		$("#imatge_ampliada").removeClass("ocult");
		$("#imatge_ampliada").html( '<img src="/img/icones/carregar.gif" alt="loader" style="margin: 100px 0 0 0" />' );
		$.getJSON( AJAX, { id_imatge: id_imatge, accio: 'canviar_imatge' },
		function( resposta )
		{
			window.location.hash = resposta.id_galeria_imatge;
			window.location = "album#"+ resposta.id_galeria_imatge;	
	
			$("#imatge_ampliada").html( '<a href="'+ resposta.fitxer +'"><img src="/img/540/540/'+ resposta.id_imatge +'" alt="" /></a>' );
			$("#imatget").html( '<a href="'+ resposta.fitxer +'"><img src="/img/540/540/'+ resposta.id_imatge +'" alt="" /></a>' );
 			$("#imatge_descripcio").html( resposta.html );
			$("#named").html( '<a name="' + resposta.id_galeria_imatge +'"></a>' );
			
			if( resposta.poblacio )
			{
				var mapat = new GMap2(document.getElementById("mapat"));
				var geocoder = new GClientGeocoder();
	
				geocoder.getLatLng( resposta.poblacio,
					function(point)
					{
						if (point)
						{
							mapat.addControl (new GSmallZoomControl());
							mapat.addControl (new GMapTypeControl());
							mapat.enableDoubleClickZoom();
							mapat.enableScrollWheelZoom();
							mapat.setCenter(point, 10);
							var marker = new GMarker(point);
							mapat.addOverlay(marker);
						}
					}
				);
			}
			
			if( $("#pagina").val() != 'totes' && $("#pagina").val() != resposta.pagina )
				canviar_fotos( resposta.pagina, resposta.id_galeria );
				
			var tmp = $("#imatge_anterior").val();
			$("#img_mini_"+tmp).removeClass("actiu");
			
			$("#imatge_anterior").val( resposta.id_galeria_imatge );
			$("#img_mini_"+resposta.id_galeria_imatge).addClass("actiu");
		});
	}
}


function canviar_fotos( pagina, id_galeria )
{
	$('#pagina').val( pagina );

 	if( id_galeria != '' )
	{
		$.post( AJAX, { pagina: pagina, id_galeria: id_galeria, accio: 'canviar_fotos' },
		function( resposta )
		{
			$('#imatges').html( resposta );
		});
	}
}
function veure_totes( id_galeria )
{
 	$('#pagina').val( 'totes' );
 	
 	if( id_galeria != '' )
	{
		$.post( AJAX, { id_galeria: id_galeria, totes: 'true', accio: 'canviar_fotos' },
		function( resposta )
		{
			$('#imatges').html( resposta );
		});
		
		$("#veure_totes").hide();
	}
}


function galeria_mapa( lloc, id_galeria_imatge )
{
	if( lloc == 'mapa' )
	{
		$("#galeria_mapa").hide();
		$("#galeria_fotografia").show();
		
		$.getJSON( AJAX, { id_galeria_imatge: id_galeria_imatge, accio: 'veure_mapa_galeria' },
		function( resposta )
		{
			if( GBrowserIsCompatible() && resposta.lat != 0 && resposta.lon != 0 )
			{
				mapat = new GMap2(document.getElementById( "mapat_galeria" ));
				var geocoder = new GClientGeocoder();
				
				mapat.addControl (new GSmallZoomControl());
				mapat.addControl (new GMapTypeControl());
				
				mapat.enableDoubleClickZoom();
				mapat.enableScrollWheelZoom();
			
				var punt = new GLatLng( resposta.lat , resposta.lon );
				
				mapat.setCenter( punt , 6 );
				marker = new GMarker( punt, {draggable: true}  );
				mapat.addOverlay(marker);
			}
		});
		
		$("#imatge_ampliada").hide();
		$("#mapat_galeria").fadeIn("slow");
	}
	else
	{
		$("#galeria_mapa").show();
		$("#galeria_fotografia").hide();
		$("#mapat_galeria").hide();
		$("#imatge_ampliada").fadeIn("slow");
	}	
}
