// JAVASCRIPT "Lightbox v1" - Portal da Juventude :: Version: 	1.0 - 2007/2008 author:alex@burocratik.com
function insertAfter(newElement,targetElement) {
var parent = targetElement.parentNode;
if (parent.lastChild == targetElement) {
parent.appendChild(newElement);
} else {
parent.insertBefore(newElement,targetElement.nextSibling);
}
}
function viewPort() {    
var xScroll, yScroll;
if (window.innerHeight && window.scrollMaxY) {	
xScroll = window.innerWidth + window.scrollMaxX;
yScroll = window.innerHeight + window.scrollMaxY;
} else if (document.body.scrollHeight > document.body.offsetHeight){ 
xScroll = document.body.scrollWidth;
yScroll = document.body.scrollHeight;
} else { 
xScroll = document.body.offsetWidth;
yScroll = document.body.offsetHeight;
}
var windowWidth, windowHeight;
if (self.innerHeight) {	
if(document.documentElement.clientWidth){
windowWidth = document.documentElement.clientWidth;
} else {
windowWidth = self.innerWidth;
}
windowHeight = self.innerHeight;
} else if (document.documentElement && document.documentElement.clientHeight) { 
windowWidth = document.documentElement.clientWidth;
windowHeight = document.documentElement.clientHeight;
} else if (document.body) { 
windowWidth = document.body.clientWidth;
windowHeight = document.body.clientHeight;
}	
if(yScroll < windowHeight){
pageHeight = windowHeight;
} else { 
pageHeight = yScroll;
}
if(xScroll < windowWidth){	
pageWidth = xScroll;
} else {
pageWidth = windowWidth;
}
return [pageWidth,pageHeight];
}
function getScrollPosition () {
position = [
window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop ];
return position;
}
var W3CDOM = (document.getElementsByTagName && document.createElement);
if (W3CDOM){
var link = document.createElement('link');
link.setAttribute("rel", "stylesheet");
link.setAttribute("type", "text/css");
link.setAttribute("href", "../Style/pousada-lightbox-js.css");
link.setAttribute("media", "screen");
document.getElementsByTagName("head")[0].appendChild(link);
}
function iniciarLboxBg(){
if (!document.createElement) return false;
if (!document.getElementById("wrapperMaster")) return false;
var fundo = document.createElement("div");
fundo.setAttribute("id","lBoxBg");
fundo.style.opacity = 0.8;
fundo.style.filter = "alpha(opacity=80)";
fundo.style.display = "none";
var aux = document.getElementById("wrapperMaster");
insertAfter(fundo,aux);
}
function fundoTamanho(elemento){
var tamanho = viewPort();
elemento.style.height=tamanho[1]+"px";
}
function iniciarLbox(){
if (!document.createElement) return false;
if (!document.getElementById("lBoxBg")) return false;
var box = document.createElement("div");
box.setAttribute("id","lBox");
box.style.display = "none";
var aux = document.getElementById("lBoxBg");
insertAfter(box,aux);
var moldura = document.createElement("div");
moldura.setAttribute("id","lBoxMoldura");
box.appendChild(moldura);
var placeholder = document.createElement("img");
placeholder.setAttribute("id","placeholder");
placeholder.setAttribute("src","../images/imgs/p-loading.gif");
placeholder.setAttribute("alt","Foto da pousada");
moldura.appendChild(placeholder);
var legenda = document.createElement("p");
legenda.setAttribute("id","legenda");
var legendaTxt = document.createTextNode("Choose an image");
legenda.appendChild(legendaTxt);
var aux2 = document.getElementById("placeholder");
insertAfter(legenda,aux2);
legenda.style.display="none";
var btFechar = document.createElement("img");
btFechar.setAttribute("id","btFechar");
btFechar.setAttribute("src","../images/imgs/p-btFechar.gif");
btFechar.setAttribute("alt","Fechar");
var aux3 = document.getElementById("legenda");
insertAfter(btFechar,aux3);
btFechar.style.display="none";
var limpar = document.createElement("br");
var aux4 = document.getElementById("btFechar");
insertAfter(limpar,aux4);
}
function lightBoxPosicao(elemento){
var posicao = getScrollPosition();
elemento.style.top=posicao[1]+15+"px";
}
function visivel(clicado){
var fundo = document.getElementById("lBoxBg");
var lightBox = document.getElementById("lBox");
var lightBoxMoldura = document.getElementById("lBoxMoldura");
if (lightBoxMoldura == clicado && lightBox == clicado){
fundo.style.display=="block";
lightBox.style.display=="block";
}else{
fundo.style.display = (fundo.style.display=="block") ? "none" : "block";
lightBox.style.display = (lightBox.style.display=="block") ? "none" : "block";
}
}
function iniciarLinksPics() {
if (!document.getElementsByTagName) return false;
if (!document.getElementById) return false;
var linksPics = document.getElementsByTagName("a");
for (var i=0; i<linksPics.length; i++ ) {
if (linksPics[i].getAttribute("href") && linksPics[i].getAttribute("rel") == "lightbox"){
linksPics[i].onclick = function() {
mostraPic(this);
return false;
} 
}
}
if (!document.getElementById("lBoxBg")) return false;
if (!document.getElementById("lBox")) return false;
if (!document.getElementById("lBoxMoldura")) return false;
if (!document.getElementById("btFechar")) return false;
var fundo = document.getElementById("lBoxBg");
var lightBox = document.getElementById("lBox");
var lightBoxMoldura = document.getElementById("lBoxMoldura");
var btFechar = document.getElementById("btFechar");
fundo.onclick = lightBox.onclick = lightBoxMoldura.onclick = btFechar.onclick = function() {
visivel(this);
return false;
}
}
function mostraPic(qualF){
if (!document.getElementById("lBoxBg")) return false;
if (!document.getElementById("lBox")) return false;
if (!document.getElementById("placeholder")) return false;
if (!document.getElementById("legenda")) return false;
if (!document.getElementById("btFechar")) return false;
var fundo = document.getElementById("lBoxBg");
var lightBox = document.getElementById("lBox");
var destino = qualF.getAttribute("href");
var placeholder = document.getElementById("placeholder");
var lightBoxMoldura = document.getElementById("lBoxMoldura");
var legenda = document.getElementById("legenda");
var btFechar = document.getElementById("btFechar")
fundoTamanho(fundo);
lightBoxPosicao(lightBox);
visivel(null);
placeholder.setAttribute("src","../images/imgs/p-loading.gif");
placeholder.style.width=lightBoxMoldura.style.width=150+"px";
placeholder.style.height=lightBoxMoldura.style.height=150+"px";
legenda.style.display="none";
btFechar.style.display="none";
var lerImg = new Image();
lerImg.onload = function(){
placeholder.src=lerImg.src;
placeholder.style.width = lightBoxMoldura.style.width = lerImg.width+"px";
placeholder.style.height = lerImg.height+"px";
lightBoxMoldura.style.height = lerImg.height+40+"px";
legenda.style.width=lerImg.width-75+"px";
legenda.style.display="block";
btFechar.style.display="block";
}
lerImg.src = destino;
if (qualF.getAttribute("title")) {
var texto = qualF.getAttribute("title");
} else {
var text = "";
}
if (legenda.firstChild.nodeType == 3) {
legenda.firstChild.nodeValue = texto;
}
return false;
}
addLoadEvent(iniciarLboxBg);
addLoadEvent(iniciarLbox);
addLoadEvent(iniciarLinksPics);