var Lightbox = Class.create();

var iOpacity=0;
var opacityTimeOut;

function setHeight(element,h){
	element = $(element);
	element.css({"height" : h+"px"});
}


Lightbox.prototype = {
	init: function() {
		var close='<a href="javascript:void(0)" onclick=" myLightbox.closeLightBox(); return false; "  title="Close" class="close"><img src="/img/close.gif" alt="Close"/></a>';
		/***************** main block *******************/
		var objBody = document.getElementsByTagName("body").item(0);

		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','overlay');
		objOverlay.style.display = 'block';
		objOverlay.style.display = 'none';
		objBody.appendChild(objOverlay);

		var objLightbox = document.createElement("div");
		objLightbox.setAttribute('id','lightbox');
		objLightbox.style.display = 'none';
		objLightbox.onclick = function(e) {	// close Lightbox is user clicks shadow overlay
			if(navigator.userAgent.search(/msie/i)!= -1) {
				if (event.srcElement.id == 'lightbox')
					myLightbox.closeLightBox();
			} else {
				if ( e.target.id == 'lightbox')
					myLightbox.closeLightBox();
			} 
			
		};
		objBody.appendChild(objLightbox);

		var objOuterImageContainer = document.createElement("div");
		objOuterImageContainer.setAttribute('id','outerImageContainer');
		objLightbox.appendChild(objOuterImageContainer);

		var objOuterImageContainerTop = document.createElement("div");
		objOuterImageContainerTop.setAttribute('id','outerImageContainerTop');
		objOuterImageContainerTop.innerHTML=close;
		objOuterImageContainer.appendChild(objOuterImageContainerTop);
		
		var objOuterImageContainerBody = document.createElement("div");
		objOuterImageContainerBody.setAttribute('id','outerImageContainerBody');
		objOuterImageContainer.appendChild(objOuterImageContainerBody);

		var objOuterImageContainerBottom = document.createElement("div");
		objOuterImageContainerBottom.setAttribute('id','outerImageContainerBottom');
		objOuterImageContainer.appendChild(objOuterImageContainerBottom);

		
		/************* page all height *******************/		
		var arrayPageSize = getPageSize();
		setHeight('#overlay', arrayPageSize[1]);
		setHeight('#lightbox', arrayPageSize[1]);
		
		
		
	},

	closeLightBox: function() {
		$('#lightbox').hide();
		opacityTimeOut = setInterval(clearOpacity, 100);
	},

	showLightBox: function(){
		$('#overlay').show(); 
		$('#lightbox').show();
	}
}

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 lightboxShow(id_value){
	var topY=window.scrollY+150;
	
	/******************* shadow effect ********************/
	$('#overlay').show(); 
	$('#lightbox').show();
	opacityTimeOut = setInterval(setOpacity, 100);
	$('#outerImageContainerBody').html($('#'+id_value).html());
	$('#outerImageContainer').css({'top' : topY+'px'});		
}

function lightboxClose(){
	myLightbox = new Lightbox();
	myLightbox.closeLightBox();		
}

function setOpacity(){
	iOpacity+=2;
	$('#overlay').css({'opacity' : '0.'+iOpacity , 'filter' : 'alpha(opacity='+iOpacity+'0)'});	
	if(iOpacity>=8)
		clearInterval(opacityTimeOut);
	
}
function clearOpacity(){
	iOpacity-=2;
	$('#overlay').css({'opacity' : '0.'+iOpacity , 'filter' : 'alpha(opacity='+iOpacity+'0)'});	
	if(iOpacity<=0){
		clearInterval(opacityTimeOut);
		$('#overlay').hide(); 
	}
}

$(document).ready(function() {
	myLightbox = new Lightbox();
})




