var CFG = { URL : 'http://www.serretllibres.com/autorsebrencs/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, inici )
{
 	if( id_imatge != '' )
	{
		$("#imatge_ampliada").removeClass("ocult");
		$("#imatge_ampliada").html( '<img src="/autorsebrencs/img/loader.gif" alt="loader" style="margin: 100px 0px 100px 0px" />' );
		$.getJSON( AJAX, { id_imatge: id_imatge, accio: 'canviar_imatge' },
		function( resposta )
		{			
			$("#imatge_ampliada").html( '<a href="'+ resposta.fitxer +'"><img class="baix1" src="/autorsebrencs/img/450/450/'+ resposta.id_imatge +'" alt="" /></a>' );
 			$("#imatge_descripcio").html( resposta.html );
			$("#named").html( '<a name="' + resposta.id_galeria_imatge +'"></a>' );

			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();
	}
}




