var AJAX = '/ajax/ajax.php';

//if( location.href.match(/#(c-\d+)$/) )
//{
//	$("#"+RegExp.$1+">:first").css("border-style","double").css("border-width","3px")
//}

$(document).ready(function(){
	$('#cap_destacat').bxSlider({
		auto: 'true',
		ticker: true,
		ticker_hover: true, 
		speed: 1500,
		pause: 6000,
		width: 500,
		margin: 40,
		controls: false
	});
});
	
function menu( lloc )
{
	if( $('#submenu_actualitat').css( "display" ) == 'block' )
		var lloc_actual = 'actualitat';
	else if( $('#submenu_municipis').css( "display" ) == 'block' )
		var lloc_actual = 'municipis';
	
	$('#submenu_'+lloc_actual).fadeOut("fast", function()
	{
		$('#submenu_'+lloc).fadeIn("fast");
	});	
}

function mostra( lloc )
{
	if ( lloc == 'esfcat')
	{
		$('#blogs').hide();
		$('#esfcat').fadeIn("fast");
	}
	else if ( lloc == 'blogs')
	{
		$('#esfcat').hide();
		$('#blogs').fadeIn("fast");
	}
}

function avis_legal()
{
	$("#avis_legal").fadeIn('slow');	
}

function les_noticies( lloc ) {

	if( lloc == 'comentaris' )
	{
		$("#mes_comentat").slideDown();
		$("#mes_llegit").slideUp();
		
		$("#comentaris_tag").removeClass("plantilla_actualitat_tag_inactiu");
		$("#comentaris_tag").addClass("plantilla_actualitat_tag_actiu");
		$("#noticies_tag").removeClass("plantilla_actualitat_tag_actiu");
		$("#noticies_tag").addClass("plantilla_actualitat_tag_inactiu");
	}
	else
	{
		$("#mes_comentat").slideUp();
		$("#mes_llegit").slideDown();
		
		$("#noticies_tag").removeClass("plantilla_actualitat_tag_inactiu");
		$("#noticies_tag").addClass("plantilla_actualitat_tag_actiu");
		$("#comentaris_tag").removeClass("plantilla_actualitat_tag_actiu");
		$("#comentaris_tag").addClass("plantilla_actualitat_tag_inactiu");
	}
}

function votar()
{
	var enq = $("input[@name='enq']:checked").attr("value");
  	if( enq )
	{
		$.getJSON( AJAX, { id_enquesta_opcio: enq, accio: 'votar' },
		function( resposta )
		{
			var vots = 0;
			vots = parseInt( vots );
				
 			$('#enquesta-opcions').fadeOut("slow", function()
 			{
				$(this).empty();

				for(var i = 0; i < resposta.opcions.length; i++)
				{
					tmp = parseInt( resposta.opcions[i].vots );
					vots = tmp + vots;
					afegeixOpcio(resposta.opcions[i].id_element, resposta.opcions[i].titol, resposta.opcions[i].width1, resposta.opcions[i].percentatge, enq, i);
				}

				html = '<div class="llegenda_esquerra gris"><img src="img/icones/vots.png" alt="Vots" /> <span id="enquesta-vots">'+ vots +'</span> vots&nbsp;&nbsp;<a href="enquesta/'+ resposta.uri  +'#comentaris"><img src="img/icones/comentari.png" alt="Comentaris" /> '+ resposta.comentaris +'</a></div></div>';

				$('#enquesta-opcions').append( html );
				$('#enquesta-opcions').fadeIn("slow",function(){animateResults();});
			});
		});
	}
	else
	{
		alert("Heu de seleccionar una opció!");
		return false;
	}
}

function afegeixOpcio(id, titol, width1, percentatge, enq, i)
{
	var html = '';

	if( id == enq )
		html += '<label for="enq_opt'+id+'"><strong>'+titol+'</strong></label><br />\n';
	else
		html += '<label for="enq_opt'+id+'">'+titol+'</label><br />\n';
		
	html += '	<div class="enq_barra" style="width: 0%;"></div>\n';
	html +=	'	<div class="enq_percent">'+percentatge+'%</div>\n';
	html +=	'<br class="p" />\n';

	$('#enquesta-opcions').append( html );
}

function animateResults()
{
  $("#enquesta-opcions div.enq_barra").each(function()
  {
      var percentage = $(this).next().text();
      $(this).css({width: "0%"}).animate({width: percentage}, 'slow');
  });
}


function inscriure( accio )
{

	var email = $("#email-titulars").val();
	var nom = $("#nom-titulars").val();
         
	if( accio == 'alta' )
		accio = 'inscriure_alta';
	else
		accio = 'inscriure_baixa';
		
	if( email )
	{
		if( !echeck( email ) )
		{
			alert("El correu electrònic que has posat és incorrecte");
			return false;
		}
		
		if( accio == 'alta' ) accio = 'inscriure_alta';

		if( accio == 'baixa' ) accio = 'inscriure_baixa';

				
		$.getJSON( AJAX, { email: email, nom: nom, accio: accio },
		function( resposta )
		{
			if( resposta.exit == 'true' ) $("#inscriure-formulari").empty();
			$("#inscrit").fadeIn("slow")
		});
	}
	else
	{
		alert("Falta posar la teva adreça electrònica");
		return false;
	}
}

function comprovaCamp ( id_camp, color )
{

	if( $("#"+id_camp).val() == "" )
	{
		$("#"+id_camp).addClass("comentari-error");
		$("#"+id_camp).removeClass("comentari-"+color);
	}
	else
	{
		$("#"+id_camp).addClass("comentari-"+color);
		$("#"+id_camp).removeClass("comentari-error");
	}
}

function comprovaMail ( id_camp, color )
{
	if ( $("#"+id_camp).val() == "" || !echeck( $("#"+id_camp).val() ) )
	{
		$("#"+id_camp).addClass("comentari-error");
		$("#"+id_camp).removeClass("comentari-"+color);
	}
	else
	{
		$("#"+id_camp).addClass("comentari-"+color);
		$("#"+id_camp).removeClass("comentari-error");
	}
}


function enviar_comentari()
{
	var error = false;
	var error_text = '';
	var id_article;
	var nom;
	var tmp;
	var email;
	var comentari;
	var poblacio = "";

	var id_article = $("#id_article").val();
	var lloc = $("#lloc").val();
	var color = $("#color").val();

	if( $("#email").val() ) return false;
	if( $("#nom").val() )
	{
		$("#nom").removeClass("comentari-error");
		$("#nom").addClass("comentari-"+color);
		var nom = $("#nom").val();
	}
	else
	{
		$("#nom").addClass("comentari-error");
		$("#nom").removeClass("comentari-"+color);
		error_text += "<li>Has de posar el teu nom</li>\n";
		error = true;
	}
	
	if( $("#poblacio").val() )
		var poblacio = $("#poblacio").val();

	if( $("#ae").val() )
	{
		var ae = $("#ae").val();
		if( echeck( ae ) )
		{
			$("#ae").removeClass("comentari-error");
			$("#ae").addClass("comentari-"+color);
		}
		else
		{
			$("#ae").addClass("comentari-error");
			$("#ae").removeClass("comentari-"+color);
			error_text += "<li>El correu elctrònic que has posat és erroni</li>\n";
			error = true;
		}
	}
	else
	{
		$("#ae").addClass("comentari-error");
		$("#ae").removeClass("comentari-"+color);
		error_text += "<li>Has de posar el correu electrònic</li>\n";
		error = true;
	}
	
	if( $("#comentari").val() )
	{
		var comentari = $("#comentari").val();
		$("#comentari").removeClass("comentari-error");
		$("#comentari").addClass("comentari-"+color);
	}
	else
	{
		$("#comentari").addClass("comentari-error");
		$("#comentari").removeClass("comentari-"+color);
		error_text += "<li>Has d'escriure un comentari</li>\n";
		error = true;
	}

 	if( !error )
	{
		$("#comentari-formulari").html( '<img src="/img/icones/loading.gif" alt="loader" style="margin: 100px 0px 100px 250px" />' );
		$.post( AJAX, { id_article: id_article, lloc: lloc, nom: nom, poblacio: poblacio, email: ae, comentari: comentari, accio: 'enviar_comentari' },
		function( resposta )
		{
			$("#comentari-error").css("display", "")
			$("#comentari-formulari").fadeOut("slow");
			$("#comentari-enviat").fadeIn("slow");
		});
	}
	else
	{
		$("#comentari-error").fadeIn("slow");
		$("#comentari-error").html( "<ul>" + error_text + "</ul>");
	}
}


function genera_comentaris( pagina, id_article, lloc, color )
{
 	if( pagina && id_article )
	{
		$("#comentaris-cnt").html( '<img src="/img/icones/loading.gif" alt="loader" style="margin: 100px 0px 100px 250px" />' );
		$.post( AJAX, { id_article: id_article, pagina: pagina, lloc: lloc, color: color, accio: 'genera_comentaris' },
		function( resposta )
		{
			$("#comentaris-cnt").html( resposta );
			window.location.hash = "comentaris";
		});
	}
	else
	{
		alert("Error");
	}
}

function veure_comentari( id_comentari, lloc )
{
 	if( id_comentari )
	{
		$.getJSON( AJAX, { id_comentari: id_comentari, lloc: lloc, accio: 'veure_comentari' },
		function( resposta )
		{
			$("#comentari-cnt-" + resposta.id_comentari ).html( resposta.text );
		});
	}
	else
	{
		alert("Error");
	}
}

function limitacio( numero, id_article, lloc, color )
{
 	if( numero && id_article && lloc )
	{
		$("#comentaris-cnt").html( '<img src="/img/icones/loading.gif" alt="loader" style="margin: 100px 0px 100px 250px" />' );
		$.post( AJAX, { numero: numero, accio: 'limitacio' },
		function()
		{
			genera_comentaris( "1", id_article, lloc, color );
		});
	}
	else
	{
		alert("Error");
	}
}





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 generaCodi()
{
	var ample = $("#ample").val();
	
	if( ample < 150 )
	{
		alert( "L'amplada no potser més petita de 150 pixels" )
	}
	else
	{
		document.getElementById("codi1").value='<script language="javascript" src="http://www.aravalles.cat/titulars.php?ample='+ ample +'"></script>';
	}
}



// Variables
var ratings_mouseover_image = new Image();
ratings_mouseover_image.src = "/img/icones/valora_over.gif";

// When User Mouse Over Ratings
function valora_actual(rating, rating_text)
{
	var id_article = $('#id_article').val();

	for(i = 1; i <= rating; i++)
		document.images['valora_' + i].src = eval("ratings_mouseover_image.src");
		
	$('#valora_text').html( rating_text );
	$('#valora_text').css("display", "inline");
}


// When User Mouse Out Ratings
function valora_off(rating_score, insert_half)
{
	for(i = 1; i <= 5; i++)
	{
		if(i <= rating_score)
			document.images['valora_' + i].src = '/img/icones/valora_on.gif';
		else if(i == insert_half)
			document.images['valora_' + i].src = '/img/icones/valora_meitat.gif';
		else
			document.images['valora_' + i].src = '/img/icones/valora_off.gif';
	}
	
	$('#valora_text').html( '' );
	$('#valora_text').css("display", "inline");
}


function valora_article( valor )
{
	var id_article = $('#id_article').val();

	var lloc = $('#lloc').val();

	$('#valora_estrelles').css("display", "none");
	$('#valora_loading').css("display", "inline");

	$.getJSON( AJAX, { lloc: lloc, valor: valor, id_article: id_article, accio: 'valorar' },
	function( resposta )
	{
		$('#valora_estrelles').css("display", "inline")
		$('#valora_loading').css("display", "none")
		
		$('#valora_estrelles').html( '' );
		var html = '';
		
		var textos=['','Molt dolent','Fluix','Interessant','Molt bo','Excepcional'];
	
		for( var i = 1; i <= 5; i++ )
		{
			if( resposta.mitja >= i )
				html += '<img src="/img/icones/valora_on.gif" alt="estrella" />';
			else if( resposta.meitat == i )
				html += '<img src="/img/icones/valora_meitat.gif" alt="estrella" />';
			else
				html += '<img src="/img/icones/valora_off.gif" alt="estrella" />';
		}
		
		if( resposta.vots == 1 )
			html += ' <small>( <strong>'+ resposta.vots +'</strong> vot )</small>';
		else
			html += ' <small>( <strong>'+ resposta.vots +'</strong> vots )</small>';

		$('#valora_estrelles').html( html );
	});
}

function comparteix_portada( url, id_article )
{
	if ($("#comparteix").length === 0) {
		var html = '<a href="http://latafanera.cat/submit.php?url=' + url + '" title="Envia a La Tafanera" target="_blank"><img src="img/icones/tafanera.png" alt="La Tafanera" /></a> ';
		html += '<a href="http://technorati.com/search/' + url + '" title="Cerca a Technorati" target="_blank"><img src="img/icones/technorati.png" alt="Technorati" /></a> ';
		html += '<a href="http://del.icio.us/post?url=' + url + '" title="Envia a Delicious" target="_blank"><img src="img/icones/delicious.png" alt="Delicious" /></a> ';
		html += '<a href="http://www.facebook.com/share.php?u=' + url + '" title="Comparteix a Facebook" target="_blank"><img src="img/icones/peu_facebook.png" alt="Facebook" /></a> ';
		html += '<a href="http://www.google.com/bookmarks/mark?op=edit&bkmk=' + url + '" title="Comparteix a Google" target="_blank"><img src="img/icones/google.png" alt="Google" /></a>';
		
		if( $("#llegenda_dreta-" + id_article) )
			$("#llegenda_dreta-" + id_article).append('<span id="comparteix">'+ html +'</span>');
		if( $("#llegenda_dreta-" + id_article) )
			$("#llegenda_dreta-" + id_article).append('<span id="comparteix">'+ html +'</span>');
		
		$("#comparteix").fadeIn("slow");
	}
}

function comparteix( url )
{
	if ($("#comparteix").length === 0) {
		var html = '<a href="http://latafanera.cat/submit.php?url=' + url + '" title="Envia a La Tafanera" target="_blank"><img src="img/icones/tafanera.png" alt="La Tafanera" /></a> ';
		html += '<a href="http://technorati.com/search/' + url + '" title="Cerca a Technorati" target="_blank"><img src="img/icones/technorati.png" alt="Technorati" /></a> ';
		html += '<a href="http://del.icio.us/post?url=' + url + '" title="Envia a Delicious" target="_blank"><img src="img/icones/delicious.png" alt="Delicious" /></a> ';
		html += '<a href="http://www.facebook.com/share.php?u=' + url + '" title="Comparteix a Facebook" target="_blank"><img src="img/icones/peu_facebook.png" alt="Facebook" /></a> ';
		html += '<a href="http://www.google.com/bookmarks/mark?op=edit&bkmk=' + url + '" title="Comparteix a Google" target="_blank"><img src="img/icones/google.png" alt="Google" /></a>';
		
		if( $(".llegenda_dreta") )
			$(".llegenda_dreta").append('<span id="comparteix">'+ html +'</span>');
		if( $(".llegenda_dret") )
			$(".llegenda_dret").append('<span id="comparteix">'+ html +'</span>');
		
		$("#comparteix").fadeIn("slow");
	}
}


function cerca_avancada()
{
	$('#cerca_avancada').show();
}

function entre_data()
{
	document.cercador.dia_inici.disabled = !document.cercador.dia_inici.disabled;
	document.cercador.mes_inici.disabled = !document.cercador.mes_inici.disabled;
	document.cercador.any_inici.disabled = !document.cercador.any_inici.disabled;
	document.cercador.dia_fi.disabled = !document.cercador.dia_fi.disabled;
	document.cercador.mes_fi.disabled = !document.cercador.mes_fi.disabled;
	document.cercador.any_fi.disabled = !document.cercador.any_fi.disabled;
}
