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._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 A=arguments[0];var B=new Date();this.options.month=A&&A.month&&Object.isNumber(A.month)?A.month-1:B.getMonth();this.options.year=A&&A.year&&Object.isNumber(A.year)?A.year:B.getFullYear();this.options.day=A&&A.day&&Object.isNumber(A.day)?A.day:(this.options.month!=B.getMonth())?1:B.getDate();B.setHours(0,0,0,0);B.setDate(this.options.day);B.setMonth(this.options.month);B.setFullYear(this.options.year);return B},_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(B,A){this.element.select(".dayselected").invoke("removeClassName","dayselected");(B.target.hasClassName("daybox")?B.target:B.target.up()).addClassName("dayselected");this._setCurrentDate(this.dateRange[A]);this._updateExternal()},_updateExternal:function(){if(Object.isFunction(this.updateelement)){this.updateelement(this.currentdate)}else{var A=$(this.updateelement);A[A.tagName=="INPUT"?"setValue":"update"](this.currentdate.format(this.options.updateformat))}},_buildHead:function(){var B=new Element(this.table?"tbody":"div",{"class":"cal_wrapper"});var A=new Element(this.table?"tr":"div",{"class":"weekbox weekboxname"});Date.prototype.daynames.sortBy(function(D,C){C-=this.options.weekdaystart;if(C<0){C+=7}return C}.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")}A.insert(C)}.bind(this));return B.insert(A)},_buildWrapper:function(){var B=new Date(this.firstofmonth.getFullYear(),this.firstofmonth.getMonth(),this.firstofmonth.getDate());var J=new Date(this.lastofmonth.getFullYear(),this.lastofmonth.getMonth(),this.lastofmonth.getDate());if(this.options.weekdaystart-B.getDay()<B.getDate()){B.setDate(B.getDate()-B.getDay()+this.options.weekdaystart)}else{B.setDate(B.getDate()-(B.getDay()+7-this.options.weekdaystart))}var D=$A($R(B,J));var H=this.table?[]:new Element("div",{"class":"calweekswrapper"});var E;var F;var A;this.dateRange=[];this.indicators=[];var I=function(K){F.insert(this._buildDay(E,K));A=K}.bind(this);D.eachSlice(7,function(L,K){E=K;F=new Element(this.table?"tr":"div",{"class":"cal_week_"+E}).addClassName("weekbox");while(L.length<7){L.push(L.last().succ())}L.map(I);H[this.table?"push":"insert"](F)}.bind(this));if(!this.options.exactweeks){var G=42-this.cells.size();var C=Math.ceil(G/7);if(C>0){G=G/C}$R(1,C).each(function(K){E+=1;F=new Element(this.table?"tr":"div",{"class":"cal_week_"+E}).addClassName("weekbox");$R(1,G).each(function(L){var M=A.succ();F.insert(this._buildDay(E,M));H[this.table?"push":"insert"](F);A=M}.bind(this))}.bind(this))}return H},_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(C,B){this.dateRange.push(B);var E="cal_day_"+C+"_"+B.getDay();var F=new Element(this.table?"td":"div",{"class":E});var A=new Element("div",{"class":E+"_date"}).addClassName("dayboxdate").update(this.options.daypadding?((B.getDate()).toPaddedString(2)):B.getDate());var G=new Element("div",{"class":E+"_value"}).addClassName("dayboxvalue");if(this.options.planner){this._updatePlanner(B,G)}F.insert(A).insert(G).addClassName("daybox").addClassName("daybox"+B.format("dddd").toLowerCase());if(this._compareDates(B,this.currentdate,"dayselected")){F.addClassName("dayselected");this.indicators.push("dayselected")}if(this._compareDates(B,new Date(),"today")){F.addClassName("today");this.indicators.push("today")}if(B.getDay()==6){F.addClassName("endweek")}var D=B.getMonth()!=this.currentdate.getMonth()?["dayoutmonth","dayinmonth"]:["dayinmonth","dayoutmonth"];F.addClassName(D[0]);if(F.hasClassName(D[1])){F.removeClassName(D[1])}this.cells.push(F);return F.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(D){var E=B[D.className.split(" ")[0]];if(!Object.isUndefined(E)){D.setAttribute("title",E)}})},_buildControls:function(){var A=[{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){A=[A[1],A[2],A[5],A[3],A[4],A[0]]}var B=new Element(this.table?"tr":"div",{"class":"calheader"});A.each(function(C){var D=new Element(this.table?"td":"div",{"class":C.p});if(C.p=="caltitle"){this.title=D;if(this.table){D.writeAttribute({colspan:2})}D.update(C.u).observe("click",C.f)}else{D.addClassName("calcontrol");D[typeof (C.u)=="object"?"insert":"update"](C.u).observe("click",C.f)}B.insert(D)}.bind(this));return B},_switchCal:function(){if(arguments[1]){var E=arguments[0];var A=arguments[1];E.date=this.currentdate}else{var A=arguments[0]}var D={f:"setTime",p:this.initDate.getTime()};var C=this.currentdate.getDate();if(A!="init"){var B=this.currentdate[A.include("month")?"getMonth":"getFullYear"]();D={f:A.include("month")?"setMonth":"setYear",p:A.include("up")?B+1:B-1}}this.currentdate[D.f](D.p);if(this.currentdate.getDate()!=C){this.currentdate.setDate(0)}if(arguments[1]){this.options.oncalchange(E)}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(E,D){if(E.getTime()==A){B=D;throw $break}});return B},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()}};