$(document).ready(function() {
	// Fetch the user data.
	$.get('/api/init', function (data) {
		var projects, handlers, handler, h;
		window.data = data;
		projects = data.user.projects;
		// TODO: iterate over the projects and fill the combo box.
		handlers = projects['MyProject'].handlers;
		Object.keys(handlers).forEach(function (h) {
			if (h === 'length') return;
			handler = handlers[h];
			console.log('method=' + handler.method + ',uri=' + handler.uri);
			// Get the DOM node with the Bespin instance inside
			var edit = document.getElementById("editor1");
			// Get the environment variable.
			var env = edit.bespin;
			// Get the editor.
			if (env && env.editor)
				env.editor.value = data.user.projects['MyProject'].handlers['GET /'].code;
		});
	});
});

window.onBespinLoad = function() {
    // Get the DOM node with the Bespin instance inside
    var edit = document.getElementById("editor1");
    // Get the environment variable.
    var env = edit.bespin;
    // Get the editor.
    var editor = env.editor;
    env.settings.set("tabstop", 4);
    editor.syntax = "js";
    editor.focus = true;
	if (window.data)
		editor.value = window.data.user.projects['MyProject'].handlers['GET /'].code;

    function nodifyMsg(msg) {
        var options = {id: 'message_from_top',
            position: 'top',
            size: 20,
            backgroundColor: '#4C4A41',
            //delay: 1500,
            delay: 3500,
            speed: 500,
            fontSize: '12px'
        };

        $.showMessage(msg, options)
    }

    $('#save-btn').click(function() {
		$.ajax({
			url: '/api/init',
			type: 'PUT',
			data: {
				method: 'GET',
				project: 'MyProject',
				code: encodeURIComponent(editor.value),
				uri: '/'
			},
			success: function () {
				nodifyMsg("File saved!");
			},
			dataType: "text",
			error: function(request, status, error) {
				nodifyMsg("Error while saving file: " + error, "error");
			}
		});
    });

    $('#revert-btn').click(function() {
        nodifyMsg("File reverted!");

    });

    $('#deploy-btn').click(function() {
        nodifyMsg("Project deployed!");

    });

}

@@ -1790,19 +1790,31 @@
 sg(%22
-Fil
+The contents wer
 e saved
-!
 %22);%0A
@@ -2010,30 +2010,158 @@
 ) %7B%0A
-        nodifyMsg(%22Fil
+%09%09$.get('/api/init', function (data) %7B%0A%09%09%09editor.value = window.data.user.projects%5B'MyProject'%5D.handlers%5B'GET /'%5D.code;%0A%09%09%09nodifyMsg(%22The contents wer
 e re
@@ -2166,21 +2166,25 @@
 reverted
-!
 %22);%0A
+%09%09%7D);
 %0A    %7D);

Implement Revert fucntionality.