BAUER.namespace("BAUER.ui.xhr");BAUER.ui.xhr.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();this.initMilkbox();this.saveOptions();},button:null,initMilkbox:function(){this.prepareHTML();this.prepareEffects();this.prepareEvents();
this.formtags=$$("select","textarea");this.activated=true;},openMilkbox:function(i,f,b){if(b){this.button=b;if(this.button.get("rev").test(/chrome:\s*true/)){this.bottom.setStyles({"display":"block"});}this.closed=false;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=i;this.currentIndex=f;this.overlay.tween("opacity",this.options.overlayOpacity);if(i.length==1){this.mode="singleFile";this.loadFile(i[f],f);}else{this.mode="fileGallery";var a=0;$$(this.prev,this.next,this.count).setStyle("display","");
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 h=this.prev.getSize().x+this.next.getSize().x+this.close.getSize().x+a+d;this.description.setStyle("margin-right",h);var e=(f!=i.length-1)?i[f+1]:i[0];
var c=(f!=0)?i[f-1]:i[i.length-1];var g=(c==e)?[c]:[c,e];this.loadFile(i[f],g);}}else{this.button=null;this.closed=false;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.overlay.tween("opacity",this.options.overlayOpacity);this.loadXhr.delay(250,this,i);}},loadFile:function(c,b){this.fileReady=false;var a=this.checkFileType(c,"swf");if(c.get("rev")&&c.get("rev").test("^dom")){var d=c.get("rev").replace(/dom\[(.*)\]/gi,"$1");this.loadDomElement($(document.body).getElement(d));
preloadFiles=false;}else{if(this.checkFileType(c,"swf")){this.loadSwf(c);}else{if(this.checkFileType(c,"jpg")||this.checkFileType(c,"jpeg")||this.checkFileType(c,"gif")){if(!this.loadedImages.contains(c.retrieve("href"))){this.canvas.addClass("mbLoading");}this.loadImage(c.retrieve("href"));}else{this.canvas.addClass("mbLoading");
this.loadXhr.delay(250,this,[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);},loadDomElement:function(a){this.currentFile=a;this.loadAux(a);},loadXhr:function(a){this.currentRequest=new Request({method:"get",autoCancel:true,url:a,onRequest:function(){}.bindWithEvent(this),onSuccess:function(b){var c=new Element("div",{id:"xhrLoadedContent"}).set("html",b);
this.currentFile=c;this.loadAux(c);}.bindWithEvent(this),onFailure:function(b){alert("Milkbox :: loadXhr: file path error");}}).send();},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(c){var d={wMode:"transparent",swLiveConnect:"false"};var b=c.retrieve("params").split("&");b.each(function(f){var e=f.split("=");
if(e.length<2){e[1]="";}d[e[0]]=e[1];},this);var a=new Swiff(c.retrieve("href"),{width:c.retrieve("width").toInt(),height:c.retrieve("height").toInt(),params:d});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.get("id")&&c.get("id")=="xhrLoadedContent"){f.extend({"height":360,"width":625});}else{if(c.get("tag")=="img"){f=f.extend(c.getProperties("width","height")).map(function(d,b){return d.toInt();});}else{if(c.get("href")&&this.checkFileType(c.get("href"),"swf")){f.extend({"height":c.retrieve("height").toInt(),"width":c.retrieve("width").toInt()});
}else{f.extend({"height":c.getStyle("height").toInt(),"width":c.getStyle("width").toInt()});if(!f.width.toInt()){f.width=300;}f.width+=10;f.height+=10;}}}var k=new Hash(this.center.getStyles("width","height")).map(function(d,b){return d.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.canvas.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 c=($chk(a.index))?a.index:0;
var b=this.getGallery(a.gallery);var f=false;var e;if(a.autoplay||(b["options"]&&b["options"].autoplay)){f=true;}if(b!=-1&&!this.opened){if(f){e=(a&&a.delay)?a.delay:(b["options"]&&b["options"].delay)?b["options"].delay:this.autoPlayDelay;this.startAutoPlay({gallery:b,index:c,delay:e});}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(d,f){if(!this.xmlFiles.contains(f)){this.xmlFiles.push(f);}var g=d;var b=g.getElements(".gallery");var e=[];var a=[];b.each(function(h,j){var m={gallery:h.getProperty("name"),autoplay:Boolean(h.getProperty("autoplay")),delay:Number(h.getProperty("delay"))};
var c=h.getChildren("a");var k=c.map(function(i){return i.setProperty("rel","milkbox["+m.gallery+"]");});e.push(k);if(m.autoplay){a.push(m);}});this.prepareGalleries(e.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);var a=this.options.canvasBorderWidth+" solid "+this.options.canvasBorderColor;this.canvas.setStyles({"border":a,"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(f){var e=this.getGallery(f.gallery);if(e==-1){return;}var c=(f.autoplay==true)?f.autoplay:false;var h=($chk(f.delay)&&c)?f.delay:this.options.autoPlayDelay;e["options"]={autoplay:c,delay:h};
},this);},startAutoPlay:function(b){var c=-1;var a,e;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];}e=(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(e<this.options.resizeDuration*2){e=this.options.resizeDuration*2;}this.options.autoPlayDelay=e/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(e,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(e,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(){},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"){if(this.currentFile.get("tag")=="img"){this.canvas.grab(this.currentFile);}else{if(this.currentFile.get("tag")=="div"){this.canvas.grab(this.currentFile);this.canvas.setStyle("background-color","transparent");
window.fireEvent("fileLoaded");}else{var g=this.currentFile.clone(true,true);var b=new Element("div").setStyle("padding","5px").adopt(g);this.canvas.grab(b);g.setStyle("display","");}}}else{(function(){this.canvas.grab(this.currentFile);}).delay(500,this);}this.canvas.tween("opacity",1);if(this.currentIndex){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 h=this.bottom.getSize().y;
var e=(c>this.canvas.getSize().y)?(this.bottom.getSize().y+this.canvas.getSize().y)-c:h;this.center.retrieve("setFinalHeight").start(c,c+e);}.bindWithEvent(this));this.center.retrieve("setFinalHeight").addEvent("onComplete",function(){if(this.button&&this.button.get("rev").test(/chrome:\s*true/)){this.bottom.setStyles({"display":"block"});
}else{this.bottom.setStyles({"visibility":"visible"});}$$(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 f=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)){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(k,j){if(e.rel==k){var g=false;var h;this.galleries.each(function(i,f){if(i[0].rel==k){g=true;h=f;return;}});if(g==true){this.galleries[h].push($(e));}else{this.galleries.push([$(e)]);}}},this);}else{this.galleries.push([$(e)]);
}},this);},prepareHTML:function(){$(document.body).getElements("*[id^=mb]").destroy();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.bottom=new Element("div",{"id":"mbBottom"}).inject(this.center);this.canvas=new Element("div",{"id":"mbCanvas"}).inject(this.center);this.navigation=new Element("div",{"id":"mbNavigation"});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","html":"X"});this.next=new Element("a",{"id":"mbNextLink","html":"&raquo;"}).setStyle("visibility","hidden");this.prev=new Element("a",{"id":"mbPrevLink","html":"&laquo;"}).setStyle("visibility","hidden");this.playpause=new Element("a",{"id":"mbPlayPause"}).setStyle("visibility","hidden");
this.count=new Element("span",{"id":"mbCount"}).setStyle("visibility","hidden");$$(this.next,this.prev,this.count,this.playpause).setStyle("display","none");this.navigation.adopt(this.close,new Element("div",{"class":"clear"}),this.prev,this.count,this.next,this.playpause);},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({"display":"block"});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();}});BAUER.milkbox=null;
window.addEvent("domready",function(){BAUER.milkbox=new BAUER.ui.xhr.Milkbox();$(document.body).addEvent("click",function(d){var b=($(d.target).match("a"))?$(d.target):$(d.target).getParent("a");if(b&&b.rel.test(/^milkbox/i)){d.preventDefault();try{this.initialize();var a=false;this.galleries.each(function(e){if(e.contains(b)){a=e;
}});if($chk(a)){this.openMilkbox(a,a.indexOf(b),b);}}catch(c){console.dir(c);}}}.bindWithEvent(BAUER.milkbox));});
