
(function($) {
	$.fn.comments = function(options){
		var opts = $.extend({}, $.fn.comments.defaults, options);

		return this.click(function(event){
			t = event.target;
			if ($(t).is('.cEdit')) {
				// Modifica un commento

				// Comment ID
				var cid = $(t).attr("id").substr(2);

				// Estrapolo il testo del commento
				var text = $.trim($('#ct'+cid).text());

				// Apro il box con la textarea per modificare il testo
				$.openBox('<form id="jForm"><fieldset><legend>'+opts.lEditComment+'</legend><textarea name="comment" id="text" rows="10" cols="30" class="text large">'+text+'</textarea></fieldset><button type="submit" name="edit" class="btn" id="jSubmit">'+opts.lUpdate+'</button> <button type="button" name="edit" class="btn" id="jReset">'+opts.lCancel+'</button></form>');

				// Cliccando su Annulla si chiude il box
				$("#jReset").click(function(){$.closeBox();});

				// Inviando la form parte la chiamata ajax per il salvataggio del commento
				$("#jForm").submit(function(){
					commentText = $("#text").val();
					$.loadingBox();
					$.post(opts.url, {"ajax": 1, "action": "edit", "c": cid, "text": commentText, "submit": true},
						function(d){
							if (d.result){
								// Tutto ok! Chiudo e modifico il commento
								$.closeBox();
								$('#ct'+cid).html(jsonToHtml(d.commentText)).fadeIn();
							}else{
								// Errore! Shackero il box e visualizzo il messaggio
								$.shakeBox(3);
								$.editBox('<h3>'+d.msgTitle+'</h3><p>'+d.msgMsg+'</p><p><a class="btn" id="jReset">Annulla</a></p>');
								$("#jReset").click(function(){$.closeBox();});
							}
						}, "json");
					return false;
				});
			} else if ($(t).is('.cDel')) {
				// Cancella un messaggio

				// Comment ID
				var cid = $(t).attr("id").substr(2);

				$.openBox('<form><p>'+opts.lConfirmDelete+'</p><button type="button" class="btn" id="jYes">'+opts.lYes+'</button> <button type="button" name="edit" class="btn" id="jNo">'+opts.lNo+'</button></form>');
				$("#jNo").click(function(){$.closeBox();});
				$("#jYes").click(function(){
					$.loadingBox();
					// Chiamata ajax per cancellare il commento
					$.post(opts.url, { "ajax": 1, "action": "delete", "c": cid, "confirm": 1 },
						function(d){
							if (d.result){
								// Tutto ok! Il commento scompare in fade
								$.closeBox();
								$("#c"+cid).fadeOut(1000, function(){
									$(this).remove();
									if ( $(".comment").length == 0 ){$("#noComments").show();}
								});
							}else{
								// Errore! Box per mostrare il messaggio
								$.shakeBox();
								$.editBox('<h3>'+d.msgTitle+'</h3><p>'+d.msgMsg+'</p><p><a class="btn" id="jReset">Annulla</a></p>');
								$("#jReset").click(function(){$.closeBox();});
							}
						}, "json");
				});
			} else if ($(t).is('.cSpam')) {
				// Segnala come spam

				// Comment ID
				var cid = $(t).attr("id").substr(2);

				$.openBox('<form><p>'+opts.lConfirmSpam+'</p><button type="button" class="btn" id="jYes">'+opts.lYes+'</button> <button type="button" name="edit" class="btn" id="jNo">'+opts.lNo+'</button></form>');
				$("#jNo").click(function(){$.closeBox();});
				$("#jYes").click(function(){
					$.loadingBox();
					// Chiamata ajax per segnalare lo spam
					$.post(opts.url, { "ajax": 1, "action": "spam", "c": cid, "confirm": 1 },
						function(d){
							if (d.result){
								// Tutto ok! Il commento scompare in fade
								$.closeBox();
								$("#c"+cid).fadeOut();
							}else{
								// Errore! Box per mostrare il messaggio
								$.shakeBox();
								$.editBox('<h3>'+d.msgTitle+'</h3><p>'+d.msgMsg+'</p><p><a class="btn" id="jReset">Annulla</a></p>');
								$("#jReset").click(function(){$.closeBox();});
							}
						}, "json");
				});
			}
		});
	}

	$.fn.comments.defaults = {
		opacity: 0.8,
		url: '/comments',
		lEditComment: 'Edit comment',
		lUpdate: 'Update',
		lCancel: 'Cancel',
		lYes: 'Yes',
		lNo: 'No',
		lConfirmDelete: 'Are you sure delete comment?',
		lConfirmSpam: 'Are you sure spam comment?'
	};
})(jQuery);

function jsonToHtml(jsonTxt){
	return jsonTxt.replace(/\\'/,"'").replace(/\\&quot;/,"&quot;");
}

// Gestisco l'invio dei commenti
$(function() {
	$("#cAddForm").submit(function(){
		// Raccolgo i dati dalla form
		var data = {};
		$(this).find("input,textarea,button").each(function(){
			data[$(this).attr("name")] = $(this).val();
		});

		$.loadingBox();

		// Chiamata ajax
		data.ajax = 1;
		$.post($(this).attr("action"), data, function(d){
			if (d.result){
				// Tutto ok! Aggiungo il messaggio in coda agli altri
				$.closeBox();
				$("#noComments").hide();
				$("#comments").append(jsonToHtml(d.htmlCode));
				$("#cAddForm input, #cAddForm textarea").val("");
			}else{
				// Errore! Box per mostrare il messaggio
				$.shakeBox();
				$.editBox('<h3>'+d.msgTitle+'</h3><p>'+d.msgMsg+'</p><p><a class="btn" id="jReset">'+d.lCancel+'</a></p>');
				$("#jReset").click(function(){$.closeBox();});
			}
		}, "json");
		return false;
	});
});
