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