
function initLightbox()
{
	var objBody = document.getElementsByTagName("body").item(0);
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.onclick = function () {hideLightbox(); return false;}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);
	
	var objLightbox = document.createElement("iframe");
	objLightbox.setAttribute('id','lightbox');
	objLightbox.style.display = 'none';
	objLightbox.style.position = 'absolute';
	objLightbox.style.zIndex = '100';
	objLightbox.style.background = 'transparent';
	objLightbox.style.border = '2px';
	objLightbox.style.margin = '0px';
	objLightbox.style.padding = '0px';
	
	objLightbox.setAttribute("marginwidth", "0");
	objLightbox.setAttribute("marginheight", "0");
	objLightbox.setAttribute("allowtransparency", "true");
	objLightbox.setAttribute("scrolling", "no");
	objLightbox.setAttribute("frameborder", "0");
	// <iframe allowTransparency="true" name="aaa3" src="http://127.0.0.1/tj/" width="336" height="360" marginwidth="0" marginheight="0" scrolling="no" frameborder="0">
	objBody.insertBefore(objLightbox, objOverlay.nextSibling);
}

function hideLightbox()
{
	var objLightbox = document.getElementById('lightbox');
	var objOverlay = document.getElementById('overlay');
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';
}

function showLightbox(url, w, h)
{
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	
	objOverlay.style.display = 'block';
	
	var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - h ) / 2);
	var lightboxLeft = ((arrayPageSize[0] - 20 - w ) / 2);
	var lightboxBottom = lightboxTop + h;
	var lightboxRight = lightboxLeft + w;

	objLightbox.setAttribute("width", w);
	objLightbox.setAttribute("height", h);

	objLightbox.src = url;
	objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
	objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
	//objLightbox.style.bottom = lightboxBottom + "px";
	//objLightbox.style.right = lightboxRight + "px";
	
	setTimeout(function(){
		objLightbox.style.display = 'block';
	}, 600);
}

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) {
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

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 addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}
}
addLoadEvent(initLightbox);
