/*
 * @author    RocketTheme http://www.rockettheme.com
 * @copyright Copyright (C) 2007 - 2012 RocketTheme, LLC
 * @license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
 */
((function(){if(typeof this.RokSprocket=="undefined"){this.RokSprocket={};}else{Object.merge(this.RokSprocket,{Mosaic:null,MosaicBuilder:null});}var b="onorientationchange" in window;
var a=new Class({Implements:[Options,Events],options:{settings:{}},initialize:function(e){this.setOptions(e);this.mosaics=document.getElements("[data-mosaic]");
this.mosaic={};this.settings={};this.curve=Browser.opera?{equation:"ease-in-out"}:{curve:"cubic-bezier(0.37,0.61,0.59,0.87)"};try{RokMediaQueries.on("every",this.mediaQuery.bind(this));
}catch(d){if(typeof console!="undefined"){console.error('Error while trying to add a RokMediaQuery "match" event',d);}}},attach:function(d,e){d=typeOf(d)=="number"?document.getElements("[data-mosaic="+this.getID(d)+"]"):d;
e=typeOf(e)=="string"?JSON.decode(e):e;var f=(d?new Elements([d]).flatten():this.mosaics);f.each(function(g){g.store("roksprocket:mosaic:attached",true);
this.setSettings(g,e,"restore");var h={loadmore:g.retrieve("roksprocket:mosaic:loadmore",function(i,k){if(i){i.preventDefault();}this.loadMore.call(this,i,g,k);
}.bind(this)),ordering:g.retrieve("roksprocket:mosaic:ordering",function(k,i){this.orderBy.call(this,k,g,i);}.bind(this)),filtering:g.retrieve("roksprocket:mosaic:filtering",function(k,i){this.filterBy.call(this,k,g,i);
}.bind(this)),document:document.retrieve("roksprocket:mosaic:document",function(k,i){this.toggleShift.call(this,k,g,i);}.bind(this))};g.addEvent("click:relay([data-mosaic-loadmore])",h.loadmore);
g.addEvent("click:relay([data-mosaic-orderby])",h.ordering);g.addEvent("click:relay([data-mosaic-filterby])",h.filtering);g.retrieve("roksprocket:mosaic:ajax",new RokSprocket.Request({model:"mosaic",action:"getPage",onRequest:this.onRequest.bind(this,g),onSuccess:function(i){this.onSuccess(i,g,g.retrieve("roksprocket:mosaic:ajax"));
}.bind(this)}));document.addEvents({"keydown:keys(shift)":h.document,"keyup:keys(shift)":h.document});this.initializeMosaic(g,function(){this.mediaQuery.delay(5,this,RokMediaQueries.getQuery());
}.bind(this));},this);},detach:function(d){d=typeOf(d)=="number"?document.getElements("[data-mosaic="+this.getID(d)+"]"):d;var e=(d?new Elements([d]).flatten():this.mosaics);
e.each(function(f){f.store("roksprocket:mosaic:attached",false);var g={loadmore:f.retrieve("roksprocket:mosaic:loadmore"),ordering:f.retrieve("roksprocket:mosaic:ordering"),filtering:f.retrieve("roksprocket:mosaic:filtering"),document:document.retrieve("roksprocket:mosaic:document")};
f.removeEvent("click:relay([data-mosaic-loadmore])",g.loadmore);f.removeEvent("click:relay([data-mosaic-orderby])",g.ordering);f.removeEvent("click:relay([data-mosaic-filterby])",g.filtering);
document.removeEvents({"keydown:keys(shift)":g.document,"keyup:keys(shift)":g.document});},this);},mediaQuery:function(e){var d;for(var f in this.mosaic){d=this.mosaic[f];
d.resize("fast");}},setSettings:function(d,g,f){var h=this.getID(d),e=Object.clone(this.getSettings(d)||this.options.settings);if(!f||!this.settings["id-"+h]){this.settings["id-"+h]=Object.merge(e,g||e);
}},getSettings:function(d){var e=this.getID(d);return this.settings["id-"+e];},getContainer:function(d){if(!d){d=document.getElements("[data-mosaic]");
}if(typeOf(d)=="number"){d=document.getElement("[data-mosaic="+d+"]");}if(typeOf(d)=="string"){d=document.getElement(d);}return d;},getID:function(d){if(typeOf(d)=="number"){d=document.getElement("[data-mosaic="+d+"]");
}if(typeOf(d)=="string"){d=document.getElement(d);}return d.get("data-mosaic");},loadMore:function(e,d,g){d=this.getContainer(d);g=(typeOf(g)=="number")?g:this.getSettings(d).page||1;
if(!d.retrieve("roksprocket:mosaic:attached")){return;}var f=d.retrieve("roksprocket:mosaic:ajax"),i=d.getElement("[data-mosaic-filterby].active"),h={moduleid:d.get("data-mosaic"),behavior:!g?"reset":"append",filter:i?i.get("data-mosaic-filterby")||"all":"all",page:++g};
if(e&&e.shift){h.all=true;}if(!f.isRunning()){f.cancel().setParams(h).send();}},filterBy:function(f,d,e){d.getElements("[data-mosaic-filterby]").removeClass("active");
e.addClass("active");d.addClass("refreshing");this.loadMore(f,d,0);},nextAll:function(e,d){e=this.getContainer(e);if(typeOf(e)=="element"){return this.next(e,d);
}e.each(function(f){this.next(f,d);},this);},toggleShift:function(h,e,f){var g=h.type||"keyup",d=e.getElements("[data-mosaic-loadmore]");if(!d.length){return true;
}if(g=="keydown"){d.addClass("load-all");}else{d.removeClass("load-all");}},onRequest:function(e){var d=e.getElements("[data-mosaic-loadmore]");if(d){d.addClass("loader");
}this.detach(e);},onSuccess:function(l,f){var g="id-"+this.getID(f),q=f.retrieve("roksprocket:mosaic:ajax"),p=f.getElement("[data-mosaic-items]"),m=l.getPath("payload.html"),n=l.getPath("payload.page"),o=l.getPath("payload.more"),e=l.getPath("payload.behavior"),k=this.getSettings(f),s=k.animations,i;
this.setSettings(f,{page:(e=="reset"?1:n)});f.removeClass("refreshing");var h=new Element("div",{html:m}),d=h.getChildren(),r={},t={};r=this.getAnimation(f,"_set").style;
moofx(d).style(r);p.adopt(d);i=new Elements(d.getElements("img").flatten());this._loadImages(i.get("src"),function(){if(e=="reset"){this.mosaic[g].bricks.each(function(v,u){(function(){r=this.getAnimation(f,"_out");
moofx(v).style(r.style);t=Object.merge({},this.curve,{duration:"250ms",callback:function(){v.dispose();}});moofx(v).animate(r.animate,t);}).delay(u*50,this);
},this);}this.mosaic[g][e](d,function(){loadmore=f.getElements("[data-mosaic-loadmore]");if(loadmore){loadmore.removeClass("loader");}d=this.mosaic[g].bricks.filter(function(u){return d.contains(u);
});d.each(function(v,u){(function(){r=this.getAnimation(f,"_in");t=Object.merge({},this.curve,{curve:"cubic-bezier(0.37,0.61,0.59,0.87)",duration:"300ms"});
moofx(v).animate(r.animate,t);}).delay(u*100,this);},this);this.attach(f);f.getElements("[data-mosaic-loadmore]").removeClass("load-all")[!o?"addClass":"removeClass"]("hide");
}.bind(this));}.bind(this));},getAnimation:function(d,i){var f=this.getSettings(d),e=f.animations||null,g={},h={_set:{style:{opacity:0},animate:{}},_out:{style:{opacity:1},animate:{opacity:0}},_in:{style:{},animate:{opacity:1}}};
e=e?e.erase("fade"):null;if(e&&e.contains("flip")){e=e.erase("scale").erase("rotate");}switch(e?e.join(","):null){case"scale":h._set["style"]=Object.merge(h._set["style"],{transform:"scale(0.5)"});
h._out["style"]=Object.merge(h._out["style"],{"transform-origin":"50% 50%"});h._out["animate"]=Object.merge(h._out["animate"],{transform:Browser.ie9?"scale(0.001)":"scale(0)"});
h._in["animate"]=Object.merge(h._in["animate"],{transform:Browser.ie9||Browser.opera?"matrix(1, 0, 0, 1, 0, 0)":"scale(1)"});break;case"rotate":h._set["style"]=Object.merge(h._set["style"],{"transform-origin":"0 0",transform:"rotate(-10deg)"});
h._out["style"]=Object.merge(h._out["style"],{"transform-origin":"0 0"});h._out["animate"]=Object.merge(h._out["animate"],{transform:"rotate(10deg)"});
h._in["animate"]=Object.merge(h._in["animate"],{transform:"rotate(0)"});break;case"rotate,scale":case"scale,rotate":h._set["style"]=Object.merge(h._set["style"],{"transform-origin":"0 0",transform:"scale(0.5) rotate(-10deg)"});
h._out["style"]=Object.merge(h._out["style"],{"transform-origin":"50% 50%"});h._out["animate"]=Object.merge(h._out["animate"],{transform:Browser.ie9?"scale(0.001) rotate(10deg)":"scale(0) rotate(10deg)"});
h._in["animate"]=Object.merge(h._in["animate"],{transform:Browser.ie9||Browser.opera?"matrix(1, 0, 0, 1, 0, 0)":"scale(1) rotate(0)"});break;case"flip":h._set["style"]=Object.merge(h._set["style"],{"transform-origin":"50% 50%",transform:"scale(0.5) rotateY(360deg)"});
h._out["style"]=Object.merge(h._out["style"],{"transform-origin":"50% 50%"});h._out["animate"]=Object.merge(h._out["animate"],{transform:Browser.ie9?"scale(0.0001) rotateY(360deg)":"scale(0.5) rotateY(360deg)"});
h._in["animate"]=Object.merge(h._in["animate"],{transform:"scale(1) rotateY(0)"});break;default:}return h[i];},orderBy:function(f,d,e){var h="id-"+this.getID(d);
if(!this.mosaic||!this.mosaic[h]){throw new Error("RokSprocket Mosaic: Mosaic class not available");}var g=e.get("data-mosaic-orderby");this.mosaic[h].order(g);
d.getElements("[data-mosaic-orderby]").removeClass("active");if(g!="random"){e.addClass("active");}},initializeMosaic:function(e,l){var k="id-"+this.getID(e),d;
if(this.mosaic&&this.mosaic[k]){if(typeof l=="function"){l.call(this.mosaic[k].bricks);}d=e.getElements("[data-mosaic-loadmore]");if(d){d.removeClass("loader");
}return this.mosaic[k];}var i=e.getElements("img"),g=e.getElement("[data-mosaic-items]"),h=e.getElement(".active[data-mosaic-orderby]"),f={container:e,animated:true,gutter:0,order:h?h.get("data-mosaic-orderby"):(e.getElements("[data-mosaic-orderby]").length?"random":"default")};
if(l&&typeof l=="function"){f.callback=l;}moofx(g).style({"transform-style":"preserve-3d","backface-visibility":"hidden",opacity:1});moofx(g.getElements("[data-mosaic-item]")).style(this.getAnimation(e,"_in").animate);
if(!i.length){d=e.getElements("[data-mosaic-loadmore]");if(d){d.removeClass("loader");}this.mosaic[k]=new RokSprocket.MosaicBuilder(g,f);}else{this._loadImages(i.get("src"),function(){d=e.getElements("[data-mosaic-loadmore]");
if(d){d.removeClass("loader");}this.mosaic[k]=new RokSprocket.MosaicBuilder(g,f);}.bind(this));}return this.mosaic[k];},_loadImages:function(d,e){return d.length?new Asset.images(d,{onComplete:e.bind(this)}):e.bind(this)();
}});var c=new Class({Implements:[Options,Events],options:{container:null,resizeable:false,animated:false,gutter:0,fitwidth:false,order:"default",containerStyle:{position:"relative"}},initialize:function(e,d){this.setOptions(d);
this.element=document.id(e)||document.getElement(e)||null;if(!this.element){throw new Error('Mosaic Builder Error: Element "'+e+'" not found in the DOM.');
}this.styleQueue=[];this.curve=Browser.opera?{equation:"ease-in-out"}:{curve:"cubic-bezier(0.37,0.61,0.59,0.87)"};this.originalState=this.getBricks();this.build();
this.init(d.callback||null);},build:function(){var d=this.element.style;this.originalStyle={height:d.height||""};Object.each(this.options.containerStyle,function(e,f){this.originalStyle[f]=d[f]||"";
},this);moofx(this.element).style(this.originalStyle);this.offset={x:this.element.getStyle("padding-left").toInt(),y:this.element.getStyle("padding-top").toInt()};
this.isFluid=this.options.columnWidth&&typeof this.options.columnWidth==="function";this.reloadItems(this.options.order||null);},init:function(d){this.getColumns();
this.reLayout(d);},getBricks:function(d){return(d?d:this.element.getElements("[data-mosaic-item]")).setStyle("position","absolute");},reloadItems:function(d,e){this.bricks=this.getBricks(e);
if(d=="random"||d=="default"){if(d=="random"){this.bricks=this.bricks.shuffle();}if(d=="default"){this.bricks=this.originalState.clone();}return this.bricks;
}this.bricks=d?this.orderBy(d):this.bricks;return this.bricks;},orderBy:function(d){var e=false;return this.bricks.sort(function(h,g){var f=h.getElement("[data-mosaic-order-"+d+"]"),i=g.getElement("[data-mosaic-order-"+d+"]");
if(!f||!i){if(console&&console.error&&!e){console.error('RokSprocket MosaicBuilder: Trying to sort by "'+d+'" but no sorting rule has been found.');}e=true;
return 0;}f=f.get("data-mosaic-order-"+d);i=i.get("data-mosaic-order-"+d);return f==i?0:(f<i?-1:1);}.bind(this));},reload:function(d){this.reloadItems();
this.init(d);},layout:function(e,n,l){for(var k=0,m=e.length;k<m;k++){this.placeBrick(e[k]);}var d={},o={};d.height=Math.max.apply(Math,this.colYs);if(this.options.fitwidth){var g=0;
k=this.cols;while(--k){if(this.colYs[k]!==0){break;}g++;}d.width=(this.cols-g)*this.columnWidth-this.options.gutter;}this.styleQueue.push({element:this.element,style:d});
var f=!this.isLaidOut?"style":(this.options.animated&&!l?"animate":"style"),h;this.styleQueue.each(function(q,p){o=Object.merge({},this.curve,{duration:"400ms"});
if(p==this.styleQueue.length-1){if(n){o.callback=n.bind(n,e);}}moofx(q.element)[f](q.style,o);},this);this.styleQueue.empty();if(n&&f=="style"){n.call(e);
}this.isLaidOut=true;},getColumns:function(){var d=this.options.fitwidth?this.element.getParent():this.element,e=d.offsetWidth;this.columnWidth=this.isFluid?this.options.columnWidth(e):this.options.columnWidth||(this.bricks.length&&this.bricks[0].offsetWidth)||e;
this.columnWidth+=this.options.gutter;this.cols=Math.ceil((e+this.options.gutter)/this.columnWidth);this.cols=Math.max(this.cols,1);},placeBrick:function(k){k=document.id(k);
var m,q,f,o;m=Math.ceil(k.offsetWidth/(this.columnWidth+this.options.gutter));m=Math.min(m,this.cols);if(m==1){f=this.colYs;}else{q=this.cols+1-m;f=[];
(q).times(function(r){o=this.colYs.slice(j,j+m);f[j]=Math.max.apply(Math,o);},this);}var d=Math.min.apply(Math,f),p=0;for(var g=0,l=f.length;g<l;g++){if(f[g]===d){p=g;
break;}}var h={top:d+this.offset.y};h.left=p*(100/this.cols)+"%";this.styleQueue.push({element:k,style:h});var n=d+k.offsetHeight+((this.options.gutter||0)),e=this.cols+1-f.length;
(e).times(function(r){this.colYs[p+r]=n;},this);},resize:function(d){var e=this.cols;this.getColumns();if((this.isFluid||d)&&this.cols!==e||d){this.reLayout(null,d);
}},reLayout:function(f,d){var e=this.cols;this.colYs=[];while(e--){this.colYs.push(0);}this.layout(this.bricks,f,d);},reset:function(d,e){d=d.filter(function(f){return f.get("data-mosaic-item")!==null||f.getElement("data-mosaic-item");
});this.bricks=this.originalState=new Elements();d.setStyles({top:0,left:0,position:"absolute"});this.appendedBricks.delay(1,this,[d,d,e]);},append:function(d,e){d=d.filter(function(f){return f.get("data-mosaic-item")!==null||f.getElement("data-mosaic-item");
});if(!d){return;}d.setStyles({top:this.element.getSize().y,left:0,position:"absolute"});this.appendedBricks.delay(1,this,[d,null,e]);},appendedBricks:function(f,e,h){var d=this.options.container.getElement("[data-mosaic-orderby].active")||this.options.container.getElement("[data-mosaic-orderby=random]"),g=d?d.get("data-mosaic-orderby"):(this.options.container.getElements("[data-mosaic-orderby]").length?"random":"default");
this.originalState.append(f);this.order(g,e,h);},order:function(e,d,f){this.reloadItems(e,d||null);this.init(f);}});this.RokSprocket.Mosaic=a;this.RokSprocket.MosaicBuilder=c;
if(b){window.addEventListener("orientationchange",function(){if(typeof RokSprocket=="undefined"||typeof RokSprocket.instances=="undefined"||typeof RokSprocket.instances.mosaic=="undefined"){return;
}var d;for(var e in RokSprocket.instances.mosaic.mosaic){d=RokSprocket.instances.mosaic.mosaic[e];d.resize("fast");}});}Element.implement({mosaic:function(e){var d=this.retrieve("roksprocket:mosaic:builder");
if(!d){d=this.store("roksprocket:mosaic:builder",new RokSprocket.MosaicBuilder(this,e));}return d;}});if(MooTools.version<"1.4.4"&&(Browser.name=="ie"&&Browser.version<9)){((function(){var d=["rel","data-next","data-mosaic","data-mosaic-items","data-mosaic-item","data-mosaic-content","data-mosaic-page","data-mosaic-next","data-mosaic-order","data-mosaic-orderby","data-mosaic-order-title","data-mosaic-order-date","data-mosaic-filterby","data-mosaic-loadmore"];
d.each(function(e){Element.Properties[e]={get:function(){return this.getAttribute(e);}};});})());}})());