
var objAutoSaveFSO = null;
try {
    objAutoSaveFSO = objApp.CreateActiveXObject("Scripting.FileSystemObject");
}
catch (err) {
}



function autosaveGetPath() {
    var pathAutoSave = objApp.DataStore + "AutoSave";

    if (!objAutoSaveFSO)
        return "";
    //
    if (!objAutoSaveFSO.FolderExists(pathAutoSave)) {
        objAutoSaveFSO.CreateFolder(pathAutoSave);
    }
    //
    pathAutoSave += "\\";
    //
    return pathAutoSave;
}

function autosaveGetWizDocumentFileName(objWizDocument) {
    if (!objAutoSaveFSO)
        return null;
    //
    var pathAutoSave = autosaveGetPath();
    var fileAutoSave = pathAutoSave + "autosave_" + objWizDocument.GUID + ".htm";
    //
    return fileAutoSave;
}

function autosaveOnTimer() {
    if (!objAutoSaveFSO)
        return;
    //
        
    var objDoc = objWindow.CurrentDocument;
    if (objDoc == null) {
        return;
    }
    //
    var objHtmlDocument = objWindow.CurrentDocumentHtmlDocument;
    if (objHtmlDocument == null) {
        return;
    }

    if (objHtmlDocument.designMode != "On") {
        return;
    }
    //
    var objElem = objHtmlDocument.documentElement;
    if (objElem == null) {
        return;
    }
    //
    var htmltext = objElem.outerHTML;
    //
    var fileAutoSave = autosaveGetWizDocumentFileName(objDoc);
    if (!fileAutoSave)
        return;
    //
    htmltext = "<base href=\"" + objHtmlDocument.URL + "\" />" + htmltext;
    //
    try {
        objCommon.SaveTextToFile(fileAutoSave, htmltext, "unicode");
    }
    catch (err) {
    }
        
}

function autosaveOnClose() {
    if (!objAutoSaveFSO)
        return;
    //
        
    var pathAutoSave = autosaveGetPath();
    var objFiles = objCommon.EnumFiles(pathAutoSave, "*.htm", false);
    var arrFiles = new VBArray(objFiles);
    var files = arrFiles.toArray();
    //
    for (var i = 0; i < files.length; i++) {
        objAutoSaveFSO.DeleteFile(files[i]);
    }
}


function autosaveCheck() {
    if (!objAutoSaveFSO)
        return;
    //  
    var pathAutoSave = autosaveGetPath();
    var objFiles = objCommon.EnumFiles(pathAutoSave, "*.htm", false);
    var arrFiles = new VBArray(objFiles);
    var files = arrFiles.toArray();
    //
    if (files.length == 0)
        return;
    //
    var ret = [];
    //
    for (var i = 0; i < files.length; i++) {
        var filename = files[i];
        //
        var baseName = objAutoSaveFSO.GetBaseName(filename);
        //
        var parts = baseName.split("_");
        if (parts.length != 2) {
            objAutoSaveFSO.DeleteFile(filename);
            continue;
        }
        //
        var str_autosave = parts[0];
        if (str_autosave.toLowerCase() != "autosave") {
            objAutoSaveFSO.DeleteFile(filename);
            continue;
        }
        var guid = parts[1];
        if (guid == null || guid == "") {
            objAutoSaveFSO.DeleteFile(filename);
            continue;
        }
        //
        var objDoc = null;
        try {
            objDoc = objDatabase.DocumentFromGUID(guid);
        }
        catch (err) {
        }
        //
        if (objDoc == null) {
            continue;
        }
        //
        var objFile = objAutoSaveFSO.GetFile(filename);
        var dtFile = objFile.DateModified;
        var dtDoc = objDoc.DataDateModified;
        //
        var strFileDate = WizComDateTimeToStr(dtFile);
        var strDocDate = WizComDateTimeToStr(dtDoc);
        //
        if (strDocDate >= strFileDate) {
            objAutoSaveFSO.DeleteFile(filename);
            continue;
        }
        //
        ret.push(filename);
    }
    //
    if (ret.length > 0) {
        var pluginpath = objApp.GetPluginPathByScriptFileName("autosave.js");
        var languageFileName = pluginpath + "plugin.ini";
        var title = objApp.LoadStringFromFile(languageFileName, "Title");
        objWindow.ShowHtmlDialog(title, pluginpath + "recovery.htm", 650, 300, "");
    }
}

eventsClose.add(autosaveOnClose);
objWindow.AddTimer("autosaveOnTimer", 1000 * 60);

autosaveCheck();


function autosaveDeleteDocumentFile(objWizDocument) {
    if (!objAutoSaveFSO)
        return;
    //
    if (!objWizDocument)
        return;
    //

    var fileAutoSave = autosaveGetWizDocumentFileName(objWizDocument);
    if (!fileAutoSave)
        return;
    //
    try {
        objAutoSaveFSO.DeleteFile(fileAutoSave);
    }
    catch (err) {
    }
}

function autosaveOnTabClose(objHtmlDocument, objWizDocument) {
    autosaveDeleteDocumentFile(objWizDocument);
}

function autosaveOnDocumentBeforeChange(objHtmlDocument, objWizDocumentOld, objWizDocumentNew) {
    autosaveDeleteDocumentFile(objWizDocumentOld);
}


eventsTabClose.add(autosaveOnTabClose);
eventsDocumentBeforeEdit.add(autosaveOnDocumentBeforeChange);

