define(function (require) {
    var activity = require("sugar-web/activity/activity");
    var datastore = require("sugar-web/datastore");

    require("Markdown.Converter");
    //require("Markdown.Sanitizer");
    require("Markdown.Editor");


    // Manipulate the DOM only when it is ready.
    require(['domReady!'], function (doc) {

        // Initialize the activity.
        activity.setup();

        inputTextContent = document.getElementById("wmd-input-second");

        //to save and resume the contents from datastore.

        var datastoreObject = activity.getDatastoreObject();

        console.log(datastoreObject);
        console.log(datastoreObject);

        inputTextContent.onblur = function () {
            console.log(inputTextContent.value);
            var jsonData = JSON.stringify((inputTextContent.value).toString());
            datastoreObject.setDataAsText(jsonData);
            datastoreObject.save(function () {});
        };

        markdownParsing();

        datastoreObject.loadAsText(function (error, metadata, data) {
            markdowntext = JSON.parse(data);
            inputTextContent.value = markdowntext;
            markdownParsing(); //it has to parse only after the input text box has been loaded
        });

        var journal = document.getElementById("insertimage");

        journal.onclick = function () {
            activity.showObjectChooser(function (error, result) {
                result1 = result.toString();
                var datastoreObject2 = new datastore.DatastoreObject(result1);
                datastoreObject2.loadAsText(function (error, metadata, data) {
                    try {
                        textdata = JSON.parse(data);
                    } catch (e) {
                        textdata = data;
                    }

                    var inputTextContent = document.getElementById("wmd-input-second");
                    inputTextContent.value += textdata;

                });

            });
        };

        function markdownParsing() {
            var converter2 = new Markdown.Converter();
            var help = function () {
                alert("Do you need help?");
            }
            var options = {
                helpButton: {
                    handler: help
                },
                strings: {
                    quoteexample: "whatever you're quoting, put it right here"
                }
            };
            var editor2 = new Markdown.Editor(converter2, "-second", options);

            editor2.run();

        }
    });

});

@@ -594,85 +594,8 @@
 );%0A%0A
-        console.log(datastoreObject);%0A        console.log(datastoreObject);%0A%0A
     
@@ -1833,32 +1833,34 @@
                 
+//
 inputTextContent
@@ -1883,56 +1883,856 @@
 ta;%0A
-%0A                %7D);%0A%0A            %7D);%0A        %7D;
+                    insertAtCursor(inputTextContent, textdata);%0A                %7D);%0A%0A            %7D);%0A        %7D;%0A%0A        function insertAtCursor(myField, myValue) %7B%0A            //IE support%0A            if (document.selection) %7B%0A                myField.focus();%0A                sel = document.selection.createRange();%0A                sel.text = myValue;%0A            %7D%0A            //MOZILLA and others%0A            else if (myField.selectionStart %7C%7C myField.selectionStart == '0') %7B%0A                var startPos = myField.selectionStart;%0A                var endPos = myField.selectionEnd;%0A                myField.value = myField.value.substring(0, startPos)%0A                    + myValue%0A                    + myField.value.substring(endPos, myField.value.length);%0A            %7D else %7B%0A                myField.value += myValue;%0A            %7D%0A        %7D
 %0A%0A  

Insert text at cursot position