// This javascript makes all links that point to an image and have the attribute 'target="_blank"' into popup windows
// that automatically re-size themselves to fit their content

if ( window.onload )  var oldOnLoad = window.onload;
window.onload = function ()
  {
  if( oldOnLoad ) oldOnLoad();
  var links = document.getElementsByTagName( "a" );
  imgLinks = new Array();
  for( var x = 0; x < links.length; x++ )
    {
	if( links[x].getAttribute( "target" ) == "_blank" )
	  {
	   var href = links[x].getAttribute( "href" );
	   href = href.substring( href.length - 4 ).toLowerCase();
	   if( href == ( ".jpg" || "jpeg" || ".png" || ".gif" || ".bmp" )) imgLinks[imgLinks.length] = links[x];
	  }
	}
  
  for( var x = 0; x < imgLinks.length; x++ )
    {
	imgLinks[x].onclick = function() { return false; };
	imgLinks[x].firstChild.onclick = function ()
	  {
	  var link = this.parentNode.getAttribute("href");
	  popImage( link );
	  }
	}
  }


function popImage( imageUrl )
  {
  opts = 'width=200,height=200,scrollbars=no,toolbar=no';
  var newWin = window.open( '', '',opts );
  if( newWin ) 
    {
	if( imageUrl.substring(0,4) != ( "http" || "ftp:" ))  //if the image url is not an absolute path then we convert it to one
	  {
	  var url = window.location.protocol + '/';
	  url += window.location.hostname 
  	  if( imageUrl.substring(0,1) != "/" ) 
	    {
	    url += window.location.pathname;
	    url = url.substring(0, url.lastIndexOf('/') + 1);
	    }
	  url = url + imageUrl;
	  }
	
	newWin.document.body.style.padding = "0";
	newWin.document.body.style.margin = "0";
	if( newWin.innerWidth )
	  {
	  var newImg = newWin.document.createElement( "img" );
	  newImg.src = url;
	  newWin.document.body.appendChild( newImg );
	  newImg.onload = function ()
	    {
		var width =  this.width;
	    var height = this.height;
		newWin.resizeTo( width + 12, height + 38);
		}
	  }
	else 
	  {
	  newWin.document.write( '<html><head></head><body style="margin: 0; padding: 0;" ><img id="image1" src="' + imageUrl + '" /></body></html>');
	  
	  
	  var newImg = newWin.document.getElementById( "image1" );
	  newImg.onload = function ()
	    {
		var newImg = newWin.document.getElementById( "image1" );
	    var width =  newImg.width;
	    var height = newImg.height;
	    newWin.resizeTo( width + 12, height + 38);
		}
	  }
	}
  }
