

// finds a good spot on the screen to position the new window
function windowpos() {
    var x;
    var y;
    if (window.screenLeft) {  //IE
         x = window.screenLeft + 10 + Math.floor(Math.random()*41);
         y = window.screenTop + 10 + Math.floor(Math.random()*41);
         y = y - 100;
    } else if (window.screenX) {
         x = window.screenX + 10 + Math.floor(Math.random()*41);
         y = window.screenY + 10 + Math.floor(Math.random()*41);
    } else {
         x = 10 + Math.floor(Math.random()*41);
         y = 10 + Math.floor(Math.random()*41);
    }
    var pos = ",left="+x+",top="+y+",";
    //alert(pos);
    return pos;
}

function focus(win) {
  try {
     if (win) {win.focus();}
  } catch (e) {

  }
}


function base_href() {
 var b = document.getElementsByTagName('base');
 if (b && b[0] && b[0].href) {
   return b[0].href;
 }
 return '';
}

/// used for new window
function new_window(url) {
  nw = window.open(url, "_blank", 'width=800,height=650,directories=yes,toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes'+windowpos());
  focus(nw);
  return false;
}

/// used for pop ups
function popup(url) {
  nw = window.open(url,'_blank', 'width=770,height=450,resizable=yes,menubar=yes,location=yes,toolbar=yes,scrollbars=yes'+windowpos());
  focus(nw);
  return false;
}

function new_db_window(url) {
  nw = window.open(url, "_blank", 'width=800,height=650,directories=yes,toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes'+windowpos());
  focus(nw);
  return false;
}

function new_db_window2(url) {
  // a slightly offset database window used on pages like this
  // http://webstage.library.hbs.edu/intra/go/venturexpert.html
  nw = window.open(url, "_blank", 'width=800,height=650,directories=yes,toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes'+windowpos());
  focus(nw);
  return false;
}

/// used for print pop ups
function printpop(url) {
  nw = window.open(url, '_blank', 'width=650,height=500,resizable=yes,location=yes,toolbar=yes,menubar=yes,scrollbars=yes'+windowpos());
  focus(nw);
  return false;
}


/// used for hc pop ups
function hcpopup(url) {
  nw = window.open(url, '_blank', 'width=470,height=550,resizable=yes,location=yes,toolbar=no,menubar=no,scrollbars=yes'+windowpos());
  focus(nw);
  return false;
}

function database_popup() {
    return new_db_window(this.href);
}

function pdf_popup() {
    //register_click_a(this);
    return new_db_window(this.href);
}

function print_popup() {
    //register_click_a(this);
    return printpop(this.href);
}

function hc_popup() {
    //register_click_a(this);
    return hcpopup(this.href);
}

function std_popup() {
    //register_click_a(this);
    return popup(this.href);
}

function std_newwindow() {
    //register_click_a(this);
    return new_window(this.href);
}

// turns all database links into popup windows
// and PDF links to new windows

function rewrite_links() {    
    var links = document.getElementsByTagName('a');
    for (var x = 0;x < links.length; x++) {
        a = links[x];
        if (!a.href) {continue;}
        var dbpopup = false;
        if (a.href.indexOf('hul.eresource') > -1 ) { dbpopup = true; }
        if (a.href.indexOf('/bkr_cgi/database/') > -1 ) { dbpopup = true; }
        if (a.href.indexOf('http://voyager.library.hbs.edu/') > -1 ) { dbpopup = true; }
        if (a.href.indexOf('holliscatalog.harvard.edu') > -1 ) { dbpopup = true; }
        if (a.href.indexOf('tdnet.com') > -1 ) { dbpopup = true; }
        if (a.href.indexOf('hul.eresource') > -1 ) { dbpopup = true; }
        if (a.href.indexOf('lewis.hul.harvard.edu') > -1 ) { dbpopup = true; }
        if (a.href.indexOf('e-research.lib.harvard.edu') > -1 ) { dbpopup = true; }
        if (a.href.indexOf('ezp1.harvard.edu') > -1 ) { dbpopup = true; } //also true for tdnet above
        if (a.href.indexOf('/bkr_cgi/voy') > -1 ) { dbpopup = true; }
        if (a.href.indexOf('/bkr_cgi/hollis') > -1 ) { dbpopup = true; }
        if (a.href.indexOf('/catalog/') > -1 ) { dbpopup = true; }
        if (dbpopup) { 
           a.className += " offsite";  //add stats
           a.onclick = database_popup; 
        }

        var pdf = false;
        if (a.href.search(/\.pdf$/) > -1) {pdf = true;}
        if (pdf) {
            if (a.className.indexOf('pdf') == -1) { a.className += ' pdf'; }   //add stats
            a.className += ' offsite';
            a.onclick = pdf_popup;
        }

        if      (a.className.indexOf('print') > -1 && a.className.indexOf('popup')) { a.onclick = print_popup; }
        else if (a.className.indexOf('hc') > -1 && a.className.indexOf('popup')) { a.onclick = hc_popup; }
        else if (a.className.indexOf('popup') > -1) { a.onclick = std_popup; }
        else if (a.className.indexOf('newwin') > -1) { a.onclick = std_newwindow; }
    }
}