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

function veure_imatge( id_imatge )
{
	$('imatge_ampliada').innerHTML = '';

 	if( id_imatge != '' )
	{
		new Ajax.Request( CFG.URL, {
			method: 'post',
			parameters: { id_imatge: id_imatge, accio: 'veure_imatge' },
			onSuccess: veure_imatge_cb.bind( this ),
			onFailure: function(){alert("ups, no puc connectar-me!");}
		} );
	}
	else
	{
		alert("Error inesperat");
		return false;
	}
}


function veure_imatge_cb( resposta )
{
	var ajax_data = resposta.responseText;
	var res = ajax_data.split("||");

	var offset = 0;
	var myWidth = 0;
  
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		offset = window.pageYOffset;
		myWidth = window.innerWidth;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		offset = document.body.scrollTop;
		myWidth = document.documentElement.clientWidth;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		offset = document.documentElement.scrollTop;
		myWidth = document.body.clientWidth;
	}

	offset = offset + 150;
	myWidth = myWidth / 3;
	if( myWidth == 0 )
		myWidth = document.body.clientWidth / 3;
		
	$('imatge_ampliada').style.display = 'block';
	$('imatge_ampliada').style.top = offset + 'px';
	$('imatge_ampliada').style.left = myWidth + 'px';
	
	var html = '<div id="imatge_ampliada_fons">';
	html += '<h5 style="color: #656551">'+ res[0] +'</h5>';
	html += '		<div style="float: right; color: #656551"><a onclick="tancar()" style="color: #656551">X</a></div><br clear="all" />\n';
	html += '		<center><a href="/imatges/'+ res[4] +'"><img src="redimensiona.php?alcada=300&amp;amplada=300&amp;id_imatge='+ res[1]  +'" alt="imatge" /></a><br /><br />';
	html += '		<div style="font-size: 15px"><a onclick="veure_imatge(\''+ res[3] +'\')" style="color: #656551">&lt;</a> <a onclick="veure_imatge(\''+ res[2] +'\')" style="color: #656551">&gt;</a></div></center>';
	html += '</div>\n';
	
	$('imatge_ampliada').innerHTML = html;
}

function tancar()
{
	$('imatge_ampliada').innerHTML = '';
}

function informacio()
{
	var opcio;
	
	var email = $('email').value;

 	if( email )
	{
		if( !echeck( email ) )
		{
			alert("El correu electrònic que has posat és incorrecte");
			return false;
		}
		
		new Ajax.Request( CFG.URL, {
			method: 'post',
			parameters: { email: email, accio: 'inscriure_alta' },
			onSuccess: inscriure_cb.bind( this ),
			onFailure: function(){alert("ups, no puc connectar-me!");}
		} );
	}
	else
	{
		alert("Falta posar el teu correu electrónic");
		return false;
	}
}

function inscriure_cb( resposta )
{
	var ajax_data = resposta.responseText;
	
	$('subscriu').innerHTML = ajax_data;
}

function echeck(str)
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
		return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
			return false
	}

		if (str.indexOf(at,(lat+1))!=-1){
			return false
		}

		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			return false
		}

		if (str.indexOf(dot,(lat+2))==-1){
			return false
		}
	
		if (str.indexOf(" ")!=-1){
			return false
		}

		return true
}

function desplega_blocs () {
	$('blocs_regidors').style.display = 'block';
}
function desplega_activitats () {
	$('activitats').style.display = 'block';
}