/**
 * @author Tran Quoc Huy
 */
isc.defineClass("OPINIATOR_SurveyResponseEditors", isc.Canvas);
OPINIATOR_SurveyResponseEditors.addProperties({
    // Construtor propertes
    /**
     * The Survey Selector for updating nodes
     */
    selector: 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_SurveyResponseEditor.create({
            ID:"SDSurveyEditorID", 
            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.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); }   
    });
	
isc.defineClass("OPINIATOR_SurveyResponse", isc.VLayout);
OPINIATOR_SurveyResponse.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_SurveyResponseSelector.create({ 
            ID: "SDSelectorID",
            target: this, 
            toolstrip: this._selectortools,
            width:"230",

            onSurveyOpen: function ( survey ) {				
				this.target.editSurvey(survey);
			 },           

            onNothingOpen: function ( orgid ){
				 this.target.editNothing();
			}

            
            });
		if (loadFirstTime) {
			this._selector.refresh();
			loadFirstTime = false;
		} else {
			// loading tree into tree grid
			this._selector.setSurveyTree();
			this._selector._getTreeGrid().setData(globalTree);	
		}
		

        this._editors = OPINIATOR_SurveyResponseEditors.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);
    },

  
    editSurvey: function ( survey ){      
            this._editors.editSurvey(survey); 
    },

   editNothing: function ( org )
        { this._editors.clearEditor(); },
   
    
    callback: function (methodName)
        { return {target:this, methodName:methodName}; }

    });
