window.wp=window.wp||{},function(a){var b,c;b=wp.themes=wp.themes||{},b.data=_wpThemeSettings,c=b.data.l10n,_.extend(b,{model:{},view:{},routes:{},router:{},template:wp.template}),b.model=Backbone.Model.extend({}),b.view.Appearance=wp.Backbone.View.extend({el:"#wpbody-content .wrap .theme-browser",window:a(window),page:0,initialize:function(){var a=this;this.window.bind("scroll.themes",function(){this.throttle=!0}),setInterval(function(){this.throttle&&(this.throttle=!1,a.scroller())},300)},render:function(){this.view=new b.view.Themes({collection:this.collection,parent:this}),this.search(),this.view.render(),this.$el.append(this.view.el)},search:function(){var a,c=this;a=new b.view.Search({collection:c.collection}),a.render(),c.$el.append(a.el)},scroller:function(){var a,b,c=this;a=this.window.scrollTop()+c.window.height(),b=c.$el.offset().top+c.$el.outerHeight(!1)-c.window.height(),b=Math.round(.9*b),a>b&&this.trigger("theme:scroll")}}),b.Collection=Backbone.Collection.extend({model:b.model,terms:"",doSearch:function(a){this.terms=a,this.terms.length>0&&this.search(this.terms),""===this.terms&&this.reset(b.data.themes),this.trigger("update")},search:function(a){var c,d,e,f=this;f.reset(b.data.themes),a=a.replace(" ",")(?=.*"),c=new RegExp("^(?=.*"+a+").+","i"),d=f.filter(function(a){return e=_.union(a.get("name"),a.get("author"),a.get("tags")),c.test(a.get("author"))&&a.set("displayAuthor",!0),c.test(e)}),f.reset(d)},paginate:function(a){var b=this;return a=a||0,b=_(b.rest(15*a)),b=_(b.first(15))}}),b.view.Theme=wp.Backbone.View.extend({className:"theme",state:"grid",html:b.template("theme"),events:{click:"expand"},render:function(){var a=this.model.toJSON();this.$el.html(this.html(a)),this.activeTheme(),this.model.get("displayAuthor")&&this.$el.addClass("display-author")},activeTheme:function(){this.model.get("active")&&(this.$el.addClass("active"),this.$el.find(".theme-name").addClass("mp6-primary"),a(".theme-overlay").addClass("active"))},expand:function(b){var c=this;b=b||window.event,a(b.target).is(".theme-actions a, .delete-theme")||this.trigger("theme:expand",c.model.cid)}}),b.view.Details=wp.Backbone.View.extend({className:"theme-overlay",events:{click:"collapse","click .delete-theme":"deleteTheme","click .left":"previousTheme","click .right":"nextTheme"},html:b.template("theme-single"),render:function(){var a=this.model.toJSON();this.$el.html(this.html(a)),this.activeTheme(),this.navigation()},activeTheme:function(){this.$el.toggleClass("active",this.model.get("active"))},collapse:function(c){var d,e=this;c=c||window.event,(a(c.target).is(".theme-backdrop")||a(c.target).is("div.back")||27===c.keyCode)&&(a("body").addClass("closing-overlay"),this.$el.fadeOut(130,function(){a("body").removeClass("theme-overlay-open closing-overlay"),e.closeOverlay(),d=document.body.scrollTop,b.router.navigate(""),document.body.scrollTop=d}))},navigation:function(){var b=this;a("body").on("keyup",function(a){39===a.keyCode&&b.trigger("theme:next",b.model.cid),37===a.keyCode&&b.trigger("theme:previous",b.model.cid),27===a.keyCode&&b.collapse()})},closeOverlay:function(){this.remove(),this.unbind(),this.trigger("theme:collapse")},screenshotGallery:function(){var b,c,d=a("#theme-screenshots");d.find("div.first").next().addClass("selected"),d.on("click","div.thumb",function(){b=a(this),c=a(this).find("img").clone(),b.siblings(".first").html(c),b.siblings(".selected").removeClass("selected"),b.addClass("selected")})},deleteTheme:function(){return confirm(b.data.settings.confirmDelete)},nextTheme:function(){var a=this;a.trigger("theme:next",a.model.cid)},previousTheme:function(){var a=this;a.trigger("theme:previous",a.model.cid)}}),b.view.Themes=wp.Backbone.View.extend({className:"themes",index:0,count:a(".theme-count"),initialize:function(a){var b=this;this.parent=a.parent,this.setView("grid"),b.currentTheme(),this.listenTo(b.collection,"update",function(){b.parent.page=0,b.currentTheme(),b.render(this)}),this.listenTo(this.parent,"theme:scroll",function(){b.renderThemes(b.parent.page)})},render:function(){this.$el.html(""),this.renderThemes(this.parent.page),this.count.text(this.collection.length)},renderThemes:function(d){var e=this;e.instance=e.collection.paginate(d),0!==e.instance.length&&(d>=1&&a(".add-new-theme").remove(),e.instance.each(function(a){e.theme=new b.view.Theme({model:a}),e.theme.render(),e.$el.append(e.theme.el),e.listenTo(e.theme,"theme:expand",e.expand,e)}),b.data.settings.canInstall&&this.$el.append('
"),this.parent.page++)},currentTheme:function(){var a,b=this;a=b.collection.findWhere({active:!0}),a&&(b.collection.remove(a),b.collection.add(a,{at:0}))},setView:function(a){return a},expand:function(c){var d=this;this.model=d.collection.get(c),b.router.navigate("theme/"+this.model.id),this.setView("detail"),a("body").addClass("theme-overlay-open"),this.overlay=new b.view.Details({model:d.model}),this.overlay.render(),this.$el.append(this.overlay.el),this.overlay.screenshotGallery(),this.listenTo(this.overlay,"theme:next",function(){d.next([d.model.cid]),d.overlay.screenshotGallery()}).listenTo(this.overlay,"theme:previous",function(){d.previous([d.model.cid]),d.overlay.screenshotGallery()})},next:function(a){var c,d,e=this;c=e.collection.get(a[0]),d=e.collection.at(e.collection.indexOf(c)+1),void 0!==d&&(this.overlay.closeOverlay(),b.router.navigate("theme/"+d.id,{trigger:!0}))},previous:function(a){var c,d,e=this;c=e.collection.get(a[0]),d=e.collection.at(e.collection.indexOf(c)-1),void 0!==d&&(this.overlay.closeOverlay(),b.router.navigate("theme/"+d.id,{trigger:!0}))}}),b.view.Search=wp.Backbone.View.extend({tagName:"input",className:"theme-search",attributes:{placeholder:c.search},events:{input:"search",keyup:"search",change:"search",search:"search"},search:function(a){"keyup"===a.type&&27===a.which&&(a.target.value=""),this.collection.doSearch(a.target.value)}}),b.routes=Backbone.Router.extend({routes:{"search/:query":"search","theme/:slug":"theme"},search:function(b){a(".theme-search").val(b)}}),_.extend(b.routes,b.data.settings.extraRoutes),b.Run={init:function(){this.themes=new b.Collection(b.data.themes),this.view=new b.view.Appearance({collection:this.themes}),this.render()},render:function(){this.view.render(),this.routes(),Backbone.history.start({root:b.data.settings.root})},routes:function(){var a=this;b.router=new b.routes,b.router.on("route:theme",function(b){a.view.view.expand(b)}),b.router.on("route:search",function(b){a.themes.doSearch(b)})}},jQuery(document).ready(_.bind(b.Run.init,b.Run))}(jQuery);