$(function(){
	if (SERVER_DATA['CMSMODE']) {
		$ce = $(".cms_editable");
		if ( $ce.length > 0) {
			$ce.editableText();
			// alert("applied editing capability!");
			
			$ce.change(function(){   
				var $t = $(this);
				 var newValue = $(this).html();
	
				var cd = $(this).attr("id");
				if (cd.length == 0) {
					alert("No CMS descriptor found!");
					return false;
				}
				cd = cd.split('.');
				var table = cd[0], field = cd[1], id = cd[2];
				var signature = "12312"; // todo : implement for security reasons
				
				 $.ajax({
					type: "POST",
					url: SERVER_DATA.__URLPATH__ + "/admin/inlineedit.php",
					data:   "signature=" + signature
							+ "&action=update"
							+ "&table=" + table 
							+ "&field=" + field
							+ "&id=" + id 
							+ "&value=" + newValue
					,
					success: function(response){
						if (response.error) {
							alert(response.message);
						} else if (response.success) {
							$t.css({opacity:0.1}).fadeTo(1500,1);
							return true;
						} else {
							alert("Unexpected error")
						}
						return false;
					},
					dataType:'json'
				 });
			 });
			
		}
	}
})
