// finds a good spot on the screen to position the new window
function windowpos() {
    var x = window.screenX + 10 + Math.floor(Math.random()*41);
    var y = window.screenY + 10 + Math.floor(Math.random()*41);
    var pos = ",screenX="+x+",screenY="+y+",";
    return pos;
}

/// used for new window
function new_window(url) {
  nw = window.open(url, "newwindow"+Date().replace(/[^0-9]/g,''), 'width=800,height=650,directories=yes,toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes'+windowpos());
  nw.focus();
  return false;
}

/// used for pop ups
function popup(url) {
  win = window.open(url,'popupwindow', 'width=750,height=650,resizable=yes,menubar=yes,location=yes,toolbar=yes,scrollbars=yes'+windowpos());
  win.focus()
  return false;
}

/// used for print pop ups
function printpop(url) {
  win = window.open(url, 'popupwindow', 'width=650,height=500,resizable=yes,location=yes,toolbar=yes,menubar=yes,scrollbars=yes'+windowpos());
  win.focus()
  return false;
}
/// used for news release pop ups
function release(url) {
  win = window.open(url,'popupwindow', 'width=596,height=500,resizable=yes,menubar=yes,location=yes,toolbar=yes,scrollbars=yes'+windowpos());
  win.focus()
  return false;
}

function print_popup() {
    return printpop(this.href);
}

function std_popup() {
    return popup(this.href);
}

function std_newwindow() {
    return new_window(this.href);
}
function std_release() {
    return release(this.href);
}

// turns PDF links to new windows

var RewriteLinks = {

    init: function() {
        var links = document.getElementsByTagName('a');
        for (var x = 0;x < links.length; x++) {
            a = links[x];

            var pdf = false;
            if (a.href.search(/\.pdf$/) > -1) {pdf = true;}
            if (pdf) {
                if (a.className.indexOf('pdf') == -1) { a.className += ' pdf '; }
                a.onclick = std_newwindow;
            }

            if      (a.className.indexOf('print') > -1 && a.className.indexOf('popup')) { a.onclick = print_popup; }
            else if (a.className.indexOf('popup') > -1) { a.onclick = std_popup; }
            else if (a.className.indexOf('newwin') > -1) { a.onclick = std_newwindow; }
            else if (a.className.indexOf('release') > -1) { a.onclick = std_release; }
        }
    },
    
    addEvent: function( obj, type, fn ) {
       if ( obj.attachEvent ) {
          obj['e'+type+fn] = fn;
          obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
          obj.attachEvent( 'on'+type, obj[type+fn] );
       } else {
          obj.addEventListener( type, fn, false );
       }
    }
    
}

RewriteLinks.addEvent(window,'load',RewriteLinks.init)