function BT_init() {
    var databasePath = objDatabase.DatabasePath;
    //
    var iniFileName = databasePath + "BlogDownloader.ini";
    var autoRun = objCommon.GetValueFromIni(iniFileName, "Common", "AutoRun") == "1";
    if (!autoRun)
        return;

    //
    var pluginPath = objApp.GetPluginPathByScriptFileName("BlogTools.js");
    //
    var wizShellFileName = objApp.AppPath + "WizShell.exe";
    var wizKMControlsFileName = objApp.AppPath + "WizKMControls.dll";
    //
    var paramBase = "\"" + wizKMControlsFileName + "\" WizKMRunScript /ScriptFileName=" + pluginPath + "BlogDownloader.js /DatabasePath=" + objDatabase.DatabasePath;
    //
    var count =  objCommon.GetValueFromIni(iniFileName, "Common", "FeedCount");
    for (var i = 0; i < count; i++) {
        var section = "Feed_" + i;
        //
        var params = paramBase + " /Section=" + section;
        //
        objCommon.RunExe(wizShellFileName, params, false);
    }
}
//
BT_init();


function BT_loadString(name) {
    var pluginPath = objApp.GetPluginPathByScriptFileName("BlogTools.js");
    //
    var iniFileName = pluginPath + "Plugin.ini";
    //
    return objApp.LoadStringFromFile(iniFileName, name);
}
function BT_checkGoogleReaderCore() {
    //
    try {
        var url = "http://www.google.com/reader/atom/?n=10";
        //
        var progress = objCommon.CreateWizObject("WizKMControls.WizProgressWindow");
        progress.Title = "Google Reader";
        progress.Show();
        //
        var xml = objCommon.URLDownloadToText(url);
        //
        progress.Hide();
        //
        var objXml = objApp.CreateActiveXObject("MSXML.DOMDocument");
        objXml.async = false;
        if (!objXml.loadXML(xml)) {
            return false;
        }
        //
        return true;
    }
    catch (err) {
    }
    return false;
}
function BT_downloadGoogleReader() {
    if (!BT_checkGoogleReaderCore()) {
        var msg = BT_loadString("strLoginGoogleReader");
        WizAlert(msg);
        //
        objWindow.ShowHtmlDialog("Google Reader", "https://www.google.com/accounts/ServiceLogin?service=reader&continue=http://www.google.com/reader&followup=http://www.google.com/reader", 600, 400, "");
    }
    if (!BT_checkGoogleReaderCore()) {
        var msg = BT_loadString("strGoogleReaderFailed");
        WizAlert(msg);
        return;
    }
    //
    var pluginPath = objApp.GetPluginPathByScriptFileName("BlogTools.js");
    //
    var wizShellFileName = objApp.AppPath + "WizShell.exe";
    var wizKMControlsFileName = objApp.AppPath + "WizKMControls.dll";
    //
    var params = "\"" + wizKMControlsFileName + "\" WizKMRunScript /ScriptFileName=" + pluginPath + "GoogleReaderDownloader.js /DatabasePath=" + objDatabase.DatabasePath;
    //
    objCommon.RunExe(wizShellFileName, params, false);
}

function BT_initGoogleReader() {
    //
    var databasePath = objDatabase.DatabasePath;
    //
    var iniFileName = databasePath + "BlogDownloader.ini";
    var autoRun = objCommon.GetValueFromIni(iniFileName, "GoogleReader", "AutoRun") == "1";
    if (!autoRun)
        return;
    //
    BT_downloadGoogleReader();
}

BT_initGoogleReader();