Object.extend(Date.prototype,{monthnames:["January","February","March","April","May","June","July","August","September","October","November","December"],daynames:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],succ:function(){var B=new Date(this.getFullYear(),this.getMonth(),this.getDate()+1);B.setHours(this.getHours(),this.getMinutes(),this.getSeconds(),this.getMilliseconds());return B},firstofmonth:function(){return new Date(this.getFullYear(),this.getMonth(),1)},lastofmonth:function(){return new Date(this.getFullYear(),this.getMonth()+1,0)},formatPadding:true,format:function(D){if(!this.valueOf()){return"&nbsp;"}var F=this;var E={yyyy:F.getFullYear(),mmmm:this.monthnames[F.getMonth()],mmm:this.monthnames[F.getMonth()].substr(0,3),mm:this.formatPadding?((F.getMonth()).succ()).toPaddedString(2):(F.getMonth()).succ(),dddd:this.daynames[F.getDay()],ddd:this.daynames[F.getDay()].substr(0,3),dd:F.getDate().toPaddedString(2),hh:h=F.getHours()%12?h:12,nn:F.getMinutes(),ss:F.getSeconds(),"a/p":F.getHours()<12?"a":"p"};return D.gsub(/(yyyy|mmmm|mmm|mm|dddd|ddd|dd|hh|nn|ss|a\/p)/i,function(A){return E[A[0].toLowerCase()]})}});var scal={};scal=Class.create();scal.prototype={initialize:function(E,F){this.element=$(E);var D=Try.these(function(){if(!Object.isUndefined(Effect)){return"Effect"}},function(){return"Element"});this.options=Object.extend({oncalchange:Prototype.emptyFunction,daypadding:false,titleformat:"mmmm yyyy",updateformat:"yyyy-mm-dd",closebutton:"X",prevbutton:"&laquo;",nextbutton:"&raquo;",yearnext:"&raquo;&raquo;",yearprev:"&laquo;&laquo;",openeffect:D=="Effect"?Effect.Appear:Element.show,closeeffect:D=="Effect"?Effect.Fade:Element.hide,exactweeks:false,dayheadlength:2,weekdaystart:0,planner:false,tabular:false},arguments[2]||{});this.table=false;this.thead=false;this.startdate=this._setStartDate(arguments[2]);if(this.options.planner){this._setupPlanner(this.options.planner)}if(this.options.tabular){this.table=new Element("table",{"class":"cal_table",border:0,cellspacing:0,cellpadding:0});this.thead=new Element("thead");this.table.insert(this.thead);this.element.insert(this.table)}this.updateelement=F;this._setCurrentDate(this.startdate);this.initDate=new Date(this.currentdate);this.controls=this._buildControls();this.title.setAttribute("title",this.initDate.format(this.options.titleformat));this._updateTitles();this[this.table?"thead":"element"].insert(this.controls);this.cal_wrapper=this._buildHead();this.cells=[];this._buildCal()},_setStartDate:function(){var C=arguments[0];var D=new Date();this.options.month=C&&C.month&&Object.isNumber(C.month)?C.month-1:D.getMonth();this.options.year=C&&C.year&&Object.isNumber(C.year)?C.year:D.getFullYear();this.options.day=C&&C.day&&Object.isNumber(C.day)?C.day:(this.options.month!=D.getMonth())?1:D.getDate();D.setHours(0,0,0,0);D.setDate(this.options.day);D.setMonth(this.options.month);D.setFullYear(this.options.year);return D},_emptyCells:function(){if(this.cells.size()>0){this.cells.invoke("stopObserving");this.cells.invoke("remove");this.cells=[]}},_buildCal:function(){this._emptyCells();if(!(Object.isUndefined(this.cal_weeks_wrapper)||this.table)){this.cal_weeks_wrapper.remove()}this.cal_weeks_wrapper=this._buildWrapper();if(this.table){this.table.select("tbody tr.weekbox:not(.weekboxname)").invoke("remove");this.table.select("tbody.cal_wrapper").invoke("remove");this.cal_weeks_wrapper.each(function(B){this.cal_wrapper.insert(B)}.bind(this))}else{this.cal_wrapper.insert(this.cal_weeks_wrapper.insert(new Element("div",{"class":"clear"})))}this[this.table?"table":"element"].insert(this.cal_wrapper)},_click:function(D,C){this.element.select(".dayselected").invoke("removeClassName","dayselected");(D.target.hasClassName("daybox")?D.target:D.target.up()).addClassName("dayselected");this._setCurrentDate(this.dateRange[C]);this._updateExternal()},_updateExternal:function(){if(Object.isFunction(this.updateelement)){this.updateelement(this.currentdate)}else{var B=$(this.updateelement);B[B.tagName=="INPUT"?"setValue":"update"](this.currentdate.format(this.options.updateformat))}},_buildHead:function(){var D=new Element(this.table?"tbody":"div",{"class":"cal_wrapper"});var C=new Element(this.table?"tr":"div",{"class":"weekbox weekboxname"});Date.prototype.daynames.sortBy(function(A,B){B-=this.options.weekdaystart;if(B<0){B+=7}return B}.bind(this)).each(function(A,F){var B=new Element(this.table?"td":"div",{"class":"cal_day_name_"+F});B.addClassName("daybox").addClassName("dayboxname").update(A.substr(0,this.options.dayheadlength));if(F==6){B.addClassName("endweek")}C.insert(B)}.bind(this));return D.insert(C)},_buildWrapper:function(){var O=new Date(this.firstofmonth.getFullYear(),this.firstofmonth.getMonth(),this.firstofmonth.getDate());var Q=new Date(this.lastofmonth.getFullYear(),this.lastofmonth.getMonth(),this.lastofmonth.getDate());if(this.options.weekdaystart-O.getDay()<O.getDate()){O.setDate(O.getDate()-O.getDay()+this.options.weekdaystart)}else{O.setDate(O.getDate()-(O.getDay()+7-this.options.weekdaystart))}var M=$A($R(O,Q));var S=this.table?[]:new Element("div",{"class":"calweekswrapper"});var L;var K;var P;this.dateRange=[];this.indicators=[];var R=function(A){K.insert(this._buildDay(L,A));P=A}.bind(this);M.eachSlice(7,function(A,B){L=B;K=new Element(this.table?"tr":"div",{"class":"cal_week_"+L}).addClassName("weekbox");while(A.length<7){A.push(A.last().succ())}A.map(R);S[this.table?"push":"insert"](K)}.bind(this));if(!this.options.exactweeks){var T=42-this.cells.size();var N=Math.ceil(T/7);if(N>0){T=T/N}$R(1,N).each(function(A){L+=1;K=new Element(this.table?"tr":"div",{"class":"cal_week_"+L}).addClassName("weekbox");$R(1,T).each(function(B){var C=P.succ();K.insert(this._buildDay(L,C));S[this.table?"push":"insert"](K);P=C}.bind(this))}.bind(this))}return S},_compareDates:function(F,D,E){return(this.indicators.indexOf(E)>=0)?false:Object.isUndefined(["getMonth","getDate","getFullYear"].find(function(A){return F[A]()!=D[A]()}))},_buildDay:function(N,H){this.dateRange.push(H);var L="cal_day_"+N+"_"+H.getDay();var K=new Element(this.table?"td":"div",{"class":L});var I=new Element("div",{"class":L+"_date"}).addClassName("dayboxdate").update(this.options.daypadding?((H.getDate()).toPaddedString(2)):H.getDate());var J=new Element("div",{"class":L+"_value"}).addClassName("dayboxvalue");if(this.options.planner){this._updatePlanner(H,J)}K.insert(I).insert(J).addClassName("daybox").addClassName("daybox"+H.format("dddd").toLowerCase());if(this._compareDates(H,this.currentdate,"dayselected")){K.addClassName("dayselected");this.indicators.push("dayselected")}if(this._compareDates(H,new Date(),"today")){K.addClassName("today");this.indicators.push("today")}if(H.getDay()==6){K.addClassName("endweek")}var M=H.getMonth()!=this.currentdate.getMonth()?["dayoutmonth","dayinmonth"]:["dayinmonth","dayoutmonth"];K.addClassName(M[0]);if(K.hasClassName(M[1])){K.removeClassName(M[1])}this.cells.push(K);return K.observe("click",this._click.bindAsEventListener(this,this.cells.size()-1))},_updateTitles:function(){var E=this.currentdate.getFullYear();var F=this.currentdate.getMonth();var D={calprevmonth:Date.prototype.monthnames[(F-1)==-1?11:F-1],calprevyear:E-1,calnextyear:E+1,calnextmonth:Date.prototype.monthnames[(F+1)==12?0:F+1]};this.controls.select(".calcontrol").each(function(B){var A=D[B.className.split(" ")[0]];if(!Object.isUndefined(A)){B.setAttribute("title",A)}})},_buildControls:function(){var C=[{p:"calclose",u:this.options.closebutton,f:this.toggleCalendar.bindAsEventListener(this)},{p:"calprevmonth",u:this.options.prevbutton,f:this._switchCal.bindAsEventListener(this,"monthdown")},{p:"calprevyear",u:this.options.yearprev,f:this._switchCal.bindAsEventListener(this,"yeardown")},{p:"calnextyear",u:this.options.yearnext,f:this._switchCal.bindAsEventListener(this,"yearup")},{p:"calnextmonth",u:this.options.nextbutton,f:this._switchCal.bindAsEventListener(this,"monthup")},{p:"caltitle",u:this.currentdate.format(this.options.titleformat),f:this._switchCal.bindAsEventListener(this,"init")}];if(this.table){C=[C[1],C[2],C[5],C[3],C[4],C[0]]}var D=new Element(this.table?"tr":"div",{"class":"calheader"});C.each(function(B){var A=new Element(this.table?"td":"div",{"class":B.p});if(B.p=="caltitle"){this.title=A;if(this.table){A.writeAttribute({colspan:2})}A.update(B.u).observe("click",B.f)}else{A.addClassName("calcontrol");A[typeof (B.u)=="object"?"insert":"update"](B.u).observe("click",B.f)}D.insert(A)}.bind(this));return D},_switchCal:function(){if(arguments[1]){var J=arguments[0];var I=arguments[1];J.date=this.currentdate}else{var I=arguments[0]}var F={f:"setTime",p:this.initDate.getTime()};var G=this.currentdate.getDate();if(I!="init"){var H=this.currentdate[I.include("month")?"getMonth":"getFullYear"]();F={f:I.include("month")?"setMonth":"setYear",p:I.include("up")?H+1:H-1}}this.currentdate[F.f](F.p);if(this.currentdate.getDate()!=G){this.currentdate.setDate(0)}if(arguments[1]){this.options.oncalchange(J)}this._update()},_update:function(){this._setCurrentDate(arguments[0]?arguments[0]:this.currentdate);this.title.update(this.currentdate.format(this.options.titleformat));this._buildCal();this._updateTitles()},_setCurrentDate:function(B){this.currentdate=new Date(B.getFullYear(),B.getMonth(),B.getDate());this.firstofmonth=this.currentdate.firstofmonth();this.lastofmonth=this.currentdate.lastofmonth()},_getCellIndexByDate:function(F){var E=F.getTime();var D=0;this.dateRange.each(function(A,B){if(A.getTime()==E){D=B;throw $break}});return D},destroy:function(){this._emptyCells();if(this.table){this.table.remove()}else{this.cal_weeks_wrapper.remove()}this.controls.descendants().invoke("stopObserving");[this.cal_wrapper,this.controls].invoke("remove")},setCurrentDate:function(B){this[(B instanceof Date)?"_update":"_switchCal"](B);if(!arguments[1]){this._updateExternal()}return this.currentdate},toggleCalendar:function(){this.options[this.element.visible()?"closeeffect":"openeffect"](this.element)},getElementByDate:function(B){return this.cells[this._getCellIndexByDate(B)]},getElementsByWeek:function(B){return this.element.select(".weekbox:nth-of-type("+(B+1)+") .daybox:not(.dayboxname)")},getSelectedElement:function(){return this.element.select(".dayselected")[0]},getTodaysElement:function(){return this.element.select(".today")[0]},getDateByElement:function(B){return this.dateRange[this.cells.indexOf(B)]},_setupPlanner:Prototype.emptyFunction,_updatePlanner:Prototype.emptyFunction,openCalendar:function(){if(!this.isOpen()){this.toggleCalendar()}},closeCalendar:function(){if(this.isOpen()){this.toggleCalendar()}},isOpen:function(){return this.element.visible()}};