$(document).ready(function(){

/* Nascondi i messaggi d'errore e di successo */
if($('.failure').length != 0) { $('.failure').hide(); }
if($('.success').length != 0) { $('.success').hide(); }

/* Invia la chiave di ricerca */
$('#search_push').click(function() { doSearch(); });
$('#search_key').keyup(function(e) { if(e.keyCode == 13) doSearch(); });

// Riduci tutti i video
$('.text object').each(function() {
	var w = $(this).attr('width');
	var h = $(this).attr('height');
	
	// Riduci il video in proporzione
	if(w > 500) {
		h = h * 500 / w; w = 500;	
		$(this).attr('width', w);
		$(this).attr('height', h);
		$(this).children('embed').attr('width', w);
		$(this).children('embed').attr('height', h);
	}
});

// Apri l'ordine del giorno se richiesto
$('.video_odg').hide();
$('.video_infos_odg').toggle(
	function() {
		var video_a = $(this).attr('id').split('_');
		var video_id = video_a[1];
		$('#video_odg_'+video_id).fadeIn();
	},
	function() {
		var video_a = $(this).attr('id').split('_');
		var video_id = video_a[1];
		$('#video_odg_'+video_id).fadeOut();
	}
);

// Apri il player Vimeo
$('.video_player').hide();
$('.video_play').toggle(
	function() {
		var video_a = $(this).attr('id').split('_');
		var video_id = video_a[2];
		$('#video_player_'+video_id).fadeIn();
	},
	function() {
		var video_a = $(this).attr('id').split('_');
		var video_id = video_a[2];
		$('#video_player_'+video_id).fadeOut();
	}
);

// Nascondi tutti i tab tranne lo 0
$('.tabbing').each(function(i) {
	if(i != 0)	$('#tab_'+i).hide();
});

// Esegui lo switching tra i tab aperti
$('.label').click(function() {
	var this_label_a = $(this).attr('id').split('_');
	var this_label_id = this_label_a[1];
	$('.label').each(function(i) {
		if(i == this_label_id) {
			$('#label_'+this_label_id).removeClass('label_disabled');
			$('#label_'+this_label_id).addClass('label_enabled');
			$('#tab_'+this_label_id).show();
		} else {
			$('#label_'+i).removeClass('label_enabled');
			$('#label_'+i).addClass('label_disabled');
			$('#tab_'+i).hide();
		}
	});
});

// Invia il form dei contatti
$('#contact_div #Invia').click(function() {
	
	/* Nascondi i messaggi d'errore e di successo */
	$('.failure').hide();
	$('.success').hide();
	
	/* Recupera i campi dal form */
	var form_data = $('#contatti').serialize();
	
	/* Invia i campi del form, via POST, allo script di destinazione */
	$.ajax({
		type: "POST",
		url: "/ajax/contact.php",
		data: form_data,
		dataType: "json",
		success: function(msg){
			
			/* Se esiste la variabile del successo, esegui la funzione di successo */
			if(msg.success != '') {
				$('.success').html(msg.success);
				$('.success').show(100);
				/* Resetta il form */
				$("#contatti")[0].reset()
			} else {
			/* Altrimenti esegui la funzione di errore */
				$('.failure').html(msg.failure);
				$('.failure').show(100);
			}
		}
	});
	
	return false;
});

/* Invia il form dei contatti */
$('#post_comment_form #send_comment').click(function() {

	$('#post_comment_form #send_comment').text('Salvataggio...');
	$('#post_comment_form #send_comment').attr('disabled', 'true');
	
	/* Nascondi i messaggi d'errore e di successo */
	$('.failure').hide();
	$('.success').hide();
	
	var form_data = $('#post_comment_form').serialize();
	
	/* Invia i campi del form, via POST, allo script di destinazione */
	$.ajax({
		type: "POST",
		url: "/ajax/user_post_comments.php",
		data: form_data,
		dataType: "json",
		success: function(msg){
			
			/* Se esiste la variabile del successo, esegui la funzione di successo */
			if(msg.success != '') {
				$('.success').html(msg.success);
				$('.success').show(100);
				$("#post_comment_form")[0].reset();
				$('#comment_count').text(msg.data.comment_count);
				$('#comment_count_phrase').text(msg.data.comment_count != 1 ? 'commenti' : 'commento');
				var new_comment = $('#comment_hidden').html()
					.replace('%1', msg.data.post_username)
					.replace('%2', msg.data.comment_date)
					.replace('%3', msg.data.post_comment);
				$('#comment_hidden').before(new_comment);
			} else {
			/* Altrimenti esegui la funzione di errore */
				$('.failure').html(msg.failure);
				$('.failure').show(100);
			}
			
			$('#post_comment_form #send_comment').removeAttr('disabled');
			$('#post_comment_form #send_comment').text('Commenta');
		}
	});
	
	return false;
});

/* Diminuisci il numero di caratteri disponibili per il messaggio */
$('#Messaggio').keyup(function() {
	var maxlength = 1000;
	var mes_len = $('#Messaggio').val().length;
	var len = maxlength - mes_len;
	if(len < 0) {
		var subs = $('#Messaggio').val().substr(0, maxlength);
		$('#Messaggio').val(subs);
		len = 0;
	}
	$('#Disponibili').val(len);
});

})

/* Funzione che invia la chiave di ricerca */
function doSearch() {
	var search_key = $('#search_key').val();
	if(search_key == '') {
		alert('Inserisci una chiave di ricerca valida.');
	} else {
		top.location = '/posts.php/k/'+ search_key.replace(' ', '_');
	}
}


