
isc.defineClass("OPINIATOR_OrgUserEditors", isc.Canvas);
OPINIATOR_OrgUserEditors.addProperties({
    // Construtor propertes
    /**
     * The Survey Selector for updating nodes
     */
    selector: null,

    /**
     * The Toolstrip to add buttons to
     */
    toolstrip: null,

    // Private Properties
    /**
     * The entity we are editing which may be either the survey or 
     * the org but never both 
     */
    _editor: null,

    /**
     *  The collection of editors 
     */
    _orgEditor: null,
    _userEditor: null,

    initWidget : function () 
        {
        this.Super("initWidget", arguments);

        this._orgEditor = OPINIATOR_OrgEditor.create({
            selector:this.selector, 
            toolstrip:this.toolstrip, 
            visibility:"hidden", 
            width:"100%", 
            height:"100%"
            });

        this.addChild(this._orgEditor);

        this._userEditor = OPINIATOR_UserEditor.create({
            selector:this.selector, 
            toolstrip:this.toolstrip,
            visibility:"hidden", 
            width:"100%", 
            height:"100%"
            });

        this.addChild(this._userEditor);
        },

    destroy: function()
        {
        delete this._editor;
        delete this._orgEditor;
        delete this._userEditor;
        delete this.selector;
        delete this.toolstrip;
        this.Super("destroy", arguments);
        },

    clearEditor : function ()
        {
        if ( isc.isAn.Object(this._editor) )
            { this._editor.hide(); }
        },

    _setEditor : function ( editor, entity )
        {
        var children = this.children;
        for ( var i = 0; i < children.length; i++ )
            {
            if ( children[i].getID() != editor.getID() )
                { children[i].hide(); }
            }
        this._editor = editor;
        editor.show();
        editor.edit(entity);
        },
        
    editUser : function ( user )
        { this._setEditor(this._userEditor, user); },

    editOrg: function ( org )
        { this._setEditor(this._orgEditor, org); },

    save: function ( callback )
        { 
        if ( isc.isAn.Object(this._editor) )
            { this._editor.save(callback); }
        },

    cancel: function ()
        { 
        if ( isc.isAn.Object(this._editor) )
            { this._editor.cancel(false); }
        },

    wasModified: function ()
        { 
        if ( isc.isAn.Object(this._editor) )
            { return this._editor.wasModified(); }
        return false;
        }

    });



/**
 * @class Class to view the results of all surveys including a 
 * the selector and a detail viewer for an individual viewer 
 * 
 * @author ktyra (10/24/2008)
 */
isc.defineClass("OPINIATOR_OrgUserDesigner", isc.VLayout);
OPINIATOR_OrgUserDesigner.addProperties({

    // Superclass Properties
    styleName:"defaultCanvas",
    width:"100%",

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

    initWidget : function () 
        {

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

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

        this._selector = OPINIATOR_OrgUserSelector.create({ 
            target: this, 
            toolstrip: this._selectortools,
            width:"230",

            onUserOpen: function ( user )
                { this.target.editUser(user); },

            onOrgOpen: function ( orgid )
                { this.target.editOrg(orgid); },

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

            onUserAdded: function ( user )
                { this.target.editUserNoCheck(user); },

            onOrgAdded: function ( org )
                { this.target.editOrgNoCheck(org); }
            });

		if ( loadFirstTimeAdmin ) {
			this._selector.refresh();
			loadFirstTimeAdmin = false;
		} else {
			this._selector.setUserTree();
			globalTreeAdmin.data = globalDataAdmin;
			this._selector._getTreeGrid().setData(globalTreeAdmin);
		}

        this._editors = OPINIATOR_OrgUserEditors.create({ 
            selector:this._selector,
            toolstrip:this._editortools
            });

        var toolarea = isc.HLayout.create({
            height:30,
            members:[this._selectortools, this._editortools]
            });

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

        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._editors;
        delete this._selectortools;
        delete this._editortools;
        this.Super("destroy", arguments);
        },

    editUserNoCheck: function ( user )
        { this._editors.editUser(user); },

    editOrgNoCheck: function ( org )
        { this._editors.editOrg(org); },

    editUser: function ( user )
        {
        if ( this._editors.wasModified() )
            {
            this._pendingUser = user;
            OPINIATOR.askOk("You have made changes that will be lost! Would you like to save first?", 
                          this.callback("askSaveReply"));
            }
        else
            { this._editors.editUser(user); }
        },

    editOrg: function ( org )
        {
        if ( this._editors.wasModified() )
            {
            this._pendingOrg = org;
            OPINIATOR.askOk("You have made changes that will be lost! Would you like to save first?", 
                          this.callback("askSaveReply"));
            }
        else
            { this._editors.editOrg(org); }
        },

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


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


     editPending: function ()
        {
        if ( isc.isA.Number(this._pendingUser) || isc.isAn.Object(this._pendingUser) )
            {
            var user = this._pendingUser;
            delete this._pendingUser;
            this._editors.editUser(user);
            }
        else if ( isc.isA.Number(this._pendingOrg) || isc.isAn.Object(this._pendingOrg) )
            {
            var org = this._pendingOrg;
            delete this._pendingOrg;
            this._editors.editOrg(org);
            }
        },

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

    });

