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 != '' )
	{
		$("#imatge_ampliada").removeClass("ocult");
		
		$("#imatge_ampliada").html( '<img src="/img/icones/loader.gif" alt="loader" style="margin: 100px 0px 100px 0px" />' );
		$.getJSON( AJAX, { id_imatge: id_imatge, accio: 'canviar_imatge' },
		function( resposta )
		{
			window.location.hash = resposta.id_entrada_imatge;
// 			window.location = "#"+ resposta.id_entrada_imatge;	
	
			$("#imatge_ampliada").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_entrada_imatge +'"></a>' );
				
//  			var variable = resposta.id_entrada_imatge % 2;
// 
//  			if( variable )
//  			{
//  				$("#imatge_ampliada_img1").fadeTo(1000,0);
//  				$("#imatge_ampliada_img2").css({opacity: "0"})
// 				
//  				var img = document.getElementById('imatge_ampliada_img2');
// 				img.src = "/img/540/540/" + resposta.id_imatge;
// 				img.alt = resposta.titol;
//  			}
//  			else
//  			{
//  				$("#imatge_ampliada_img2").fadeTo(1000,0);
//  				$("#imatge_ampliada_img1").css({opacity: "0"})
// 				
//  				var img = document.getElementById('imatge_ampliada_img1');
// 				img.src = "/img/540/540/" + resposta.id_imatge;
// 				img.alt = resposta.titol; 				
//  			}
// 			
// 
//  			
//  			if( variable )
//  			{
//  				var img = document.getElementById('imatge_ampliada_img1');
// 				img.src = "";
// 				img.alt = "";
// 				
//  				$("#imatge_ampliada_img1").hide();
//  				$("#imatge_ampliada_img2").show();
//  				$("#imatge_ampliada_img2").fadeTo(1000,1);
//  			}
//  			else
//  			{
//  				var img = document.getElementById('imatge_ampliada_img2');
// 				img.src = "";
// 				img.alt = "";
// 				
//  				$("#imatge_ampliada_img2").hide();
//  				$("#imatge_ampliada_img1").show();
//  				$("#imatge_ampliada_img1").fadeTo(1000,1);
// 			}

			if( $("#pagina").val() != 'totes' && $("#pagina").val() != resposta.pagina )
				canviar_fotos( resposta.pagina, resposta.id_entrada );
				
			var tmp = $("#imatge_anterior").val();
			$("#img_mini_"+tmp).removeClass("actiu");
			$("#imatge_anterior").val( resposta.id_entrada_imatge );
			$("#img_mini_"+resposta.id_entrada_imatge).addClass("actiu");
		});
	}
}


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

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

