
isc.defineClass("OPINIATOR_OrgKPIEditor", OPINIATOR_TabSet);
OPINIATOR_OrgKPIEditor.addProperties({

    // Constructor Properties
    /**
     * The selector that contains the key node to this org for 
     * refresh purposes.
     */
    selector: null,
    
    /**
     * Toolstrip to add save and cancel buttons
     */
    toolstrip: null,

    // Private Properties

    // The save button in the toolstrip
    _saveButton: null,

    // The cancel button in the toolstrip
    _cancelButton: null,

    // The org that holds the kpi list
    _orgid: null,

    // The KPIList we are editing
    _kpilist: null,

    // Superclass Properties
    destroyOnUpdate: false,

    initWidget: function ()
        {
        this._saveButton = OPINIATOR_Button.create({
            title:"Save",
            visibility: "hidden",
            target:this,
            layoutAlign:"center",
            icon: "icons/16/save.png",
            click: function ()
                { this.target.save(); return true;}
            });

        this._cancelButton = OPINIATOR_Button.create({
            title:"Cancel",
            layoutAlign:"center",
            visibility: "hidden",
            icon: "icons/16/cancel.png",
            target:this,
            click: function ()
                { this.target.cancel(true); return true;}
            });
            

        this.tabs = new Array();

        if ( !OPINIATOR_MAIN.user.orgUnitRef.isRootOrg )
            {
            this.tabs.add({
                title:"KPI",
                openPane: function ( parent, pane, kpilist ) 
                    {
                    if ( !isc.isAn.Object(pane) ) 
                        { pane = OPINIATOR_KPIListEditor.create({parentEditor:parent}); }
                    pane.editList(kpilist.catrefs, kpilist.kpis);
                    return pane;
                    },
                savePane: function ( parent, pane, kpilist )  
                    { kpilist.kpis = pane.getList(); }
                });
            }

        this.toolstrip.addMember(this._cancelButton);
        this.toolstrip.addMember(this._saveButton);
        this.Super("initWidget", arguments);
        },

    destroy: function ()
        {
        this.toolstrip = null;
        this.selector = null;
        this._saveButton = null;
        this._cancelButton = null;
        this._orgid = null;
        this._kpilist = null;
        this.Super("destroy", arguments);
        },

    enableSave: function ()
        {
        this._saveButton.show();
        this._cancelButton.show();
        },

    disableSave: function ()
        {
        this._saveButton.hide();
        this._cancelButton.hide();
        },

    cancel: function ( reedit )
        {
        this.disableSave();
        if ( reedit )
            { this.edit(this._orgid); }
        },

    save: function ( callback )
        {
        if ( isc.isAn.Object(callback) )
            { this._callbackOnSave = callback; }
        else
            { delete this._callbackOnSave; }

        this.savePanes(this._kpilist);
        this.showBusy(true, OPINIATOR.BUSY_DELAY);
        WebClientSvc.updateOrgKPIList(this._orgid, this._kpilist, 
                                      this.callback("_saveDone"), this.callback("error"));
        },

    _saveDone: function ( kpilist )
        {
        this.showBusy(false);
        if ( isc.isAn.Object(this._callbackOnSave) )
            { Class.fireCallback(this._callbackOnSave); }
        else
            { 
            this._kpilist = kpilist;
            this.disableSave();
            this.openPanes(kpilist); 
            }
        },

    /**
     * Edits the kpilist of orgunit.  If the survey is passed in as 
     * a number its assumed to be the survey id and the record will 
     * be retrieved from the surver. 
     * 
     * @author ktyra (3/27/2009)
     */
    edit: function ( orgid )
        {
        if ( this._orgid != orgid )
            {
            this._orgid = orgid;
            this.showBusy(true, OPINIATOR.BUSY_DELAY);
            WebClientSvc.getOrgKPIList(orgid, this.callback("_edit"), this.callback("error"));
            }
        else
            { this._edit(this._kpilist); }
        },

    _edit: function ( kpilist )
        {
        this.showBusy(false);
        this._kpilist = kpilist;
        this._kpilist.kpis = OPINIATOR.toArray(this._kpilist.kpis);
        this._kpilist.catrefs = OPINIATOR.toArray(this._kpilist.catrefs);
        this.openPanes(this._kpilist);
        },

    wasModified: function ()
        { return this._saveButton.isVisible(); },

    error: function (code, msg)
        {
        this.showBusy(false);
        switch ( msg )
            {
            case "StaleState":
                OPINIATOR.warn("Changes were made to the list by another user while you were editing.\nYour changes were lost! Please re-edit.");
                break;

            case "DeletedState":
                OPINIATOR.warn("This OrgUnit has been deleted by another user.");
                this.selector.refresh();
                this.hide();
                break;

            default:
                OPINIATOR.warn(msg);
                break;
            }
        },

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

    });

