/*
 * @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(){this.RokGallery.Blocks=new Class({Implements:Events,initialize:function(){this.list=document.id("gallery-list");this.rubberband=RokGallery.rubberband;
this.blocks=this.list.getElements(".gallery-block");this.batch=["publish","unpublish","tag","delete"];this.batchRequests={update:new Request({url:RokGallery.url,method:"post",onSuccess:this.batchSuccess.bind(this,"update"),data:{model:"Files",action:"update"}}),"delete":new Request({url:RokGallery.url,method:"post",onSuccess:this.batchSuccess.bind(this,"delete"),data:{model:"Files",action:"delete"}})};
this.bounds={activation:{mouseenter:this.activate.bind(this)},blocks:{flip:this.flip,unflip:this.unflip,edit:this.edit.bind(this),publish:this.publish.bind(this),remove:this.remove.bind(this)}};
this.disabled=false;this.editFileSettings=new RokGallery.FileSettings("file-settings");this.attach();this.batchActions();},attach:function(a){((a)?document.id(a)||document.getElements(a):this.blocks)["addEvents"](this.bounds.activation);
},addBlocks:function(a){this.blocks.push(a);this.blocks=Array.flatten(this.blocks);a.each(function(b){b.set("placement");this.attach(b);["elements","index","selected"].each(function(c){if(c=="index"){(this.rubberband||RokGallery.rubberband).add(b);
}else{(this.rubberband||RokGallery.rubberband)[c].push(b);}RokGallery.blocks.updateCounter();},this);},this);},batchActions:function(){var b,a=this;this.batch.each(function(c){b=document.id("toolbar-"+c);
b.removeEvents("click");b.addEvent("click",function(k){k.stop();var n=this.rubberband||RokGallery.rubberband;var j;if(n.detached){return b;}if(!n.selected.length){window.Popup.setPopup({type:"warning",title:"No selection",message:"No image has been selected. In order to "+c+" multiple images, you need to select them first.<br />To do so, start dragging in the page and a rubberband will show allowing you to select multiple images.<br /><br />Alternatively, you can also use the shift key to select or unselect a single block, by simply holding the shift key and clicking on an image block.",buttons:{ok:{show:false},cancel:{show:true,label:"close"}}}).open();
}else{this.multiSelection={elements:n.selected,settings:[],mode:false};var i=(c=="delete")?"delete":(c=="tag")?"tag":"update",f={},d=[],g;this.multiSelection.mode=c;
this.multiSelection.elements.each(function(e){this.activate(e);this.multiSelection.settings.push(e.retrieve("switcher:filesettings"));if(c=="publish"||c=="unpublish"){e.retrieve("switcher:filesettings").publish.addClass("loader");
}else{if(c=="delete"&&n.selected.length<=4){this.removeAnimation(e,"start");}}d.push(e.retrieve("file-id"));},this);if(c=="tag"){var h=new MassTagsManager({url:RokGallery.url});
j=function(){var r={ids:h.ids,tags:h.input.get("value").clean().replace(/,\s/,",").split(",")};var p=h.getRadioAction();if((r.tags.length==1&&!r.tags[0].length)){var e=h.input,o=h.input.getPrevious("label");
var q="#ed2e26";o.set("tween",{duration:250,link:"chain",transition:"quad:out"});e.set("tween",{duration:250,link:"chain",transition:"quad:out"});o.tween("color",q).tween("color","#888").tween("color",q).tween("color","#888").tween("color",q).tween("color","#888");
e.tween("border-color",q).tween("border-color","#ddd").tween("border-color",q).tween("border-color","#ddd").tween("border-color",q).tween("border-color","#ddd");
window.Popup.statusBar.getElement(".button.ok").removeEvents("click");window.Popup.statusBar.getElement(".button.ok").addEvent("click",j);}else{h[p+"Tags"](r);
}};window.Popup.setPopup({type:"",title:"Multiple Files Tag",message:"Loading...",buttons:{ok:{show:true,label:"apply"},cancel:{show:true,label:"close"}},"continue":j}).open();
var l=window.Popup.popup.getElement(".tags-info");h.tagsInfo=l||new Element("div.tags-info").inject(window.Popup.popup.getElement(".statusbar .clr"),"before");
window.Popup.statusBar.getElement(".button.cancel").addEvent("click:once",function(){h.tagsInfo.dispose();});return this;}switch(c){case"publish":f={ids:d,settings:{published:true}};
break;case"unpublish":f={ids:d,settings:{published:false}};break;case"delete":f={ids:d};break;}f={params:JSON.encode(f)};g=Object.merge(Object.clone(this.batchRequests[i]["options"]["data"]),f);
if(i=="delete"){var m=this;j=function(){this.multiSelection.elements.each(function(e){if(n.selected.length<=4){this.removeAnimation(e,"stop");}},this);
}.bind(this);window.Popup.setPopup({type:"warning",title:"File Deletion - Are you sure?",message:"<p>You are about to delete <strong>"+this.multiSelection.elements.length+"</strong> Images and all the Slices associated to them.</p><p>This operation is irreversible, are you sure you want to continue?</p>",buttons:{ok:{show:true,label:"yes"},cancel:{show:true,label:"no"}},"continue":function(){this.statusBar.getElement(".button.cancel").removeEvent("click:once",j);
if(m.editFileSettings.isOpen){m.editFileSettings.beforeClose();}m.batchRequests[i].send({data:g});this.close();}}).open();window.Popup.statusBar.getElement(".button.cancel").addEvent("click:once",j);
}else{this.batchRequests[i].send({data:g});}}return b;}.bind(this));},this);},activate:function(a){var c=document.id(a)||a.target.getParent(".gallery-block")||a.target;
if(c.retrieve("activated")){return;}c.removeEvents(this.bounds.activation).store("activated",true);c.store("file-id",c.get("id").match(/[0-9]{1,}/).join("").toInt());
var b={front:c.getElement(".front-view"),back:c.getElement(".back-view"),edit:c.getElements(".image-edit"),publish:c.getElements(".image-publish, .image-unpublish"),remove:c.getElements(".image-delete"),open:c.getElement(".info-switcher"),close:c.getElement(".image-close")};
if(!Supports3D){b.front.setStyle("opacity",1).set("tween",{duration:600,link:"cancel"});b.back.setStyles({opacity:0,zIndex:1000}).set("tween",{duration:600,link:"cancel"});
}c.store("switcher:filesettings",b);b.block=c;b.open.store("switcher:filesettings",b);b.close.store("switcher:filesettings",b);b.edit.store("switcher:filesettings",b);
b.publish.store("switcher:filesettings",b);b.remove.store("switcher:filesettings",b);if(!c.retrieve("descScrollbar")){var d=c.getElement(".back-view .image-description");
c.store("descScrollbar",new Scrollbar(d,{fixed:true,wrapStyles:{"padding-right":10}}));}if(!this.disabled){this.enable(c);}},enable:function(a){this.disabled=false;
if(!a){a=this.blocks;}this.switchEvents(a,"addEvent");return this;},disable:function(a){this.disabled=true;if(!a){a=this.blocks;}this.switchEvents(a,"removeEvent");
return this;},switchEvents:function(c,e){if(!e){return;}if(typeOf(c)=="array"){c=new Elements(c);}if(typeOf(c)=="elements"){c.each(function(i){this.activate(i);
this.switchEvents(i,e);},this);return;}var h=c.retrieve("switcher:filesettings");var b=h.open,g=h.close,f=h.edit,d=h.publish,a=h.remove;if(typeOf(b)=="array"){b=b.clean();
g=g.clean();b.each(function(k,j){k[e]("click",this.bounds.blocks.flip);g[j][e]("click",this.bounds.blocks.unflip);d[j][e]("click",this.bounds.blocks.publish);
a[j][e]("click",this.bounds.block.remove);},this);}else{b[e]("click",this.bounds.blocks.flip);g[e]("click",this.bounds.blocks.unflip);f[e]("click",this.bounds.blocks.edit);
d[e]("click",this.bounds.blocks.publish);a[e]("click",this.bounds.blocks.remove);}}.protect(),flip:function(a){var b=(typeOf(a)=="element"?a:this),c=b.retrieve("switcher:filesettings");
if(c.block.retrieve("flipped")){return;}if(!Supports3D){c.back.fade("in").setStyle("z-index",800);c.front.fade("out").setStyle("z-index",500);}else{c.block.addClass("flip");
}c.block.store("flipped",true);},unflip:function(a){var b=(typeOf(a)=="element"?a:this),c=b.retrieve("switcher:filesettings");if(!c.block.retrieve("flipped")){return;
}if(!Supports3D){c.front.fade("in").setStyle("z-index",800);c.back.fade("out").setStyle("z-index",500);}else{c.block.removeClass("flip");}c.block.store("flipped",false);
},edit:function(a){var c=(a.target||a).retrieve("switcher:filesettings"),b=c.block;this.editFileSettings.inject(b).load();this.unflipAll(b);this.disable();
},publish:function(a){var e,c,g=(a.target||a).retrieve("switcher:filesettings"),d=g.block,b=d.retrieve("switcher:publishing");if(!b){d.store("switcher:publishing",b=new Request({url:RokGallery.url,method:"post",onSuccess:this.publishSuccess.bind(this,d),data:{model:"File",action:"update"}}));
}if(b.isRunning()){return;}var f=d.retrieve("file-id");g.publish.addClass("loader");if(g.publish.hasClass("image-publish").every(Math.floor)){e={params:JSON.encode({id:f,file:{published:true}})};
}else{e={params:JSON.encode({id:f,file:{published:false}})};}c=Object.merge(Object.clone(b.options.data),e);b.send({data:c});},publishSuccess:function(f,a){if(!JSON.validate(a)){return this.popup({title:"File Publishing - Invalid Response",message:'<p class="error-intro">The response from the server had an invalid JSON string while trying to publish/unpublish an Image. Following is the reply.</p>'+a});
}a=JSON.decode(a);if(a.status!="success"){return this.popup({title:"File Publishing - Error",message:a.message});}var c=f.retrieve("switcher:filesettings").publish;
c.removeClass("loader");if(c.hasClass("image-publish").every(Math.floor)){c.removeClass("image-publish").addClass("image-unpublish");}else{c.removeClass("image-unpublish").addClass("image-publish");
}var e=c.hasClass("image-publish").every(Math.floor)?"publish":"unpublish",d=e=="publish"?"unpublished":"published";c.set("title",e);f.getElements(".indicator-published, .indicator-unpublished").removeClass("indicator-published").removeClass("indicator-unpublished").addClass("indicator-"+d);
if(this.editFileSettings.isOpen&&this.editFileSettings.id==f.retrieve("file-id")){this.editFileSettings.setPublished(e!="publish");var b=this.editFileSettings.slices[this.editFileSettings.currentSlice].id;
this.editFileSettings.slices=a.payload.file.Slices;this.editFileSettings.slices.each(function(h,g){if(h.id==b){this.editFileSettings.loadSlice(this.editFileSettings.currentSlice,true);
this.editFileSettings.currentSlice=g;}},this);this.editFileSettings.loadSlice(this.editFileSettings.currentSlice,true);}return this;},setPublishState:function(b){var d=this.editFileSettings.current,a=d.retrieve("switcher:filesettings").publish;
a.removeClass("image-publish").removeClass("image-unpublish").addClass("image-"+(b?"unpublish":"publish"));a.set("title",(b?"unpublish":"publish"));var c=d.getElements(".indicator-published, .indicator-unpublished");
c.removeClass("indicator-published").removeClass("indicator-unpublished");c.addClass("indicator-"+(b?"published":"unpublished"));this.editFileSettings.setPublished(b);
},batchSuccess:function(d,b){var c=this.multiSelection,a=this.rubberband||RokGallery.rubberband;this.multiSelection=null;if(d=="update"){c.settings.each(function(f){var e=f.publish;
e.removeClass("loader");},this);}if(d=="delete"){c.settings.each(function(e){if(a.selected.length<=4){this.removeAnimation(e.block,"stop");}},this);}if(!JSON.validate(b)){return this.popup({title:"Batch Action - Invalid Response",message:'<p class="error-intro">The response from the server had an invalid JSON string while trying to perform a batch action on Images. Following is the reply.</p>'+b});
}b=JSON.decode(b);if(b.status!="success"){return this.popup({title:"Batch Action - Error",message:b.message});}if(d=="update"){c.settings.each(function(f){var g=c.mode=="publish",i=f.block,e=f.publish;
e.removeClass("image-publish").removeClass("image-unpublish").addClass("image-"+(g?"unpublish":"publish"));e.set("title",(g?"unpublish":"publish"));var h=i.getElements(".indicator-published, .indicator-unpublished");
h.removeClass("indicator-published").removeClass("indicator-unpublished");h.addClass("indicator-"+(g?"published":"unpublished"));if(this.editFileSettings.isOpen&&this.editFileSettings.id==i.retrieve("file-id")){this.editFileSettings.element.getElement(".editfile-publish, .editfile-unpublish").set("text",g);
}},this);}if(d=="delete"){c.elements.each(function(e){e.fade("out").retrieve("tween").chain(function(){var f=e.getParent(".gallery-row");this.removeAnimation(e,"stop");
e.getParent(".gallery-block-wrapper").dispose();this.eraseBlock(e);this.reorder(f);}.bind(this));},this);}return this;},remove:function(i){var b,e,g=(i.target||i).retrieve("switcher:filesettings"),c=g.block,d=c.retrieve("switcher:removing"),j=this;
if(!d){c.store("switcher:removing",d=new Request({url:RokGallery.url,method:"post",onSuccess:this.removeSuccess.bind(this,c),data:{model:"File",action:"delete"}}));
}if(d.isRunning()){return;}var a=c.retrieve("file-id");this.removeAnimation(c,"start");b={params:JSON.encode({id:a})};e=Object.merge(Object.clone(d.options.data),b);
var h=c.getElement(".gallery-description h1").get("text"),f=function(){this.removeAnimation(c,"stop");}.bind(this);window.Popup.setPopup({type:"warning",title:"File Deletion - Are you sure?",message:"<p>You are about to delete the Image <strong>"+h+"</strong> and all the Slices associated to it.</p><p>This operation is irreversible, are you sure you want to continue?</p>",buttons:{ok:{show:true,label:"yes"},cancel:{show:true,label:"no"}},"continue":function(){this.statusBar.getElement(".button.cancel").removeEvent("click:once",f);
if(j.editFileSettings.isOpen){j.editFileSettings.beforeClose();}d.send({data:e});this.close();}}).open();window.Popup.statusBar.getElement(".button.cancel").addEvent("click:once",f);
},removeSuccess:function(b,a){if(!JSON.validate(a)){this.removeAnimation(b,"stop");return this.popup({title:"File Deletion - Invalid Response",message:'<p class="error-intro">The response from the server had an invalid JSON string while trying to delete an Image. Following is the reply.</p>'+a});
}a=JSON.decode(a);if(a.status!="success"){this.removeAnimation(b,"stop");return this.popup({title:"File Deletion - Error",message:a.message});}b.fade("out").retrieve("tween").chain(function(){var c=b.getParent(".gallery-row");
this.removeAnimation(b,"stop");b.dispose();this.eraseBlock(b).reorder(c);}.bind(this));return this;},removeAnimation:function(d,c){if(SupportsFrames){if(c=="start"){d.addClass("remove");
}if(c=="stop"){d.removeClass("remove");}return this;}var b=d.retrieve("switcher:removeAnim");if(!b){var a=new Fx.Tween(d,{duration:5,fps:12,onComplete:function(){var e=this.to[0].value;
if(e==-2){this.start("left",2);}else{this.start("left",-2);}}}).set("left",0);b=a;d.store("switcher:removeAnim",a);}if(c=="start"){b.start("left",-2);}if(c=="stop"){b.cancel().set("left",0);
}return this;},flipAll:function(a){var b=this.blocks.filter(function(c){return !c.retrieve("flipped");});b.each(function(c){if(c.retrieve("flipped")){return;
}this.activate(c);c.retrieve("switcher:filesettings")["open"].fireEvent("click");},this);if(a){this.unflip(a);}},unflipAll:function(a){var b=this.blocks.filter(function(c){return c.retrieve("flipped");
});b.each(function(c){this.activate(c);c.retrieve("switcher:filesettings")["close"].fireEvent("click");},this);if(a){this.flip(a);}},eraseBlock:function(b){var a=this.rubberband;
this.blocks.erase(b);if(a){["elements","index","selected"].each(function(c){if(a[c].contains(b)){a[c].erase(b);}this.updateCounter();},this);}return this;
},reorder:function(a){RokGallery.loadMore.refresh();return;},updateCounter:function(){var b=document.getElement(".total-count"),c=b.getElement(".total-selected > span"),d=b.getElement(".total-files > span"),a=this.rubberband||RokGallery.rubberband;
if(a){c.set("text",a.selected.length);}else{c.set("text",0);}},popup:function(a){var b={type:"warning",title:"Error",message:"",buttons:{ok:{show:false},cancel:{show:true,label:"close"}}};
window.Popup.setPopup(Object.merge(b,a)).open();}});})());