var lightbox_w = null;
var lightbox_i = null;


function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}



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 pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}
function getKey(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}

	if(keycode == 27 || !keycode) {hideLightbox();}
	key = String.fromCharCode(keycode).toLowerCase();
	if(key == 'x'){ hideLightbox(); }
}


function listenKey () {	document.onkeypress = getKey; }
	

function showLightbox(img_array, cur)
{

	if(lightbox_i == null)
	{

///		alert(cur);
		ge('uimg['+cur+']').style.cursor='progress';
	}



	objLink = img_array[cur];

	if(lightbox_w == null) lightbox_w=cur;

	var objLightbox = ge('lightbox');
	var objImage = ge('lightboxImage');
	var objLightboxHref = ge('lightboxhref');
	var objLoadingImage = ge('loadingImage');
	




	objLoadingImage.style.display = 'block';


	
	objLightboxHref.onclick = function()
	{



//		alert(img_array);

		if(cur>=img_array.length-1) cur=-1;

		++cur;



		if(lightbox_w==cur) 
			closeit();
		else
			showLightbox(img_array, cur);


	}



	var arrayPageSize = getPageSize();
//	alert(arrayPageSize);
	var arrayPageScroll = getPageScroll();
//	alert(arrayPageScroll);

	var imgPreload = new Image();

	
	window.document.body.style.cursor = 'progress';


	imgPreload.onload=function(){



		if(lightbox_i == null)
		{

			ge('uimg['+cur+']').style.cursor='pointer';
			lightbox_i=1;
		}

		window.document.body.style.cursor = 'default';

		ge('overlay').style.display='';
		objImage.src = objLink;


		var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - imgPreload.height) / 2);
		var lightboxLeft = ((arrayPageSize[0] - 20 - imgPreload.width) / 2);
		
		a = (lightboxTop < 0) ? "0" : lightboxTop;
		b = (lightboxLeft < 0) ? "0" : lightboxLeft;

		pad = objLightbox.style.padding.replace('px','').split(' ')[0];




		objLightbox.style.top = ((a-pad<0)?0:a-pad) + "px";
		objLightbox.style.left = ((b-pad<0)?0:b-pad) + "px";




		objLightbox.style.width = (imgPreload.width) + "px";
		objLightbox.style.height = (imgPreload.height) + "px";

		objLightbox.style.display = 'block';
		objLoadingImage.style.display = 'none';

		listenKey();
		return false;
	}

	imgPreload.src = objLink;
	
}





function hideLightbox()
{




	ge('lightbox').style.display = 'none';
	ge('overlay').style.display='none';

	document.onkeypress = '';

	lightbox_w=null;
	lightbox_i=null;
}



function closeit()
{

	hideLightbox();
}




function fadeIn(opacity) 
{

    if (opacity <= 100) 
	{
      setOpacity(opacity);
      opacity += 10;
      window.setTimeout("fadeIn("+opacity+")", 40);
    }
}



function setOpacity(opacity) 
{

  obj = ge('lightboxImage');

  opacity = (opacity == 100)?99.999:opacity;
  
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}
