﻿// JScript File

$(function(){

	/*if(settings == undefined){
		var settings = {}
	}*/
	
	//settaggi
	var settings = {
		approve_html : 'approva il commento',
		unapprove_html : 'disapprova il commento',
		loader_html : '<img src="/immagini/loader.gif" />'
	};
	
	$(".moderacommento").html(settings.loader_html)
	$(".moderacommento[href='#approva']").html(settings.approve_html)
	$(".moderacommento[href='#disapprova']").html(settings.unapprove_html)
	

	$(".moderacommento").click(function(){
		var $this = $(this)	
		//id elemento in formato mod[]id_commento, ricavo l'id del commento
		var commento = this.id.slice(3)
		//se c'è il loader, lo mostro
		if(settings.loader_html.length > 0){ $this.html(settings.loader_html) }
		//salvo lo stato attuale del commento
		var status
		if($this.attr("href") == "#approva"){
			//se il link mi chiede di approvare, allora non lo è ;)
			status = 0
		}else{
			//... e viceversa
			status = 1
		}
		//per definizione new_status è l'opposto di status
		var new_status = (status == 0) ? 1 : 0				
		$.post(
			"/handlers/moderacommento.ashx",
			{id: commento, a: new_status},
			function(data){
				//in italiano: SE ha funzionato, considero il commento nel nuovo stato ALTRIMENTI considero lo stato originale
				var display_mode = (data > 0) ? new_status : status 
				if(display_mode==1){
					//è approvato, devo mostrare la disapprovazione
					$this.attr("href", "#disapprova").html(settings.unapprove_html)
				} else {
					//...e viceversa
					$this.attr("href", "#approva").html(settings.approve_html)
				}
			}
		
		);
		
		return false
		
	
	});

});