var Milkbox=new Class({Implements:[Options,Events],options:{overlayOpacity:.7,topPosition:50,initialWidth:250,initialHeight:250,canvasBorderWidth:"0px",canvasBorderColor:"#000000",canvasPadding:"0px",resizeDuration:500,resizeTransition:"sine:in:out",autoPlay:!1,autoPlayDelay:7,removeTitle:!1,autoSize:!0,maxHeight:0,imageOfText:"of",onXmlGalleries:$empty,onClosed:$empty,onFileReady:$empty},initialize:function(n){this.setOptions(n),this.autoPlayBkup={autoPlayDelay:this.options.autoPlayDelay,autoPlay:this.options.autoPlay},this.fullOptionsBkup={},this.galleries=[],this.families=[],this.xmlFiles=[],this.loadedImages=[],this.currentFile=null,this.currentIndex=null,this.currentGallery=null,this.currentRequest=null,this.currentResponse=null,this.mode=null,this.closed=!0,this.busy=!0,this.paused=!0,this.fileReady=!1,this.eventsok=!1,this.first=!0,this.activated=!1,this.intObj=null,this.formtags=null,this.prepareGalleries(),this.options.overlayOpacity==0&&(this.options.overlayOpacity=.0001),this.saveOptions();if(this.galleries.length==0)return;this.initMilkbox(!0)},initMilkbox:function(n){n&&(this.formtags=$$("select","textarea")),this.prepareHTML(),this.prepareEffects(),this.prepareEvents(),this.activated=!0},openMilkbox:function(n,t){var u,e,i;this.closed=!1,this.formtags&&this.formtags.length!=0&&this.formtags.setStyle("display","none"),this.overlay.setStyles({top:-$(window).getScroll().y,height:$(window).getScrollSize().y+$(window).getScroll().y}),this.center.setStyle("top",$(window).getScroll().y+this.options.topPosition),this.currentGallery=n,this.currentIndex=t,this.overlay.tween("opacity",this.options.overlayOpacity);if(n.length==1)this.mode="singleFile",this.loadFile(n[t],t);else{this.mode="fileGallery",u=0,$$(this.prev,this.next,this.count).setStyle("display","block"),this.options.autoPlay&&(this.playpause.setStyle("display","block"),u=this.playpause.getSize().x),e=this.center.getStyle("border-right-width").toInt(),i=this.prev.getSize().x+this.next.getSize().x+this.close.getSize().x+u+e,this.navigation.setStyle("width",i),this.description.setStyle("margin-right",i);var f=t!=n.length-1?n[t+1]:n[0],r=t!=0?n[t-1]:n[n.length-1],o=r==f?[r]:[r,f];this.loadFile(n[t],o)}},loadFile:function(n,t){this.fileReady=!1;var i=this.checkFileType(n,"swf");i?this.loadSwf(n):(this.loadedImages.contains(n.retrieve("href"))||this.center.addClass("mbLoading"),this.loadImage(n.retrieve("href"))),t&&this.preloadFiles(t)},preloadFiles:function(n){n.each(function(n){var i=this.checkFileType(n.retrieve("href"),"swf");i||this.preloadImage(n.retrieve("href"))},this)},loadImage:function(n){var t=new Asset.image(n,{onload:function(t){this.loadedImages.contains(n)||this.loadedImages.push(n),this.currentFile=t,this.loadAux(this.currentFile)}.bindWithEvent(this)})},preloadImage:function(n){if(!this.loadedImages.contains(n))var t=new Asset.image(n,{onload:function(){this.loadedImages.push(n)}.bindWithEvent(this)})},loadSwf:function(n){var t=new Swiff(n.retrieve("href"),{width:n.retrieve("width").toInt(),height:n.retrieve("height").toInt(),params:{wMode:"opaque",swLiveConnect:"false"}});this.currentFile=t,this.loadAux(n)},loadAux:function(n){this.fileReady=!0,this.fireEvent("fileReady"),$$(this.description,this.navigation).setStyle("visibility","hidden"),this.navigation.setStyle("height",""),$$(this.next,this.prev,this.close).setStyle("backgroundPosition","0 0"),this.showFile(n)},showFile:function(n){if(this.closed)return;var t=new Hash,f=new Hash,i,u,r,s,h,e,o;i=u={},r=s=h=e=o=0,this.options.canvasBorderWidth.toInt()!=0&&this.canvas.getStyle("borderWidth").toInt()==0&&(h=this.options.canvasBorderWidth+" solid "+this.options.canvasBorderColor,this.canvas.setStyle("border",h)),this.options.canvasPadding.toInt()!=0&&this.canvas.getStyle("padding").toInt()==0&&(e=this.options.canvasPadding,this.canvas.setStyle("padding",e)),u=this.canvas.getSize(),r=this.canvas.getStyle("borderWidth").toInt()*2+this.canvas.getStyle("padding").toInt()*2,this.canvas.setStyles({opacity:0,width:"",height:""}),n.retrieve("width")?t.extend({height:n.retrieve("height").toInt(),width:n.retrieve("width").toInt()}):(t=t.extend(n.getProperties("width","height")).map(function(n){return n.toInt()}),this.options.autoSize&&(t=this.computeSize(t),n.setProperties({width:t.width,height:t.height}))),f=f.extend(this.center.getStyles("width","height")).map(function(n){return n.toInt()}),t.width!=f.width&&(i.width=t.width+r,i.marginLeft=-(i.width/2).round()),s=u.y-r>0?f.height-u.y:0,i.height=t.height+r+s,this.canvas.setStyles({width:t.width,height:t.height}),this.center.removeClass("mbLoading"),this.first&&(o=500,this.first=!1),(function(){this.center.morph(i)}).delay(o,this)},computeSize:function(n){var t=n,e=window.getSize(),u={width:e.x-60,height:e.y-68-this.options.topPosition*2},i,r,f=Math.max(u.height,u.width);return f==u.width?(i=f/t.width,r="height"):(i=f/t.height,r="width"),i=i<=1?i:1,t=t.map(function(n){return Math.floor(n*i)}),i=u[r]/t[r]<=1?u[r]/t[r]:1,t=t.map(function(n){return Math.floor(n*i)}),this.options.maxHeight>0&&(i=this.options.maxHeight/t.height<1?this.options.maxHeight/t.height:1,t=t.map(function(n){return Math.floor(n*i)})),t},showGallery:function(n){if(!n||!n.gallery)return;var u=$chk(n.index)?n.index:0,t=this.getGallery(n.gallery),r=!1,i;(n.autoplay||t.options&&t.options.autoplay)&&(r=!0),t==-1||this.opened||(r?(i=n&&n.delay?n.delay:t.options&&t.options.delay?t.options.delay:this.autoPlayDelay,this.startAutoPlay({gallery:t,index:u,delay:i})):this.openMilkbox(t,u))},addGalleries:function(n){this.currentRequest=new Request({method:"get",autoCancel:!0,url:n,onRequest:function(){}.bindWithEvent(this),onSuccess:function(t){var r=t.replace(/(<a.+)\/>/gi,"$1></a>");this.setGalleries(new Element("div",{html:r}),n)}.bindWithEvent(this),onFailure:function(){alert("Milkbox :: addGalleries: XML file path error or local Ajax test: please test addGalleries() on-line")}}),this.currentRequest.send()},setGalleries:function(n,t){this.xmlFiles.contains(t)||this.xmlFiles.push(t);var u=n,f=u.getElements(".gallery"),i=[],r=[];f.each(function(n){var u={gallery:n.getProperty("name"),autoplay:Boolean(n.getProperty("autoplay")),delay:Number(n.getProperty("delay"))},e=n.getChildren("a"),f=e.map(function(n){return n.setProperty("rel","milkbox["+u.gallery+"]")});i.push(f),u.autoplay&&r.push(u)}),this.prepareGalleries(i.flatten()),this.setAutoPlay(r),this.activated||this.initMilkbox(),this.fireEvent("xmlGalleries")},checkFileType:function(n,t){var i=null,r;return i=$type(n)!="string"?n.retrieve("href"):n,r=new RegExp(".("+t+")$","i"),i.split("?")[0].test(r)},getGallery:function(n){var i=null,t,r;return i=n.test(/^milkbox/i)?this.families:this.families.map(function(n){var t=n.trim(),r=t.slice(0,t.length).substr(8);return r.replace(/(.+)]$/,"$1")}),t=i.indexOf(n),r=t!=-1?this.galleries[t]:t,r},setFileProps:function(n,t){var i=t.split(",");i.each(function(t){var r=t.trim().split(":");n.store(r[0].trim(),r[1].trim())},this)},changeOptions:function(n){if(!n)return;this.setOptions(n),this.center.get("morph").setOptions({transition:this.options.resizeTransition,duration:this.options.resizeDuration})},saveOptions:function(n){this.fullOptionsBkup=$chk(n)?n:this.options},restoreOptions:function(){this.setOptions(this.fullOptionsBkup);var n=this.options.canvasBorderWidth+" solid "+this.options.canvasBorderColor;this.canvas.setStyles({border:n,padding:this.options.canvasPadding}),this.center.get("morph").setOptions({transition:this.options.resizeTransition,duration:this.options.resizeDuration})},reloadGalleries:function(){this.galleries=[],this.families=[],this.formtags=$$("select","textarea"),this.activated||this.initMilkbox(!1),this.prepareGalleries(),this.removeGalleriesEvents(),this.setGalleriesEvents();if(this.xmlFiles.length==0)return;this.xmlFiles.each(function(n){this.addGalleries(n)}.bind(this))},setAutoPlay:function(n){var t=$type(n)=="object"?[n]:n;t.each(function(n){var r=this.getGallery(n.gallery),t,i;if(r==-1)return;t=n.autoplay==!0?n.autoplay:!1,i=$chk(n.delay)&&t?n.delay:this.options.autoPlayDelay,r.options={autoplay:t,delay:i}},this)},startAutoPlay:function(n){var t=-1,r,i;n&&n.gallery&&($type(n.gallery)=="array"?t=n.gallery:$type(n.gallery)=="string"&&(t=this.getGallery(n.gallery))),t==-1&&(t=this.galleries[0]),i=n&&n.delay&&$type(n.delay)=="number"?n.delay*1e3:t.options&&t.options.delay?t.options.delay*1e3:this.options.autoPlayDelay*1e3,r=n&&n.index&&$type(n.index)=="number"?n.index:0,i<this.options.resizeDuration*2&&(i=this.options.resizeDuration*2),this.options.autoPlayDelay=i/1e3,this.options.autoPlay||this.setOptions({autoPlay:!0,autoPlayDelay:this.options.autoPlayDelay});if(this.closed){this.openMilkbox(t,r);if(this.mode!="fileGallery")return;this.addEvent("fileReady",function(){this.intObj=this.next_prev_aux.periodical(i,this,[null,"next"]),this.removeEvents("fileReady")}.bindWithEvent(this))}else this.closed||this.next_prev_aux(null,"next"),this.intObj=this.next_prev_aux.periodical(i,this,[null,"next"]);this.paused=!1},stopAutoPlay:function(){this.intObj&&($clear(this.intObj),this.intObj=null),this.playpause.setStyle("backgroundPosition","0 -44px"),this.paused=!0},removeGalleriesEvents:function(){this.galleries.each(function(n){$$(n).removeEvents("click")},this)},setGalleriesEvents:function(){this.galleries.each(function(n){$$(n).addEvent("click",function(t){var r=$(t.target).match("a")?$(t.target):$(t.target).getParent("a"),i;t.preventDefault(),i=this.getGallery(r.rel),i.options&&i.options.autoplay&&this.setOptions({autoPlay:i.options.autoplay,autoPlayDelay:i.options.delay}),this.options.autoPlay?this.startAutoPlay({gallery:n,index:n.indexOf(r)}):this.openMilkbox(n,n.indexOf(r))}.bindWithEvent(this))},this)},prepareEvents:function(){this.setGalleriesEvents(),this.next.addEvent("click",this.next_prev_aux.bindWithEvent(this,"next")),this.prev.addEvent("click",this.next_prev_aux.bindWithEvent(this,"prev")),$$(this.next,this.prev,this.close).addEvents({mouseover:function(){this.setStyle("backgroundPosition","0 -22px")},mouseout:function(){this.setStyle("backgroundPosition","0 0")}}),$(window.document).addEvent("keydown",function(n){if(this.mode!="fileGallery"||this.busy==!0)return;n.key=="right"||n.key=="space"?this.next_prev_aux(n,"next"):n.key=="left"?this.next_prev_aux(n,"prev"):n.key=="esc"&&this.closeMilkbox()}.bindWithEvent(this)),this.playpause.addEvents({mouseover:function(){this.paused==!1?this.playpause.setStyle("backgroundPosition","0 -22px"):this.playpause.setStyle("backgroundPosition","0 -66px")}.bindWithEvent(this),mouseout:function(){this.paused==!1?this.playpause.setStyle("backgroundPosition","0 0"):this.playpause.setStyle("backgroundPosition","0 -44px")}.bindWithEvent(this),click:function(){if(this.paused==!1)this.stopAutoPlay(),this.paused=!0,this.playpause.setStyle("backgroundPosition","0 -66px");else{var n=this.currentGallery.options&&this.currentGallery.options.delay?this.currentGallery.options.delay:this.options.autoPlayDelay;this.startAutoPlay({gallery:this.currentGallery,index:this.currentIndex+1,delay:n}),this.paused=!1,this.playpause.setStyle("backgroundPosition","0 0")}}.bindWithEvent(this)}),this.overlay.get("tween").addEvent("onComplete",function(){this.overlay.getStyle("opacity")==this.options.overlayOpacity?this.center.tween("opacity",1):this.overlay.getStyle("opacity")==0&&this.overlay.setStyles({height:0,top:""})}.bindWithEvent(this)),this.center.get("morph").addEvent("onComplete",function(){var t,n,r,i;$type(this.currentFile)=="element"?this.canvas.grab(this.currentFile):(function(){this.canvas.grab(this.currentFile)}).delay(500,this),this.canvas.tween("opacity",1),t=this.mode=="showThisImage"?this.specialDescription:this.currentGallery[this.currentIndex].retrieve("title"),$chk(t)&&(this.description.innerHTML=t),this.mode=="fileGallery"&&this.count.appendText(this.currentIndex+1+" "+this.options.imageOfText+" "+this.currentGallery.length),n=this.center.getStyle("height").toInt(),this.navigation.setStyle("height",this.bottom.getStyle("height").toInt()),r=this.bottom.getSize().y,i=n>this.canvas.getSize().y?this.bottom.getSize().y+this.canvas.getSize().y-n:r,this.bottom.setStyle("display","none"),this.center.retrieve("setFinalHeight").start(n,n+i)}.bindWithEvent(this)),this.center.retrieve("setFinalHeight").addEvent("onComplete",function(){this.bottom.setStyles({visibility:"visible",display:"block"}),$$(this.description,this.navigation).setStyle("visibility","visible");var t=$(window).getScrollSize().y,n=$(window).getScroll().y;this.overlay.setStyles({height:t+n,top:-n}),this.busy=!1}.bindWithEvent(this)),window.addEvent("resize",function(){if(this.overlay.getStyle("opacity")==0)return;var t=$(window).getScrollSize().y,n=$(window).getScroll().y;this.overlay.setStyles({height:t+n,top:-n})}.bindWithEvent(this)),$$(this.overlay,this.close).addEvent("click",this.closeMilkbox.bindWithEvent(this)),this.eventsok=!0},next_prev_aux:function(n,t){if(n)n.preventDefault(),this.stopAutoPlay();else if(this.busy||!this.fileReady)return;this.busy=!0;var r,i;t=="next"?(r=this.currentIndex!=this.currentGallery.length-1?this.currentIndex+=1:this.currentIndex=0,i=this.currentIndex!=this.currentGallery.length-1?this.currentIndex+1:0):(r=this.currentIndex!=0?this.currentIndex-=1:this.currentIndex=this.currentGallery.length-1,i=this.currentIndex!=0?this.currentIndex-1:this.currentGallery.length-1),this.canvas.empty(),this.description.empty(),this.count.empty(),this.loadFile(this.currentGallery[r],[this.currentGallery[i]])},prepareEffects:function(){this.overlay.set("tween",{duration:"short",link:"cancel"}),this.center.set("tween",{duration:"short",link:"chain"}),this.center.set("morph",{duration:this.options.resizeDuration,link:"chain",transition:this.options.resizeTransition}),this.center.store("setFinalHeight",new Fx.Tween(this.center,{property:"height",duration:"short"})),this.canvas.set("tween",{link:"chain"})},prepareGalleries:function(n){var t=[],i=n?n:$$("a");i.each(function(n){n.rel&&n.rel.test(/^milkbox/i)&&n.href.split("?")[0].test(/\.(gif|jpg|jpeg|png|swf)$/i)&&(n.rel.length>7&&!this.families.contains(n.rel)&&this.families.push(n.rel),t.push(n))},this),t.each(function(n){$(n).store("href",n.href),$(n).store("rel",n.rel),$(n).store("title",n.title),this.checkFileType(n.href,"swf")&&this.setFileProps($(n),n.rev),this.options.removeTitle&&$(n).removeProperty("title"),n.rel.length>7?this.families.each(function(t){if(n.rel==t){var u=!1,r;this.galleries.each(function(n,i){if(n[0].rel==t){u=!0,r=i;return}}),u==!0?this.galleries[r].push($(n)):this.galleries.push([$(n)])}},this):this.galleries.push([$(n)])},this)},prepareHTML:function(){this.overlay=new Element("div",{id:"mbOverlay",styles:{opacity:0,visibility:"visible",height:0,overflow:"hidden"}}).inject($(document.body)),this.center=new Element("div",{id:"mbCenter",styles:{width:this.options.initialWidth,height:this.options.initialHeight,marginLeft:-(this.options.initialWidth/2),opacity:0}}).inject($(document.body)),this.canvas=new Element("div",{id:"mbCanvas"}).inject(this.center),this.bottom=new Element("div",{id:"mbBottom"}).inject(this.center).setStyle("visibility","hidden"),this.navigation=new Element("div",{id:"mbNavigation"}).setStyle("visibility","hidden"),this.description=new Element("div",{id:"mbDescription"}).setStyle("visibility","hidden"),this.bottom.adopt(this.navigation,this.description,new Element("div",{"class":"mbClear"})),this.close=new Element("a",{id:"mbCloseLink"}),this.next=new Element("a",{id:"mbNextLink"}),this.prev=new Element("a",{id:"mbPrevLink"}),this.playpause=new Element("a",{id:"mbPlayPause"}),this.count=new Element("span",{id:"mbCount"}),$$(this.next,this.prev,this.count,this.playpause).setStyle("display","none"),this.navigation.adopt(this.close,this.next,this.prev,this.playpause,new Element("div",{"class":"mbClear"}),this.count)},closeMilkbox:function(){this.cancelAllEffects(),this.stopAutoPlay(),this.setOptions(this.autoPlayBkup),this.currentFile=null,this.currentIndex=null,this.currentGallery=null,this.currentRequest=null,this.currentResponse=null,$$(this.prev,this.next,this.playpause,this.count).setStyle("display","none"),this.playpause.setStyle("backgroundPosition","0 0");var t=this.center.getStyle("border-right-width").toInt(),n=this.close.getSize().x+t;this.navigation.setStyles({width:n,height:"",visibility:"hidden"}),this.description.setStyle("margin-right",n),this.description.empty(),this.bottom.setStyles({visibility:"hidden",display:""}),this.canvas.setStyles({opacity:0,width:"",height:""}),this.canvas.empty(),this.count.empty(),this.center.setStyles({opacity:0,width:this.options.initialWidth,height:this.options.initialHeight,marginLeft:-(this.options.initialWidth/2)}),this.overlay.tween("opacity",0),this.formtags&&this.formtags.length!=0&&this.formtags.setStyle("display",""),this.mode=null,this.closed=!0,this.first=!0,this.fileReady=!1,this.fireEvent("closed")},cancelAllEffects:function(){this.overlay.get("tween").cancel(),this.center.get("morph").cancel(),this.center.get("tween").cancel(),this.center.retrieve("setFinalHeight").cancel(),this.canvas.get("tween").cancel()}})
