$(document).ready(function() {
	var ACTION_URL = "/archivio/insert.action";
	
	if ($("#numCommenti").val() != undefined) {
		$(".nCommenti").html($("#numCommenti").val() + " commenti");
	} else {
		$(".nCommenti").html("0 commenti");
	}
	
	$("div.send_commenti").each( function(){
		var that = $(this); // Il div contenitore.
		that.hide();
		var form = $("form.dlt-form", this);
		//form.hide();
		form.clearForm = function (form) {
			// iterate over all of the inputs for the form
			// element that was passed in
			$(':input', form).each(function() {
				var type = this.type;
				var tag = this.tagName.toLowerCase(); // normalize case
				// it's ok to reset the value attr of text inputs,
				// password inputs, and textareas
				if (type == 'text' || type == 'password' || tag == 'textarea')
					this.value = "";
				// checkboxes and radios need to have their checked state cleared
				// but should *not* have their 'value' changed
				else if (type == 'checkbox' || type == 'radio')
					this.checked = false;
				// select elements need to have their 'selectedIndex' property set to -1
				// (this works for both single and multiple select elements)
				else if (tag == 'select')
					this.selectedIndex = -1;
			});
		};

		form.submit(function(){
			var currentLocation=""+window.location;
			currentLocation=currentLocation.substring("http://max.rcs.it/".length);
			var currentSezione=currentLocation.split("/")[0];
			var currentSottoSezione=currentLocation.split("/")[1];
			var url = ACTION_URL + "?commento.nickname=" + $("input.dltNickname", this).val();
				url+="&commento.testo=" + escape($("textarea.dltTesto", this).val());
				url+="&commento.email=" + $("input.dltEmail", this).val();
				url+="&commento.idDocumento=" + $("input.dltIdDoc", this).val();
				url+="&commento.contesto=" + $("input.dltContesto", this).val();
				url+="&commento.titoloDoc=" + $("input.dltTitoloDoc", this).val();
				url+="&maxIndex=" + $("input.dltMaxIndex", this).val();
				url+="&commento.sezione="+currentSezione;
				url+="&commento.sottosezione="+currentSottoSezione;
				url+="&commento.path=/"+currentLocation;

			var outputSelector = "#dltContainer_" + $("input.dltContesto", this).val() + "_" + $("input.dltIdDoc", this).val();

			if ($(".list_commenti")) {
				var height = $(".list_commenti").css("height");
				$(".list_commenti").html("<li style='height:" + height + ";margin:20px;text-align:center'><table style='width:100%;height:100%'><tr><td style='text-align:center;vertical-align:middle'><img src='/libs/css/default_theme/assets/ajax-loader3.gif'/></td></tr></table></li>");
			}
			$.ajax({
				type: "GET",
				url: url,
				error: function(data){
					$(outputSelector).html(data);
					that.hide();
					$(".nCommenti").html("0 commenti");
				},
				success: function(data){
					$(outputSelector).html(data);
					that.hide();
					form.clearForm(form);
					$(".nCommenti").html($("#numCommenti").val() + " commenti");
					$('#dltVediTutti').click(function(){
						viewAll($(this).attr("href"));
						return false;
					});
				}
			});
			//this.action = url;
			return false;
		});

		// Bindo il click del bottone invia al form.
		$('#dltInviaForm').click( function(){
			form.submit();
			return false;
		});
		
	});

	$("a.scriviCommento").each( function(){
		$(this).click(function(){
			var formSelector = $(this).attr("href");
			$(formSelector).show();
			return false;
		});
	});
	
	$('#dltVediTutti').click(function(){
		viewAll($(this).attr("href"));
		return false;
	});


	var maxChars=4000;
	$("#center textarea.dltTesto").each(function(){
		$(this).keydown(function(){
			var testoCommento="" + $("textarea.dltTesto").val();
			var testoLength=testoCommento.length+1;
			if(testoLength<=maxChars){
				$("#center span.dltCharCounter").html(maxChars-testoLength);
			} else {
				$("#center textarea.dltTesto").val(testoCommento.substring(0,maxChars));
				$("#center span.dltCharCounter").html("0");
			}
		});
	});
	
	function viewAll(href) {
		url =  "/archivio/" + href;
		var outputSelector = "#dltContainer_" + $("input.dltContesto").val() + "_" + $("input.dltIdDoc").val();
		
		if ($(".list_commenti")) {
			var height = $(".list_commenti").css("height");
			$(".list_commenti").html("<li style='height:" + height + ";margin:20px;text-align:center'><table style='width:100%;height:100%'><tr><td style='text-align:center;vertical-align:middle'><img src='/libs/css/default_theme/assets/ajax-loader3.gif'/></td></tr></table></li>");
		}	
			
		$.ajax({
			type: "GET",
			url: url,
			error: function(data){
				$(outputSelector).html(data);
				$(".nCommenti").html("0 commenti");
			},
			success: function(data){
				$(outputSelector).html(data);
				$(".nCommenti").html($("#numCommenti").val() + " commenti");
			}
		});	

	}
	
});

