// key and menu control for this script is from do_creator.js file
console.log('saver.js;');

/*
$(document).keyup(function(e) {
		if (e.keyCode == 83) { saveDo_doContent();}          		// S
		if (e.keyCode == 68) { saveDo_doObj();}          		// D
        if (e.keyCode == 70) { saveDo_tObj();}          		// F
	});
*/
// convert do content object to text and js variable file and save it
// include js file into index.html to load all content into variable object
function saveDo_doContent() {
    saveDo(doContent,"doContent");
}

function saveDo_doObj() {
    saveDo(doObj,"doObj");
}

function saveDo_tObj() {
    saveDo(tObj,"tObj");
}

function saveDo(doObj, file_name) {
    var text = "";
    //var text = "console.log('slide1.js;');\n"
    // make nice json reading
    
    var obj = {};
    
    obj.section0 = {} ;
    obj.section0.slide0 =doObj['section0']['slide0'];
    
    
    var jsontext = JSON.stringify(obj);
    jsontext = replaceAll('{','{\n',jsontext);
    jsontext = replaceAll('}','\n}',jsontext);
    jsontext = replaceAll('},','},\n',jsontext);
    jsontext = replaceAll('"section','\t"section',jsontext);
    jsontext = replaceAll('"slide','\t\t"slide',jsontext);
//    jsontext = replaceAll('"template":"','\t\t\t"template":"',jsontext);
    text = text + "var doContent = "+jsontext+';\n';
    
	/* no menu in tests
    jsontext = JSON.stringify(menuItems);
    jsontext = replaceAll('}','\n}',jsontext);
    jsontext = replaceAll('"item','\n\t"item',jsontext);
    text = text + "var menuItems = "+jsontext+';\n';
    
    
    text = text + "console.log('doContent:');\n"
    text = text + "console.log(doContent);\n"
    */
    var blob = new Blob([text], {type: "text/plain;charset=utf-8"});

    saveAs(blob, file_name +".txt"); // blob is saved in file 
    // initialize ActiveXObject and create an object of Scripting.FileSystemObject.
	// replace sting in string
	function replaceAll(find, replace, str) {
		return str.replace(new RegExp(find, 'g'), replace);
	}
}