﻿$.editable.addInputType('notes', {
	element : function(settings, original) {
		var textarea = $('<textarea>').css({"border": "2px solid yellow"});
		if (settings.rows) {
			textarea.attr('rows', settings.rows);
		} else {
			textarea.height(settings.height);
		}
		if (settings.cols) {
			textarea.attr('cols', settings.cols);
		} else {
			textarea.width(settings.width);
		}
		$(this).append(textarea);
		return(textarea);
	},
	content : function(string, settings, original) { 
		$('textarea', this).text(string.replace(/(?:<br>|<BR>)/g, '\r'));
	},
	plugin : function(settings, original) {
		if (!$.browser.msie) {
			$('textarea', this).autogrow(settings.autogrow);
		} else {
			return false
		}
	}
});


var sn = function() {
	return {
		notify: function(calledby, handler, action, param1, param2, param3, param4, param5 ) {
			$.post(
				snmanager.handlers[handler],
				{ a: action, p1: param1, p2: param2, p3: param3, p4: param4, p5: param5},
				function(data) {
					if (parseInt(data)==0) {
						alert("Errore nell'elaborazione della richiesta");
					} else {
						//alert(data);
						$("#"+calledby).html(data)
					}
				}
			);
		},
		
		getparams: function(p) {
			var params = new Array("", "", "", "", "", "");
			var actiondata = p.split("__");
			for (i=0; i<actiondata.length; i++) {
				params[i] = actiondata[i]
			}
			return params;
		}, 
		
		box: function(el) {
			//var element = el.split("__");
			var element = $(el).attr("id").split("__");
			$.get(
				"/handler/box.ashx",
				{n: element[0], id: element[1]},
				function(data) {
					$(el).html(data)
				}
			)
			/*
			$("#" + el + " .snbox").each(function(i) {
				sn.box($(this).attr("id"))
			});
			*/
		},
		
		get_note: function(user_from, user_to) {
			$.get(
				"/handler/notes.ashx",
				{u: user_from, t: 1, o: user_to},
				function(data) {
					return data.replace(/(?:\n\r|\n|\r)/g, '<br />');
				},
				"text"
			)
		},
		
		edit_note: function() {
			if ($("#note_text").editable != undefined) {
				var params = sn.getparams($("#note .editbox").attr("id"))
				$("#note_text").editable(
				
					function(value, settings) {
						$.post(
							"/handler/notes.ashx", 
							{ a: "update", u: params[1], t: 1, o: params[2], n: value},
							function(data) {
								if (parseInt(data)==0) {
									alert("Si è verificato un errore nell'elaborazione...")
								}
							}
						);
						
						if(window.console) {
							console.log(value);
						}
						return value.replace(/(?:\n\r|\n|\r)/g, '<br>');
					},
					{
						indicator : "...",
						type      : 'notes',
						name 	  : 'notetext',
						tooltip   : 'Clicca qui per modificare...',
						width	  : "278px",
						/*height	  : "280px",*/
						cssclass  : "editable",
						onblur    : 'submit'
						/*,
						callback  : function(value, settings) {
							$(this).html(value.replace(/(?:\n\r|\n|\r)/g, '<br>'));
						}*/
					}
				);
			}
		},

		handle_events: function() {
			$("button.accetta").click(function() {
				var params = sn.getparams($(this).attr("param"));
				var calledby = $(this).parent().parent().get(0).id;
				sn.notify(calledby, params[0], 1, params[1], params[2], params[3], params[4], params[5]);

			});
	
			$("button.rifiuta").click(function(){
				var params = sn.getparams($(this).attr("param"));
				var calledby = $(this).parent().parent().get(0).id;
				sn.notify(calledby, actiondata[0], 0, params[1], params[2], params[3], params[4], params[5]);
			});
	
			$("button.blocca").click(function(){
				var params = sn.getparams($(this).attr("param"));
				var calledby = $(this).parent().parent().get(0).id;
				sn.notify(calledby, actiondata[0], -1, params[1], params[2], params[3], params[4], params[5]);
			});
			
			$(".snbox").each(function(i) {
				sn.box(this)
			});
			
					
			$("#note .editbox").each(function(i) {
				title = $(this).attr("title")
				id = $(this).attr("id")
				var element = id.split("__");
				if (parseInt(element[1]) > 0) {
					$(".#note").show();
					$("#" + id).append("<p class='title'>" + title + "</p>");
					var data = new Date()
					$.get(
						"/handler/notes.ashx?r=" + data.getMilliseconds(),
						{u: element[1], t: 1, o: element[2]},
						function(data) {
							$("#" + id).append("<div id='note_text'>" + data.replace(/(?:\n\r|\n|\r)/g, '<br>') + "</div>")
							sn.edit_note();
						}
					)
				} else {
					$(".#note").hide();
				}
			});
			
			if ($("#userstatus").editable != undefined) {
				$("#userstatus").editable(
					function(value, settings) { 
						$.post(
							"/handler/status.ashx", 
							{ a: 'update', u: userid, s: value },
							function(data) {
								if (parseInt(data)==0) {
									alert("Si è verificato un errore nell'elaborazione...")
								}
							}
						);
						if(window.console) {console.log(value);}
						$("#userstatus_date").html("Aggiornato pochi istanti fa")
						return(value);
	
					},
					{
						indicator : "...",
						tooltip   : 'Clicca per modificare il stuo stato...',
						cancel    : 'Cancella',
						submit    : 'OK',
						style	  : "width:400px"
					}
				);
			}
		}
	}
}();


$(function() {
	sn.handle_events();
});
