﻿var changed = false;
var updating = false;

$(document).ready(function() {
    $("body").on("keyup", source, function(event) {
        changed = true;
        if (!updating) {
            updating = true;
            $("#loading").removeClass("hidden");
            setTimeout(UpdatePreview, 2000);
        }
    });
});

function UpdatePreview() {
    var content = { "strContent": $(source).val() };

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: ajaxURL,
        data: JSON.stringify(content),
        dataType: "json",
        success: function(msg) {
            if (msg.d != null) {
                $("#content").html(msg.d);
                $("#content a").addClass("override");
            }
        },
        error: function(xhr, textStatus, errorThrown) {
            changed = true;
        },
        complete: function(xhr, textStatus) {
            if (changed) {
                changed = false;
                setTimeout(UpdatePreview, 2000);
            } else {
                updating = false;
                $("#loading").addClass("hidden");
            }
        }
    });
}

