$(document).ready(function(){

	$('.voteButton').click(function(){
		$.getJSON(	"/article/vote.php",
				{ id: $(this).attr("art") },
				function(data){ updateCounter(data); }
		);
		return false;
	});
	
	$('.voteButton2').click(function(){
		$.getJSON(	"/encuesta/vote.php",
				{ id: $(this).attr("art") },
				function(data){ updateCounter(data); }
		);
		return false;
	});

/*
	//formulari d'opinió
	$('#opinionHiddden').hide();
	$('#opinion1').focus( function() { $('#opinionHiddden').show(); } );
*/

});

//
// Funcions de votar article
//
function updateCounter(data) {
	$('#totalvotes').empty().append(data.totalvotes);
	$('#totalvotes2').empty().append(data.totalvotes);
	$('#txt_totalvotes').empty().append((data.totalvotes==1)? 'voto':'votos');
	$('#txt_totalvotes2').empty().append((data.totalvotes==1)? 'voto':'votos');
	alert(data.message);
}


//
// Funcions del modul d'opinions
//

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function ValidatePost(f)
{
	if (f.txt_comentario.value=='')
	{
		alert("El campo 'Comentario' es obligatorio");
		f.txt_comentario.focus();
		return false;
	}

	if (f.txt_comentario.value.length > 500)
	{
		alert("El campo 'Comentario' no puede tener más de 500 carácteres");
		f.txt_comentario.focus();
		return false;
	}

	str = f.txt_Name.value.trim();
	if (str=='')
	{
		alert("El campo 'Nombre' es obligatorio");
		f.txt_Name.focus();
		return false;
	}

	str = f.txt_Email.value.trim();
	if (str=='')
	{
		alert("El campo 'Email' es obligatorio");
		f.txt_Email.focus();
		return false;
	}
	if (!isValidEmail(str)) {
		alert("El campo 'Email' no es correcto");
		f.txt_Email.focus();
		return false;
	}

	if (f.txt_security_code.value.length != 6)
	{
		alert("El campo del captcha es obligatorio y debe contener 6 carácteres");
		f.txt_security_code.focus();
		return false;
	}

	if (!f.newPostPrivacy.checked) {
		alert("Debe aceptar las cláusulas de privacidad para poder opinar");
		f.newPostPrivacy.focus();
		return false;
	}
	return true;
}

var newOpiwindow = '';
function viewOpinions(id,tId) {
    	var url = "/opinions/list.php?id="+id+"&tId="+tId;
        if (!newOpiwindow.closed && newOpiwindow.location) {
        	newOpiwindow.location.href = url;
        } else {
    	    var w = '800';
        	var h = '600';
    	    LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
        	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
        	settings ='height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',toolbar=0,location=0,status=0,menubar=0,scrollbars=1,toolbar=0,resizable=0';
        	newOpiwindow = window.open(url,"Opinions",settings);
        }
        if (window.focus) newOpiwindow.focus();
}

//
// Altres funcions
//
function mailAntiSpam(u,d,n,t){
document.write('<a title="'+t+'" href="mailto:'+u+'@'+d+'">'+n+'</a>');
}
