dojo.require("dijit.Editor");
dojo.require("dijit._editor._Plugin");
dojo.require("dijit._Widget");
dojo.require("dijit._Container");

//create namespace
jmaki.namespace("jmaki.widgets.dojo.dijit.editor");

/**
 * @constructor
*/
jmaki.widgets.dojo.dijit.editor.Widget = function(wargs) {
    
    var self = this;
    var topic = "/dojo/dijit/editor/";
    var container = document.getElementById(wargs.uuid + "_container");

    var toolbar = "dijit._editor.plugins.SmallToolbar";

    if (wargs.publish) {
        topic = wargs.publish;
    }

    if (wargs.args) {

        if (wargs.args.toolbar) {
            if (wargs.args.toolbar == "full") {
                toolbar = "dijit._editor.plugins.FullToolbar";
            }
        }
        if (wargs.args.toolbar) {
            if (wargs.args.toolbar == "medium") {
                toolbar = "dijit._editor.plugins.MediumToolbar";
            }
        }
        if (wargs.args.toolbar) {
            if (wargs.args.toolbar == "small") {
                toolbar = "dijit._editor.plugins.SmallToolbar";
            }
        }
    }

    var plugins =  [ toolbar ];
    var eargs = {plugins : plugins, shareToolbar: false};

    if (wargs.value) {  
        container.innerHTML = wargs.value;
    }
    
    self.wrapper = new dijit.Editor( eargs, container);

    this.setValue = function(_v) {
        self.wrapper.replace(_v);
    }

    this.getValue = function() {
        return this.wrapper.getValue();
    }
    
    this.saveState = function() {
        jmaki.publish(topic + "onSave", {id: wargs.uuid, wargs: wargs, value: self.getValue()});
    }

    dojo.connect(self.wrapper, "_saveContent", self, 'saveState');
    
    if (wargs.service) {
 	    var url = wargs.service;
            dojo.io.bind({
                url: url,
                load: function (type,data,evt) {
                    self.wrapper.replace(data);
                }
            });
    }
}

dojo.declare("dijit._editor.plugins.SmallToolbar", null,
	function(){
		var _p = dijit._editor._Plugin;
		var _tb = dijit.form.ToggleButton;
		this.plugins = [
			new _p({ command: "cut" }),
			new _p({ command: "copy" }),
			new _p({ command: "paste" }),
			new _p({ button: new dijit.ToolbarSeparator() }),
                        new dijit.SavePlugin()
		];
	},
	{
		plugins: [],
		setEditor: function(editor){
			dojo.forEach(this.plugins, function(i){ i.setEditor(editor); });
		},
		setToolbar: function(toolbar){
			dojo.forEach(this.plugins, function(i){ i.setToolbar(toolbar); });
		}
	}
);

dojo.declare("dijit._editor.plugins.MediumToolbar", null,
	function(){
		var _p = dijit._editor._Plugin;
		var _tb = dijit.form.ToggleButton;
		this.plugins = [
			new _p({ command: "cut" }),
			new _p({ command: "copy" }),
			new _p({ command: "paste" }),
                        new dijit.SavePlugin() ,
			new _p({ button: new dijit.ToolbarSeparator() }),
			new _p({ buttonClass: _tb, command: "bold" }),
			new _p({ buttonClass: _tb, command: "italic" }),
			new _p({ buttonClass: _tb, command: "underline" }),
			new _p({ buttonClass: _tb, command: "strikethrough" })
		];
	},
	{
		plugins: [],
		setEditor: function(editor){
			dojo.forEach(this.plugins, function(i){ i.setEditor(editor); });
		},
		setToolbar: function(toolbar){
			dojo.forEach(this.plugins, function(i){ i.setToolbar(toolbar); });
		}
	}
);

dojo.declare("dijit._editor.plugins.FullToolbar", null,
	function(){
		var _p = dijit._editor._Plugin;
		var _tb = dijit.form.ToggleButton;
		this.plugins = [
			new _p({ command: "cut" }),
			new _p({ command: "copy" }),
			new _p({ command: "paste" }),
                        new dijit.SavePlugin() ,
		        new _p({ button: new dijit.ToolbarSeparator() }),
			new _p({ buttonClass: _tb, command: "bold" }),
			new _p({ buttonClass: _tb, command: "italic" }),
			new _p({ buttonClass: _tb, command: "underline" }),
			new _p({ buttonClass: _tb, command: "strikethrough" }),
			new _p({ button: new dijit.ToolbarSeparator() }),
			new _p({ command: "insertOrderedList" }),
			new _p({ command: "insertUnorderedList" }),
			new _p({ command: "indent" }),
			new _p({ command: "outdent" })
		];
	},
	{
		plugins: [],
		setEditor: function(editor){
			dojo.forEach(this.plugins, function(i){ i.setEditor(editor); });
		},
		setToolbar: function(toolbar){
			dojo.forEach(this.plugins, function(i){ i.setToolbar(toolbar); });
		}
	}
);

dojo.declare(
	"dijit.SaveButton",
	[ dijit.form.ToggleButton, dijit._Templated ],
{
	templateString: '<div class="dijitEditorIcon dijitEditorIconSave"></div>',
	postCreate: function(){ dojo.setSelectable(this.domNode, true); }
});

dojo.declare(
	"dijit.SavePlugin",
	[ dijit._editor._Plugin,  ],
        function() {
            var _self = this;
        },
{
    editor: null,
    iconClassPrefix: "dijitEditorIcon",
    button: null,
    queryCommand: null,
    command: "",
    commandArg: null,
    useDefaultCommand: false,
    buttonClass: dijit.form.Button,
    useDefaultCommand: false,
    
    setEditor: function(/*Widget*/editor){
        this.editor = editor;
        this._initButton(editor);
    },
    buttonClass: dijit.form.Button,
    updateInterval: 200, // only allow updates every two tenths of a second
    _initButton: function(editor){
        var label = "Save";
        var className ="dijitEditorIconSave";
	if(!this.button){
            var props = {
	        label: label,
	        iconClass: className
	    };

            this.button = new this.buttonClass(props);
            dojo.connect(this.button, "onClick",  function() {
                editor._saveContent();
            });
        }
    },

    setToolbar: function(/*Widget*/toolbar){
        if(this.button){
	    toolbar.addChild(this.button);
	}
    }    
});



