/*
 * Copyright (C) 2014, 2015 NoteDown
 *
 * This file is part of the NoteDown project.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
$(function() {

    var handleResize = function() {
        var windowHeight = window.innerHeight;
        var textareaHeight = windowHeight - 145;
        $('#editor').css('height', textareaHeight);
        $('#preview').css('height', textareaHeight);
        render();
    };

    var render = function() {
        if($('#toggle-preview-pane').is(":checked") && $('#toggle-preview-pane').is(":visible")) {
            $('#preview').html(marked($('#editor').val()));
        }
    };

    var scroll = function() {
        if($('#toggle-preview-pane').is(":checked") && $('#toggle-preview-pane').is(":visible")) {
            $("#preview").scrollTop($("#editor").scrollTop());
        }
    };

    var scrollToEndWhenEditAtEnd = function() {
        if($('#toggle-preview-pane').is(":checked")) {
            var length = $('#editor').val().length;
            var caret = $('#editor').caret();
            if (caret == length) {
                $("#preview").scrollTop($("#editor").prop("scrollHeight"));
            }
        }
    };

    var togglePreviewPane = function() {
        $('#preview').parent().toggleClass('hide');
        $('#editor').parent().toggleClass('col-sm-6 col-sm-12');
        if($('#toggle-preview-pane').is(":checked")) {
            render();
            scroll();
        }
    };

    var saveInProgress = false;
    var save = function() {
        if(!saveInProgress && ($("#title").val().length > 0 || $("#editor").val().length > 0)) {
            saveInProgress = true;
            $("#saved-label").addClass("hide");
            $("#saving-label").removeClass("hide");
            var note = {
                id: $("#id").val(),
                title: $("#title").val(),
                content: $("#editor").val()
            };
            var headers = {};
            headers[$("meta[name='_csrf_header']").attr("content")] = $("meta[name='_csrf']").attr("content");
            $.ajax({
                type: "POST",
                url: "/api/note/",
                data: note,
                headers: headers,
                success: function(data) {
                    $("#id").val(data.id);
                    saveInProgress = false;
                    $("#saved-label #timeago").remove();
                    $("#saved-label").append("<span id=\"timeago\" title=\"" + new Date().toISOString() + "\"></span>");
                    $("#timeago").timeago();
                    $("#saved-label").removeClass("hide");
                    $("#saving-label").addClass("hide");
                }
            });
        }
    };

    var lastAutoSave = undefined;
    var saveNeeded = false;
    var autoSave = function() {
        saveNeeded = true;
        clearTimeout(lastAutoSave);
        lastAutoSave = setTimeout(function() {
            save();
            saveNeeded = false;
        }, 2000);
    };

    $(window).resize(handleResize);

    $('#editor').keyup(function() {
        autoSave();
        render();
        scrollToEndWhenEditAtEnd();
    });

    $("#editor").scroll(scroll);

    $("#toggle-preview-pane").change(togglePreviewPane);

    if($("#timeago").prop("title").length > 0) {
        $("#timeago").timeago();
        $("#saved-label").removeClass("hide");
    }

    $(window).bind("beforeunload", function() {
        if (saveNeeded) {
            return notedown.javascript.exitWarning;
        }
    });
    handleResize();
    render();

    tabOverride.set($("#editor")[0]);
});
@@ -4025,24 +4025,403 @@
   %7D%0A    %7D);%0A
+%0A    $(document).keydown(function(event) %7B%0A            // If Control or Command key is pressed and the S key is pressed%0A            // run save function. 83 is the key code for S.%0A            if((event.ctrlKey %7C%7C event.metaKey) && event.which == 83) %7B%0A                save();%0A                event.preventDefault();%0A                return false;%0A            %7D;%0A        %7D%0A    );%0A%0A
     handleRe

add ctrl/cmd+s key binding to save note