var Milkbox=new Class({Implements:[Options,Events],options:{overlayOpacity:0.7,topPosition:50,initialWidth:250,initialHeight:250,canvasBorderWidth:"0px",canvasBorderColor:"#000000",canvasPadding:"0px",resizeDuration:500,resizeTransition:"sine:in:out",autoPlay:false,autoPlayDelay:7,removeTitle:false,onXmlGalleries:$empty,onClosed:$empty,onFileReady:$empty},initialize:function(A){this.setOptions(A);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=true;this.busy=true;this.paused=true;this.fileReady=false;this.eventsok=false;this.first=true;this.intObj=null;this.formtags=null;this.prepareGalleries();if(this.galleries.length==0){return }this.initMilkbox();this.saveOptions()},initMilkbox:function(){this.prepareHTML();this.prepareEffects();this.prepareEvents();this.formtags=$$("select","textarea");this.activated=true},openMilkbox:function(B,D){this.closed=false;if(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=B;this.currentIndex=D;this.overlay.tween("opacity",this.options.overlayOpacity);if(B.length==1){this.mode="singleFile";this.loadFile(B[D],D)}else{this.mode="fileGallery";var A=0;$$(this.prev,this.next,this.count).setStyle("display","block");if(this.options.autoPlay){this.playpause.setStyle("display","block");var A=this.playpause.getSize().x}var C=this.center.getStyle("border-right-width").toInt();var H=this.prev.getSize().x+this.next.getSize().x+this.close.getSize().x+A+C;this.navigation.setStyle("width",H);this.description.setStyle("margin-right",H);var F=(D!=B.length-1)?B[D+1]:B[0];var G=(D!=0)?B[D-1]:B[B.length-1];var E=(G==F)?[G]:[G,F];this.loadFile(B[D],E)}},loadFile:function(C,B){this.fileReady=false;var A=this.checkFileType(C,"swf");if(!A){if(!this.loadedImages.contains(C.retrieve("href"))){this.center.addClass("mbLoading")}this.loadImage(C.retrieve("href"))}else{this.loadSwf(C)}if(B){this.preloadFiles(B)}},preloadFiles:function(A){A.each(function(D,B){var C=this.checkFileType(D.retrieve("href"),"swf");if(!C){this.preloadImage(D.retrieve("href"))}},this)},loadImage:function(B){var A=new Asset.image(B,{onload:function(C){if(!this.loadedImages.contains(B)){this.loadedImages.push(B)}this.currentFile=C;this.loadAux(this.currentFile)}.bindWithEvent(this)})},preloadImage:function(B){if(!this.loadedImages.contains(B)){var A=new Asset.image(B,{onload:function(C){this.loadedImages.push(B)}.bindWithEvent(this)})}},loadSwf:function(B){var A=new Swiff(B.retrieve("href"),{width:B.retrieve("width").toInt(),height:B.retrieve("height").toInt(),params:{wMode:"opaque",swLiveConnect:"false"}});this.currentFile=A;this.loadAux(B)},loadAux:function(A){this.fileReady=true;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(A)},showFile:function(B){if(this.closed){return }var D=new Hash();var E={};var G=null;var A=null;if(this.options.canvasBorderWidth.toInt()!=0&&this.canvas.getStyle("borderWidth").toInt()==0){G=this.options.canvasBorderWidth+" solid "+this.options.canvasBorderColor;this.canvas.setStyle("border",G)}if(this.options.canvasPadding.toInt()!=0&&this.canvas.getStyle("padding").toInt()==0){A=this.options.canvasPadding;this.canvas.setStyle("padding",A)}var J=this.canvas.getSize();var C=this.canvas.getStyle("borderWidth").toInt()*2+this.canvas.getStyle("padding").toInt()*2;this.canvas.setStyles({opacity:0,width:"",height:""});if(!B.retrieve("width")){D=D.extend(B.getProperties("width","height")).map(function(L,K){return L.toInt()})}else{D.extend({height:B.retrieve("height").toInt(),width:B.retrieve("width").toInt()})}var I=new Hash(this.center.getStyles("width","height")).map(function(L,K){return L.toInt()});if(D.width!=I.width){E.width=D.width+C;E.marginLeft=-(E.width/2).round()}var H=(J.y-C>0)?I.height-J.y:0;E.height=D.height+C+H;this.canvas.setStyles({width:D.width,height:D.height});this.center.removeClass("mbLoading");var F=0;if(this.first){F=500;this.first=false}(function(){this.center.morph(E)}).delay(F,this)},showGallery:function(A){if(!A||!A.gallery){return }var C=($chk(A.index))?A.index:0;var B=this.getGallery(A.gallery);var E=false;var D;if(A.autoplay||(B.options&&B.options.autoplay)){E=true}if(B!=-1&&!this.opened){if(E){D=(A&&A.delay)?A.delay:(B.options&&B.options.delay)?B.options.delay:this.autoPlayDelay;this.startAutoPlay({gallery:B,index:C,delay:D})}else{this.openMilkbox(B,C)}}},addGalleries:function(A){this.currentRequest=new Request({method:"get",autoCancel:true,url:A,onRequest:function(){}.bindWithEvent(this),onSuccess:function(D,B){var C=D.replace(/(<a.+)\/>/gi,"$1></a>");this.setGalleries(new Element("div",{html:C}),A)}.bindWithEvent(this),onFailure:function(B){alert("Milkbox :: addGalleries: XML file path error or local Ajax test: please test addGalleries() on-line")}});this.currentRequest.send()},setGalleries:function(C,E){if(!this.xmlFiles.contains(E)){this.xmlFiles.push(E)}var F=C;var B=F.getElements(".gallery");var D=[];var A=[];B.each(function(H,I){var K={gallery:H.getProperty("name"),autoplay:Boolean(H.getProperty("autoplay")),delay:Number(H.getProperty("delay"))};var G=H.getChildren("a");var J=G.map(function(L){return L.setProperty("rel","milkbox["+K.gallery+"]")});D.push(J);if(K.autoplay){A.push(K)}});this.prepareGalleries(D.flatten());this.setAutoPlay(A);if(!this.activated){this.initMilkbox()}this.fireEvent("xmlGalleries")},checkFileType:function(B,C){var A=null;if($type(B)!="string"){A=B.retrieve("href")}else{A=B}var D=new RegExp(".("+C+")$","i");return A.split("?")[0].test(D)},getGallery:function(A){var D=null;if(A.test(/^milkbox/i)){D=this.families}else{D=this.families.map(function(G){var H=G.trim();var E=H.slice(0,H.length).substr(8);var F=E.replace(/(.+)]$/,"$1");return F})}var B=D.indexOf(A);var C=(B!=-1)?this.galleries[B]:B;return C},setFileProps:function(C,A){var B=A.split(",");B.each(function(F,E){var D=F.trim().split(":");C.store(D[0].trim(),D[1].trim())},this)},changeOptions:function(A){if(!A){return }this.setOptions(A);this.center.get("morph").setOptions({transition:this.options.resizeTransition,duration:this.options.resizeDuration})},saveOptions:function(A){if($chk(A)){this.fullOptionsBkup=A}else{this.fullOptionsBkup=this.options}},restoreOptions:function(){this.setOptions(this.fullOptionsBkup);b=this.options.canvasBorderWidth+" solid "+this.options.canvasBorderColor;this.canvas.setStyles({border:b,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.prepareGalleries();this.removeGalleriesEvents();this.setGalleriesEvents();if(this.xmlFiles.length==0){return }this.xmlFiles.each(function(B,A){this.addGalleries(B)}.bind(this))},setAutoPlay:function(B){var A=($type(B)=="object")?[B]:B;A.each(function(E){var D=this.getGallery(E.gallery);if(D==-1){return }var C=(E.autoplay==true)?E.autoplay:false;var F=($chk(E.delay)&&C)?E.delay:this.options.autoPlayDelay;D.options={autoplay:C,delay:F}},this)},startAutoPlay:function(B){var C=-1;var A,D;if(B&&B.gallery){if($type(B.gallery)=="array"){C=B.gallery}else{if($type(B.gallery)=="string"){C=this.getGallery(B.gallery)}}}if(C==-1){C=this.galleries[0]}D=(B&&B.delay&&($type(B.delay)=="number"))?B.delay*1000:(C.options&&C.options.delay)?C.options.delay*1000:this.options.autoPlayDelay*1000;A=(B&&B.index&&($type(B.index)=="number"))?B.index:0;if(D<this.options.resizeDuration*2){D=this.options.resizeDuration*2}this.options.autoPlayDelay=D/1000;if(!this.options.autoPlay){this.setOptions({autoPlay:true,autoPlayDelay:this.options.autoPlayDelay})}if(this.closed){this.openMilkbox(C,A);if(this.mode!="fileGallery"){return }this.addEvent("fileReady",function(){this.intObj=this.next_prev_aux.periodical(D,this,[null,"next"]);this.removeEvents("fileReady")}.bindWithEvent(this))}else{if(!this.closed){this.next_prev_aux(null,"next")}this.intObj=this.next_prev_aux.periodical(D,this,[null,"next"])}this.paused=false},stopAutoPlay:function(){if(this.intObj){$clear(this.intObj);this.intObj=null}this.playpause.setStyle("backgroundPosition","0 -44px");this.paused=true},removeGalleriesEvents:function(){this.galleries.each(function(A){$$(A).removeEvents("click")},this)},setGalleriesEvents:function(){this.galleries.each(function(A){$$(A).addEvent("click",function(D){var B=($(D.target).match("a"))?$(D.target):$(D.target).getParent("a");D.preventDefault();var C=this.getGallery(B.rel);if(C.options&&C.options.autoplay){this.setOptions({autoPlay:C.options.autoplay,autoPlayDelay:C.options.delay})}if(this.options.autoPlay){this.startAutoPlay({gallery:A,index:A.indexOf(B)})}else{this.openMilkbox(A,A.indexOf(B))}}.bindWithEvent(this))},this)},prepareEvents:function(A){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(B){if(this.mode!="fileGallery"||this.busy==true){return }if(B.key=="right"||B.key=="space"){this.next_prev_aux(B,"next")}else{if(B.key=="left"){this.next_prev_aux(B,"prev")}else{if(B.key=="esc"){this.closeMilkbox()}}}}.bindWithEvent(this));this.playpause.addEvents({mouseover:function(B){if(this.paused==false){this.playpause.setStyle("backgroundPosition","0 -22px")}else{this.playpause.setStyle("backgroundPosition","0 -66px")}}.bindWithEvent(this),mouseout:function(){if(this.paused==false){this.playpause.setStyle("backgroundPosition","0 0")}else{this.playpause.setStyle("backgroundPosition","0 -44px")}}.bindWithEvent(this),click:function(){if(this.paused==false){this.stopAutoPlay();this.paused=true;this.playpause.setStyle("backgroundPosition","0 -66px")}else{var B=(this.currentGallery.options&&this.currentGallery.options.delay)?this.currentGallery.options.delay:this.options.autoPlayDelay;this.startAutoPlay({gallery:this.currentGallery,index:this.currentIndex+1,delay:B});this.paused=false;this.playpause.setStyle("backgroundPosition","0 0")}}.bindWithEvent(this)});this.overlay.get("tween").addEvent("onComplete",function(){if(this.overlay.getStyle("opacity")==this.options.overlayOpacity){this.center.tween("opacity",1)}else{if(this.overlay.getStyle("opacity")==0){this.overlay.setStyles({height:0,top:""})}}}.bindWithEvent(this));this.center.get("morph").addEvent("onComplete",function(){if($type(this.currentFile)=="element"){this.canvas.grab(this.currentFile)}else{(function(){this.canvas.grab(this.currentFile)}).delay(500,this)}this.canvas.tween("opacity",1);var D=(!(this.mode=="showThisImage"))?this.currentGallery[this.currentIndex].retrieve("title"):this.specialDescription;if($chk(D)){this.description.innerHTML=D}if(this.mode=="fileGallery"){this.count.appendText((this.currentIndex+1)+" of "+this.currentGallery.length)}var B=this.center.getStyle("height").toInt();this.navigation.setStyle("height",this.bottom.getStyle("height").toInt());var E=this.bottom.getSize().y;var C=(B>this.canvas.getSize().y)?(this.bottom.getSize().y+this.canvas.getSize().y)-B:E;this.bottom.setStyle("display","none");this.center.retrieve("setFinalHeight").start(B,B+C)}.bindWithEvent(this));this.center.retrieve("setFinalHeight").addEvent("onComplete",function(){this.bottom.setStyles({visibility:"visible",display:"block"});$$(this.description,this.navigation).setStyle("visibility","visible");var B=$(window).getScrollSize().y;var C=$(window).getScroll().y;this.overlay.setStyles({height:B+C,top:-C});this.busy=false}.bindWithEvent(this));window.addEvent("resize",function(){if(this.overlay.getStyle("opacity")==0){return }var B=$(window).getScrollSize().y;var C=$(window).getScroll().y;this.overlay.setStyles({height:B+C,top:-C})}.bindWithEvent(this));$$(this.overlay,this.close).addEvent("click",function(){this.closeMilkbox()}.bindWithEvent(this));this.eventsok=true},next_prev_aux:function(D,C){if(D){D.preventDefault();this.stopAutoPlay()}else{if(this.busy||!this.fileReady){return }}this.busy=true;var E=this.currentIndex;if(C=="next"){var A=(this.currentIndex!=this.currentGallery.length-1)?this.currentIndex+=1:this.currentIndex=0;var B=(this.currentIndex!=this.currentGallery.length-1)?this.currentIndex+1:0}else{var A=(this.currentIndex!=0)?this.currentIndex-=1:this.currentIndex=this.currentGallery.length-1;var B=(this.currentIndex!=0)?this.currentIndex-1:this.currentGallery.length-1}this.canvas.empty();this.description.empty();this.count.empty();this.loadFile(this.currentGallery[A],[this.currentGallery[B]])},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(A){var D=[];var C;var B=(A)?A:$$("a");B.each(function(E){if(E.rel&&E.rel.test(/^milkbox/i)&&E.href.split("?")[0].test(/\.(gif|jpg|jpeg|png|swf)$/i)){if(E.rel.length>7&&!this.families.contains(E.rel)){this.families.push(E.rel)}D.push(E)}},this);D.each(function(E){$(E).store("href",E.href);$(E).store("rel",E.rel);$(E).store("title",E.title);if(this.checkFileType(E.href,"swf")){this.setFileProps($(E),E.rev)}if(this.options.removeTitle){$(E).removeProperty("title")}if(E.rel.length>7){this.families.each(function(I,H){if(E.rel==I){var F=false;var G;this.galleries.each(function(K,J){if(K[0].rel==I){F=true;G=J;return }});if(F==true){this.galleries[G].push($(E))}else{this.galleries.push([$(E)])}}},this)}else{this.galleries.push([$(E)])}},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":"clear"}));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":"clear"}),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 A=this.center.getStyle("border-right-width").toInt();var B=this.close.getSize().x+A;this.navigation.setStyles({width:B,height:"",visibility:"hidden"});this.description.setStyle("margin-right",B);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);if(this.formtags.length!=0){this.formtags.setStyle("display","")}this.mode=null;this.closed=true;this.first=true;this.fileReady=false;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()}});window.addEvent("domready",function(){milkbox=new Milkbox()});
