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

window.onload = function() {
	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 )
{
	idImatge = id_imatge;
	
 	if( id_imatge != '' )
	{
		new Ajax.Request( CFG.URL, {
			method: 'post',
			parameters: { id_imatge: id_imatge, accio: 'canviar_imatge' },
			onSuccess: canviar_imatge_cb.bind( this ),
			onFailure: function(){alert("ups, no puc connectar-me!");}
		} );
	}
}
function canviar_imatge_cb( resposta )
{
	var resposta = resposta.responseText;

	window.location.hash = idImatge;
	
	Element.removeClassName($('imatge_ampliada'), 'ocult');
	Element.addClassName($('imatge_descripcio'), 'ocult');
	$('imatge_ampliada').innerHTML = resposta;
}


function canviar_fotos( pagina, id_galeria )
{
	var foto = '';
	
	if( window.location.hash )
		foto = window.location.hash;

 	if( id_galeria != '' )
	{
		new Ajax.Request( CFG.URL, {
			method: 'post',
			parameters: { pagina: pagina, id_galeria: id_galeria, foto: foto, accio: 'canviar_fotos' },
			onSuccess: canviar_fotos_cb.bind( this ),
			onFailure: function(){alert("ups, no puc connectar-me!");}
		} );
	}
}
function canviar_fotos_cb( resposta )
{
	var resposta = resposta.responseText;
		
	$('imatges').innerHTML = resposta;
}
function veure_totes_cb( resposta )
{
	var resposta = resposta.responseText;
		
	$('imatges').innerHTML = resposta;
	$('veure-totes').style.display = 'none';
	
}

function postal_form( id_galeria_imatge )
{
	var html = '<div class="postal"><br />';
	html += '	<input type="hidden" id="id_galeria_imatge" value="'+ id_galeria_imatge +'" />\n';
	html += '	<label for="nom_e">Nom del destinatari</label>\n';
	html += '	<input id="nom_e" type="text" value="" name="nom" />\n';
	html += '	<br class="p" />\n';
	html += '	<label for="email_e">Correu electrònic del destinatari</label>\n';
	html += '	<input id="email_e" type="text" value="" name="email" />\n';
	html += '	<br class="p" />\n';
	html += '	<label for="r_nom">El teu nom</label>\n';
	html += '	<input id="r_nom" type="text" value="" name="r_nom" />\n';
	html += '	<br class="p" />\n';
	html += '	<label for="r_email">El teu correu</label>\n';
	html += '	<input id="r_email" type="text" value="" name="r_email" />\n';
	html += '	<br class="p" />\n';
	html += '	<label for="comentari">Text de la postal</label>\n';
	html += '	<textarea id="comentari" name="comentari"></textarea><br /><br />\n';
	html += '	<div style="float: left;"<input type="image" src="img/icones/enviar.gif" onclick="enviar_email()" style="border: none; background-color: transparent; width: 62px" /></div>\n';
	html += '<div style="float: right; margin: 10px 10px 0px 0px"><a onclick="tancar()"><img src="/img/icones/tancar.gif" alt="tancar" /></a></div><br class="p" />\n';
	html += '</div><br />\n';

	$( 'postal_form' ).innerHTML = html;
	$( 'postal_form' ).style.display = 'block';
}
function tancar( )
{
	$( 'postal_form' ).innerHTML = '';
	$( 'postal_form' ).style.display = 'none';
}

function enviar_email()
{
	var email = $('email_e').value;
	var nom = $('nom_e').value;
	var r_email = $('r_email').value;
	var r_nom = $('r_nom').value;
	var comentari = $('comentari').value;
	var id_galeria_imatge = $('id_galeria_imatge').value;
		
	if( !email )
	{
		alert("Has de posar el correu electrònic del destinatari");
		return false;
	}
	
	if( !nom )
	{
		alert("Has de posar el nom del destinatari");
		return false;
	}
	
	if( !r_email )
	{
		alert("Has de posar el teu correu electrònic");
		return false;
	}
	
	if( !r_nom )
	{
		alert("Has de posar el teu nom");
		return false;
	}
		
	new Ajax.Request( CFG.URL, {
		method: 'post',
		parameters: { email: email, nom: nom, r_email: r_email, r_nom: r_nom, id_galeria_imatge: id_galeria_imatge, comentari: comentari, accio: 'postal' },
		onSuccess: enviar_email_cb.bind( this ),
		onFailure: function(){alert("ups, no puc connectar-me!");}
	} );

}

function enviar_email_cb()
{
	var html = '	<br /><br />La postal s\'ha enviat correctament<br />\n';
	html += '	<div style="float: right"><a onclick="tancar()"><img src="/img/icones/tancar.gif" alt="tancar" /></a></div><br class="p" /><br />\n';

	$( 'postal_form' ).innerHTML = html;
	
	setTimeout('enviar_email_cb2()',2000)
}

function enviar_email_cb2()
{
 	$( 'postal_form' ).style.display = 'none';
}