//Encapsulate inside a namespace
var microarray = new function() {

  //
  // Private stuff
  //
  var rp_ajax_species_list = "";
  var atlas_ajax_species_list = "";
  var current_form = "";

  function setRpForm() {
    var form = '<table style="border:none" summary="Layout of fields">'+
                  '<tr>'+
                    '<td style="width:150px"><input id="aeexp" name="aeexp" title="e.g. leukemia" type="text" style="width:100%" size="20"/></td>'+
                    '<td><label for="aeexp">&nbsp;Keyword</label></td>'+
                  '</tr>'+
                  '<tr>'+
                    '<td style="width:150px"><select id="aespecies" name="aespecies" title="Select your species" style="width:100%">'+
                        '<option value="">Loading...</option></select>'+
                    '</td>'+
                    '<td><label for="aespecies">&nbsp;Species</label></td>'+
                  '</tr>'+
                  '<tr><td>&nbsp;</td></tr>'+
                '</table>';

    $('#ae_dynamic').html(form);
    setSpecies();
    current_form="rp";
    // Enter key searching on text fields
    $('#aeexp').keyup(function(e) {
      if(e.keyCode == 13) {
        microarray.ae();
      }
    });
  }

  function setAtlasForm() {
    var form = '<table style="border:none" summary="Layout of fields">'+
                  '<tr>'+
                    '<td style="width:150px"><input id="aegene" name="aegene" title="e.g. ASPM" type="text" style="width:100%" size="20"/></td>'+
                    '<td><label for="aegene" id="aegene_label">&nbsp;Genes</label></td>'+
                  '</tr>'+
                  '<tr>'+
                    '<td><select id="ae_atlas_updw" name="ae_atlas_updw" style="width:100%">'+
                        '<option value="UP_DOWN">up/down in</option>'+
                        '<option value="UP">up in</option>'+
                        '<option value="DOWN">down in</option>'+
                      '</select>'+
                    '</td>'+
                    '<td><label for="ae_atlas_updw">&nbsp;</label></td>'+
                  '</tr>'+
                  '<tr>'+
                    '<td><input id="aeexp" name="aeexp" title="e.g. liver" type="text" style="width:100%" size="20"/></td>'+
                    '<td><label for="aeexp">&nbsp;Conditions</label></td>'+
                  '</tr>'+
                  '<tr>'+
                    '<td><select id="aespecies" name="aespecies" title="Select your species" style="width:150px">'+
                        '<option value="">Loading...</option></select>'+
                    '</td>'+
                    '<td><label for="aespecies">&nbsp;Species</label></td>'+
                  '</tr>'+
                  '<tr><td>&nbsp;</td></tr>'+
                '</table>';

    $('#ae_dynamic').html(form);
    setSpecies();
    current_form="atlas";
    // Enter key searching on text fields
    $('#aegene, #aeexp').keyup(function(e) {
      if(e.keyCode == 13) {
        microarray.ae();
      }
    });
  }

  function setSpecies() {
      // empty previous settings
      $('#aespecies').html('');

      if ($('#aedb').val()=="atlas") {
         if (atlas_ajax_species_list != "") {
            $('#aespecies').html(atlas_ajax_species_list);
         }
         else {
            // Load atlas species
            $('#aespecies').html('<option value="">Loading...</option>');
            $('#aespecies').attr("disabled","true");
            $.ajax({
                url: '/gxa/list-species.jsp',
                type: 'GET',
                dataType: 'html',
                timeout: 5000,
                error: function() {
                    $('#aespecies').html('<option>Error getting atlas species list</option>');
                },
                success: function(data) {
                    atlas_ajax_species_list = data;
                    $('#aespecies').html(atlas_ajax_species_list);
                    $('#aespecies').removeAttr("disabled");
                }
            });
         }
      }
      else {
         if (rp_ajax_species_list != "") {
            $('#aespecies').html(rp_ajax_species_list);
         }
         else {
            // Load archive species
            $('#aespecies').html('<option value="">Loading...</option>');
            $('#aespecies').attr("disabled","true");
            $.ajax({
                url: '/microarray-as/ae/servlets/query/species-select/html',
                type: 'GET',
                dataType: 'html',
                timeout: 5000,
                error: function() {
                    $('#aespecies').html('<option>Error getting archive species list</option>');
                },
                success: function(data) {
                    rp_ajax_species_list = data;
                    $('#aespecies').html(rp_ajax_species_list);
                    $('#aespecies').removeAttr("disabled");
                }
            });
         }
      }
  }



  //
  // Public stuff
  //
  return {

    // Used for submitting search query to appropriate database
    ae:function() {
       var db = $('#aedb').val();
       var expr = encodeURIComponent($('#aeexp').val());
       var spec = encodeURIComponent($('#aespecies').val());

       if (db=="rp") {
          if (spec=="") {
             if (expr=="") {
                document.location.href="/microarray-as/ae/browse.html";
             }
             document.location.href="/microarray-as/ae/browse.html?keywords="+expr;
          }
          else {
             document.location.href="/microarray-as/ae/browse.html?keywords="+expr+"&species="+spec;
          }
       }
       else {
         var gene = encodeURIComponent($('#aegene').val());
         var updw = encodeURIComponent($('#ae_atlas_updw').val());
         document.location.href="/gxa/qrs?gprop_0=&fact_0=&gval_0="+gene+"&fexp_0="+updw+"&specie_0="+spec+"&fval_0="+expr;
       }
    },

    // Called to setup dynamic query form 
    setForm:function() {
      if ($('#aedb').val()=="rp" && current_form!="rp") {setRpForm();}
      if ($('#aedb').val()=="atlas" && current_form!="atlas") {setAtlasForm();}
    }

  };

}

