var myPopup = null;

function applyPopups()
{
  a = document.getElementsByTagName("a");

  for(i=0; i<a.length; i++)
  {
    if(a[i].getAttribute("target") && a[i].getAttribute("target") == "_blank")
    {
      a[i].firstChild.data = "#";

      a[i].onclick = function()
      {
      	// Make href blank, we don't want two popups
        url = this.getAttribute("href");
        title = this.getAttribute("title");

        myPopup = window.open('', 'popup', 'resizable = no, toolbar = no, scrollbars = no, width = 320, height = 480');
		myPopup.document.write('<html><head><title>' + title + '</title><meta http-equiv="imagetoolbar" content="no"></head><body style="margin: 0; padding: 0;"><table style="margin: 0;" width="100%" height="100%" cellspacing="0" cellpadding="0"><tr><td valign="middle"><img style="margin-left: 8px;" src="' + url + '" onload="opener.resizePopup(this.width, this.height)"></td></tr></table></body></html>');
		myPopup.document.close();

        return false;
      }
    }
  }
}

function GetInnerSize () {
	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return [x,y];
}

function resizePopup (w, h, x, y) {
	// make sure we have a final x/y value
	// pick one or the other windows value, not both
	if (x==undefined) x = myPopup.screenLeft || myPopup.screenX;
	if (y==undefined) y = myPopup.screenTop || myPopup.screenY;

	// for now, move the window to the top left
	// then resize to the maximum viewable dimension possible
	myPopup.moveTo(0,0);
	myPopup.resizeTo(screen.availWidth,screen.availHeight);

	// now that we have set the browser to it's biggest possible size
	// get the inner dimensions.  the offset is the difference.
	var inner = GetInnerSize();
	var ox = screen.availWidth-inner[0];
	var oy = screen.availHeight-inner[1];

	// now that we have an offset value, size the browser
	// and position it
	myPopup.resizeTo(w+ox + 20, h+oy - 60);
	myPopup.moveTo(x,y);
}

window.onload = applyPopups;