﻿var path;
var loggedIn = false;

function doSave() {
    var content = FCKeditorAPI.GetInstance('editorboxtextarea').GetHTML();
    $.post("/Editinplace/Admin/editinplace.aspx", { content: content, path: path }, function() {
        $(".editablebox").load(path);
    });
    $("#status").text("Update Successful. (Click out of box to return to site)");

}

function FCKeditor_OnComplete(editorInstance) {
    $(this).load(
        path,
        function(data) {
            editorInstance.EditorDocument.body.innerHTML = data
        }
    );
}

function Login() {
    if ($("#adminlogin").length > 0) {

        $.post("/Editinplace/authenticate.aspx", { username: $("#username").attr("value"), password: $("#password").attr("value") }, function() {
            window.location.reload();
        }
        );
    }
    
}

function Logout() {
    $.post("/Editinplace/logout.aspx", function() {
        window.location.reload();
    });
}

$(document).ready(function() {
    $.get("/Editinplace/Admin/checkauthentication.aspx", function(data) {
        if (data == "True") {
            $("#topbar").append("<div id='title'>Redtap Admin System - You are in admin mode</div> <div id='login'><a href='#' onclick='Logout();'>Logout</a></div>");
            $("#topbar").show();

            if ($(".editablebox").length > 0) {
                var editbox = $(".editablebox");
                editbox.addClass("editableboxactive");
                path = editbox.attr('class').split(' ')[1].replace(/inc\:/, '');

                $("body").append("<div class='jqmWindow' id='ex2'>Loading...<img src='/Editinplace/loadingAnimation.gif' alt='Loading' /></div>");

                var myLoad = function(hash) {

                    var oFCKeditor = new FCKeditor('editorboxtextarea');
                    oFCKeditor.BasePath = "/fckeditor/";
                    
                    oFCKeditor.Height = '450';
                    oFCKeditor.ReplaceTextarea();
                }

                $('#ex2').jqm({
                    ajax: "/Editinplace/editor.html",
                    onLoad: myLoad
                })

                $(".editablebox").click(function() {
                    $("#status").text("");
                });

                $(".editablebox").hover(
              function() {
                  $(this).before("<a href='#' class='ex2trigger'>Click to edit.</a>");
                  $(this).click(function() {
                      $("#ex2").jqmShow();
                  });
              },
              function() {
                  $(document).find(".ex2trigger").remove();
              });
            }
        }
    })
});

