isc.defineClass("OPINIATOR_CategoryDesigner", isc.VLayout);
OPINIATOR_CategoryDesigner.addProperties({
    // Superclass Properties
    styleName:"defaultCanvas",
    width:"100%",

    // Private Properties
    _selector: null,
    _editor: null,
    _selectortools: null,
    _editortools: null,

    initWidget : function () 
        {

        this._selectortools = isc.ToolStrip.create({
            ID: "CDSelectorToolsID",
            align:"left",
            layoutLeftMargin:3,
            styleName: "defaultCanvas", width:"50%",
            height:30
            });

        this._editortools = isc.ToolStrip.create({
            ID: "CDEditorToolsID",
            align:"right",
            layoutRightMargin:3,
            styleName: "defaultCanvas",
            width:"50%",
            height:30
            });

        this._selector = OPINIATOR_CategorySelector.create({ 
            ID: "CDSelectorID",
            target: this, 
            toolstrip: this._selectortools,
            width:"230",

            onCategoryOpen: function ( category )
                { this.target.editCategory(category); },

            onNothingOpen: function ()
                { this.target.editNothing(); },

            onCategoryAdded: function ( category )
                { this.target.editCategoryNoCheck(category); }
            });

        this._editor = OPINIATOR_CategoryEditor.create({ 
            ID: "CDCategoryEditorID",
            selector:this._selector,
            toolstrip:this._editortools
            });

		// this_.selectools : buttons on the bottom left
		// this._edittortools: buttons on the bottom right
        var toolarea = isc.HLayout.create({
            ID:"_toolareaID",
            height:30,
            members:[this._selectortools, this._editortools]
            });

        var area = isc.HLayout.create({
            ID:"_areaID",
            resizeBarClass:OPINIATOR_ResizeBar,
            members: [this._selector, this._editor]
            });

        this.members = [area, toolarea];

        // call superclass implementation
        this.Super("initWidget", arguments);
        },

    destroy: function()
        {
        if ( isc.isAn.Object(this._selector) )
            {
            delete this._selector.target;
            delete this._selector;
            }
        delete this._editor;
        delete this._selectortools;
        delete this._editortools;
        this.Super("destroy", arguments);
        },

    editCategoryNoCheck: function ( category )
        { this._editor.edit(category); },

    editCategory: function ( category )
        {
        if ( this._editor.wasModified() )
            {
            this._pending = category;
            OPINIATOR.askOk("You have made changes that will be lost! Would you like to save first?", 
                          this.callback("askSaveReply"));
            }
        else
            { this._editor.edit(category); }
        },

    editNothing: function ()
        { this._editor.clearEditor(); },

    askSaveReply: function ( value )
        {
        if ( value )
            { this._editor.save(this.callback("editPending")); }
        else
            { 
            this._editor.cancel();
            this.editPending(); 
            }
        },

     editPending: function ()
        {
        if ( isc.isAn.Number(this._pending) || isc.isAn.Object(this._pending) )
            {
            var category = this._pending;
            delete this._pending;
            this._editor.edit(category);
            }
        },

    error: function (code, msg)
        {
        this._editor.showBusy(false);
        OPINIATOR.warn(msg);
        },

    callback: function (methodName)
        { return {target:this, methodName:methodName}; }

    });

