var lBox;

function LightBox()
{
	LightBox.iW = 200;
	LightBox.iH = 200;
	LightBox.maxAlpha = 80;
	LightBox.hRes = false;
	LightBox.imagem = null;
	LightBox.objURI = null;
	
	this.init();
}

LightBox.prototype.verificaLinks = function() {
	var ancoras = document.getElementsByTagName('a');
	for(var i = 0; i < ancoras.length; i++) {
		var ancora = ancoras[i];
		var relAtributo = String(anchor.getAttribute('rel'));
		if(ancora.getAttribute('href')) {
			if(relAtributo.toLowerCase().match('lightbox')) {
				ancora.onclick = function() { lBox.init(); return false};
			}
		}		
	}
};

LightBox.prototype.init = function() {
	var objBody = document.getElementsByTagName('body').item(0);

	if(document.getElementById('lightbox')) {
		objBody.removeChild(document.getElementById('lightbox'));
		objBody.removeChild(document.getElementById('Main'));
	}
	
	var objLightBox = document.createElement("div");
		objLightBox.setAttribute('id', 'lightbox');
		objLightBox.style.display = 'none';
		LightBox.setAlpha(objLightBox, 0);
		objBody.appendChild(objLightBox);
	var objMain = document.createElement("div");
		objMain.setAttribute('id', 'Main');
		objMain.style.display = 'none';
		objBody.appendChild(objMain);
	var objOut = document.createElement("div");
		objOut.setAttribute('id', 'outerContainer');
		objMain.appendChild(objOut);
	var objCentro = document.createElement("div");
		objCentro.setAttribute('id', 'centroContainer');
		objOut.appendChild(objCentro);
	var divFechar = document.createElement("div");
		divFechar.setAttribute('id', 'fecharDiv');
		objOut.appendChild(divFechar);
	var objFechar = document.createElement("a");
		objFechar.setAttribute('href', 'javascript: void 0');
		divFechar.appendChild(objFechar);
	var loadingObj = document.createElement("div");
		loadingObj.setAttribute('id', 'loading');
		loadingObj.style.display = '';
		objOut.appendChild(loadingObj);
	var objFlash = document.createElement("div");
		objFlash.setAttribute('id', 'flashObj');
		objOut.appendChild(objFlash);
	objFechar.innerHTML = "X";

	objLightBox.onclick = function() { lBox.fim(); return false; };
	objFechar.onclick = function() { lBox.fim(); return false; };
	objMain.onclick = function(e) {
		e = (e) ? e : window.event; // pega o evento enviado por parametro (e = standard, window.event = IE)
		var id = (e.target) ? e.target.id : e.srcElement.id;
		if(id == "Main") {
			lBox.fim();
			return false;
		}
	}

	LightBox.hRes = false;
};

LightBox.prototype.abrir = function(URI, w, h) {
	LightBox.objURI = URI;
	LightBox.iW = w;
	LightBox.iH = h;
	document.getElementById('lightbox').style.display = '';
	document.getElementById('Main').style.top = (parseInt((this.getPageScroll()) ? this.getPageScroll() : 0)+ 50) + "px";
	document.getElementById('Main').style.display = '';
	LightBox.fadeIn('lightbox', 0.05);
	if(URI.indexOf('.swf') > 0) {
		LightBox.resize(LightBox.iW, LightBox.iH);
	} else {
		LightBox.imagem = new Image();
		LightBox.imagem.id = 'imagem';
		LightBox.imagem.src = URI;
		LightBox.imagem.onload = function() {
					var imgWidth = this.width;
					var imgHeight = this.height;
					LightBox.resize(imgWidth, imgHeight);			
				}
	}
};

LightBox.prototype.getPageScroll = function(){
	if(self.pageYOffset) { // todos exceto IE
		return self.pageYOffset;
	} else if(document.documentElement && document.documentElement.scrollTop){ // IE6 com doctype stric
		return document.documentElement.scrollTop;
	} else { // outros IE
		document.body.scrollTop;
	}
};

LightBox.prototype.getDocSize = function() {
	var htmlheight = document.body.parentNode.scrollHeight;
	var htmlwidth = document.body.parentNode.scrollWidth;
	
	var windowheight = (document.all) ? document.body.clientHeight : window.innerHeight;
	
	return (htmlheight < windowheight) ? {width: htmlwidth, height: windowheight} : {width: htmlwidth, height: htmlheight}; 
};

LightBox.prototype.getScreenSize = function() {
	if(window.innerWidth) { // se o browser suporta window.innerHeight
		return {width: window.innerWidth, height: window.innerHeight};
	} else if(document.all) { // se o browser suporta document.all (IE4+)
		return {width: document.body.clientWidth, height: document.body.clientHeight};
	}
};

LightBox.resize = function(w, h) {
	LightBox.resizeH('outerContainer', h+40, 0.09);
	LightBox.resizeW('outerContainer', w+20, 0.10);
};

LightBox.resizeH = function(eid, h, tempo) {
	var object = document.getElementById(eid);
	var timer = (tempo*1000)/50;
	var fHeight = object.offsetHeight;
	var i = setInterval(
			function() {
				object.style.height = fHeight + "px";
				if(parseInt(object.style.height) >= h){
					clearInterval(i);
					LightBox.hRes = true;
				}
				fHeight +=4;
			}, timer);
};



LightBox.resizeW = function(eid, w, tempo) {
	var x;
	if(!LightBox.hRes) {
		x = setTimeout("LightBox.resizeW('" + eid + "', " + w + ", " + tempo + ")", 100);
		return;
	}
	clearTimeout(x);
	var object = document.getElementById(eid);
	var timer = (tempo*1000)/50;
	var fWidth = object.offsetWidth;
	var i = setInterval(
			function() {
				object.style.width = fWidth + "px";
				if(parseInt(object.style.width) >= w) {
					clearInterval(i);
					if(!LightBox.imagem){
						swfobject.embedSWF(LightBox.objURI, "flashObj", LightBox.iW, LightBox.iH, "10.0.0");
					} else {
						document.getElementById('loading').style.display = 'none';
						document.getElementById('flashObj').appendChild(LightBox.imagem);
					}
				}
				fWidth +=4;			
			}, timer);
};

LightBox.fadeIn = function(eid, tempo) {
	var object = document.getElementById(eid);
	var alpha = 0;
	var timer = (tempo*1000)/50;
	var i = setInterval(
			function() {
				if(alpha >= LightBox.maxAlpha) {
					clearInterval(i);
				}
				LightBox.setAlpha(object, alpha);
				alpha += 4;
			}, timer);
};

LightBox.fadeOut = function(eid, tempo) {
	var object = document.getElementById(eid);
	var alpha = LightBox.maxAlpha;;
	var timer = (tempo*1000)/50;
	var i = setInterval(
			function() {
				if(alpha <= 0) {
					clearInterval(i);
					document.body.removeChild(object);
				}
				LightBox.setAlpha(object, alpha);
				alpha -=4;
			}, timer);
};

LightBox.setAlpha = function(obj, alpha) {
	obj.style.opacity = alpha/100;
	obj.style.MozOpacity = alpha/100;
	obj.style.KhtmlOpacity = alpha/100;
	obj.style.filter = "alpha(opacity=" + alpha + ")";
};

LightBox.prototype.fim = function() {
	document.getElementById('Main').style.display = 'none';
	//LightBox.fadeOut('lightbox', 0.05);

	this.init();
};

// 

if(window.addEventListener) {
	window.addEventListener("load", initLightbox, false);
} else if(window.attachEvent) {
	window.attachEvent("onload", initLightbox);
} else {
	window.onload = function() { iniLightbox();};
}

function initLightbox() { lBox = new LightBox(); lBox.abrir('image-1.jpg');}
