// Javascript library relating to pop-ups
// last changed 8/29/06

var defs = new Array();
defs.phase1 = "BACs in phase 1...";
defs.phase2 = "BACs in phase 2...";
defs.phase3 = "BACS in phase 3...";
defs.mapUMN = "Based on University of Minnesota map.";
defs.mapUCD = "Based on University of California - Davis map.";
defs['bm_evidenceSSRBS'] = "SSRBS definition";
defs['bm_evidenceDN+SSRBS'] = "DN_SSRBS definition";
defs['lg_confidenceCC'] = "CC definition";
defs['lg_confidenceOVERLAP'] = "OVERLAP definition";
var defline = "no definition given";
//alert("defs = "+defs+" of length "+defs.length);


//////////// Are we using the loathsome IE? ////////
var IE = document.all?true:false

////////////// Mouse capture code //////////////////

// If Mozilla -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;
var mouse_X = 0
var mouse_Y = 0

// Retrieve mouse pos.
function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    mouse_X = event.clientX + document.body.scrollLeft
    mouse_Y = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    mouse_X = e.pageX
    mouse_Y = e.pageY
  }  
  // catch possible negative values in NS4
  if (mouse_X < 0){mouse_X = 0}
  if (mouse_Y < 0){mouse_Y = 0}  
  return true
}


/////// Functions to hide and show popups ////////

function show(obj) { 
  document.getElementById(obj).style.display="block"; 
}
function hide(obj) { 
  document.getElementById(obj).style.display="none"; 
}

// Handle a chromosome link
function chrLink(chr) {
  document.getElementById('linkbox').style.top = adjustY(mouse_Y-50, 120);
  document.getElementById('linkbox').style.left = adjustX(mouse_X+15, 120);
  document.contigviewerform.chromosome.value = "chr"+chr;
  document.bacreportform.chr.value = chr;
  document.geneticemapform.lg.value = chr;
  document.assemblytable.chr.value = chr;
  show('linkbox');
}

// Handle a BAC popup link
function popupBACLinks(acc, ver, clone) {
//  hide('defbox');
  document.getElementById('linkbox').style.top = adjustY(mouse_Y-50, 170);
  document.getElementById('linkbox').style.left = adjustX(mouse_X+30, 120);

  document.genbankform.val.value = acc;
  document.lisform.seq_name.value = acc + "." + ver;

//  document.ensemblform.clone.value = acc + "." + ver;
  document.ouform.name.value = acc;
  document.tigrform.name.value = acc + "." + ver;


  if (acc.length > 0) {
    document.bacform.search.value = acc;
  } else {
    document.bacform.search.value = clone;
  }
  document.gabform.accession.value = acc;
  document.passportform.bac_acc.value = acc;

  document.annotationform.bac_acc.value = acc;


  show('linkbox');
}

// Handle a marker popup link
function popupMarkerLinks(marker) {
//  hide('defbox');
  document.getElementById('markerlinkbox').style.top = adjustY(mouse_Y-50, 100);
  document.getElementById('markerlinkbox').style.left = adjustX(mouse_X+30, 120);
  document.ucdavisform.marker.value = marker;
  document.markerform.search.value = marker;
  show('markerlinkbox');
}

// Handle a contig popup link
function popupContigLinks(contig) {
//  hide('defbox');
  document.getElementById('contiglinkbox').style.top = adjustY(mouse_Y-50, 100);
  document.getElementById('contiglinkbox').style.left = adjustX(mouse_X+30, 120);
  document.ucdavisformB.ref_map_aids.value = "MtCookKimUCDavisMtGenomev3_"+contig;
  document.contigform.search.value = contig;
//  document.urmelform.contig_id.value = contig;
  show('contiglinkbox');
}


function popupDesc(which) {
  hide('linkbox');
  if (defs[which]) {
    document.defform.definition.value = defs[which];
  } else {
    document.defform.definition.value = "No definition.";
  }
  document.getElementById('defbox').style.top = adjustY(mouse_Y-150, 300);
  document.getElementById('defbox').style.left = adjustX(mouse_X+20, 200);
  show('defbox');
}


function adjustY(y, height) {
  if (IE) {
    scroll_y = document.body.scrollTop;
    max_y = document.body.clientHeight;
  } else {
    scroll_y = window.pageYOffset;
    max_y = window.innerHeight;
  }
  top_y = y - scroll_y;
  bottom_y = top_y + height;
//alert("top y: "+top_y+", bottom y: "+bottom_y+", max y: "+max_y+", scroll y: "+scroll_y);
  if (bottom_y > max_y) top_y -= (bottom_y - max_y);
  if (top_y < 0) top_y = 0;
  new_y = top_y + scroll_y;
//alert("new y: "+new_y);
  return new_y;
}

function adjustX(x, width) {
  if (IE) {
    scroll_x = document.body.scrollLeft;
    max_x = document.body.clientWidth;
  } else {
    scroll_x = window.pageXOffset;
    max_x = window.innerWidth;
  }
  left_x = x - scroll_x;
  right_x = left_x + width;
//alert("top y: "+top_y+", bottom y: "+bottom_y+", max y: "+max_y+", scroll y: "+scroll_y);
  if (right_x > max_x) left_x -= (right_x - max_x);
  if (left_x < 0) left_x = 0;
  new_x = left_x + scroll_x;
//alert("new y: "+new_y);
  return new_x;
}


function popupLinkTo(linkname, formname) {
  if (linkname == 'assembly') {
    openContigViewerApplet(document.contigviewerform.chromosome.value);
  } else if (linkname == 'assemblytable') {
//    window.open("assembly_table.php", "", "width=950,height=450,scrollbars=yes,toolbar=yes");
    window.document.location = "assembly_table.php", "", "width=950,height=450,scrollbars=yes,toolbar=yes";
  } else {
    document[formname].submit();
  }
  hide('linkbox');
}


function openContigViewerApplet(chr) {
//    window.open("/cgi-bin/genome/contigviewer_CGI_BAC_updater.pl?chromosome="+chr, 
//                "", "width=950,height=510,resizable");
    window.open("contig_viewer.php?chromosome="+chr, 
                "", "width=1200,height=630,resizable");
}


function displayDesc() {
  document.write(defline);
}


