$(document).ready(function(){
	
	$(function() {
		$("#AAinside").accordion({
			autoHeight: false,
			collapsible: true
		});
		
		$("#AAinside > div").accordion({
			autoHeight: false,
			collapsible: true,
		});
	});
	
	$("a.nImage").colorbox();
	
	
	
	
	
	/* JS COMMENTI */
	/***************/
	
	/* display del form standard di inserimento post */
	$.get( 'http://'+ top.location.host + '/includes/comment.trigger.php', { replyto: '0', nId: $('#commenti').attr('class') }, function(data) {
		$('#commenti').after('<br /><br />'+ data);
	});
	
	
	
	/* inserimento nuovo commento */
	$('.comAdd').live('submit', function() {
		var qstring = '';
		qstring += 'autore='+ encodeURIComponent($(this).find('input[name=autore]').val());
		qstring += '&mail='+ encodeURIComponent($(this).find('input[name=mail]').val());
		qstring += '&replyto='+ encodeURIComponent($(this).find('input[name=replyto]').val());
		qstring += '&contenuto='+ encodeURIComponent($(this).find('textarea[name=contenuto]').val());
		qstring += '&nId='+ $('#commenti').attr('class') + '&add=1';
		
		var questo = $(this);
		// inserimento commento
		$.post( 'http://'+ top.location.host + '/includes/comment.trigger.php', qstring, function(data) {
			$.colorbox({
				'html' : data
			});
			//svuotamento form
			$(questo).find('input[type=text]').val('');
			$(questo).find('textarea').val('');
			//aggiornamento lista commenti
			$.get( 'http://'+ top.location.host + '/includes/comment.trigger.php', { PagStrt: '1', nId: $('#commenti').attr('class') }, function(dataret) {
				$('#commenti').empty().append(dataret);
			});
		});
		return false;
	});
	
	
	
	
	/* voto di un commento */
	$('.vote').live('focus', function() {
		$(this).blur();
	});
	$('.vote').live('click', function(e) {
		e.preventDefault();
		var voto;
		if( $(this).hasClass('plus') ) voto = 'plus';
		if( $(this).hasClass('minus') ) voto = 'minus';
		$.post( 'http://'+ top.location.host + '/includes/comment.trigger.php', { vote: voto, vComId: $(this).attr('href'), nId: $('#commenti').attr('class') }, function(data) {
			$.colorbox({
				'html' : data
			});
		});
	});
	
	
	
	/* display form di risposta ad un commento */
	$('.rispondi').live('focus', function() {
		$(this).blur();
	});
	$('.rispondi').live('click', function(e) {
		e.preventDefault();
		if( $(this).parent().parent().next('.rispondiA').size() ){
			$(this).parent().parent().next('.rispondiA').slideToggle('slow');
		} else {
			$('.rispondiA').remove();
			var box = $(this).parents('.boxCom');
			var replyto = $(this).attr('href');
			var reply = $(this).parents('.boxCom').find('.utente').html();
			reply = reply.substring(0, reply.indexOf('<small>'));
			$.get( 'http://'+ top.location.host + '/includes/comment.trigger.php', { replyto: replyto, nId: $('#commenti').attr('class') }, function(data) {
				var op = '<div class="rispondiA"><span>Rispondi a '+ reply +'</span>'+ data +'</div>';
				$(op).hide().insertAfter(box).slideDown('slow');
			});
		}
	});
	
	
	
	
	/* sostituzione intero box tramite i collegamenti di navigazione */
	$('#commenti .navigation a').live('click', function(e) {
		e.preventDefault();
		$.get( 'http://'+ top.location.host + '/includes/comment.trigger.php', { PagStrt: $(this).attr('href'), nId: $('#commenti').attr('class') }, function(data) {
			$('#commenti').empty().append(data);
		});
	});
	
	
	$("#commenti").delegate(".boxCom",'hover', function() {
		$(this).find('.controlli').toggleClass("hover");
	});
	
	
});
