//	by Martín Acuña Lledó <martin@nosbase.com> - http://www.nosbase.com
//	03/30/06
var nosLightBox = Class.create();

nosLightBox.prototype = {
	initialize : function() {
		var objBody = document.getElementsByTagName("body").item(0);
		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','overlay');
		objOverlay.style.display = 'none';
		var objLoading = document.createElement("img");
		objLoading.setAttribute('id','loading');
		objLoading.setAttribute('src','gfx/loading.gif');
		objLoading.style.display='none';
		var objLightBox = document.createElement("div");
		objLightBox.setAttribute('id','lightbox');
		objOverlay.appendChild(objLoading);
		//objOverlay.onclick = function() { NosLb.hide(); return false; }
		objBody.appendChild(objLightBox);
		objBody.appendChild(objOverlay);
	},

//	getPageSize()
//	Code from http://huddletogether.com/projects/lightbox2/
	getPageSize : function(){
		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;
	},
	
//      getPageScroll()
//      Code from http://huddletogether.com/projects/lightbox2/
	getPageScroll : function(){

		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;
	},
	
	show: function(urlDir,waitFor){
		var arrayPageSize = this.getPageSize();
		var arrayPageScroll = this.getPageScroll();
		$('overlay').style.height = arrayPageSize[1] +"px";
		$('lightbox').style.top = arrayPageScroll[1] + (arrayPageSize[3] / 15) +"px";
		$('loading').style.top = (arrayPageScroll[1] + (arrayPageSize[3] / 15) + 180) +"px";
		new Effect.Appear('overlay', { duration: 0.2, from: 0.0, to: 0.9});
		var getNewMsg = {
			method: 'get',
				onSuccess: function(t) {
					Element.update('lightbox', t.responseText);
					$(waitFor).onload=function(){
						Element.hide('loading');
						new Effect.Appear('lightbox', { duration: 0.2, from: 0.0, to: 1});
						//new Effect.Opacity('lightbox',{ from: 9.0, to: 1.5 });
					}	
				},
				// Handle 404
				on404: function(t) {
					Element.hide('loading');
					Element.update('lightbox','Error 404: location "' + t.statusText + '" was not found.');
				},
				// Handle other errors
				onFailure: function(t) {
					Element.hide('loading');
					Element.update('lightbox','Error ' + t.status + ' -- ' + t.statusText);
				},
				frequency:5						
		}
		Element.hide('lightbox');
		Element.update('lightbox','');
		Element.show('loading');
		new Ajax.Request(urlDir, getNewMsg);
		return(false);
	},
	hide: function(){
		new Effect.Fade('overlay', { duration: 0.2});
		Element.update('lightbox','');
		return(false);
	},
	
	change: function(urlDir,waitFor){
		var getNewMsg = {
			method: 'get',
				onSuccess: function(t) {
					Element.update('lightbox', t.responseText);
					$(waitFor).onload=function(){
						Element.hide('loading');
						new Effect.Appear('lightbox', { duration: 0.2, from: 0.0, to: 1});
					}	
				},
				// Handle 404
				on404: function(t) {
						Element.hide('loading');
						Element.update('lightbox','Error 404: location "' + t.statusText + '" was not found.');
						new Effect.Appear('lightbox', { duration: 0.2});
				},
				// Handle other errors
				onFailure: function(t) {
					Element.hide('loading');
					Element.update('lightbox','Error ' + t.status + ' -- ' + t.statusText);
					new Effect.Appear('lightbox', { duration: 0.2});
				},
				frequency:5						
		}
		Element.hide('lightbox');
		Element.update('lightbox','');
		Element.show('loading');
		new Ajax.Request(urlDir, getNewMsg);
		return(false);
	}
}

function initNosLightBox() { NosLb = new nosLightBox(); }	