(function(){var t=function(t,e){return function(){return t.apply(e,arguments)}},e={}.hasOwnProperty,i=function(t,i){function n(){this.constructor=t}for(var r in i)e.call(i,r)&&(t[r]=i[r]);return n.prototype=i.prototype,t.prototype=new n,t.__super__=i.prototype,t},n=[].slice;(function(t,e){var i,n,r;return("undefined"!=typeof define&&null!==define?define.amd:void 0)?define("backbone.giraffe",["jquery","underscore","backbone"],function(i,n,r){return t.Giraffe=e(t,i,n,r)}):("undefined"!=typeof module&&null!==module?module.exports:void 0)?(i=require("jquery"),r=require("underscore"),n=require("backbone"),module.exports=e(t,i,r,n)):t.Giraffe=e(t,t.$,t._,t.Backbone)})(this,function(e,r,o,s){var a,l,u,p,h,c,f;if(!r)throw Error("Giraffe cannot find jQuery");if(!o)throw Error("Giraffe cannot find Underscore");if(!s)throw Error("Giraffe cannot find Backbone");return u=s.Giraffe={version:"0.2.8",app:null,apps:{},views:{}},p=e.Giraffe,u.noConflict=function(){return e.Giraffe=p,this},l=r(window),a=r(document),u.View=function(e){function s(e){this.render=t(this.render,this),u.configure(this,e),this.children=[],this.parent=null,this._renderedOnce=!1,this._isAttached=!1,this._createEventsFromUIElements(),"string"==typeof this.templateStrategy&&u.View.setTemplateStrategy(this.templateStrategy,this),s.__super__.constructor.apply(this,arguments)}return i(s,e),s.defaultOptions={disposeOnDetach:!0},s.prototype._beforeInitialize=function(){return this._cache(),this.$el.attr("data-view-cid",this.cid),this.setParent(u.View.getClosestView(this.$el)),this._cacheUiElements()},s.prototype._attachMethods=["append","prepend","html","after","before","insertAfter","insertBefore"],s.prototype._siblingAttachMethods=["after","before","insertAfter","insertBefore"],s.prototype.attachTo=function(t,e){var i,n,r,s,a,l;if(s=(null!=e?e.method:void 0)||"append",r=(null!=e?e.forceRender:void 0)||!1,l=(null!=e?e.suppressRender:void 0)||!1,!this.$el)throw Error("Trying to attach a disposed view. Make a new one or create the view with the option `disposeOnDetach` set to false.");if(!o.contains(this._attachMethods,s))throw Error("The attach method '"+s+"' isn't supported - must be one of ["+this._attachMethods+"].");if(n=u.View.to$El(t),1!==n.length)throw Error("Expected to attach to a single element but found "+n.length+" elements");return this.trigger("attaching",this,n,e),i=o.contains(this._siblingAttachMethods,s)?n.parent():n,"insertAfter"===s&&(s="after"),"insertBefore"===s&&(s="before"),this.detach(!0),this.setParent(u.View.getClosestView(i)),"html"===s&&(u.View.detachByEl(n),n.empty()),n[s](this.$el),this._isAttached=!0,a=!l&&(!this._renderedOnce||r||this.alwaysRender),a&&this.render(e),this.saveScrollPosition&&this._loadScrollPosition(),null!=this.documentTitle&&(document.title=this.documentTitle),this.trigger("attached",this,n,e),this},s.prototype.attach=function(t,e){var i,n;if(n=null,null!=e?e.el:void 0){if(i=u.View.to$El(e.el,this.$el,!0),!i.length)throw Error("Attempting to attach to an element that doesn't exist inside this view!");n=i}else n=this.$el;return t.attachTo(n,e),this},s.prototype.render=function(t){var e;return this.trigger("rendering",this,t),this.beforeRender.apply(this,arguments),this._renderedOnce=!0,this.detachChildren(null!=t?t.preserve:void 0),e=this.templateStrategy.apply(this,arguments)||"",this.$el.empty()[0].innerHTML=e,this._cacheUiElements(),this.afterRender.apply(this,arguments),this.trigger("rendered",this,t),this},s.prototype.beforeRender=function(){},s.prototype.afterRender=function(){},s.prototype.templateStrategy=function(){return""},s.prototype.template=null,s.prototype.serialize=function(){return this},s.prototype.detach=function(t){return null==t&&(t=!1),this._isAttached?(this._isAttached=!1,this.saveScrollPosition&&this._saveScrollPosition(),this.trigger("detaching",this,t),this.$el.detach(),this.trigger("detached",this,t),this.disposeOnDetach&&!t&&this.dispose(),this):this},s.prototype.detachChildren=function(t){var e,i,n,r;for(null==t&&(t=!1),r=this.children.slice(),i=0,n=r.length;n>i;i++)e=r[i],"function"==typeof e.detach&&e.detach(t);return this},s.prototype._saveScrollPosition=function(){return this._scrollPosition=this._getScrollPositionEl().scrollTop(),this},s.prototype._loadScrollPosition=function(){return null!=this._scrollPosition&&this._getScrollPositionEl().scrollTop(this._scrollPosition),this},s.prototype._getScrollPositionEl=function(){var t;return"boolean"==typeof this.saveScrollPosition||this.$el.is(this.saveScrollPosition)?this.$el:(t=u.View.to$El(this.saveScrollPosition,this.$el).first(),t.length?t:(t=u.View.to$El(this.saveScrollPosition).first(),t.length?t:this.$el))},s.prototype.addChild=function(t){var e;return o.contains(this.children,t)||(null!=(e=t.parent)&&e.removeChild(t,!0),t.parent=this,this.children.push(t)),this},s.prototype.addChildren=function(t){var e,i,n;for(i=0,n=t.length;n>i;i++)e=t[i],this.addChild(e);return this},s.prototype.removeChild=function(t,e){var i;return null==e&&(e=!1),i=o.indexOf(this.children,t),-1!==i&&(this.children.splice(i,1),t.parent=null,e?"function"==typeof t.detach&&t.detach(!0):"function"==typeof t.dispose&&t.dispose()),this},s.prototype.removeChildren=function(t){var e,i,n,r;for(null==t&&(t=!1),r=this.children.slice(),i=0,n=r.length;n>i;i++)e=r[i],this.removeChild(e,t);return this},s.prototype.setParent=function(t){return t&&t!==this?t.addChild(this):this.parent&&(this.parent.removeChild(this,!0),this.parent=null),this},s.prototype.isAttached=function(t){return null!=t?t.$el?!!t.$el.find(this.$el).length:this.$el.parent().is(t):!!a.find(this.$el).length},s.prototype.ui=null,s.prototype._cacheUiElements=function(){var t,e,i;if(this.ui){i=this.ui;for(t in i)e=i[t],this[t]=function(){switch(typeof e){case"string":return this.$(e);case"function":return e.call(this);default:return e}}.call(this)}return this},s.prototype._uncacheUiElements=function(){var t;if(this.ui)for(t in this.ui)delete this[t];return this},s.prototype._createEventsFromUIElements=function(){var t,e,i,n;if(!this.events||!this.ui)return this;"function"==typeof this.ui&&(this.ui=this.ui.call(this)),"function"==typeof this.events&&(this.events=this.events.call(this)),n=this.events;for(t in n)e=n[t],i=this._getEventKeyFromUIElements(t),i!==t&&(delete this.events[t],this.events[i]=e);return this},s.prototype._getEventKeyFromUIElements=function(t){var e,i,n,r;return n=t.split(" "),i=n.length,2>i?t:(e=n[i-1],r=this.ui[e],r?(n[i-1]=r,n.join(" ")):t)},s.prototype.dataEvents=null,s.prototype._uncache=function(){return delete u.views[this.cid],this},s.prototype._cache=function(){return u.views[this.cid]=this,this},s.prototype.invoke=function(){var t,e,i;for(e=arguments[0],t=arguments.length>=2?n.call(arguments,1):[],i=this;i&&!i[e];)i=i.parent;if("function"==typeof(null!=i?i[e]:void 0))return i[e].apply(i,t);throw Error("No such method name in view hierarchy '"+e+"'")},s.prototype.appEvents=null,s.prototype._dispose=function(){if(this.setParent(null),this.removeChildren(),this._uncacheUiElements(),this._uncache(),this._isAttached=!1,!this.$el)throw Error("Disposed of a view that has already been disposed");return this.remove(),this.$el=null,this},s.prototype.beforeDispose=function(){},s.prototype.afterDispose=function(){},s.prototype.beforeInitialize=function(){},s.prototype.afterInitialize=function(){},s.detachByEl=function(t,e){var i,n,r,o;for(null==e&&(e=!1),n=u.View.to$El(t);(i=n.find("[data-view-cid]:first")).length;)r=i.attr("data-view-cid"),o=u.View.getByCid(r),o.detach(e);return this},s.getClosestView=function(t){var e,i;return e=u.View.to$El(t),i=e.closest("[data-view-cid]").attr("data-view-cid"),u.View.getByCid(i)},s.getByCid=function(t){return u.views[t]},s.to$El=function(t,e,i){var n;return null==i&&(i=!1),e?(n=u.View.to$El(e),(null!=t?t.$el:void 0)&&(t=t.$el),i&&n.is(t)?n:n.find(t)):(null!=t?t.$el:void 0)?t.$el:t instanceof r?t:r(t)},s.setDocumentEvents=function(t,e){var i,n,s,l,p,h;for(null==e&&(e=u.View._documentEventPrefix),e=e||"","string"==typeof t&&(t=t.split(" ")),o.isArray(t)||(t=[t]),t=o.compact(t),u.View.removeDocumentEvents(),u.View._currentDocumentEvents=t,u.View._documentEventPrefix=e,l=function(t,e,i){return a.on(t,i,function(t){var n,o,s;return n=r(t.target).closest(i),o=n.attr(e),s=u.View.getClosestView(n),s.invoke(o,t)})},p=0,h=t.length;h>p;p++)n=t[p],i=e+n,s="["+i+"]",l(n,i,s);return t},s.removeDocumentEvents=function(t){var e,i,n,r,o;if(null==t&&(t=u.View._documentEventPrefix),t=t||"",e=u.View._currentDocumentEvents,null!=e?e.length:void 0){for(r=0,o=e.length;o>r;r++)i=e[r],n="["+t+i+"]",a.off(i,n);return u.View._currentDocumentEvents=null}},s.setDocumentEventPrefix=function(t){return null==t&&(t=""),u.View.setDocumentEvents(u.View._currentDocumentEvents,t)},s.setTemplateStrategy=function(t,e){var i,n;if(i=typeof t,"function"===i)n=t;else{if("string"!==i)throw Error("Unrecognized template strategy '"+t+"'");switch(t.toLowerCase()){case"underscore-template-selector":n=function(){var t,e=this;if(!this.template)return"";if(!this._templateFn)switch(typeof this.template){case"string":t=this.template,this._templateFn=o.template(r(t).html()||"");break;case"function":this._templateFn=function(i){return t=e.template(),o.template(r(t).html()||"",i)};break;default:throw Error("this.template must be string or function")}return this._templateFn(this.serialize.apply(this,arguments))};break;case"underscore-template":n=function(){var t=this;if(!this.template)return"";if(!this._templateFn)switch(typeof this.template){case"string":this._templateFn=o.template(this.template);break;case"function":this._templateFn=function(e){return o.template(t.template(),e)};break;default:throw Error("this.template must be string or function")}return this._templateFn(this.serialize.apply(this,arguments))};break;case"jst":n=function(){var t;if(!this.template)return"";if(!this._templateFn)switch(typeof this.template){case"string":t=this.template,this._templateFn=function(){return t};break;case"function":this._templateFn=this.template;break;default:throw Error("this.template must be string or function")}return this._templateFn(this.serialize.apply(this,arguments))};break;default:throw Error("Unrecognized template strategy: "+t)}}return e?e.templateStrategy=n:u.View.prototype.templateStrategy=n},s}(s.View),u.View.setTemplateStrategy("underscore-template-selector"),u.View.setDocumentEvents(["click","change"],"data-gf-"),u.App=function(e){function n(){this._onUnload=t(this._onUnload,this),this.app=this,this._initializers=[],this.started=!1,n.__super__.constructor.apply(this,arguments)}return i(n,e),n.prototype._cache=function(){return null==u.app&&(u.app=this),u.apps[this.cid]=this,this.routes&&null==this.router&&(this.router=new u.Router({app:this,triggers:this.routes})),l.on("unload",this._onUnload),n.__super__._cache.apply(this,arguments)},n.prototype._uncache=function(){return u.app===this&&(u.app=null),delete u.apps[this.cid],this.router&&(this.router=null),l.off("unload",this._onUnload),n.__super__._uncache.apply(this,arguments)},n.prototype._onUnload=function(){return this.dispose()},n.prototype.appEvents=null,n.prototype.routes=null,n.prototype.addInitializer=function(t){return this.started?(t.call(this,this._startOptions),o.extend(this,this._startOptions)):this._initializers.push(t),this},n.prototype.start=function(t){var e,i=this;return null==t&&(t={}),this._startOptions=t,this.trigger("app:initializing",t),e=function(n){var r;if(n)throw Error(n);return r=i._initializers.shift(),r?2===r.length?r.call(i,t,e):(r.call(i,t),e()):(o.extend(i,t),i.started=!0,i.trigger("app:initialized",t))},e(),this},n}(u.View),u.Router=function(t){function e(t){if(u.configure(this,t),!this.app)throw Error("Giraffe routers require an app! Please create an instance of Giraffe.App before creating a router.");if(this.app.addChild(this),"function"==typeof this.triggers&&(this.triggers=this.triggers.call(this)),!this.triggers)throw Error("Giraffe routers require a `triggers` map of routes to app events.");this._routes={},this._bindTriggers(),e.__super__.constructor.apply(this,arguments)}return i(e,t),e.prototype.namespace="",e.prototype._fullNamespace=function(){return this.parentRouter?this.parentRouter._fullNamespace()+"/"+this.namespace:this.namespace},e.prototype.triggers=null,e.prototype._bindTriggers=function(){var t,e,i,r,o,s=this;if(!this.triggers)throw Error("Expected router to implement `triggers` hash in the form {route: appEvent}");e=this._fullNamespace(),e.length>0&&(e+="/"),o=this.triggers,r=function(t,e,i){var r;return 0===e.indexOf("-> ")?r=function(){var t;return t=e.slice(3),s.navigate(t,{trigger:!0})}:0===e.indexOf("=> ")?r=function(){var t;return t=e.slice(3),s.navigate(i+t,{trigger:!0})}:(t=i+t,r=function(){var i,r;return i=arguments.length>=1?n.call(arguments,0):[],(r=s.app).trigger.apply(r,[e].concat(n.call(i),[t]))},s._registerRoute(e,t)),s.route(t,e,r)};for(i in o)t=o[i],r(i,t,e);return this},e.prototype._unbindTriggers=function(){var t;return t=this._getTriggerRegExpStrings(),s.history.handlers=o.reject(s.history.handlers,function(e){return o.contains(t,""+e.route)})},e.prototype._getTriggerRegExpStrings=function(){return o.map(o.keys(this.triggers),function(t){return""+s.Router.prototype._routeToRegExp(t)})},e.prototype.cause=function(){var t,e,i,r,a;return e=arguments[0],t=arguments.length>=2?n.call(arguments,1):[],r=this.getRoute.apply(this,[e].concat(n.call(t))),null!=r?(i=t[t.length-1],s.history.navigate(r,o.extend({trigger:!0},o.isObject(i)?i:{}))):(a=this.app).trigger.apply(a,[e].concat(n.call(t)))},e.prototype.isCaused=function(){var t,e,i;return e=arguments[0],t=arguments.length>=2?n.call(arguments,1):[],i=this.getRoute.apply(this,[e].concat(n.call(t))),null!=i?this._getLocation()===i:!1},e.prototype._getLocation=function(){return s.history._hasPushState?window.location.pathname.slice(1):window.location.hash},e.prototype.getRoute=function(){var t,e,i;return e=arguments[0],t=arguments.length>=2?n.call(arguments,1):[],i=this._routes[e],null!=i?(i=this._reverseHash.apply(this,[i].concat(n.call(t))),i?s.history._hasPushState?i:"#"+i:""===i?"":null):null},e.prototype._registerRoute=function(t,e){return this._routes[t]=e},e.prototype._reverseHash=function(){var t,e,i,r,s;return r=arguments[0],t=arguments.length>=2?n.call(arguments,1):[],e=t[0],null==e?r.replace(/\(.+?\)/g,""):(s=/[:|\*]\w+/g,i=o.isObject(e)?r.replace(s,function(t){var i,n;return i=t.slice(1),null!=(n=e[i])?n:""}):r.replace(s,function(){var e;return e=t.shift(),null!=e?e:""}),i=i.replace(/\(.+\)/g,function(t){return t.match(/\w+/g)?t.replace(/\(|\)/g,""):""}))},e.prototype.reload=function(t){return t&&(s.history.stop(),window.location=t),window.location.reload()},e.prototype.appEvents=null,e.prototype._dispose=function(){return this._unbindTriggers()},e}(s.Router),u.Model=function(t){function e(t,i){u.configure(this,i),e.__super__.constructor.apply(this,arguments)}return i(e,t),e.defaultOptions={omittedOptions:"parse"},e.prototype.appEvents=null,e.prototype._dispose=function(){var t;return null!=(t=this.collection)?t.remove(this):void 0},e}(s.Model),u.Collection=function(t){function e(t,i){u.configure(this,i),e.__super__.constructor.apply(this,arguments)}return i(e,t),e.defaultOptions={omittedOptions:"parse"},e.prototype.model=u.Model,e.prototype.appEvents=null,e.prototype._dispose=function(){var t,e,i,n;for(n=this.models.slice(),e=0,i=n.length;i>e;e++)t=n[e],t.dispose();return this},e.prototype._removeReference=function(t){return e.__super__._removeReference.apply(this,arguments),t._disposed?void 0:"function"==typeof t.dispose?t.dispose():void 0},e}(s.Collection),u.configure=function(t,e){var i,n,r,s;if(!o.isObject(t))throw Error("Giraffe.configure requires an object as the first parameter");return n=o.extend({},u.defaultOptions,null!=(r=t.constructor)?r.defaultOptions:void 0,t.defaultOptions,e),i=null!=(s=n.omittedOptions)?s:t.omittedOptions,i!==!0&&o.extend(t,o.omit(n,i)),null==t.dispose&&(t.dispose=u.dispose),null==t.app&&(t.app=u.app),t.appEvents&&u.bindAppEvents(t),t.initialize?u.wrapFn(t,"initialize",t._beforeInitialize,h):h.call(t),t},u.defaultOptions={},h=function(){return this.dataEvents?u.bindDataEvents(this):void 0},u.dispose=function(){var t;return t=arguments.length>=1?n.call(arguments,0):[],"function"==typeof this.trigger&&this.trigger.apply(this,["disposing",this].concat(n.call(t))),"function"==typeof this.beforeDispose&&this.beforeDispose.apply(this,t),this._disposed=!0,"function"==typeof this._dispose&&this._dispose.apply(this,t),this.app=null,"function"==typeof this.stopListening&&this.stopListening(),"function"==typeof this.trigger&&this.trigger.apply(this,["disposed",this].concat(n.call(t))),"function"==typeof this.afterDispose&&this.afterDispose.apply(this,t),this},u.bindAppEvents=function(t){return u.bindEventMap(t,t.app,t.appEvents)},u.bindDataEvents=function(t){var e,i,n,r,o,s;if(i=t.dataEvents){"function"==typeof i&&(i=t.dataEvents());for(n in i){if(e=i[n],o=n.split(" "),2>o.length)throw Error("Data event must specify target object, ex: {'change collection': 'handler'}");if(s=o.pop(),s="this"===s||"@"===s?t:t[s],!s)throw Error("Target object not found for data event '"+n+"'");r=o.join(" "),u.bindEvent(t,s,r,e)}return t}},u.bindEvent=function(){var t;return t=arguments.length>=1?n.call(arguments,0):[],t.push("listenTo"),c.apply(null,t)},u.unbindEvent=function(){var t;return t=arguments.length>=1?n.call(arguments,0):[],t.push("stopListening"),c.apply(null,t)},u.bindEventMap=function(){var t;return t=arguments.length>=1?n.call(arguments,0):[],t.push("listenTo"),f.apply(null,t)},u.unbindEventMap=function(){var t;return t=arguments.length>=1?n.call(arguments,0):[],t.push("stopListening"),f.apply(null,t)},c=function(t,e,i,n,r){if("string"==typeof n&&(n=t[n]),"function"!=typeof n)throw Error("callback for `'"+i+"'` not found");return t[r](e,i,n)},f=function(t,e,i,n){var r,o;if("function"==typeof i&&(i=i.call(t)),i){for(o in i)r=i[o],c(t,e,o,r,n);return t}},u.wrapFn=function(t,e,i,r){var o,s;return s=t[e],"function"==typeof s?(o=e[0].toUpperCase()+e.slice(1),t[e]=function(){var e,a,l,u;return e=arguments.length>=1?n.call(arguments,0):[],null!=i&&i.apply(t,e),"function"==typeof t[l="before"+o]&&t[l].apply(t,e),a=s.apply(t,e),"function"==typeof t[u="after"+o]&&t[u].apply(t,e),null!=r&&r.apply(t,e),a}):void 0},u})}).call(this);