 
// create a namespace if not already defined
flykit.namespace("flykit.flytedstage");


/*
 * --------------------------------------------------
 * WIDGET
 * --------------------------------------------------
 */
 
 /**
 * Create a flyted gene widget
 * @class
 * A widget for displaying genes showing expression in given stages
 * @constructor
 * @param {flykit.flytedstage.Service} service the service to use to fetch data
 * @param {flykit.flytedstage.DefaultRenderer} renderer the renderer to use
 */
flykit.flytedstage.GeneWidget = function( service, renderer ) {

    var _context = "flykit.flytedstage.GeneWidget";
    try {
        
        flykit.debug("call private constructor", _context);
        this.__init__(service, renderer);
        
    } catch (unexpected) {
        flykit.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
		
};



/** @private */
flykit.flytedstage.GeneWidget.prototype._controller = null;

/**
 * @private
 * @type flykit.mvcutils.GenericModel2
 */ 
flykit.flytedstage.GeneWidget.prototype._model = null;

/**
 * @private
 */
flykit.flytedstage.GeneWidget.prototype._renderer = null;

/**
 * @private
 */
flykit.flytedstage.GeneWidget.prototype._service = null;

/**
 * @private
 */ 
flykit.flytedstage.GeneWidget.prototype._geneSelectedEvent = null;

/**
 * Do initialisation of a widget.
 * @private
 * @param {flykit.flytedstage.Service} service
 * @param {flykit.flytedstage.GeneWidget.DefaultRenderer} renderer
 * @throws flykit.UnexpectedException
 */
flykit.flytedstage.GeneWidget.prototype.__init__ = function(service, renderer) {
    try {

        this._service = service;
        this._renderer = renderer;
        
        this._geneSelectedEvent = new YAHOO.util.CustomEvent("GENESELECTED", this);
        
        // create a model
        this._model = new flykit.mvcutils.GenericModel2();
        this._model.setDefinition(flykit.flytedstage.GeneWidget.modelDefinition);
        
        // instantiate the controller
        this._controller = new flykit.flytedstage.GeneWidget.Controller(this._model, service, this);
        
        // connect the renderer to the model
        this._renderer.connect(this._model);
        
        
        // instantiate a user event handler and pass to renderer
        this._renderer.setUserEventHandler(new flykit.flytedstage.UserEventHandler(this._controller));
        
    } catch (error) {
        throw new flykit.UnexpectedException("flykit.flytedstage.GeneWidget.prototype._init", error);
    }
};


flykit.flytedstage.GeneWidget.prototype.subscribe = function(type, listener, obj) {
    var _context = "flykit.flytedstage.GeneWidget.prototype.subscribe";
    try {
        if (type == "GENESELECTED") {
            flykit.debug("widget: gene selected");
            this._geneSelectedEvent.subscribe(listener, obj);
        }
        
    } catch (e) {
        flykit.debug("caught "+e.name, ", "+e.message, _context);
        throw new flykit.UnexpectedException(_context, e);
    }    
};

/**
 * @param {String} stageName
 */
flykit.flytedstage.GeneWidget.prototype.findProbesExpressedInStage = function( stageName ) {
    try {
        // pass through to controller
        this._controller.findProbesExpressedInStage(stageName);
    } catch (error) {
        throw new flykit.UnexpectedException("flykit.flytedstage.GeneWidget.prototype.findProbesExpressedInStage", error);
    }
};


/**
 * @param {String} stageName
 */
flykit.flytedstage.GeneWidget.prototype.findProbesExpressedInStageBatch = function( stageNames ) {
    try {
        // pass through to controller
        this._controller.findProbesExpressedInStageBatch(stageNames);
    } catch (error) {
        throw new flykit.UnexpectedException("flykit.flytedstage.GeneWidget.prototype.findProbesExpressedInStageBatch", error);
    }
};

/**
 * Set the selection index, where more than one genes are available.
 * @param {Number} index the new selection index
 */
flykit.flytedstage.GeneWidget.prototype.setSelectionIndex = function( index ) {
    var _context = "flykit.flytedstage.GeneWidget.prototype.setSelectionIndex";
    try {
        flykit.debug("pass through to controller", _context);
        this._controller.setSelectionIndex(index);
    } catch (e) {
        flykit.debug("caught "+e.name, ", "+e.message, _context);
        throw new flykit.UnexpectedException(_context, e);
    }    
};



flykit.flytedstage.UserEventHandler = function( controller ) {

    /**
     * @private
     * Handle a mouse click on a result.
     * @param event the browser event
     * @param {Number} index the index of the result clicked
     */
	this._onResultClicked = function( event, index ) {
	    var _context = "flykit.flytedstage.UserEventHandler this._onResultClicked";
	    try {
            flykit.info("received click event, call the controller to set selection: "+index, _context); 
            
            if (this.checked)
                controller.setSelectionIndex(index);
            else
                controller.removeSelectionIndex(index); //TODO
            
            //controller.setSelectionIndex(index);	        
        } catch (e) {
            flykit.debug("caught "+e.name, ", "+e.message, _context);
            throw new flykit.UnexpectedException(_context, e);
        }    
	};	
	
};



/*
 * --------------------------------------------------
 * CONTROLLER
 * --------------------------------------------------
 */


/**
 * Create a controller for a flyted development stage widget.
 * @class
 * A controller class for the flyted development stage widget internal MVC.
 * @constructor
 * @param {flykit.mvcutils.GenericModel2} model the model to store widget state data
 * @param {flykit.flytedstage.Service} service the service to use to fetch data
 * @param {flykit.flytedstage.GeneWidget} widget the widget to control
 */
flykit.flytedstage.GeneWidget.Controller = function( model, service, controllee ) {
	
	/**
	 * @private
	 */
	this._model = null;
	
	/**
	 * @private
	 */
	this._service = null;
	
	/**
     * @private
     * @type flykit.flyatlas.Widget
     */
    this._controllee = null;
    
    this._parent = controllee;

    // do initialisation
    this._init(model, service, controllee);
	
};	


/**
 * @param {flykit.mvcutils.GenericModel2} model
 * @param {flykit.flytedstage.Service} service
 * @param {flykit.flytedstage.GeneWidget} controllee
 * @throws flykit.UnexpectedException
 */
flykit.flytedstage.GeneWidget.Controller.prototype._init = function( model, service, controllee ) {
    this._model = model;
    this._service = service;
    this._controllee = controllee;
};


/**
 * @param {String} stageName
 */
flykit.flytedstage.GeneWidget.Controller.prototype.findProbesExpressedInStage = function( stageName ) {
    try {
        // pass through to private implementation
        this._findProbesExpressedInStage(stageName, this._getProbessSuccess(), this._getProbesFailure());
    } catch (error) {
        throw new flykit.UnexpectedException("flykit.flytedstage.GeneWidget.Controller.prototype.findProbesExpressedInStage", error);
    }
};

/**
 * @param {String} stageName
 */
flykit.flytedstage.GeneWidget.Controller.prototype.findProbesExpressedInStageBatch = function( stageNames ) {
    try {
        // pass through to private implementation
        this._findProbesExpressedInStageBatch(stageNames, this._getProbessSuccess(), this._getProbesFailure());
    } catch (error) {
        throw new flykit.UnexpectedException("flykit.flytedstage.GeneWidget.Controller.prototype.findProbesExpressedInStageBatch", error);
    }
};

/**
 * @private
 * @param {String} stageName
 * @param {Function} success
 * @param {Function} failure
 */
flykit.flytedstage.GeneWidget.Controller.prototype._findProbesExpressedInStage = function( stageName, success, failure ) {
    try {
        flykit.debug("in _findProbesExpressedInStage, stageName: "+stageName);
        
        this._model.set("RESULTS", null);
        
        this._model.set("QUERY", stageName);
        
        //this._model.set("MODE", "PROBE");
        
        this._model.set("STATE", "PENDING");
        
        flykit.debug("set selection index null");
        this._model.set("SELECTIONINDEX", null);        
        
        this._service.findProbesExpressedInStage(stageName, success, failure);
        
    } catch (error) {
        throw new flykit.UnexpectedException("flykit.flytedstage.GeneWidget.Controller.prototype._findProbesExpressedInStage", error);
    }
};



/**
 * @private
 * @param {String} stageName
 * @param {Function} success
 * @param {Function} failure
 */
flykit.flytedstage.GeneWidget.Controller.prototype._findProbesExpressedInStageBatch = function( stageNames, success, failure ) {
    try {
        flykit.debug("in _findProbesExpressedInStageBatch, stageNames: "+stageNames);
        
        this._model.set("RESULTS", null);
        
        this._model.set("QUERY", stageNames);
        
        //this._model.set("MODE", "PROBE");
        
        this._model.set("STATE", "PENDING");
        
        flykit.debug("set selection index null");
        this._model.set("SELECTIONINDEX", null);        
        
        this._service.findProbesExpressedInStageBatch(stageNames, success, failure);
        
    } catch (error) {
        throw new flykit.UnexpectedException("flykit.flytedstage.GeneWidget.Controller.prototype._findProbesExpressedInStageBatch", error);
    }
};



/**
 * @private
 * @type Function
 */
flykit.flytedstage.GeneWidget.Controller.prototype._getProbessSuccess = function() {
    var self = this;
    return function( groups ) {
        try {
            self._model.set("RESULTS", groups);
            self._model.set("STATE", "READY");
        } catch (error) {
            throw new flykit.UnexpectedException("anonymous callback (from flykit.flytedstage.GeneWidget.Controller.prototype._getProbessSuccess)", error);
        }
    }
};


/**
 * @private
 * @type Function
 */
flykit.flytedstage.GeneWidget.Controller.prototype._getProbesFailure = function() {
    var self = this;
    return function( response ) {
        try {
            flykit.err("request failed: "+response.status+" "+response.statusText);
            self._model.set("MESSAGE", "there was an error retrieving data from the server, see the logs for more info");
            self._model.set("STATE", "SERVERERROR");
        } catch (error) {
            throw new flykit.UnexpectedException("anonymous callback (from flykit.flytedstage.GeneWidget.Controller.prototype._getProbesFailure)", error);
        }
    }
};



flykit.flytedstage.GeneWidget.Controller.prototype.setSelectionIndex = function( obj ) {
   
    var _context = "flykit.flytedstage.GeneWidget.prototype.setSelectionIndex";
    var badIndex = false;
    try {
        var selectedFlybaseId = obj[0];
        var index = obj[1];
        
        flykit.debug("setSelectionIndex, index: "+index, _context);
        
        var indexArray = this._model.get("SELECTIONINDEX");
        if (indexArray != null){
            var notIn = true;
            for (var i =0; i < indexArray.length; i++){
                if (indexArray[i][1]==index){
                    notIn = false;
                    break;
                }
            }
            if (notIn)
                indexArray.push (obj);
            this._model.set("SELECTIONINDEX", indexArray);
        }else {
            indexArray = new Array();
            indexArray[0] = obj;
            this._model.set("SELECTIONINDEX", indexArray);
        }
        
        var event = this._parent._geneSelectedEvent;
        
        //var genes = [];
        var querynames = [];
        
        for (var i=0; i < indexArray.length; i ++) {
            querynames[querynames.length] = indexArray[i][0];
            //genes[genes.length] = indexArray[i][3];
            
        }
        event.fire(querynames); 
        
    } catch (e) {
        flykit.debug("caught "+e.name, ", "+e.message, _context);
        throw new flykit.UnexpectedException(_context, e);
    }   
    
    if (badIndex) {
        throw new flykit.flytedstage.SelectionOutOfBounds("index "+index+" cannot apply to results length "+results.length);
    } 
};

















flykit.flytedstage.GeneWidget.Controller.prototype.removeSelectionIndex = function( obj ) {
   
    var _context = "flykit.flytedstage.GeneWidget.prototype.removeSelectionIndex";
    var badIndex = false;
    try {
        var selectedFlybaseId = obj[0];
        var index = obj[1];
        
        flykit.debug("setSelectionIndex, index: "+index, _context);
        var results = this._model.get("RESULTS");
        
        if (results != null) {
            if (index >= 0 && index < results.length) {
                var indexArray = this._model.get("SELECTIONINDEX");
                if (indexArray != null){
                    var selected = false;
                    var toberemoved = 0;
                    for (var i =0; i < indexArray.length; i++){
                        if (indexArray[i][1]==index){
                            toberemoved = i;
                            selected = true;
                            break;
                        }
                    }
                    if (selected){
                        indexArray.splice(toberemoved,1);
                    }
                    this._model.set("SELECTIONINDEX", indexArray);
                }
                
                var event = this._parent._geneSelectedEvent;
                
                var querynames = [];
                
                for (var i=0; i < indexArray.length; i ++) {
                    querynames[querynames.length] = indexArray[i][0];
                }
                event.fire(querynames); 
            }
            else {
                badIndex = true;
            }
        }
    } catch (e) {
        flykit.debug("caught "+e.name, ", "+e.message, _context);
        throw new flykit.UnexpectedException(_context, e);
    }   
    
    if (badIndex) {
        throw new flykit.flyatlas.SelectionOutOfBounds("index "+index+" cannot apply to results length "+results.length);
    } 
};










/**
 * @class
 * An exception thrown if selection index is out of bounds.
 * @constructor
 * @param {String} message
 */
flykit.flytedstage.SelectionOutOfBounds = function( message ) {
	this.name = "flykit.flytedstage.SelectionOutOfBounds";
	this.message = message;
}


/*
 * --------------------------------------------------
 * MODEL DEFINITION
 * --------------------------------------------------
 */


/**
 * Definition of flyted imagewidget model.
 */
flykit.flytedstage.GeneWidget.modelDefinition = {

	properties : [ "STATE", "RESULTS", "QUERY", "ERRORMESSAGE", "SELECTIONINDEX" ],
	
	values : {
		"STATE" : [ "PENDING", "READY", "SERVERERROR", "UNEXPECTEDERROR" ]
	},
	
	initialize : function( data ) {
		data["STATE"] = "READY";
		data["RESULTS"] = null;
		data["QUERY"] = null;
		data["ERRORMESSAGE"] = null;
		data["SELECTIONINDEX"] = null;
	}

};


/*
 * ----------------------------------------------------------------
 *                             DEFAULT RENDERER
 * ----------------------------------------------------------------
 */
 
 
/**
 * @class
 */
/*flykit.flytedstage.GeneWidget.DefaultRenderer = function() {

    /\** @private *\/
    this._canvas = null;
    
    /\** @private *\/
    this._pendingPane = null;
    
    /\** @private *\/
    this._resultsPane = null;
    
    /\** @private *\/
    this._resultsSummaryPane = null;
    
    /\** @private *\/
    this._messagePane = null;
        
};*/


flykit.flytedstage.GeneWidget.DefaultRenderer = function() {};


/** 
 * @private 
 * @type Element
 */
flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._canvas = null;

/**
 * @private 
 * @type Element
 */
flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._pendingPane = null;

/**
 * @private 
 * @type Element
 */
flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._resultsSummaryPane = null;

/**
 * @private 
 * @type Element
 */
flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._resultsPane = null;

/**
 * @private 
 * @type Element
 */
flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._messagePane = null;



flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._userEventHandler = null;


/**
 * @param {Element} canvas
 * @throws flykit.UnexpectedException
 */
flykit.flytedstage.GeneWidget.DefaultRenderer.prototype.setCanvas = function( canvas ) {
    try {
//    this._canvas = canvas;
        this._canvas = $(canvas);
        this._initCanvas();
    } catch (error) {
        throw new flykit.UnexpectedException("flykit.flytedstage.GeneWidget.DefaultRenderer.prototype.setCanvas", error);
    }
};



flykit.flytedstage.GeneWidget.DefaultRenderer.prototype.setUserEventHandler = function( handler ) {
	this._userEventHandler = handler;
};


/**
 * @private
 * @throws flykit.UnexpectedException
 */
flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._initCanvas = function() {
    try {
        flykit.debug("_initCanvas");
        
        var canvas = this._canvas;
        canvas.addClass("flytedDevelopmentStageWidget");
        
        // setup pending pane
        var pp = $("<p class='pendingPane'>pending...</p>").hide();
        canvas.append(pp);
        this._pendingPane = pp;
    
        // setup results summary pane
        var rsp = $("<p class='resultsSummaryPane'>this text should never be displayed</p>").hide();
        canvas.append(rsp);
        this._resultsSummaryPane = rsp;
        
        // setup results pane
        var rp = $("<div class='resultsPane'></div>").hide();
        canvas.append(rp);
        this._resultsPane = rp;
    
        // message pane
        var mp = $("<p class='messagePane'>this text should never be displayed</p>").hide();
        canvas.append(mp);
        this._messagePane = mp;    
        
    } catch (error) {
        throw new flykit.UnexpectedException("flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._initCanvas", error);
    }    
};

/**
 * @param {flykit.mvcutils.GenericModel2} model
 * @throws flykit.UnexpectedException
 */
flykit.flytedstage.GeneWidget.DefaultRenderer.prototype.connect = function( model ) {
    try {
        model.subscribeAll(this._onModelChanged, this);
    } catch (error) {
        throw new flykit.UnexpectedException("flykit.flytedstage.GeneWidget.DefaultRenderer.prototype.connect", error);
    }    
};

/**
 * @private
 * @throws flykit.UnexpectedException
 */
flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._onModelChanged = function( type, args, self ) {
    try {
        var handlers = {
            "STATE":"_onStateChanged",
            "QUERY":"_onQueryChanged",
            "RESULTS":"_onResultsChanged",
            "MESSAGE":"_onMessageChanged",
            "SELECTIONINDEX":"_onSelectionIndexChanged"
        };
        var handler = handlers[type];
        flykit.debug("handler: "+handler);
        // call the handler
        self[handler](args[0], args[1], args[2]);
    } catch (error) {
        throw new flykit.UnexpectedException("flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._onModelChanged", error);
    }    
};


/**
 * @private
 * @param {String} from
 * @param {String} to
 * @param {Function} get
 * @throws flykit.UnexpectedException
 */
flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._onQueryChanged = function( from, to, get ) {
    // do nothing, we will access the value later
};


/**
 * @private
 * @param {String} from
 * @param {String} to
 * @param {Function} get
 * @throws flykit.UnexpectedException
 */
flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._onStateChanged = function( from, to, get ) {
    var _context = "flykit.flyted.ImageWidget.DefaultRenderer.prototype._onStateChanged";
    try {
        flykit.debug("new state " + to, _context);
        if ( to == "PENDING" ) {
            this._pendingPane.show();
            this._messagePane.empty().hide();
            this._resultsSummaryPane.hide();
            this._resultsPane.hide();
        }
        else if ( to == "READY" ) {
            flykit.debug("show results", _context);
            this._pendingPane.hide();
            this._messagePane.hide(); // hide for now, not needed yet
            //this._canvas.show();
            this._resultsSummaryPane.show();
            this._resultsPane.show();         
        } 
        else if ( to == "SERVERERROR" || to == "UNEXPECTEDERROR" ) {
            this._pendingPane.hide();
            this._messagePane.show();
            this._resultsSummaryPane.hide();
            this._resultsPane.hide();         
        } 
        else {
            // this should never happen
            throw {message:"invalid state: "+to};
        }
    } catch (error) {
        throw new flykit.UnexpectedException("flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._onStateChanged", error);
    }    
};


/**
 * @private
 * @throws flykit.UnexpectedException
 */
flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._onResultsChanged = function( from, to, get ) {
    try {    
        flykit.debug("results changed: "+to);
        
        if (to == null) {
            flykit.debug("empty results summary pane");
            this._resultsSummaryPane.empty();
            flykit.debug("empty results pane");
            this._resultsPane.empty();
        }
        else {
    
            flykit.debug("empty results summary pane");
            this._resultsSummaryPane.empty();
            flykit.debug("empty results pane");
            this._resultsPane.empty();

            var query = get("QUERY"); // fetch from model
            
            flykit.debug("render results summary");
            this._renderResultsSummary(query, to.length);
    
            if (to.length > 0) {
                flykit.debug("render results");
                this._renderResults(to);        
            }    
        }    
    } catch (error) {
        throw new flykit.UnexpectedException("flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._onResultsChanged", error);
    }    
};


/**
 * @private
 * @throws flykit.UnexpectedException
 */
flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._onMessageChanged = function( from, to, get ) {
    try {
        this._messagePane.html(to);
    } catch (error) {
        throw new flykit.UnexpectedException("flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._onMessageChanged", error);
    }    
};


flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._onSelectionIndexChanged = function( from, to ) {
    var _context = "flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._onSelectionIndexChanged";
    try {
        
        flykit.debug("from:"+from+", to:"+to, _context);
        
        if (to != null) {
            // TODO: add class
            
            /*for (var i =0; i < to.length; i ++){
                var gene = to[i][0];
                this._renderGeneSelectionMessage(gene);
            }*/
        }
        
    } catch (e) {
        flykit.debug("caught "+e.name, ", "+e.message, _context);
        throw new flykit.UnexpectedException(_context, e);
    }   
};


/**
 * @private
 * Render the results summary pane.
 * @param {String} query the user's query
 * @param {Number} count number of results found
 * @throws flykit.UnexpectedException
 */
flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._renderResultsSummary = function( query, count ) {
    flykit.debug("in _renderResultsSummary");
    try {
        var content = "found ";
        content += count;
        content += " matching gene";
        content += (count == 0 || count > 1) ? "s " : " ";
        content += "from <a href='http://www.fly-ted.org'>flyted.org</a> ("+flykit.flytedstage.provenance+") whose ";
        content += "in situ hybridization images showing expressions in the stage ";
        
        if (query.length==1)
            content += "<strong>" + query[0] + "</strong>";
        else {
            for (var i = 0; i < query.length-1; i ++){
                var queryStage = query[i];
                content += "<strong>" + query[i] + "</strong> and ";    
            }
            content += "<strong>" + query[query.length-1] + "</strong>";
        }
        
        this._resultsSummaryPane.html(content);
    } catch (error) {
        throw new flykit.UnexpectedException("flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._renderResultsSummary", error);
    }   
};


/**
 * @private
 * Render a message for the given selection.
 * @param {flyui.flyted.Gene} gene the selected gene (may be null for no selection)
 */
flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._renderGeneSelectionMessage = function( gene ) {
    var _context = "flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._renderGeneSelectionMessage";
    try {
        
        if (gene == null) {
            this._messagePane.innerHTML = "selected gene: <strong>please select one of the genes from the list above, or try another query</strong>";
        }
        else {
            this._messagePane.innerHTML = "selected gene: <strong>"+gene + "</strong>)";
        }  
              
    } catch (e) {
        flyui.debug("caught "+e.name, ", "+e.message, _context);
        throw new flykit.UnexpectedException(_context, e);
    }   
};

/**
 * @private
 * Render the results summary pane.
 * @param {String} query the user's query
 * @param {Number} count number of results found
 * @throws flykit.UnexpectedException
 */
flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._renderResults = function( results ) {
    var _context = "flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._renderResults";
    
    try{
        if (results.length>0) {
            var table = $("<table id=\"gene-table\"></table>");
            var thead = $("<thead>");
            table.append(thead);
            var tr = $("<tr>");
            thead.append(tr);
            tr.append($("<th>").append("Gene"));
            tr.append($("<th>").append(""));
            
            /*var inputbox = $("<input>");
            inputbox.attr("type", "checkbox");
            inputbox.attr("name", "all");
            inputbox.attr("value", "all");
            inputbox.attr("id", "default-0");
            
            var flybaseIDs = new Array();
            
            for (var i  =0; i < results.length; i++){
                var result = results[i];
                
                var flybase = result.flybase;
                var flybaseID = flybase.substring(flybase.indexOf("SO_0000704/")+11, flybase.length);
                
                flybaseIDs[flybaseIDs.length] = flybaseID;
            }
            
            YAHOO.util.Event.addListener(inputbox, "click", this._userEventHandler._onResultClicked, [flybaseIDs,0]); //TODO, pass the genes in
                        
            tr.append($("<th>").append(inputbox));*/
            
            var tbody = $("<tbody>");
            table.append(tbody);
            
            
            
            for (var i  =0; i < results.length; i++){
                var result = results[i];
                var genename = result.probeLabel;
                var flybase = result.flybase;
                var flybaseID = flybase.substring(flybase.indexOf("SO_0000704/")+11, flybase.length);
                
                var tr = $("<tr>");
                tbody.append(tr);
                
                tr.append($("<td>").append(genename + " (" + flybaseID + ")"));
                
                var inputbox = $("<input>");
                inputbox.attr("type", "checkbox");
                inputbox.attr("name", genename);
                inputbox.attr("value", flybase);
                inputbox.attr("id", "default-"+i+1);
                            
                tr.append($("<td>").append(inputbox));
                
                // subscribe to onclick event
                flykit.debug("subscribe to the event",_context);
                YAHOO.util.Event.addListener(inputbox, "click", this._userEventHandler._onResultClicked, [flybaseID,i+1]); //TODO, pass the genes in            
            }
        }
        
        this._resultsPane.append(table);
        
    }catch (error) {
        throw new flykit.UnexpectedException("flykit.flytedstage.GeneWidget.DefaultRenderer.prototype._renderResultsSummary", error);
    }  
 }

 
