$(document).ready(function(){
	$('.voteButton').click(function(){

		$.getJSON(	"vote.php",
				{ id: $(this).attr("art") },
				function(data){
					$('#totalvotes').empty().append(data.totalvotes);
					$('#txt_totalvotes').empty().append((data.totalvotes==1)? 'vot':'vots');
					alert(data.message);
				}
		);

		return false;
	});


	//Enviar amigo DIALOG
	$('body').append($('#enviaaunamigo_modal')); //avoid ie6 bug
	$('#enviaaunamigo_modal').jqm({modal: true});
	//Enviar amigo FORM
	$('#enviaramigoform').ajaxForm({ beforeSubmit: submit_amigo, success: showResponse_amigo, dataType: 'json', error: raiseError }); 

	//Corregir DIALOG
	$('body').append($('#corregir_modal')); //avoid ie6 bug
	$('#corregir_modal').jqm({modal: true});
	//Corregir FORM
	$('#corregirform').ajaxForm({ beforeSubmit: submit_corregir, success: showResponse_corregir, dataType: 'json', error: raiseErrorCorregir }); 

	//formulari d'opinió
	$('#opinionHiddden').hide();
	$('#opinion1').focus( function() { $('#opinionHiddden').show(); } );
	

});

//
// Funcions de votar article
//
function vote_article(pk) {

	$.getJSON(	"vote.php",
			{ id: pk },
			function(data){
				$('#totalvotes'+pk).empty().append(data.totalvotes);
				$('#txt_totalvotes'+pk).empty().append((data.totalvotes==1)? 'vot':'vots');
				alert(data.message);
			}
	);
}

//
// Funcions del dialog enviar a un amic
//

function showPopup_amigo(id) {
	$('#enviaaunamigo_pk').val(id);
	$('#enviaaunamigo_modal').jqmShow();
}

function submit_amigo(formData, jqForm, options) {
	var f = jqForm[0]; 
	if (f.txt_SourceName.value=='')
	{
		alert("El camp \'El teu nom\' és obligatori");
		f.txt_SourceName.focus();
		return false;
	}
	
	if (f.txt_DestMail.value=='')
	{
		alert("El camp \'Email del teu amic\' és obligatori");
		f.txt_DestMail.focus();
		return false;
	}
	
	if (!isValidEmail(f.txt_DestMail.value))
	{
		alert("Format incorrecte en el camp \'Emails dels teus amics\'");
		f.txt_DestMail.focus();
		return false;
	}

	document.getElementById("btrecomendar").disabled=true;	
}

function showResponse_amigo(data, statusText)  { 
	alert(data.message);
	$('#enviaaunamigo_modal').jqmHide();
	document.getElementById("btrecomendar").disabled=false;
	if (data.status == 1) $('#enviaramigoform').clearForm();
}

function raiseError() {
	alert("Error: no és possible enviar el correu electrònic");
	document.getElementById("btrecomendar").disabled=false;
}


//
// Funcions del dialog corregir
//
function showPopup_corregir(id) {
	$('#corregir_pk').val(id);
	$('#corregir_modal').jqmShow();
}

function submit_corregir(formData, jqForm, options) {
	var f = jqForm[0]; 
	if (f.txt_SourceName.value=='')
	{
		alert("El camp 'El teu nom' és obligatori");
		f.txt_SourceName.focus();
		return false;
	}
	if (f.txt_comment.value=='')
	{
		alert("El camp 'Comentari' és obligatori");
		f.txt_comment.focus();
		return false;
	}
	document.getElementById("btcorregir").disabled=true;
	
}

function showResponse_corregir(data, statusText)  { 
	alert(data.message);
	$('#corregir_modal').jqmHide();
	document.getElementById("btcorregir").disabled=false;
	if (data.status == 1) $('#corregirform').clearForm();
}

function raiseErrorCorregir() {
	alert("Error: no és possible enviar el correu electrònic");
	document.getElementById("btrecomendar").disabled=false;
}

//
// Funcions del modul d'opinions
//

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function ValidatePost(f)
{
	if (f.txt_comentario.value=='')
	{
		alert("El camp 'Comentari' és obligatori");
		f.txt_comentario.focus();
		return false;
	}

	if (f.txt_comentario.value.length > 500)
	{
		alert("El camp 'Comentari' no pot tenir més de 500 caràcters");
		f.txt_comentario.focus();
		return false;
	}

	str = f.txt_Name.value.trim();
	if (str=='')
	{
		alert("El camp 'Nom' és obligatori");
		f.txt_Name.focus();
		return false;
	}

	if (f.txt_security_code.value.length != 6)
	{
		alert("El camp del captcha és obligatori i ha de contenir 6 caràcters");
		f.txt_security_code.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){
document.write('<a href="mailto:'+u+'@'+d+'">'+n+'</a>');
}