/* it requires ete.js and jquery loaded */

/* phylomeDB application is a modified ete WebPlugin Application. The
   application uses WSGI and it contains all ete methods + phylomeDB
   methods. */

var global_sid = ""

// This overwrites ete default URL config 
var ete_webplugin_URL = "/wsgi/phylomedb.py";

function get_phylomedb_tree(seqid, phyid, method, tree_features){
  var treeid = random_tid();
  tree2recipient[treeid]="#phylomedb_tree_img";
  if (phyid == undefined){
    var phyid = "";
  }
  if (method == undefined){
    var method = "";
  }
  $("#img").html('<img src="loader.gif">');
  $("#img").load(ete_webplugin_URL+'/search_tree', {"seqid": seqid, "phyid": phyid, "method": method, "tree_features": tree_features, "treeid": treeid, "sid": global_sid});
}

function load_orthologs(safe_newick, target){
  $("#orthologs").html('<img src="loader.gif">');
  $("#orthologs").load(ete_webplugin_URL+'/html_orthologs', {"safe_newick":safe_newick, "target_seed":target, "sid": global_sid});
}

function fuzzy_search(query){
  $("#img").html('<img src="loader.gif">');
  $("#img").load(ete_webplugin_URL+'/fuzzy_search', {"fuzzy_term": query, "sid": global_sid});
}

function get_phylomedb_alg(seqid, phyid, alg_type, recipient){
  $(recipient).html('<img src="loader.gif">')
  $(recipient).load(ete_webplugin_URL+'/search_alg', {"seqid": seqid, "phyid": phyid, "alg_type": alg_type,  "sid": global_sid});
}

function get_phylomedb_seqinfo(seqid){
  $("#phylomedb_seqinfo").html('<img src="loader.gif">');
  $("#phylomedb_seqinfo").load(ete_webplugin_URL+'/seqid_info', {"seqid": seqid, "sid": global_sid});
}

function load_phylomes_list(target){
  $(target).html('<img src="loader.gif">')
  $(target).load(ete_webplugin_URL+'/phylomes_list', {"sid": global_sid});
}

function load_proteomes_info(phyid, target){
  $(target).html('<img src="loader.gif">')
  $(target).load(ete_webplugin_URL+'/proteomes_list', {"phyid":phyid, "sid": global_sid});
}

function download_data(seqid, phyid, target){
  $.post(ete_webplugin_URL+'/download_data', {"sid": global_sid, "seqid": seqid, "phyid":phyid}, function(data){
      window.open(data);
  });

}

function link_to_info(seqid){
  window.location="?q=node/3&seqid="+seqid;
}

function link_to_trees(seqid){
  window.location="?q=node/1&seqid=" + seqid ;
}

function load_proteomes_list(phyid){
  return 0;
}

function load_blast_hits(sequence, recipient){
  $(recipient).html('<img src="loader.gif">')
  $(recipient).load(ete_webplugin_URL+'/blast_hits', {"sid": global_sid, "sequence":sequence});
}

function load_phylome_browser(phyid, start, offset, recipient){
  $(recipient).html('<img src="loader.gif">');
  $(recipient).load(ete_webplugin_URL+'/browse_phylome', {"phyid": phyid, "sid": global_sid, "browser_id_start":start, "browser_id_length": offset});
}

function set_tree_features(seqid, phyid, method){
  var allVals = [];
  $("input[name=tree_feature_selector]").each(function(){
      if ($(this).is(":checked")){
          allVals.push($(this).val());
      }
    });
  get_phylomedb_tree(seqid, phyid, method, allVals.join(","))
}

function search_in_tree(treeid, search_index_action){
  var term = $("#ete_search_target").val() +  "::" + $("#ete_search_term").val();
  run_action(treeid, "", search_index_action, term);
}

function show_tree_box(e, boxname) {
  $(boxname).css('left',e.pageX-2 );
  $(boxname).css('top',e.pageY-2 );
  $(boxname).css('position',"absolute" );
  $(boxname).css('background-color',"#fff" );
  $(boxname).draggable({ handle: '.tree_box_header' });
  $(boxname).show();
}


