isc.defineClass("OPINIATOR_SurveyEditors", isc.Canvas);
OPINIATOR_SurveyEditors.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,

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

        this.addChild(OPINIATOR_SurveyEditor.create({
            ID:"SDSurveyEditorID", 
            selector:this.selector, 
            toolstrip:this.toolstrip, 
            visibility:"hidden", 
            width:"100%", 
            height:"100%"
            }));

        this.addChild(OPINIATOR_OrgKPIEditor.create({
            ID:"SDOrgEditorID", 
            selector:this.selector, 
            toolstrip:this.toolstrip,
            visibility:"hidden", 
            width:"100%", 
            height:"100%"
            }));
        },

    destroy: function()
        {
        // Clear out the category cache
        OPINIATOR_CategoryCache.clear();

        this._editor = null;
        this.selector = null;
        this.toolstrip = null;
        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);
        },
        
    editSurvey : function ( survey )
        { this._setEditor(SDSurveyEditorID, survey); },

    editOrg: function ( org )
        { 
        if ( org.isRootOrg )
            { this.clearEditor(); }
        else
            { this._setEditor(SDOrgEditorID, 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_SurveyDesigner", isc.VLayout);
OPINIATOR_SurveyDesigner.addProperties({ ID:"SurveyEditorID",

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

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

    initWidget : function () 
        {

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

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

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

            onSurveyOpen: function ( survey ) { 
				// edit survey 
				if (isc.isA.Number(survey)) {
					selectedSurvey = survey;	
				} else {
					// create new survey
					selectedSurvey = null;
				}
				this.target.editSurvey(survey);
			 },

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

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

            onSurveyAdded: function ( survey )
                { this.target.editSurveyNoCheck(survey); }

            });
		if (loadFirstTime) {
			this._selector.refresh();
			loadFirstTime = false;
		} else {
			// loading tree into tree grid
			this._selector.setSurveyTree();
			this._selector._getTreeGrid().setData(globalTree);	
		}
		

        this._editors = OPINIATOR_SurveyEditors.create({ 
            ID: "SDEditorsID",
            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) )
            {
            this._selector.target = null;
            this._selector = null;
            }
        this._editors = null;
        this._selectortools = null;
        this._editortools = null;
        this.Super("destroy", arguments);
        },

    editSurveyNoCheck: function ( survey )
        { this._editors.editSurvey(survey); },

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

    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._pendingSurvey) || isc.isAn.Object(this._pendingSurvey) )
            {
            var survey = this._pendingSurvey;
            delete this._pendingSurvey;
            this._editors.editSurvey(survey);
            }
        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}; }

    });

