function makeProp(wdo,hgo,wd,hg){
	if(wd==undefined){
		wd= document.documentElement.clientWidth;
		hg= document.documentElement.clientHeight;
	}
	var nwd=wdo-wd;
	var nhg=hgo-hg;
	if(nwd<0 && nhg<0){ return {'width':wdo,'height':hgo} } 
	else {
		if (nwd>=nhg){return {'width':wd,'height':(wd/wdo)*hgo}} 
		else {return new Hash({'width':(hg/hgo)*wdo,'height':hg})}
	}
}
function wheelOn(){ FW=new Fx.Tween($("lbW")).start('opacity', 1) }
function wheelOff(){ FW=new Fx.Tween($("lbW")).start('opacity', 0) }
function overlayOn(){ $("lbO").setStyle("display",""); FO=new Fx.Tween($("lbO")).start('opacity', 0.75) }
function overlayOff(){ new Fx.Tween($("lbO")).start('opacity', 0).chain(function(){$("lbO").setStyle("display","none");$("lbM").setStyle("display","none");$("lbM").empty()}) }

function lbClose(){
	var F=new Fx.Tween($("lbM")).start('opacity',0).chain(function(){
		FW=new Fx.Tween($("lbW")).start('opacity', 0)
		overlayOff()
	})
}

function res2(I,M,hg_desc){
	I.inject($("lbX"),"after")
	var prop=makeProp(I.width,I.height,window.innerWidth,window.innerHeight-(hg_desc-14))
	I.setStyles(prop);
	M.setStyles({
		'margin-left':-(prop.width)/2,
		'margin-top':-(prop.height+hg_desc)/2,
		'padding-bottom':hg_desc,
		"padding-top":"14px",
		'width':prop.width,
		'height':prop.height
	})
	wheelOff()
	FX=new Fx.Tween($("lbX")).start('opacity',1)
	FI=new Fx.Tween($("lbI")).start('opacity',1)
	FT=new Fx.Tween($("lbT")).start('opacity',1)
}

function punch(){
	if ($("lbI").src!=this.href){
		var href=this.href
		var desc=this.name
		wheelOn()
		var FX=new Fx.Tween($("lbX"),{"duration":200}).start('opacity',0)
		var FT=new Fx.Tween($("lbT"),{"duration":200}).start('opacity',0)
		var FI=new Fx.Tween($("lbI"),{"duration":200}).start('opacity',0).chain(function(){
			$("lbD").set("html", desc)
			T=$("lbT")
			M=$("lbM")
			T2=T.clone();
			T2.set("html",T.get("html"))
			M.adopt(T2)
			var hg_desc=T2.getSize().y
			T2.destroy();
			$("lbI").destroy();
			var I = new Element('img', { 'id':'lbI','src': href, "opacity":"0" })
			if (I.complete){
				res2(I,M,hg_desc)
			} else {
				I.addEvent("load", function(){
					res2(I,M,hg_desc)
				})
			}
		})
	}
	return false;
}


function res1(I,M,T){
	M.setStyle("display","")
	var hg_desc=T.getSize().y
	var prop=makeProp(I.width,I.height,window.innerWidth,window.innerHeight-(hg_desc-14))
	M.setStyles(prop);
	I.setStyles(prop);
	I.inject($("lbX"),"after")
	M.setStyles({
		'margin-left':-(prop.width)/2,
		'margin-top':(-(prop.height+hg_desc)/2),
		"padding-bottom":hg_desc,
		"padding-top":"14px"
	});
	wheelOff()
	var F = new Fx.Tween(M).start('opacity', 1)
}


function boxer(id){
	overlayOn()
	wheelOn()
	if($defined(this.parentNode)){
		if (id==""){id=this.parentNode.parentNode.parentNode.parentNode.id}
		if ($chk(this.parentNode.parentNode.parentNode.parentNode.id)){ var id=this.parentNode.parentNode.parentNode.parentNode.id }
	}
	else {var id=window.location.hash.split("#")[1]}
	var jsonRequest = new Request.JSON({
		url: "./haha.php", 
		onComplete: function(job){
			var M=$("lbM")
			var X=new Element('div', {
				"id":"lbX",
				"styles":{
					"position":"absolute",
					"background":"url(ajax/img_ajax_close.gif)",
					"right":"0px",
					"top":"-2px",
					"width":"9px",
					"height":"9px",
					"display":"block",
					'cursor':'pointer'
				}
			});
			X.addEvent("click", lbClose)
			if ($defined(job.job[0].desc)){
				desc=job.job[0].desc
			} else {
				desc=job.desc
			}
			var T = new Element("div").setStyles({
				"id":"lbT"
			}).set("html","<div id='descricao'><dl><dt>Cliente: </dt><dd>" + job.cliente + "</dd><dt>Descrição: </dt><dd id='lbD'>" + desc + "</dd></dl></div>");
			L=new Element("div", {'id':"lbL"}).adopt(new Element("p",{"class":"h_img_list"}).appendText("Imagens: "))
			job.job.each(function(image){
				var desc2=job.desc
				if ($defined(image)){
					if ($defined(image.desc)){
						desc2=image.desc
					}
					el=new Element('a', {"class":"lbOp","href":image.imagem, "name":desc2}).appendText("a ").addEvent("click", punch)
					L.adopt("html",el)
				}
			})
			L.adopt(new Element("span", {'class':'cleaner'}))
			L.inject(T, 'top')
			M.adopt(X,T)
			T.id="lbT"
	    	var I = new Element('img', { 'id':'lbI','src': job.job[0].imagem });
			if (I.complete){
				res1(I,M,T)
			} else {
				I.addEvent("load", function(){
					res1(I,M,T)
				})
			}
		}
	}).get({'id': id});
	return false
}


function kickbox(){
	var W=new Element('div', { 
		'id':'lbW', 
		'opacity':0
	});
	var M=new Element('div', { 
		'id':'lbM', 
		'opacity':0, 
		'styles':{
			'z-index':9999,
			'position':'fixed',
			'top':'50%',
			'left':'50%'
		} 
	});
	var O=new Element('div', {
		'opacity':0,
		'id':'lbO'
	});
	$$("body").adopt(O,M,W);
	O.addEvent("click", lbClose)
	if ($$(window.location.hash)!="" && window.location.hash!=""){
		if (window.location.hash!="#topo" && window.location.hash!="#ilustracao" && window.location.hash!="#internet" && window.location.hash!="#logotipo" && window.location.hash!="#print")
		boxer(window.location.hash)
	}
	$$(".lb a").addEvent("click", boxer);
	$(document.body).getElements('a[target=_blank]').removeEvents();
}