// 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=475,height=500,resizable=yes,menubar=yes,location=yes,toolbar=yes,scrollbars=yes'+windowpos());
  win.focus()
  return false;
}

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

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

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

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

function newSameWindow_popup() {
	return newSameWindow(this.href);
}

// turns PDF links to new windows

function rewrite_links() {
    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 = pdf_popup;
		}

		if (a.className.indexOf('popup') > -1) { a.onclick = std_popup;}
		else if (a.className.indexOf('newSameWindow') > -1) { a.onclick = newSameWindow_popup; }	
		else if (a.className.indexOf('new_window') > -1) { a.onclick = std_newwindow; }

    }
}

