

// finds a good spot on the screen to position the new window
function windowpos() {
    trace('window pos');
    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);
    trace('finished pos');
    return pos;
}

function focus(win) {
  trace('begin focus');
  try {
     if (win) {win.focus();}
  } catch (e) {

  }
  trace('focus done');
}


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) {
  trace('new_window');
  analytics.view(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) {
  trace('popup');
  analytics.view(url);
  nw = window.open(url,'_blank', 'width=750,height=450,resizable=yes,menubar=yes,location=yes,toolbar=yes,scrollbars=yes'+windowpos());
  focus(nw);
  return false;
}

function new_db_window(url) {
  trace('new_db_window');
  analytics.view(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) {
  trace('new_db_window2');
  // a slightly offset database window used on pages like this
  // http://webstage.library.hbs.edu/intra/go/venturexpert.html
  analytics.view(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 print pop ups
function printpop(url) {
  trace('new_db_window');
  analytics.view(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) {
  trace('hcpopup');
  analytics.view(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() {
    trace('database_popup');
    return new_db_window(this.href);
}

function pdf_popup() {
    trace('pdf_popup');
    //register_click_a(this);
    return new_db_window(this.href);
}

function print_popup() {
    trace('print_popup');
    //register_click_a(this);
    return printpop(this.href);
}

function hc_popup() {
    trace('hc_popup');
    //register_click_a(this);
    return hcpopup(this.href);
}

function std_popup() {
    trace('std_popup');
    //register_click_a(this);
    return popup(this.href);
}

function std_newwindow() {
    trace('std_newwindow');
    //register_click_a(this);
    return new_window(this.href);
}


function is_offsite_link(a) {
   var thisdomain = document.location.host;
   var linkdomain = a.host.replace(/:\d+/,'');
   if (linkdomain && linkdomain != thisdomain) {
       return 1
   } else {
       return 0;
   }
}

// turns all database links into popup windows
// and PDF links to new windows

function rewrite_links() {
    trace("performing 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; }
        
        if (is_offsite_link(a) && !a.onclick) {
            a.onclick = function() {
                var link = this.href;
                analytics.view(link,function(){document.location=link});
                return false;
            }
        }
    }
}