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 A=new Date(this.getFullYear(),this.getMonth(),this.getDate()+1);A.setHours(this.getHours(),this.getMinutes(),this.getSeconds(),this.getMilliseconds());return A},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(B){if(!this.valueOf()){return"&nbsp;"}var C=this;var A={yyyy:C.getFullYear(),mmmm:this.monthnames[C.getMonth()],mmm:this.monthnames[C.getMonth()].substr(0,3),mm:this.formatPadding?((C.getMonth()).succ()).toPaddedString(2):(C.getMonth()).succ(),dddd:this.daynames[C.getDay()],ddd:this.daynames[C.getDay()].substr(0,3),dd:C.getDate().toPaddedString(2),hh:h=C.getHours()%12?h:12,nn:C.getMinutes(),ss:C.getSeconds(),"a/p":C.getHours()<12?"a":"p"};return B.gsub(/(yyyy|mmmm|mmm|mm|dddd|ddd|dd|hh|nn|ss|a\/p)/i,function(D){return A[D[0].toLowerCase()]})}});var scal={};scal=Class.create();scal.prototype={initialize:function(A,C){this.element=$(A);var B=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:B=="Effect"?Effect.Appear:Element.show,closeeffect:B=="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.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=C;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 B=arguments[0];var A=new Date();this.options.month=B&&B.month&&Object.isNumber(B.month)?B.month-1:A.getMonth();this.options.year=B&&B.year&&Object.isNumber(B.year)?B.year:A.getFullYear();this.options.day=B&&B.day&&Object.isNumber(B.day)?B.day:(this.options.month!=A.getMonth())?1:A.getDate();A.setHours(0,0,0,0);A.setDate(this.options.day);A.setMonth(this.options.month);A.setFullYear(this.options.year);return A},_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(A){this.cal_wrapper.insert(A)}.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(A,B){this.element.select(".dayselected").invoke("removeClassName","dayselected");(A.target.hasClassName("daybox")?A.target:A.target.up()).addClassName("dayselected");this._setCurrentDate(this.dateRange[B]);this._updateExternal(A)},_updateExternal:function(A){if(Object.isFunction(this.updateelement)){this.updateelement(this.currentdate,A)}else{var B=$(this.updateelement);B[B.tagName=="INPUT"?"setValue":"update"](this.currentdate.format(this.options.updateformat))}},_buildHead:function(){var A=new Element(this.table?"tbody":"div",{"class":"cal_wrapper"});var B=new Element(this.table?"tr":"div",{"class":"weekbox weekboxname"});Date.prototype.daynames.sortBy(function(C,D){D-=this.options.weekdaystart;if(D<0){D+=7}return D}.bind(this)).each(function(D,E){var C=new Element(this.table?"td":"div",{"class":"cal_day_name_"+E});C.addClassName("daybox").addClassName("dayboxname").update(D.substr(0,this.options.dayheadlength));if(E==6){C.addClassName("endweek")}B.insert(C)}.bind(this));return A.insert(B)},_buildWrapper:function(){var H=new Date(this.firstofmonth.getFullYear(),this.firstofmonth.getMonth(),this.firstofmonth.getDate());var F=new Date(this.lastofmonth.getFullYear(),this.lastofmonth.getMonth(),this.lastofmonth.getDate());if(this.options.weekdaystart-H.getDay()<H.getDate()){H.setDate(H.getDate()-H.getDay()+this.options.weekdaystart)}else{H.setDate(H.getDate()-(H.getDay()+7-this.options.weekdaystart))}var J=$A($R(H,F));var D=this.table?[]:new Element("div",{"class":"calweekswrapper"});var A;var B;var G;this.dateRange=[];this.indicators=[];var E=function(K){B.insert(this._buildDay(A,K));G=K}.bind(this);J.eachSlice(7,function(K,L){A=L;B=new Element(this.table?"tr":"div",{"class":"cal_week_"+A}).addClassName("weekbox");while(K.length<7){K.push(K.last().succ())}K.map(E);D[this.table?"push":"insert"](B)}.bind(this));if(!this.options.exactweeks){var C=42-this.cells.size();var I=Math.ceil(C/7);if(I>0){C=C/I}$R(1,I).each(function(K){A+=1;B=new Element(this.table?"tr":"div",{"class":"cal_week_"+A}).addClassName("weekbox");$R(1,C).each(function(M){var L=G.succ();B.insert(this._buildDay(A,L));D[this.table?"push":"insert"](B);G=L}.bind(this))}.bind(this))}return D},_compareDates:function(C,B,A){return(this.indicators.indexOf(A)>=0)?false:Object.isUndefined(["getMonth","getDate","getFullYear"].find(function(D){return C[D]()!=B[D]()}))},_buildDay:function(A,G){this.dateRange.push(G);var C="cal_day_"+A+"_"+G.getDay();var D=new Element(this.table?"td":"div",{"class":C});var F=new Element("div",{"class":C+"_date"}).addClassName("dayboxdate").update(this.options.daypadding?((G.getDate()).toPaddedString(2)):G.getDate());var E=new Element("div",{"class":C+"_value"}).addClassName("dayboxvalue");if(this.options.planner){this._updatePlanner(G,D,E)}D.insert(F).insert(E).addClassName("daybox").addClassName("daybox"+G.format("dddd").toLowerCase());if(this._compareDates(G,this.currentdate,"dayselected")){D.addClassName("dayselected");this.indicators.push("dayselected")}if(this._compareDates(G,new Date(),"today")){D.addClassName("today");this.indicators.push("today")}if(G.getDay()==6){D.addClassName("endweek")}var B=G.getMonth()!=this.currentdate.getMonth()?["dayoutmonth","dayinmonth"]:["dayinmonth","dayoutmonth"];D.addClassName(B[0]);if(D.hasClassName(B[1])){D.removeClassName(B[1])}this.cells.push(D);return D.observe("click",this._click.bindAsEventListener(this,this.cells.size()-1))},_updateTitles:function(){var A=this.currentdate.getFullYear();var C=this.currentdate.getMonth();var B={calprevmonth:Date.prototype.monthnames[(C-1)==-1?11:C-1],calprevyear:A-1,calnextyear:A+1,calnextmonth:Date.prototype.monthnames[(C+1)==12?0:C+1]};this.controls.select(".calcontrol").each(function(E){var D=B[E.className.split(" ")[0]];if(!Object.isUndefined(D)){E.setAttribute("title",D)}})},_buildControls:function(){var B=[{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){B=[B[1],B[2],B[5],B[3],B[4],B[0]]}var A=new Element(this.table?"tr":"div",{"class":"calheader"});B.each(function(D){var C=new Element(this.table?"td":"div",{"class":D.p});if(D.p=="caltitle"){this.title=C;if(this.table){C.writeAttribute({colspan:2})}C.update(D.u).observe("click",D.f)}else{C.addClassName("calcontrol");C[typeof (D.u)=="object"?"insert":"update"](D.u).observe("click",D.f)}A.insert(C)}.bind(this));return A},_switchCal:function(){if(arguments[1]){var B=arguments[0];var C=arguments[1];B.date=this.dateRange[10]}else{var C=arguments[0]}var A={f:"setTime",p:this.initDate.getTime()};if(C!="init"){var D=B.date[C.include("month")?"getMonth":"getFullYear"]();A={f:C.include("month")?"setMonth":"setYear",p:C.include("up")?D+1:D-1}}this.currentdate[A.f](A.p);if(arguments[1]){this.options.oncalchange(B)}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(A){this.currentdate=new Date(A.getFullYear(),A.getMonth(),A.getDate());this.firstofmonth=this.currentdate.firstofmonth();this.lastofmonth=this.currentdate.lastofmonth()},_getCellIndexByDate:function(C){var A=C.getTime();var B=0;this.dateRange.each(function(D,E){if(D.getTime()==A){B=E;throw $break}});return B},update:function(){this._update()},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(A){this[(A instanceof Date)?"_update":"_switchCal"](A);if(!arguments[1]){this._updateExternal()}return this.currentdate},toggleCalendar:function(){this.options[this.element.visible()?"closeeffect":"openeffect"](this.element)},getElementByDate:function(A){return this.cells[this._getCellIndexByDate(A)]},getElementsByWeek:function(A){return this.element.select(".weekbox:nth-of-type("+(A+1)+") .daybox:not(.dayboxname)")},getSelectedElement:function(){return this.element.select(".dayselected")[0]},getTodaysElement:function(){return this.element.select(".today")[0]},getDateByElement:function(A){return this.dateRange[this.cells.indexOf(A)]},_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()}};