/**
 *  MyPost
 *  Crea il   
 */
var MyPost = Class.create({
 
	mailToName:'mailToName',       //nome destinatario 
	mailToEmail:'mailToEmail',             //email destinatario
	mailFromName: 'mailFromName',         //nome mittete
	mailFromEmail: 'mailFromEmail',       //nome mittente
	mailText: 'mailText',                       //testo messaggio

	postText:'postText',               //campo testo del post
	postSenderName:'postSenderName',  //campo nome del mittente
	postType:'postType',                    //campo che indica pubblico o privato
	 
	
    responseBox: 'messaggi',        //div dove mostra il messaggio di errore
    elementType:undefined,         //tipo di elemento commentato
    elementId:undefined,          //id elemento commentato
    userType:undefined,          //indica se è un utente loggato o no
    requestType:undefined,
    requestUrl: undefined,
    
	//initialize: function() {	},

	//inposta i dati di base del form
	setInfo: function(requestType,elementType,elementId,user){
		this.requestType=requestType;
		this.elementType= elementType;
		this.elementId = elementId;
		this.user=user;
		//imposta i parametri in base al tipo di richiesta
		if(requestType=='post'){
          this.requestUrl='/commenti/get-form/';
          this.box='postBox';
          this.boxOk='postBoxOk';
	    }
	    if(requestType=='mail'){
          this.requestUrl='/commenti/get-mailform/';
          this.box='mailBox';
          this.boxOk='mailBoxOk';
	    }
		
	},

	//mostra le info settate
	getInfo: function(){
		alert(this.requestType+" - "+this.elementType + " - " + this.elementId + " - " +this.user);
		alert(this.requestUrl+" - "+this.box + " - " + this.boxOk);
	},

	//aggiunge il form alla pagina
	getForm: function(){
		new Ajax.Updater(this.box,this.requestUrl,{
			onComplete: function(){
				if(Post.requestType=='post'){
					$('postSubmit').observe('click', (function() { Post.submitForm(); }).bind(this));
				}
				if(Post.requestType=='mail'){
					$('mailSubmit').observe('click', (function() { Post.submitMailForm(); }).bind(this));
				}
			}
		});
	},
	//invia il form alla pagina di inserimento
	submitForm: function(){
		if($(this.postType).checked == true){
			var pubblico=1;
		}else {
			var pubblico=0;
		}
		
		var url="/commenti/salva-commento";        
		new Ajax.Request(url, {
			method:'post',
			parameters: { 
			    mittente_commento:     $(this.postSenderName).value,
			    commenti_pubblico:     pubblico,
			    testo_commento:        $(this.postText).value,
			    tipo_elemento_sigla:   this.elementType,
			    elemento_id:           this.elementId
			},
			//prende la risposta della chiamata ajax
			onSuccess: function(transport){
				var json = transport.responseText.evalJSON()
				Post.formResponse(json);
			}
		});
	},
	submitMailForm: function(){
		var url="/commenti/invia-messaggio";        
		new Ajax.Request(url, {
			method:'post',
			parameters: { 
			    mittente_nome:        $(this.mailFromName).value,
			    mittente_email:       $(this.mailFromEmail).value,
			    destinatario_nome:    $(this.mailToName).value,
			    destinatario_email:   $(this.mailToEmail).value,           
                testo:                $(this.mailText).value,
                tipo_elemento_sigla:  this.elementType,
			    elemento_id:          this.elementId
			},
			//prende la risposta della chiamata ajax
			onSuccess: function(transport){
				var json = transport.responseText.evalJSON()
				Post.formResponse(json);
				//mostra l'icona con il numero di volte che è stato segnalato
				if(json.numero_segnalazioni!="" && json.numero_segnalazioni!=0){
					$('segnalato_leggi').show();
					$('segnalato_leggi').innerHTML=json.numero_segnalazioni
				
				}
			}
		});
	},
	//interpreta la risposta della chiamata ajax
    formResponse: function(json){
    	if(json.success==1){
					$(this.box).hide();
				}
		var messaggi='';
		json.messaggi.each(function(messaggio) {
			messaggi=messaggi+messaggio
		})
     
		$(this.responseBox).show();
		$(this.responseBox).innerHTML=messaggi;
    },
    
	//mostra il box contenitore del form
	showBox: function(){
		$(this.box).show();
	},
    
	//toggle del box che contiene il form
	toggleBox: function(){
		$(this.box).toggle();
		$(this.responseBox).hide();
	},
	
	clearInput: function(){
		$(this.postText).value='';
		$(this.postSenderName).value='';
	},
	
	hideAll:function(){
		var elm=$('actionBox').getElementsByClassName('actionB');
		for (var i=0;i<elm.length;i++){
			if(elm[i]['id']!=this.box){ //nasconde tutti i box diversi da quello cliccato
			    $(elm[i]['id']).hide();
			}
		}
	}
});
//inizializa
var Post = new MyPost();  
function togglePost(requestType,elementType,elementId,user){
	//inizializza le informazioni dell'elemento e il tipo di richiesta
	Post.setInfo(requestType,elementType,elementId,user); 
	//se non è ancora caricato carica il form, altrimenti lo mostra e basta
	if($(Post.boxOk)){
	   Post.hideAll();
	   Post.toggleBox();
	}else{
       Post.hideAll();
       Post.getForm();
       Post.showBox();
	}	
}
