flykit.namespace("flykit.apps.bydevelopmentstage");

flykit.apps.bydevelopmentstage.CustomGeneFinderRenderer = function() {};
flykit.apps.bydevelopmentstage.CustomGeneFinderRenderer.prototype = new flykit.genefinder.DefaultRenderer(); 

/**
 * @class
 * @constructor
 */
flykit.apps.bydevelopmentstage.Application = function() {
	
    var that = this;
    
    var flytedstage = null;
    
    var flyted = null;
	
	this.initQueryTable = function() {
        var content = "<p><label for=\"stage\">Stage:&nbsp;</label>";
         
        content += "<select id=\"select\">";
        
        content += "<option value=\"Ejaculatory_duct\">Ejaculatory duct</option>";
        
        content += "<option value=\"Male_accessory_gland\">Male accessory gland</option>";
        
        content += "<option value=\"Seminal_vesicle\">Seminal vesicle</option>";
        
        content += "<option value=\"Testicular_duct\">Testicular duct</option>";
        
        content += "<option value=\"Hub_cell\">Hub cell</option>";
        
        content += "<option value=\"Cyst_cell\">Cyst cell</option>";
        
        content += "<option value=\"Cyst_cell_of_spermatid\">Cyst cell of spermatid</option>";
        
        content += "<option value=\"Primary_spermatocyte\">Primary spermatocyte</option>";
        
        content += "<option value=\"Early_primary_spermatocyte\">Early primary spermatocyte</option>";
        
        content += "<option value=\"Mid_primary_spermatocyte\">Mid primary spermatocyte</option>";
        
        content += "<option value=\"Late_primary_spermatocyte\">Late primary spermatocyte</option>";
        
        content += "<option value=\"Secondary_spermatogonium\">Secondary spermatogonium</option>";
        
        content += "<option value=\"Waste_bag\">Waste bag</option>";
        
        content += "<option value=\"Elongation-stage_spermatid\">Elongation stage spermatid</option>";
        
        content += "<option value=\"Early_elongation-stage_spermatid\">Early elongation stage spermatid</option>";
        
        content += "<option value=\"Early-mid_elongation-stage_spermatid\">Early-mid elongation stage spermatid</option>";
        
        content += "<option value=\"Mid_elongation-stage_spermatid\">Mid elongation stage spermatid</option>";
        
        content += "<option value=\"Mid-late_elongation-stage_spermatid\">Mid-late elongation stage spermatid</option>";
        
        content += "<option value=\"Late_elongation-stage_spermatid\">Late elongation stage spermatid</option>";
        
        content += "<option value=\"Terminal_epithelial_cell\">Terminal epithelial cell</option>";
        
        content += "<option value=\"Vas_deferens\">Vas deferens</option>";
        
        content += "<option value=\"Comet-like_pattern_of_distal_end_of_elongating_spermatids\">Comet-like pattern of distal end of elongating spermatids</option>";
        
        content += "<option value=\"Cup-like_pattern_of_distal_end_of_elongating_spermatids\">Cup-like pattern of distal end of elongating spermatids</option>";
        
        content += "<option value=\"No_staining\">No staining</option>";
        
        content += "<option value=\"Junctions_between_early_primary_spermatocyte_cells\">Junctions between early primary spermatocyte cells</option>";
        
        content += "</select>&nbsp;";
         
        content += "</p>";                    
	     
        $(".stage").html(content);
    };
	
    
    // called by this.init
    
    this.initFlytedStage = function() {
        try {
            
            flykit.info("instantiate a service for the flyted development stage widget");
            var service = new flykit.flytedstage.Service("../../../conf/flyted");
            
            flykit.info("instantiate a renderer for the flyted development stage widget");
            var renderPane = $("#flytedDevelopmentStageWidget");

            var renderer = new flykit.flytedstage.GeneWidget.DefaultRenderer();
            renderer.setCanvas(renderPane);
            
            flykit.info("instantiate a flyted development stage widget");
            flytedstage = new flykit.flytedstage.GeneWidget(service, renderer);
            
            
        } catch (error) {
            flykit.apps.expressionmashup.handleError(error);
        }
    };
    
    // called by this.init
    
    this.initFlyted = function() {
        try {
            
            flykit.info("instantiate a service for the flyted development stage widget");
            var service = new flykit.flyted.Service("../../../conf/flyted");
            
            flykit.info("instantiate a renderer for the flyted development stage widget");
            var renderPane = document.getElementById("flytedSimpleBatchImageWidget");
            var renderer = new flykit.flyted.SimpleBatchWidget.DefaultRenderer();
            renderer.setCanvas(renderPane);
            
            flykit.info("instantiate a flyted image widget");
            flyted = new flykit.flyted.SimpleBatchWidget(service, renderer);
            
        } catch (error) {
            flykit.apps.expressionmashup.handleError(error);
        }
    };
    
   
    
    this.init = function() {
        var context = "flykit.apps.bydevelopmentstage.Application.init";
        try {
    		
    		var logReader = new YAHOO.widget.LogReader("logger");

            this.initQueryTable();
            this.initFlytedStage();
            this.initFlyted();
            
            flykit.debug("hook form submission to widget call", context);
            YAHOO.util.Event.addListener("queryForm", "submit", this.onFormSubmitted);
            
            // attach gene selection listener to genefinder
            flytedstage.subscribe("GENESELECTED", this.onGeneSelected, null);

    		flykit.debug("application loaded, showing main pane", context);
    		$("#loadingPane").hide();
    		$("#applicationPane").show();
    		
        } catch (error) {
            flykit.apps.bydevelopmentstage.handleError(error);
        }
	};
	
	
	
	this.onFormSubmitted = function(event) {
        var context = "flykit.apps.bydevelopmentstage.Application.onFormSubmitted";
        try {
            flykit.debug("form submitted", context);
            flykit.debug("hide the flyted image widget", context);
            $("#flytedSimpleBatchImageWidget").hide();
            var profile = new Array();
            flykit.debug("build profile object" + profile.length, context);
            
            $(".stage").each(function(index) {
                profile[profile.length] = $(this).find("select").val();
                
                flykit.debug("build profile "+index+": " + profile[index], context);
            });
            
            
           flykit.debug("number the tissue profile: " + profile.length);
            
           that.doQuery(profile);
            
        } catch (error) {
            flykit.apps.bydevelopmentstage.handleError(error);
        }
	    
	};

    this.doQuery = function(profile) {
        var context = "flykit.apps.bydevelopmentstage.Application.doQuery";
        try {
            $("#errorMessage")
               .hide()
               .html("this should never be visible");
            
            flykit.debug("initiate request for probes and genes by profile", context);
            
            flytedstage.findProbesExpressedInStageBatch(profile);
            
        } catch (error) {
            flykit.apps.bydevelopmentstage.handleError(error);
        }
    };

    
    this.onGeneSelected = function(type, args) {
        var _context = "_inOnGeneSelected, application";
        try {
            var genes = args[0];
            flykit.debug("gene selected: "+genes, _context);
            
            flyted.findImagesByGeneBatch(genes);
            
            $("#flytedSimpleBatchImageWidget").show();
            
        } catch (error) {
            flykit.apps.expressionmashup.handleError(error);
        }
    };  
    	
	this.init();
};

flykit.apps.bydevelopmentstage.handleError = function(error) {
    flykit.err(error.name+" :: "+error.message);
    $("#errorMessage")
       .html("an error has occurred, please see the log for more details")
       .show();
};
    

// on dom ready, instantiate application
YAHOO.util.Event.onDOMReady(function() {new flykit.apps.bydevelopmentstage.Application();});
