//This javascript file uses core.js which is a js helper file.

//It is being used here to wait for the page to load and to 
//attach events by handling browser differences.

//ImagePopup object.
var imagePopup = {
	init: function()
	{
		//Get the p elements with a class of enlargeimage.
		var enlargeContainers = Core.getElementsByClass("enlargeimage");
		
		//Get the img elements inside the p elements.
		var enlargeImages = [];
		for(var j=0; j<enlargeContainers.length; j++)
		{
			enlargeImages[enlargeImages.length] = enlargeContainers[j].firstChild;
		}
		
		//Add mouseover and mouseout listeners to all the enlarge images.
		for(var i=0; i < enlargeImages.length; i++)
		{
			Core.addEventListener(enlargeImages[i], "click", imagePopup.clickOpenListener);
		}
	},
	
	//Enlarge button click function
	clickOpenListener: function(event)
	{
		
		//Close any popups that are open.
		imagePopup.closeOpenPopups();
			
		var enlargeImage = this;
		var popupId = "imagePopup" + enlargeImage.id;
		var divPopup = document.getElementById(popupId);
		
		//In browsers other than IE6 we are using fixed positioning for the popup
		//so that it always appears in the center of the screen regardless of the
		//viewports size. The calculations are below.
		var imgWidth = 550;
		var imgHeight = 416;
		
		var viewWidth = imagePopup.getViewportSize()[0];
		var viewHeight = imagePopup.getViewportSize()[1];
		
		var popupLeft = (viewWidth - imgWidth) / 2;
		var popupTop = (viewHeight - imgHeight) /2;
		
		popupLeft = popupLeft + "px";
		popupTop = popupTop + "px";
		
		divPopup.style.left = popupLeft;
		divPopup.style.top = popupTop;
		
		
		//Check for IE6
		//If the browser is IE6 we are using absolute positioning for the popup
		//relative to the table element. We are showing the popup towards
		//the center of the table.
		var isIE = (navigator.appName=="Microsoft Internet Explorer");
		var IEversion = navigator.appVersion;
		if(isIE)
		{
			IEversion = parseInt(IEversion.substr(IEversion.indexOf("MSIE")+4));
			
			if(IEversion < 7)
			{
				divPopup.style.left = "185px";
				divPopup.style.top = "30px";
			}
		}

		//Show the popup
		divPopup.style.display="block";
		
		//Add the listener to close the popup
		Core.addEventListener(divPopup, "click", imagePopup.clickCloseListener);
	},
	
	//Close click function
	clickCloseListener: function(event)
	{
		var enlargeImage = this;
		enlargeImage.style.display="none";
	},
	
	//Function which gets the current viewport size
	getViewportSize: function()
	{
  	var viewportwidth;
  	var viewportheight;
  	
  	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
			if (typeof window.innerWidth != 'undefined') {
				viewportwidth = window.innerWidth, viewportheight = window.innerHeight
			}
			
			// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
			else 
				if (typeof document.documentElement != 'undefined' &&
				typeof document.documentElement.clientWidth !=
				'undefined' &&
				document.documentElement.clientWidth != 0) {
					viewportwidth = document.documentElement.clientWidth, viewportheight = document.documentElement.clientHeight
				}
				
				// older versions of IE
				else {
					viewportwidth = document.getElementsByTagName('body')[0].clientWidth, viewportheight = document.getElementsByTagName('body')[0].clientHeight
				}
			
			return [viewportwidth, viewportheight];
	},//getViewportSize
	
	//Function which closes any open popups
	closeOpenPopups: function()
	{
  	var popupElements = Core.getElementsByClass("imagepopup");
  	
  	var thumbElement = null;
  	var popupElement = null;
  	
  	for (var i = 0; i < popupElements.length; i++)
		{
  		thumbElement = popupElements[i];
  		
  		if (thumbElement.id.indexOf("imagePopup") != -1)
			{
  			popupElement = thumbElement;
  			
  			if (popupElement.style.display == "block")
				{
  				popupElement.style.display = "none";
  			}
  		}
  	}
  }//closeOpenPopups
		
};

//Initialize the object only once the page has finished loading.
Core.start(imagePopup);