﻿/*
1.0.3 -- 2008-03-13
	* fix a problem dupulicated "Loading image ..." message.
1.0.2 -- 2008-03-12
	* fix a broblem "Loading image ..." message is not hidden after second execution
1.0.1 -- 2008.01.15
	* fix a problem "Loading image..." message is not shown from second execution.
1.0 -- 2008.01.12
	* first release.
*/

function set_image_position(an_imageNode)
{
	if (an_imageNode.width) {
		w_width = window.innerWidth;
		if (an_imageNode.width > w_width) {
			an_imageNode.style.left = window.pageXOffset+32+'px';
		} else {
			an_imageNode.style.left = Math.floor(window.pageXOffset
									+(w_width-an_imageNode.width)/2) + 'px';
			
			an_imageNode.setAttribute('width', an_imageNode.width );
		}
		an_imageNode.setAttribute('width', an_imageNode.width );
	} else {
		an_imageNode.style.left = window.pageXOffset+32+'px';
	}
	
	if (an_imageNode.height) {
		w_height = window.innerHeight;
		if (an_imageNode.height > w_height) {
			an_imageNode.style.top = window.pageYOffset+32+'px';
		} else {
			an_imageNode.style.top = Math.floor(window.pageYOffset
								+(w_height-an_imageNode.height)/2) + 'px';
			an_imageNode.setAttribute('height', an_imageNode.height );
		}
		an_imageNode.setAttribute('height', an_imageNode.height );
	} else {
		an_imageNode.style.top = window.pageYOffset+32+'px';
	}
}

function popupImage(a_link)
{  
  if (!popupImage.imageNode) {
		popupImage.imageNode = new Object();
		popupImage.overlay = document.createElement('div');
		with(popupImage) {
			overlay.style.position = "fixed";
      overlay.style.left = "0";
      overlay.style.top = "0";
      overlay.style.opacity = "0.7";
      overlay.style.backgroundColor ="black";
      overlay.style.width = "100%";
      overlay.style.height = "100%";
      overlay.style.fontSize = "14px";
      overlay.style.textAlign = 'center';
      overlay.style.color = 'black';
      overlay.appendChild(document.createTextNode("Loading image ..."));
		}
  }
    
  var width = 0;
  var height = 0;
  if (!popupImage.imageNode[a_link]) {
    var an_imageNode = document.createElement('img');
    popupImage.imageNode[a_link] = an_imageNode;
    an_imageNode.setAttribute('src', a_link.href );
    an_imageNode.addEventListener('click' ,closeImage , true);
    an_imageNode.style.position = 'absolute';
    an_imageNode.style.margin = '0';
    an_imageNode.style.visibility = 'hidden';
    
    an_imageNode.onload = function() {
			set_image_position(this);
			this.style.visibility = 'visible';
			popupImage.overlay.style.color = "black";
		}
		
		with(popupImage) {
			overlay.style.color = 'white';
		}
	} 
	
  with(popupImage){
    var an_imageNode = imageNode[a_link];
    set_image_position(an_imageNode);
    document.body.appendChild(overlay);
    document.body.appendChild(an_imageNode);
  }

  function closeImage(){
  	document.body.removeChild(this);
  	document.body.removeChild(popupImage.overlay);
  }
    
   return false;
}
