﻿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(c,e){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=c;this.currentIndex=e;this.overlay.tween("opacity",this.options.overlayOpacity);if(c.length==1){this.mode="singleFile";this.loadFile(c[e],e)}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 d=this.center.getStyle("border-right-width").toInt();var i=this.prev.getSize().x+this.next.getSize().x+this.close.getSize().x+a+d;this.navigation.setStyle("width",i);this.description.setStyle("margin-right",i);var g=(e!=c.length-1)?c[e+1]:c[0];var h=(e!=0)?c[e-1]:c[c.length-1];var f=(h==g)?[h]:[h,g];this.loadFile(c[e],f)}},loadFile:function(d,c){this.fileReady=false;var a=this.checkFileType(d,"swf");if(!a){if(!this.loadedImages.contains(d.retrieve("href"))){this.center.addClass("mbLoading")}this.loadImage(d.retrieve("href"))}else{this.loadSwf(d)}if(c){this.preloadFiles(c)}},preloadFiles:function(a){a.each(function(e,c){var d=this.checkFileType(e.retrieve("href"),"swf");if(!d){this.preloadImage(e.retrieve("href"))}},this)},loadImage:function(c){var a=new Asset.image(c,{onload:function(d){if(!this.loadedImages.contains(c)){this.loadedImages.push(c)}this.currentFile=d;this.loadAux(this.currentFile)}.bindWithEvent(this)})},preloadImage:function(c){if(!this.loadedImages.contains(c)){var a=new Asset.image(c,{onload:function(d){this.loadedImages.push(c)}.bindWithEvent(this)})}},loadSwf:function(c){var a=new Swiff(c.retrieve("href"),{width:c.retrieve("width").toInt(),height:c.retrieve("height").toInt(),params:{wMode:"opaque",swLiveConnect:"false"}});this.currentFile=a;this.loadAux(c)},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(c){if(this.closed){return}var f=new Hash();var g={};var i=null;var a=null;if(this.options.canvasBorderWidth.toInt()!=0&&this.canvas.getStyle("borderWidth").toInt()==0){i=this.options.canvasBorderWidth+" solid "+this.options.canvasBorderColor;this.canvas.setStyle("border",i)}if(this.options.canvasPadding.toInt()!=0&&this.canvas.getStyle("padding").toInt()==0){a=this.options.canvasPadding;this.canvas.setStyle("padding",a)}var l=this.canvas.getSize();var e=this.canvas.getStyle("borderWidth").toInt()*2+this.canvas.getStyle("padding").toInt()*2;this.canvas.setStyles({opacity:0,width:"",height:""});if(!c.retrieve("width")){f=f.extend(c.getProperties("width","height")).map(function(m,d){return m.toInt()})}else{f.extend({height:c.retrieve("height").toInt(),width:c.retrieve("width").toInt()})}var k=new Hash(this.center.getStyles("width","height")).map(function(m,d){return m.toInt()});if(f.width!=k.width){g.width=f.width+e;g.marginLeft=-(g.width/2).round()}var j=(l.y-e>0)?k.height-l.y:0;g.height=f.height+e+j;this.canvas.setStyles({width:f.width,height:f.height});this.center.removeClass("mbLoading");var h=0;if(this.first){h=500;this.first=false}(function(){this.center.morph(g)}).delay(h,this)},showGallery:function(a){if(!a||!a.gallery){return}var e=($chk(a.index))?a.index:0;var c=this.getGallery(a.gallery);var h=false;var f;if(a.autoplay||(c.options&&c.options.autoplay)){h=true}if(c!=-1&&!this.opened){if(h){f=(a&&a.delay)?a.delay:(c.options&&c.options.delay)?c.options.delay:this.autoPlayDelay;this.startAutoPlay({gallery:c,index:e,delay:f})}else{this.openMilkbox(c,e)}}},addGalleries:function(a){this.currentRequest=new Request({method:"get",autoCancel:true,url:a,onRequest:function(){}.bindWithEvent(this),onSuccess:function(e,c){var d=e.replace(/(<a.+)\/>/gi,"$1></a>");this.setGalleries(new Element("div",{html:d}),a)}.bindWithEvent(this),onFailure:function(c){alert("Milkbox :: addGalleries: XML file path error or local Ajax test: please test addGalleries() on-line")}});this.currentRequest.send()},setGalleries:function(e,g){if(!this.xmlFiles.contains(g)){this.xmlFiles.push(g)}var h=e;var d=h.getElements(".gallery");var f=[];var a=[];d.each(function(j,k){var n={gallery:j.getProperty("name"),autoplay:Boolean(j.getProperty("autoplay")),delay:Number(j.getProperty("delay"))};var c=j.getChildren("a");var m=c.map(function(i){return i.setProperty("rel","milkbox["+n.gallery+"]")});f.push(m);if(n.autoplay){a.push(n)}});this.prepareGalleries(f.flatten());this.setAutoPlay(a);if(!this.activated){this.initMilkbox()}this.fireEvent("xmlGalleries")},checkFileType:function(c,d){var a=null;if($type(c)!="string"){a=c.retrieve("href")}else{a=c}var e=new RegExp(".("+d+")$","i");return a.split("?")[0].test(e)},getGallery:function(a){var e=null;if(a.test(/^milkbox/i)){e=this.families}else{e=this.families.map(function(h){var i=h.trim();var f=i.slice(0,i.length).substr(8);var g=f.replace(/(.+)]$/,"$1");return g})}var c=e.indexOf(a);var d=(c!=-1)?this.galleries[c]:c;return d},setFileProps:function(d,a){var c=a.split(",");c.each(function(g,f){var e=g.trim().split(":");d.store(e[0].trim(),e[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(c,a){this.addGalleries(c)}.bind(this))},setAutoPlay:function(c){var a=($type(c)=="object")?[c]:c;a.each(function(h){var f=this.getGallery(h.gallery);if(f==-1){return}var e=(h.autoplay==true)?h.autoplay:false;var i=($chk(h.delay)&&e)?h.delay:this.options.autoPlayDelay;f.options={autoplay:e,delay:i}},this)},startAutoPlay:function(c){var e=-1;var a,f;if(c&&c.gallery){if($type(c.gallery)=="array"){e=c.gallery}else{if($type(c.gallery)=="string"){e=this.getGallery(c.gallery)}}}if(e==-1){e=this.galleries[0]}f=(c&&c.delay&&($type(c.delay)=="number"))?c.delay*1000:(e.options&&e.options.delay)?e.options.delay*1000:this.options.autoPlayDelay*1000;a=(c&&c.index&&($type(c.index)=="number"))?c.index:0;if(f<this.options.resizeDuration*2){f=this.options.resizeDuration*2}this.options.autoPlayDelay=f/1000;if(!this.options.autoPlay){this.setOptions({autoPlay:true,autoPlayDelay:this.options.autoPlayDelay})}if(this.closed){this.openMilkbox(e,a);if(this.mode!="fileGallery"){return}this.addEvent("fileReady",function(){this.intObj=this.next_prev_aux.periodical(f,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(f,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(f){var c=($(f.target).match("a"))?$(f.target):$(f.target).getParent("a");f.preventDefault();var d=this.getGallery(c.rel);if(d.options&&d.options.autoplay){this.setOptions({autoPlay:d.options.autoplay,autoPlayDelay:d.options.delay})}if(this.options.autoPlay){this.startAutoPlay({gallery:a,index:a.indexOf(c)})}else{this.openMilkbox(a,a.indexOf(c))}}.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(c){if(this.mode!="fileGallery"||this.busy==true){return}if(c.key=="right"||c.key=="space"){this.next_prev_aux(c,"next")}else{if(c.key=="left"){this.next_prev_aux(c,"prev")}else{if(c.key=="esc"){this.closeMilkbox()}}}}.bindWithEvent(this));this.playpause.addEvents({mouseover:function(c){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 c=(this.currentGallery.options&&this.currentGallery.options.delay)?this.currentGallery.options.delay:this.options.autoPlayDelay;this.startAutoPlay({gallery:this.currentGallery,index:this.currentIndex+1,delay:c});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 f=(!(this.mode=="showThisImage"))?this.currentGallery[this.currentIndex].retrieve("title"):this.specialDescription;if($chk(f)){this.description.innerHTML=f}if(this.mode=="fileGallery"){this.count.appendText((this.currentIndex+1)+" of "+this.currentGallery.length)}var c=this.center.getStyle("height").toInt();this.navigation.setStyle("height",this.bottom.getStyle("height").toInt());var g=this.bottom.getSize().y;var e=(c>this.canvas.getSize().y)?(this.bottom.getSize().y+this.canvas.getSize().y)-c:g;this.bottom.setStyle("display","none");this.center.retrieve("setFinalHeight").start(c,c+e)}.bindWithEvent(this));this.center.retrieve("setFinalHeight").addEvent("onComplete",function(){this.bottom.setStyles({visibility:"visible",display:"block"});$$(this.description,this.navigation).setStyle("visibility","visible");var c=$(window).getScrollSize().y;var d=$(window).getScroll().y;this.overlay.setStyles({height:c+d,top:-d});this.busy=false}.bindWithEvent(this));window.addEvent("resize",function(){if(this.overlay.getStyle("opacity")==0){return}var c=$(window).getScrollSize().y;var d=$(window).getScroll().y;this.overlay.setStyles({height:c+d,top:-d})}.bindWithEvent(this));$$(this.overlay,this.close).addEvent("click",function(){this.closeMilkbox()}.bindWithEvent(this));this.eventsok=true},next_prev_aux:function(f,d){if(f){f.preventDefault();this.stopAutoPlay()}else{if(this.busy||!this.fileReady){return}}this.busy=true;var g=this.currentIndex;if(d=="next"){var a=(this.currentIndex!=this.currentGallery.length-1)?this.currentIndex+=1:this.currentIndex=0;var c=(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 c=(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[c]])},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 e=[];var d;var c=(a)?a:$$("a");c.each(function(f){if(f.rel&&f.rel.test(/^milkbox/i)&&f.href.split("?")[0].test(/\.(gif|jpg|jpeg|png|swf)$/i)){if(f.rel.length>7&&!this.families.contains(f.rel)){this.families.push(f.rel)}e.push(f)}},this);e.each(function(f){$(f).store("href",f.href);$(f).store("rel",f.rel);$(f).store("title",f.title);if(this.checkFileType(f.href,"swf")){this.setFileProps($(f),f.rev)}if(this.options.removeTitle){$(f).removeProperty("title")}if(f.rel.length>7){this.families.each(function(k,j){if(f.rel==k){var g=false;var h;this.galleries.each(function(l,i){if(l[0].rel==k){g=true;h=i;return}});if(g==true){this.galleries[h].push($(f))}else{this.galleries.push([$(f)])}}},this)}else{this.galleries.push([$(f)])}},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 c=this.close.getSize().x+a;this.navigation.setStyles({width:c,height:"",visibility:"hidden"});this.description.setStyle("margin-right",c);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()});