/* Function to addEventListener to onload
 * @param func - a function which should be executed once the page has loaded
 * http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 * it will work even if something has previously been assigned to window.onload
 * without using addLoadEvent itself. 
 */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function GetAnchors() {
  var x = document.getElementsByTagName('a');
	for (var i=0;i<x.length;i++) {
		if (x[i].className.indexOf('newWindow') != -1) {
			x[i].onkeypress = openNewWindow;
			x[i].onclick = openNewWindow;
			x[i].setAttribute("title", "Pop-up Window");
		} else if (x[i].className.indexOf('pdfWindow') != -1) {
			x[i].onkeypress = openNewWindow;
			x[i].onclick = openNewWindow;
			x[i].setAttribute("title", "PDF file");
		} else if (x[i].className.indexOf('external') != -1) {
		  x[i].onkeypress = openNewWindow;
			x[i].onclick = openNewWindow;
			x[i].setAttribute("title", "External link");
		}
	}
}

function openNewWindow(e) {
  var features = '';
  var start;
  var end;
  var width;
  var height;
  width = parseInt(getDimensionFrom(this, 'w'))>0 ? getDimensionFrom(this, 'w') : '';
  height = parseInt(getDimensionFrom(this, 'h'))>0 ? getDimensionFrom(this, 'h') : '';
  if (height.length>0 || width.length>0) {
    features += height.length>0 ? 'height='+height+',' : '';
    features += width.length>0 ? 'width='+width+',' : '';
    features = features.substr(0, features.length-1);
  } 
  if (!e) var e = window.event;
  if (features.length>0) {
    window.open(this.href, "_new", features);
  } else {
    window.open(this.href);
  }
  return false;
}

function getDimensionFrom(obj, attrib) {
  if (obj.className.indexOf(" "+attrib) != -1) {
    start = obj.className.indexOf(" "+attrib) + 1
    end = obj.className.indexOf(" ", start);
    end = (end == -1) ? obj.className.length - start : end - start;
    return obj.className.substr(start+1, end-1);
  } else {
    return "";
  }
}

if (document.getElementById) addLoadEvent(GetAnchors);
