var Fatsoma={version:"0.1",nextID:1,getIdentifier:function(){return"fui"+this.nextID++},Common:{},UI:{Generators:{}},Temp:{},Time:{constants:(function(){var d,f,c,b,e,a;d=$H();f=$A([["ms",1000],["sec",60],["min",60],["hour",24],["day",7],["week"]]);for(c=0;c<f.length-1;c++){e=f[c][0];a=1;for(b=c+1;b<f.length;b++){a*=f[b-1][1];d.set(e+"_in_"+f[b][0],a)}}return d})(),getUnits:function(c){var j,g,a,b,f,d,e,h,m,l,k,n;a=this.constants;j=$A(["sec","min","hour","day"]);units=$H();e=c;for(g=0;g<j.length;g++){b=j[g];h=b+"s";f="total_"+h;l=a.get("ms_in_"+b);d=Math.floor(c/l);units.set(f,d);k=a.get(b+"_in_"+j[g+1]);n=e/l;if(k==undefined){m=n}else{m=n%k}e-=m*l;m=Math.floor(m);units.set(h,m)}return units},getHumanReadableDate:function(c){var e=$w("Monday Tuesday Wednesday Thursday Friday Saturday Sunday");var a=$w("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");var b=function(d){if(Math.floor((d%100)/10)==1){return d+"th"}else{switch(d%10){case 1:return d+"st";case 2:return d+"nd";case 3:return d+"rd";default:return d+"th"}}};return e[c.getDay()]+" "+a[c.getMonth()]+" "+b(c.getDate())+" "+c.getFullYear();return +c.getDate().toString().padLeft(2,"0")+"-"+(c.getMonth()+1).toString().padLeft(2,"0")+"-"+c.getFullYear()}},String:{afterLast:function(b,a){a=RegExp.escape(a);var d=new RegExp(a+"([^"+a+"]+)$");var c;if(c=d.exec(b)){return c[1]}return false},beforeFirst:function(b,a){a=RegExp.escape(a);var d=new RegExp("^([^"+a+"]+)"+a+"$");var c;if(c=d.exec(b)){return c[1]}return b},stripChars:function(c,b){if(is_array(b)){b=b.uniq();var a=c.toArray().reject(function(d){return b.include(d)}).join("");return a}else{if(typeof b=="string"){if(isCharClass(b)){b=new RegExp(b,"g");return c.replace(b,"")}else{return stripChars(c,b.toArray())}}else{throw"Attempted to strip chars from '"+c+"' but did not pass in an acceptable variable (array or string) to strip by"}}},restrictChars:function(d,c){if(is_array(c)){c=c.uniq();var a=d.toArray().select(function(e){return c.include(e)}).join("");return a}else{if(typeof c=="string"){if(isCharClass(c)){var b=invertCharClass(c);return stripChars(d,b)}else{return restrictChars(d,c.toArray())}}}},isCharClass:function(a){return/^\[([^\]]|\\\])*\]$/.test(a)},invertCharClass:function(a){if(isCharClass(a)){if(/^\[\^/.test(a)){return a.replace(/^\[\^/,"[")}else{return a.replace(/^\[/,"[^")}}else{return false}},addCommas:function(b,a){if(a==undefined){a=true}nstr=b.toString();if(a!==true&&nstr.beforeFirst(".").restrictChars("[0-9]").length<=4){return nstr}find=/^([+-]?\d+)(\d\d\d)/;while(find.match(nstr)){nstr=nstr.replace(find,"$1,$2")}return nstr},str_reverse:function(a){return a.toArray().reverse().join("")},ltrim:function(c,a){if(a==undefined){a=" "}var b=0;c.toArray().each(function(d){if(a.include(d)){b++}else{throw $break}});return c.substr(b)},rtrim:function(b,a){b=Fatsoma.String.str_reverse(b);b=Fatsoma.String.ltrim(b,a);b=Fatsoma.String.str_reverse(b);return b}}};OrderedHash=Class.create(Hash,{_sortedKeySearch:function(a,b){keys=this.keys();keys.sort(b);key=keys.find(a);if(!key){return null}return this.get(key)},getNext:function(a){return this._sortedKeySearch(function(b){return b>a},function(d,c){return d-c})},getPrevious:function(a){return this._sortedKeySearch(function(b){return b<a},function(d,c){return c-d})}});Fatsoma.Cookie={pairs:(function(){var b=$H();var a=$A(document.cookie.split("; "));a.each(function(c){c=c.split("=");b.set(c[0],c[1])});return b})(),set:function(e,d,c){var b=e+"="+d;if(c){var a=new Date();a.setTime(a.getTime()+c);b+="; expires="+a.toGMTString()}document.cookie=b;if(d!==null){this.pairs.set(e,d)}else{this.pairs.unset(e)}},get:function(a){return this.pairs.get(a)},unset:function(a){this.set(a,null,-1)}};Fatsoma.Common.getPath=function getPath(a){if(a==undefined){a=true}var c=window.location+"";var b=c.replace(/http:\/\/[^\/]+/,"");if(a){b=b.replace(/\?.*$/,"")}return b};Fatsoma.Common.pluralize=function pluralize(b,a){if(b==1){return"1 "+a}else{return b+" "+a+"s"}};Fatsoma.Common.HashTokens=Class.create({pairs:null,_observeInterval:500,_observeTimeout:null,_oldHash:null,initialize:function(a){this.pairs=$H();this.loadCurrent();if(a!=undefined){this._observeInterval=a}this.initObserver();Event.observe(document,"window:hashchange",function(){this.loadCurrent()}.bind(this))},loadCurrent:function(){var a=Fatsoma.String.ltrim(window.location.hash,"#").parseQuery();a=$H(a);changed=$A();a.each(function(b){if(b.value!=this.pairs.get(b.key)){changed.push($H({key:b.key,previous:this.pairs.get(b.key),value:b.value}))}}.bind(this));this.pairs.update(a);changed.each(function(b){document.fire("hashchange:"+b.get("key"),{previous:b.get("previous"),value:b.get("value")})})},set:function(a,b){this.pairs.set(a,b);this.update()},get:function(a){return this.pairs.get(a)},unset:function(a){this.pairs.unset(a);this.update()},update:function(){window.location.hash=this.pairs.toQueryString()},redirectTo:function(a){window.location=a+"#"+this.toString()},toString:function(){return this.pairs.toQueryString()
},initObserver:function(){if(this.hasNativeHashChangeSupport()){window.onhashchange=function(){document.fire("window:hashchange")}}else{this._oldHash=document.location.hash;this.checkHash()}},hasNativeHashChangeSupport:function(){return"onhashchange" in window},respondTo:function(a,b){document.observe("hashchange:"+a,b);document.observe("dom:loaded",function(){if(this.get(a)!=undefined){b()}}.bind(this).defer())},checkHash:function(){this._observeTimeout=setTimeout(function(){if(this._oldHash!=document.location.hash){document.fire("window:hashchange");this._oldHash=document.location.hash}this.checkHash()}.bind(this),this._observeInterval)}});Fatsoma.Common.WindowHash=new Fatsoma.Common.HashTokens();Fatsoma.Common.QueryString=Class.create(Hash,{loadCurrent:function(){this.update(window.location.search.toQueryParams())},toString:function(){return this.toQueryString()},refresh:function(){window.location=window.location.pathname+"?"+this}});Fatsoma.QueryString=new Fatsoma.Common.QueryString();Fatsoma.QueryString.loadCurrent();Fatsoma.UI.SimpleHover=Class.create({element:null,active:false,hovering:false,hoveroutChecker:null,activateCallback:null,deactivateCallback:null,initialize:function(a){a=$(a);if(!a){throw"Attempted to create SimpleHover from an element that could not be found"}this.bindTo(a);this.element=a;this.element.hide()},bindTo:function(a){a=$(a);if(!a){throw"Attempted to bind SimpleHover to an element that could not be found"}a.observe("mouseout",this._boundOut.bindAsEventListener(this));a.observe("mouseover",this._boundOver.bindAsEventListener(this))},_boundOver:function(a){this.hovering=true;this.show()},_boundOut:function(a){this.hovering=false},hide:function(a){if(!this.active){return}this.active=false;this.hoveroutChecker.stop();this.element.hide();if(this.deactivateCallback!=null){this.deactivateCallback()}},show:function(){if(this.active){return}this.active=true;this.hoveroutChecker=new PeriodicalExecuter(function(a){if(!this.hovering){this.hide()}}.bind(this),0.025);this.element.show();if(this.activateCallback!=null){this.activateCallback()}},onActivate:function(a){this.activateCallback=a},onDeactivate:function(a){this.deactivateCallback=a}});Fatsoma.UI.CheckboxList=Class.create({count:0,initialize:function(){this.checkboxes=$A();this.globalCheckboxes=$A();this.setCheckState=this._setCheckboxes.curry(this.checkboxes);this.setGlobalCheckState=this._setCheckboxes.curry(this.globalCheckboxes);this.fatID=Fatsoma.getIdentifier()},_fireUpdate:function(){document.fire(this.fatID+":update")},addUpdateObserver:function(a){document.observe(this.fatID+":update",a)},addCheckbox:function(a){a=$(a);a.observe("click",this.clickListener.bindAsEventListener(this));if(a.checked){++this.count}this.checkboxes.push(a);this._updateGlobalCheckboxes()},addGlobalCheckbox:function(a){a=$(a);a.observe("click",this.globalClickListener.bindAsEventListener(this));this.globalCheckboxes.push(a)},clickListener:function(b){var a=$(b.element());if(a.checked){++this.count}else{--this.count}this._updateGlobalCheckboxes();this._fireUpdate()},_updateGlobalCheckboxes:function(){if(this.count==this.checkboxes.length){this.setGlobalCheckState(true)}else{if(this.count===0){this.setGlobalCheckState(false)}}},globalClickListener:function(b){var a=$(b.element());this.checkboxes.each(function(c){c.checked=a.checked}.bind(this));if(a.checked){this.count=this.checkboxes.length}else{this.count=0}this._fireUpdate()},_setCheckboxes:function(a,b){if(b===undefined){throw"state argument not supplied in _setCheckboxes"}a.each(function(c){c.checked=b})}});Fatsoma.UI.Generators.CheckboxList=function(a,b){var c=new Fatsoma.UI.CheckboxList();Event.observe(document,"dom:loaded",function(){$$("#"+a+' input[type="checkbox"]').each(function(d){c.addCheckbox(d)});c.addGlobalCheckbox(a+"_all");c.addUpdateObserver(function(){var d=$(a+"_status");d.update(Fatsoma.Common.pluralize(c.count,b)+" selected")})});return c};Object.extend(String.prototype,{to_i:function(){return parseInt(this)},to_f:function(){return parseFloat(this)},padLeft:function(a,b){return new Array(a-this.length+1).join(b||" ")+this},padRight:function(a,b){return this+new Array(a-this.length+1).join(b||" ")}});Object.extend(Event,{_isDomReady:false,asap:function(a){if(this._isDomReady){a()}else{document.observe("dom:loaded",a)}}});document.observe("dom:loaded",function(){Event._isDomReady=true});Object.extend(Array.prototype,{argmin:function(a){a=a||Prototype.K;if(this.length==0){return null}var b=this.first(),c=a(this.first());this.each(function(d){val=a(d);if(val<c){b=d;c=val}});return b},argmax:function(a){return this.argmin(function(b){return -a(b)})}});
