/* lightbox.js */
var lightbox = {
	objID 		: 'lightbox'
	, reload 	: false
	, width 	: 800
	, height 	: 400
	, reload	: false
	, scrllPosX	: 0
	, scrllPosY	: 0
	, child		: null
	, closeBtn	: false
	
	, init : function () {
		if (parent.lightbox.closeBtn) { lightbox.showClose(); }
	}
	
	, display : function (width, height, Obj) {
			var innerHTML 	= Obj.innerHTML;
			var pageSize 	= klGeneric.getPageSize();
			var pageScroll 	= klGeneric.getPageScroll();
			var scr_w = 0, scr_h = 0, leftPos = 0, topPos = 0;
			scr_w = pageSize[0];
			scr_h = pageSize[3]+(pageScroll[0]*2);
			leftPos	= Math.round((scr_w-width)/2);
			topPos 	= Math.round((scr_h-height)/2);
			bod 	= document.getElementsByTagName('body')[0];
			
			if (Obj.fullscr)
			{
				var arrayPageSize = klGeneric.getPageSize();
				width 	= (arrayPageSize[2]);
				height 	= (arrayPageSize[3]);
				leftPos	= '-1';
				topPos	= 0;
				fullScrOpts = 'background-color:#fff; position:absolute; border:0;';
				$(bod).setStyle({overflow:'hidden'});
				lightbox.scrllPosX = pageScroll[0];
				lightbox.scrllPosY = pageScroll[1];
			}
			else
			{
				width += 2;
				height += 2;
				fullScrOpts = '';
			}
			
			if (!$(lightbox.objID))
			{
				var boxObj 				= Builder.node('div', { id:lightbox.objID, className:'lBox', style:'overflow: hidden; top:'+topPos+'px; left:'+leftPos+'px; height:'+height+'px;'+' width:'+width+'px;'+fullScrOpts});
				bod 					= document.getElementsByTagName('body')[0];
				boxObj.onselectstart 	= new Function("return false");
				boxObj.innerHTML 		= (innerHTML) ? iframeCode : null;
				bod.appendChild(boxObj);
			}
	}
	
	, show : function(width, height, Obj) {
		lightbox.reload	= (Obj.reload) ? true : false;
		if (Obj.fullscr===true || Obj.overlay===false) { lightbox.display(width,height,Obj); }
		else { klGeneric.overlay({animate:1, func:lightbox.hide, afterFinish:function(){lightbox.display(width,height,Obj)}, state:1}); }
	}
	
	, hide : function() {
		if ($(lightbox.objID)) {
			if (lightbox.scrllPosX || lightbox.scrllPosY)
			{
				window.scrollTo(lightbox.scrllPosX, lightbox.scrllPosY);
				lightbox.scrollPosX = 0;
				lightbox.scrollPosY = 0;
			}
			bod = document.getElementsByTagName('body')[0];
			$(bod).setStyle({overflow:'auto'});
			$(lightbox.objID).hide();
			$(lightbox.objID).remove();
		}
		var afterFinishFunc = (lightbox.reload) ? klGeneric.pageRefresh : null;
		klGeneric.overlay({state:0,animate:1,afterFinish:afterFinishFunc});
	}
	
	, showClose : function() {
		var closeBtn = Builder.node('a', { href:'#', className:'lClose' });
		Event.observe(closeBtn, 'focus', function() { closeBtn.blur(); }, false);
		Event.observe(closeBtn, 'click', function() { parent.lightbox.hide(); return false; }, false);
		bod = document.getElementsByTagName('body')[0];
		bod.appendChild(closeBtn);
	}
	
	, iframe : function(Obj) {
		if (!Obj.src && !Obj.srcObj) return;
		if (Obj.srcObj) { Obj.src = Obj.srcObj.getAttribute('href'); }
		var w = (Obj.width) 	? Obj.width : lightbox.width;
		var h = (Obj.height) 	? Obj.height : lightbox.height;
		
		if (Obj.close===true) 
		{ 
			lightbox.child = Obj.src; 
			lightbox.closeBtn = true;
		}
		else
		{
			lightbox.closeBtn = false;
		}
		
		var d 		= new Date();
		frameID		= d.getTime();
		iframeCode 	= '<iframe src="'+Obj.src+'" name="iF'+frameID+'" id="iF'+frameID+'" width="100%" height="100%" marginwidth="0" marginheight="0" frameborder="0"></iframe>';
		lightbox.show(w, h, {innerHTML:iframeCode, reload:Obj.reload, fullscr:Obj.fullscr});
	}

};


onContent(lightbox.init);