/*  ----------------------------------------------------------------------
 *  This is a compressed and slightly modified version of:
 * 	Lightbox v2.05
 *	by Lokesh Dhakar - http://www.lokeshdhakar.com
 *	Last Modification: 3/18/11
 *
 *	For more information, visit:
 *	http://lokeshdhakar.com/projects/lightbox2/
 *
 *	Licensed under the Creative Commons Attribution 2.5 License
 *  http://creativecommons.org/licenses/by/2.5/
 *  - Free for use in both personal and commercial projects
 *	- Attribution requires leaving author name, author link, and the license
 *    info intact.
 *	
 *  Thanks: Scott Upton(uptonic.com), Peter-Paul Koch(quirksmode.com),
 *  and Thomas Fuchs(mir.aculo.us) for ideas, libs, and snippets.
 *  Artemy Tregubenko (arty.name) for cleanup and help in updating to latest
 *  ver of proto-aculous.
 *  --------------------------------------------------------------------- */
LightboxOptions=Object.extend({fileLoadingImage:BM.assetsDir+"/lightbox_loading.gif",fileBottomNavCloseImage:BM.assetsDir+"/lightbox_close.gif",overlayOpacity:0.8,animate:true,resizeSpeed:9,borderSize:10,labelImage:"",labelOf:"/"},window.LightboxOptions||{});
var Lightbox=Class.create();
Lightbox.prototype={imageArray:[],activeImage:undefined,initialize:function(){this.updateImageList();
this.keyboardAction=this.keyboardAction.bindAsEventListener(this);
if(LightboxOptions.resizeSpeed>10){LightboxOptions.resizeSpeed=10
}if(LightboxOptions.resizeSpeed<1){LightboxOptions.resizeSpeed=1
}this.resizeDuration=LightboxOptions.animate?((11-LightboxOptions.resizeSpeed)*0.15):0;
this.overlayDuration=LightboxOptions.animate?0.2:0;
var b=(LightboxOptions.animate?250:1)+"px";
var a=$$("body")[0];
a.appendChild(Builder.node("div",{id:"bmlbox_overlay"}));
a.appendChild(Builder.node("div",{id:"bmlbox_lightbox"},[Builder.node("div",{id:"bmlbox_outerImageContainer"},Builder.node("div",{id:"bmlbox_imageContainer"},[Builder.node("img",{id:"bmlbox_lightboxImage"}),Builder.node("div",{id:"bmlbox_hoverNav"},[Builder.node("a",{id:"bmlbox_prevLink",href:"#"}),Builder.node("a",{id:"bmlbox_nextLink",href:"#"})]),Builder.node("div",{id:"bmlbox_loading"},Builder.node("a",{id:"bmlbox_loadingLink",href:"#"},Builder.node("img",{src:LightboxOptions.fileLoadingImage})))])),Builder.node("div",{id:"bmlbox_imageDataContainer"},Builder.node("div",{id:"bmlbox_imageData"},[Builder.node("div",{id:"bmlbox_imageDetails"},[Builder.node("span",{id:"bmlbox_caption"}),Builder.node("span",{id:"bmlbox_numberDisplay"})]),Builder.node("div",{id:"bmlbox_bottomNav"},Builder.node("a",{id:"bmlbox_bottomNavClose",href:"#"},Builder.node("img",{src:LightboxOptions.fileBottomNavCloseImage})))]))]));
$("bmlbox_overlay").hide().observe("click",(function(){this.end()
}).bind(this));
$("bmlbox_lightbox").hide().observe("click",(function(d){if(d.element().id=="lightbox"){this.end()
}}).bind(this));
$("bmlbox_outerImageContainer").setStyle({width:b,height:b});
$("bmlbox_prevLink").observe("click",(function(d){d.stop();
this.changeImage(this.activeImage-1)
}).bindAsEventListener(this));
$("bmlbox_nextLink").observe("click",(function(d){d.stop();
this.changeImage(this.activeImage+1)
}).bindAsEventListener(this));
$("bmlbox_loadingLink").observe("click",(function(d){d.stop();
this.end()
}).bind(this));
$("bmlbox_bottomNavClose").observe("click",(function(d){d.stop();
this.end()
}).bind(this));
[$("bmlbox_prevLink"),$("bmlbox_nextLink")].each(function(d){d.setOpacity(0.3);
d.onmouseover=function(){d.setOpacity(0.99)
};
d.onmouseout=function(){d.setOpacity(0.3)
}
});
var c=this;
(function(){var d="overlay lightbox outerImageContainer imageContainer lightboxImage hoverNav prevLink nextLink loading loadingLink imageDataContainer imageData imageDetails caption numberDisplay bottomNav bottomNavClose";
$w(d).each(function(e){c[e]=$("bmlbox_"+e)
})
}).defer()
},updateImageList:function(){this.updateImageList=Prototype.emptyFunction;
document.observe("click",(function(a){var b=a.findElement("a[rel^=bm_lightbox]")||a.findElement("area[rel^=bm_lightbox]");
if(b){a.stop();
this.start(b)
}}).bind(this))
},start:function(f){$$("select","object","embed").each(function(g){g.style.visibility="hidden"
});
var c=this.getPageSize();
$("bmlbox_overlay").setStyle({width:c[0]+"px",height:c[1]+"px"});
new Effect.Appear(this.overlay,{duration:this.overlayDuration,from:0,to:LightboxOptions.overlayOpacity});
this.imageArray=[];
var b=0;
if((f.getAttribute("rel")=="bm_lightbox")){this.imageArray.push([f.href,f.title])
}else{this.imageArray=$$(f.tagName+'[href][rel="'+f.rel+'"]').collect(function(g){return[g.href,g.title]
}).uniq();
while(this.imageArray[b][0]!=f.href){b++
}}this.hovernavHeight=this.imageArray.length>1?35:0;
this.lightboxImage.style.marginTop=this.hovernavHeight+"px";
this.hoverNav.style.height=this.hovernavHeight?"100%":"0px";
var a=document.viewport.getScrollOffsets();
var e=a[1]+(document.viewport.getHeight()/20);
var d=a[0];
this.lightbox.setStyle({top:e+"px",left:d+"px"}).show();
this.changeImage(b)
},changeImage:function(b){this.activeImage=b;
if(LightboxOptions.animate){this.loading.show()
}this.lightboxImage.hide();
this.hoverNav.hide();
this.prevLink.hide();
this.nextLink.hide();
this.imageDataContainer.setStyle({opacity:0.0001});
this.numberDisplay.hide();
var a=new Image();
a.onload=(function(){this.lightboxImage.src=this.imageArray[this.activeImage][0];
var e=window.innerHeight||document.documentElement.clientHeight;
var d=window.innerWidth||document.documentElement.clientWidth;
var i=d<481?(d-40):800;
var h=e*0.8;
var c=a.height;
var g=a.width;
if(i<g){var f=i/g;
g=Math.floor(f*g);
c=Math.floor(f*c)
}if(h<c){var f=h/c;
c=Math.floor(f*c);
g=Math.floor(f*g)
}this.lightboxImage.width=g;
this.lightboxImage.height=c;
this.resizeImageContainer(g,c)
}).bind(this);
a.src=this.imageArray[this.activeImage][0]
},resizeImageContainer:function(e,f){var h=this.outerImageContainer.getWidth();
var c=this.outerImageContainer.getHeight();
var g=(e+LightboxOptions.borderSize*2);
var j=(f+LightboxOptions.borderSize*2);
j=j+this.hovernavHeight;
var k=(g/h)*100;
var b=(j/c)*100;
var i=h-g;
var a=c-j;
if(a!=0){new Effect.Scale(this.outerImageContainer,b,{scaleX:false,duration:this.resizeDuration,queue:"front"})
}if(i!=0){new Effect.Scale(this.outerImageContainer,k,{scaleY:false,duration:this.resizeDuration})
}var d=0;
if((a==0)&&(i==0)){d=100;
if(Prototype.Browser.IE){d=250
}}(function(){this.prevLink.setStyle({height:f+"px"});
this.nextLink.setStyle({height:f+"px"});
this.imageDataContainer.setStyle({width:g+"px"});
this.showImage()
}).bind(this).delay(d/1000)
},showImage:function(){this.loading.hide();
new Effect.Appear(this.lightboxImage,{duration:this.resizeDuration,queue:"end",afterFinish:(function(){this.updateDetails()
}).bind(this)});
this.preloadNeighborImages()
},updateDetails:function(){this.caption.update(this.imageArray[this.activeImage][1]).show();
if(this.imageArray.length>1){this.numberDisplay.update(LightboxOptions.labelImage+" "+(this.activeImage+1)+" "+LightboxOptions.labelOf+"  "+this.imageArray.length).show()
}new Effect.Parallel([new Effect.SlideDown(this.imageDataContainer,{sync:true,duration:this.resizeDuration,from:0,to:1}),new Effect.Appear(this.imageDataContainer,{sync:true,duration:this.resizeDuration})],{duration:this.resizeDuration,afterFinish:(function(){var a=this.getPageSize();
this.overlay.setStyle({width:a[0]+"px",height:a[1]+"px"});
this.updateNav()
}).bind(this)})
},updateNav:function(){this.hoverNav.show();
if(this.activeImage>0){this.prevLink.show()
}if(this.activeImage<(this.imageArray.length-1)){this.nextLink.show()
}this.enableKeyboardNav()
},enableKeyboardNav:function(){document.observe("keydown",this.keyboardAction)
},disableKeyboardNav:function(){document.stopObserving("keydown",this.keyboardAction)
},keyboardAction:function(d){var a=d.keyCode;
var b;
if(d.DOM_VK_ESCAPE){b=d.DOM_VK_ESCAPE
}else{b=27
}var c=String.fromCharCode(a).toLowerCase();
if(c.match(/x|o|c/)||(a==b)){this.end()
}else{if((c=="p")||(a==37)){if(this.activeImage!=0){this.disableKeyboardNav();
this.changeImage(this.activeImage-1)
}}else{if((c=="n")||(a==39)){if(this.activeImage!=(this.imageArray.length-1)){this.disableKeyboardNav();
this.changeImage(this.activeImage+1)
}}}}},preloadNeighborImages:function(){var a,b;
if(this.imageArray.length>this.activeImage+1){a=new Image();
a.src=this.imageArray[this.activeImage+1][0]
}if(this.activeImage>0){b=new Image();
b.src=this.imageArray[this.activeImage-1][0]
}},end:function(){this.disableKeyboardNav();
this.lightbox.hide();
new Effect.Fade(this.overlay,{duration:this.overlayDuration});
$$("select","object","embed").each(function(a){a.style.visibility="visible"
})
},getPageSize:function(){var c,a;
if(window.innerHeight&&window.scrollMaxY){c=window.innerWidth+window.scrollMaxX;
a=window.innerHeight+window.scrollMaxY
}else{if(document.body.scrollHeight>document.body.offsetHeight){c=document.body.scrollWidth;
a=document.body.scrollHeight
}else{c=document.body.offsetWidth;
a=document.body.offsetHeight
}}var b,d;
if(self.innerHeight){if(document.documentElement.clientWidth){b=document.documentElement.clientWidth
}else{b=self.innerWidth
}d=self.innerHeight
}else{if(document.documentElement&&document.documentElement.clientHeight){b=document.documentElement.clientWidth;
d=document.documentElement.clientHeight
}else{if(document.body){b=document.body.clientWidth;
d=document.body.clientHeight
}}}if(a<d){pageHeight=d
}else{pageHeight=a
}if(c<b){pageWidth=c
}else{pageWidth=b
}return[pageWidth,pageHeight]
}};
document.observe("dom:loaded",function(){new Lightbox()
});
