﻿/*****************************************/
/* Master Autosave Methods and Variables */
/*****************************************/
// Editor reference
var ASEditorLink = null;
// Hidden divs holding visual cues
var ASWarningID = null;
var ASAutosaveIndicatorID = null;
// User acknowledgement member
var ASAck = false;
// Autosave window reference - set by autosave window (iframe)
var ASAutosaveWindow = null; 
// Action functions
var ASTimeoutFcn = null;
// Function to return editor content
function GetEditorContent() {
    return ASEditorLink.get_html(true);
}
// Function to retain the reference to the calling editor
function StoreEditorReference(sender, args) {
    // Store editor reference
    ASEditorLink = sender;
}
// Function to show an active autosave being handled
function ShowAutosaveIndicator(toshow) {
    if (document.getElementById(ASAutosaveIndicatorID))
        document.getElementById(ASAutosaveIndicatorID).className = ((toshow) ? "" : "hidden");
}
// Function to show idle warning to editor
function ShowWarning(toshow) {
    if (document.getElementById(ASWarningID))
        document.getElementById(ASWarningID).className = ((toshow) ? "" : "hidden");
}
// Function for editor to acknowledge idle warning
function EditorAcknowledge() {
    // Hide the warning
    ShowWarning(false);
    ASAck = true;
}
// Function to kill the remote autosave window
function KillRemoteAutosave() {
    if (ASAutosaveWindow)
        ASAutosaveWindow.KillAutoSave();
}
// Function the force the editor to close
function ForceEditorClose() {
    // Hide original warning (if still visible)
    ShowWarning(false);
    // Trigger the save working content button
    ASTimeoutFcn();
}

/*****************************************/
/* Slave Autosave Methods and Variables  */
/*****************************************/
// Autosave interval reference, function, and timespan
var ASAutoSaveTime = 60000;  // 1 minute - This can be set to something different at runtime
var ASAutoSaveInterval = null;
var ASAutoSaveFcn = null;
// Working storage area reference
var ASWorkingStorageID = null;
// Working editor acknowledgement reference
var ASWorkingEditorAckID = null;
// Function to stop the autosave interval
function KillAutoSave() { if (ASAutoSaveInterval) window.clearInterval(ASAutoSaveInterval); }
// Function to start the autosave interval
function StartAutoSave() {
    KillAutoSave();
    window.parent.ASAutosaveWindow = window;
    ASAutoSaveInterval = window.setInterval(ASAutoSave, ASAutoSaveTime);
}
// Autosave Function
function ASAutoSave() {
    if (window.parent.ASEditorLink != null) {
        // Show the indicator
        IndicateActiveAutosave(true)
        // Get current working content
        document.getElementById(ASWorkingStorageID).value = window.parent.GetEditorContent()
        // Get the current acknowledgement state
        document.getElementById(ASWorkingEditorAckID).value = ((window.parent.ASAck == false) ? "0" : "1")// window.parent.GetEditorContent()
        // Call the autosave function
        ASAutoSaveFcn();
    }
    else {
        KillAutoSave();
        alert('Autosave functionality does not work in this window configuration.');
    }
}
// Editor Warning Function
function WarnEditor() {
    window.parent.ASAck = false;
    window.parent.ShowWarning(true);
    window.parent.focus();
}
function IndicateActiveAutosave(toshow) {
    window.parent.ShowAutosaveIndicator(toshow);
}
// Reset Editor Acknowledgement Function
function ResetAcknowledgement() {
    window.parent.ASAck = false;
    window.parent.ShowWarning(false);
}
// Close Content Editor Function
function CloseContentEditor() {
    KillAutoSave();
    window.parent.ForceEditorClose();
}