function tog(id) {
  if ($(id).style.display == 'none') {
    $(id).style.display = 'block';
  } else {
    $(id).style.display = 'none';
  }
}

function popSearchHelp() {
  var p = "<h2>Searching help and hints:</h2><ul id='searchHelp'><li>Simple logic operators can be used to connect the search terms: <ul><li><b>AND:</b> both terms must be present (for example: <i>human</i> <b>AND</b> <i>genome</i>). This is the default mode.</li><li><b>OR:</b> either of the terms must be present (for example: <i>human</i> <b>OR</b> <i>sapiens</i>)</li><li><b>AND NOT:</b> second term must not be present (for example: <i>vertebrata</i> <b>AND NOT</b> <i>mammalia</i>)</li></ul></li><li>Look for phrases using double quotes (for example: <b>\"</b><i>gene expression</i><b>\"</b>)</li><li>Use the advanced options to adjust the number of results shown and change the sorting options</li></ul>";
  return overlib(p,WRAP,CAPTION, "Help",CLOSECLICK,STICKY);
}

function expTutCat(id) {
  //  if ($$('.tutC' + id).getStyle('display') == 'none') {
  if ($('bttutC' + id).hasClass('fmin')) {
    $('bttutC' + id).removeClass('fmin');
    $$('.tutC' + id).setStyle('display', 'none');
  } else {
    $('bttutC' + id).addClass('fmin');
    $$('.tutC' + id).setStyle('display', '');
  }
}

function toggleAllTut() {
  if ($('expAll').innerHTML == 'Expand all') {
    $('expAll').innerHTML = 'Collapse all';
    $('tutorials').getElements('tr').setStyle('display', '');
    $('tutorials').getElements('a[class=fbut]').each(function(item){item.addClass('fmin');});
    $$('.fbut').each(function(item){item.addClass('fmin');});

  } else {
    $('expAll').innerHTML = 'Expand all';
    $('tutorials').getElements('tr[class!=tutCatS]').setStyle('display', 'none');
    $('tutorials').getElements('a[class!=fbut]').each(function(item){item.removeClass('fmin');});
  }
}

function expRes(id) {
  if ($(id).style.display == 'none') {
    $('bt' + id).innerHTML = '[-]';
    $(id).style.display = 'block';
  } else {
    $('bt' + id).innerHTML = '[+]';
    $(id).style.display = 'none';
  }
}

function recentQuery() {
  var rImg = new Request( {url: document.getElementsByTagName('base')[0].href + '/cgi/ajax/recentQueries.cgi' , onFailure: ajaxError, onSuccess: popRecent}).send();
}
function popRecent(res) {
  return overlib(res, WRAP,CAPTION,'Recent queries',STICKY,CLOSECLICK);
}

function favQuery() {
  var rImg = new Request( {url: document.getElementsByTagName('base')[0].href + '/cgi/ajax/favoriteQueries.cgi' , onFailure: ajaxError, onSuccess: popFavs}).send();
}
function popFavs(res) {
  return overlib(res, WRAP,CAPTION,'Favorite queries',STICKY,CLOSECLICK);
}
function addCart(ref) {
  nwn("http://www.openhelix.com/cgi-bin/ccp51/cp-app.cgi?&pg=prod&ref=" + ref);
}
function nwn(url) {
   window.open(url,'OHpop','');
   //   window.open(url,'OHpop','toolbar=no,menubar=no,resizable=no,scrollbars=no,height=200,width=300');
   OHpop.focus();
}
function popTut(id) {
  if ($('lnCw') && $('lnCw').checked) {
    document.location = document.getElementsByTagName('base')[0].href + 'cgi/showMovie.cgi?id=' + id;
  } else {
    window.open(document.getElementsByTagName('base')[0].href + 'cgi/showMovie.cgi?id=' + id, 'OHtutorial', 'width=1035,height=710,resizable=1,scrollbars=1');
  }
}

function ajaxError() {
  return overlib('<span style="background: #faa;font-size: 18px">An error occured in the AJAX request to the server.</span>', WRAP, TIMEOUT, 4000);
}

function initSrcPg() {
  lnToggle();
  resSlide();
  if (pQry) {
    $('qField').value = pQry;
    $('oq').value = pQry;
  }
}
function resSlide() {
  $$('.ftgt').each(function(el){
      el.setStyle('display', 'none');
    });
  $$('.fbut','.fbuts').each(function(el){
      el.addEvent('click', function(e) {
          e=new Event(e);
          var dst = $(e.target.rel);
          if (dst.getStyle('display') == 'none') {
            dst.setStyle('display','block');
            if (e.target.hasClass('fbuts')) {
              e.target.addClass('fmins');
            } else {
              e.target.addClass('fmin');
            }
          } else {
            dst.setStyle('display','none');
            e.target.removeClass('fmins');
            e.target.removeClass('fmin');
          }
          e.stop();
        });
    });
}
function tTog() {
  $$('.ftgt').each(function(el){
      el.setStyle('display', 'none');
    });
  $$('.fbut','.fbuts').each(function(el){
      el.addEvent('click', function(e) {
          e=new Event(e);
          var dst = $(e.target.rel);
          if (dst.getStyle('display') == 'none') {
            dst.setStyle('display','block');
            if (e.target.hasClass('fbuts')) {
              e.target.addClass('fmins');
            } else {
              e.target.addClass('fmin');
            }
          } else {
            dst.setStyle('display','none');
            e.target.removeClass('fmins');
            e.target.removeClass('fmin');
          }
          e.stop();
        });
    });
}

function gotoPage(c) {
  var l = 'q=' + $('oq').value + '&amp;o=' + c + '&amp;p=' + $('pPg').value + '&amp;s=' + $('sTyp').value + '&amp;lt=' + $('lnTyp').value; 
  document.location = document.getElementsByTagName('base')[0].href + 'cgi/search.cgi?' + l;
}

function lnToggle() {
  var tgtR = '';
  var tgtO = '';
  if ($('lnNw').checked) {
    tgtR = 'ohelixResWin';
    tgtO = 'ohelixHelWin';
    $('lnTyp').value = 2;
  } else {
    $('lnTyp').value = 1;
  }

  var lns =   $('srchRes').getElements('a');
  if ($('sponsHd')) { lns = lns.concat($('sponsHd').getElements('a'));}
  lns.each(function(el){
      if (el.rel == 'lR') {
        el.target = tgtR;
        el.addEvent('click', offToRes);
      } else if (el.rel == 'oH') {
        el.target = tgtO;
      }
    });
}
function offToRes(ev) {
  if ($('lnNw') && $('lnNw').checked) {
   var rq = new Request( {method: 'get', url: document.getElementsByTagName('base')[0].href + '/cgi/ajax/toRes.cgi', data: 'r=' + ev.target.rev, onFailure: function(){window.open(ev.target.href, 'ohelixResWin');}, onSuccess: function(){window.open(ev.target.href, 'ohelixResWin');}}).send();
  } else {
    var rq = new Request( {method: 'get', url: document.getElementsByTagName('base')[0].href + '/cgi/ajax/toRes.cgi', data: 'r=' + ev.target.rev, onFailure: function(){ document.location = ev.target.href;}, onSuccess: function(){document.location = ev.target.href;}}).send();
  }
  return false;
}
function offToOh(ev) {
  if (!ev.target.rev) {return;}
  if ($('lnNw') && $('lnNw').checked) {
    new Request( {method: 'get', url: document.getElementsByTagName('base')[0].href + '/cgi/ajax/toOH.cgi', data: 'r=' + ev.target.rev, onFailure: function(){window.open(ev.target.href, 'ohelixResWin');}, onSuccess: function(){window.open(ev.target.href, 'ohelixResWin');}}).send();
  } else {
    new Request( {method: 'get', url: document.getElementsByTagName('base')[0].href + '/cgi/ajax/toOH.cgi', data: 'r=' + ev.target.rev, onFailure: function(){document.location = ev.target.href;}, onSuccess: function(){document.location = ev.target.href;}}).send();
  }
  return false;
}
function othToRes(ev,nw) {
  var tgt = ev.target;
  if (tgt.nodeName != 'A') { tgt = tgt.parentNode;}
  if (!nw) {
    new Request( {method: 'get', url: document.getElementsByTagName('base')[0].href + '/cgi/ajax/toRes.cgi', data: 'r=' + tgt.rev, onFailure: function(){window.open(tgt.href, 'ohelixResWin');}, onSuccess: function(){window.open(tgt.href, 'ohelixResWin');}}).send();
  } else {
    new Request( {method: 'get', url: document.getElementsByTagName('base')[0].href + '/cgi/ajax/toRes.cgi', data: 'r=' + tgt.rev, onFailure: function(){document.location = tgt.href;}, onSuccess: function(){document.location = tgt.href;}}).send();
  }
 return false;
}

function selectTab (i) {
  $$('tabNav','.active').removeClass('active');
  $('tbtn'+i).addClass('active');
  $$('main','.dblks').setStyle("display","none");
  $('tblk'+i).setStyle("display",'block');
  
}
function initButPop() {
  $$('.buyBtn').each(function(item){
                      item.removeEvents('mouseover');
                      item.removeEvents('mouseout');
                      item.addEvent('mouseover', function(ev){
                          return overlib("<nobr><b style='font-size:14px'>Buy now using Google Checkout</b></nobr>",WRAP);
                        }.bind(this));
                      item.addEvent('mouseout', function(ev){
                          return nd();
                        }.bind(this));
                    }.bind(this));
}
function surPop() {
  if (!sln) { return; }
  window.setTimeout(showSur, 5000);
}

function showSur() {
  return overlib("<div id='srvy'><h1>" + shd + "</h1><p>" + sde + "</p><p><a href='" + sln + "' onclick='cClick()' target='_new'>Open survey in a new window</a><br/><a href='" + sln + "' onclick='cClick()'>Open survey in current window</a></p></div>", CAPTION, 'OpenHelix User Survey',DRAGGABLE, CLOSECLICK, WRAP, STICKY,TEXTPADDING, 6,REF,'persBox', REFC,'LR', REFP,'UR');
}

function initRnd() {
  Nifty("div.rnd", "normal transparent");
}
window.addEvent('domready', initRnd);
