$(function(){
shortcut.add("Meta+E",function() {
	authenticate(edit)
});
shortcut.add("Control+E",function() {
	authenticate(edit)
});
});


function edit() {

	$(".editable").each(function (el) {
		var content = this;
		var editable = $("<form action=\"/ventana/update.php\" method=\"POST\"></form>")
		.html($("<textarea name=\"content\"/>").val($(this).html()))
		.prepend($("<input type=\"hidden\" name=\"id\" value=\""+$(this).attr("id")+"\" />"))
			;
			$(content).before($("<br />")).before(editable);

			$(editable).find('textarea').tinymce({
				theme: "advanced",
		theme_advanced_toolbar_location: "top",
		plugins : "save",
		theme_advanced_buttons1: "save,|,bold,italic,underline,|,fontselect,fontsizeselect,forecolor,backcolor,|,bullist,numlist,|,outdent,indent,|,undo,redo,|,hr",
		theme_advanced_buttons2: "",
		theme_advanced_buttons3: "",

	width:$(content).width(),
	height:$(content).height(),
		
			oninit:function(){
					$(content).hide();
					$(editable).show();
			}
			}
			);
	
	});			
}

function authenticate(success) {
	var dialog= $('<div>').dialog({
		open: function () {
			$(this).load('/ventana/login.html', function () {
				$(this).find("[type=submit]").click(

				function () {
					$.ajax({
						url: "/ventana/authenticate.php",
						type: "GET",
						data:{
						username:$("#ventana-username").val(),
						password:$("#ventana-password").val()
						},
						success: function(data){
												 if(data=="1"){
												  $(dialog).dialog('destroy');
												  success();
												}else{
											                         alert("Login Failed"); 
											                         } 
					}
					})

					return false;
				});
			});
		},
		height: $.browser.msie ? 289 : 260,
		width: 400,
		resizable: false,
		title: 'Login'
	});

}

