// ---------------------------------------------------------------
//
//  viewImage
// -----------
//
//  Javascript popup image function
//
// ---------------------------------------------------------------

// Function to set the opacity of the given DOM element
// Opacity is in the range of 0-100
function setElementOpacity(Element, NewOpacity)
{
if (NewOpacity < 0)
	NewOpacity = 0;
if (NewOpacity > 100)
	NewOpacity = 100;
	
// create range value values
ZeroToOne = (NewOpacity/100.0).toFixed(2);
ZeroTo100 = NewOpacity.toFixed(0);

// create filter style string
FilterStr = "alpha(opacity=" + ZeroTo100 + ")";

Element.style.filter = FilterStr;
Element.style.opacity = ZeroToOne;
Element.style.mozOpacity = ZeroToOne;
}

// Function to bind the given function to an event on the given element
// cross browser compatible.
function bindEvent(Element, Event, Handler)
{
	// NS/Firefox	
	if (window.addEventListener)
		Element.addEventListener(Event, Handler, false);
	// IE
	if (window.attachEvent)
		Element.attachEvent("on" + Event, Handler);
}

// Method to create a 50% alpha screen of the given colour and add it to the body element
function CreateScreen(color, opacity, z_index, id)
{
var DocBody = document.getElementsByTagName("BODY")[0];
	
switch (color)
	{
	case false:	color = "FFFFFF";	break;
	case "black":	color = "000000";	break;
	case "white":	color = "FFFFFF";	break;
	}
	
element = document.createElement("div");

element.style.backgroundColor = "#" + color;
element.style.position = "absolute";
element.style.top = DocBody.scrollTop + "px";
element.style.left = "0px";
element.style.width = "100%";
element.style.height = DocBody.offsetHeight + "px";
element.style.zIndex = z_index;

setElementOpacity(element, opacity);

element.innerHTML = "&nbsp;";
if (id)
	element.id = id;

DocBody.appendChild(element);

return element;
}


function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
	//Netscape compliant
	scrOfY = window.pageYOffset;
	scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	//DOM compliant
	scrOfY = document.body.scrollTop;
	scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	//IE6 standards compliant mode
	scrOfY = document.documentElement.scrollTop;
	scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}


// function to remove the given element from the DOM tree
function removeElement(element)
{
	element.parentNode.removeChild(element);
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function JAM_InitGetWindowSize()
{
res = new Object();

// proper web browsers (mozilla, netscape, opera)
if (typeof window.innerWidth != 'undefined')
	{
	res.width = window.innerWidth;
	res.height = 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)
	{
	res.width = document.documentElement.clientWidth;
	res.height = document.documentElement.clientHeight;
	}
// older versions of IE
else
	{
	res.width = document.getElementsByTagName('body')[0].clientWidth;
	res.height = document.getElementsByTagName('body')[0].clientHeight;
	}
	
return res;
}

// get the window size once on startup then return a copy of this if requested
JAM_WindowSize = JAM_InitGetWindowSize();

function JAM_GetWindowSize()
{
	var Size = getPageSize();
	var res = new Object();
	res.width = Size[2];
	res.height = Size[3];
	return res;

}

function viewImage(imgSrc, imgWidth, imgHeight)
{
	function Close()
	{
		removeElement(Container);
		removeElement(Screen);
	}

	// create background screen
	var Screen = CreateScreen("black", 65, 65000, "");
	
	var DocBody = document.getElementsByTagName("BODY")[0];
	
	Screen.align = "center";
	Screen.style.paddingTop = "248px";
	
	// get screen size
	var Size = JAM_GetWindowSize();
	var Width = Size.width;
	var Height = Size.height;
	
	var Scroll = getScrollXY();
	
	// create container div
	var Container = document.createElement("div");
	Container.style.position = "absolute";
	//Container.style.backgroundColor = "#fca204";
	Container.style.left = ((Width - imgWidth) / 2) + "px";
	Container.style.top = DocBody.scrollTop + ((Height - imgHeight) / 2) + "px";
	Container.style.width = imgWidth + "px";
	//Container.style.textAlign = "center";
	//Container.style.border = "1px solid #000000";
	Container.style.zIndex = 65001;
	Container.setAttribute("class","popup");
	Container.className = "popup";		// IE non standards fix
	
	var FullImage = document.createElement("img");
	FullImage.src = imgSrc;
	FullImage.width = imgWidth;
	FullImage.height = imgHeight;
	FullImage.border = 0;
	
	bindEvent(FullImage, "click", Close);
	Container.appendChild(FullImage);
	
	var CloseText = document.createElement("div");
	CloseText.appendChild( document.createTextNode("Close") );
	CloseText.setAttribute("class", "close");
	CloseText.className = "close";	// IE non standards fix
	//CloseText.style.color = "#FFFFFF";
	CloseText.style.cursor = "pointer";
	
	bindEvent(CloseText, "click", Close);
	Container.appendChild(CloseText);
	
	DocBody.appendChild(Container);
}