DateInput=(function(A){function B(C,D){if(typeof (D)!="object"){D={}}A.extend(this,B.DEFAULT_OPTS,D);this.input=A(C);this.bindMethodsToObj("show","hide","hideIfClickOutside","selectDate","prevMonth","nextMonth");this.build();this.selectDate();this.hide()}B.DEFAULT_OPTS={month_names:["January","February","March","April","May","June","July","August","September","October","November","December"],short_month_names:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],short_day_names:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],start_of_week:1};B.prototype={build:function(){this.monthNameSpan=A('<span class="month_name"></span>');var C=A('<p class="month_nav"></p>').append(A('<a href="#" class="prev">&laquo;</a>').click(this.prevMonth)," ",this.monthNameSpan," ",A('<a href="#" class="next">&raquo;</a>').click(this.nextMonth));var D="<table><thead><tr>";A(this.adjustDays(this.short_day_names)).each(function(){D+="<th>"+this+"</th>"});D+="</tr></thead><tbody></tbody></table>";this.dateSelector=this.rootLayers=A('<div class="date_selector"></div>').css({display:"none",position:"absolute",zIndex:100}).append(C,D).appendTo(document.body);if(A.browser.msie&&A.browser.version<7){this.ieframe=A('<iframe class="date_selector_ieframe" frameborder="0" src="#"></iframe>').css({position:"absolute",display:"none",zIndex:99}).insertBefore(this.dateSelector);this.rootLayers=this.rootLayers.add(this.ieframe)}this.tbody=A("tbody",this.dateSelector);this.input.change(this.bindToObj(function(){this.selectDate()}))},selectMonth:function(D){this.currentMonth=D;var I=this.rangeStart(D),H=this.rangeEnd(D);var C=this.daysBetween(I,H);var F="";for(var E=0;E<=C;E++){var G=new Date(I.getFullYear(),I.getMonth(),I.getDate()+E);if(this.isFirstDayOfWeek(G)){F+="<tr>"}if(G.getMonth()==D.getMonth()){F+='<td date="'+this.dateToString(G)+'"><a href="#">'+G.getDate()+"</a></td>"}else{F+='<td class="unselected_month" date="'+this.dateToString(G)+'">'+G.getDate()+"</td>"}if(this.isLastDayOfWeek(G)){F+="</tr>"}}this.monthNameSpan.empty().append(this.monthName(D)+" "+D.getFullYear());this.tbody.empty().append(F);A("a",this.tbody).click(this.bindToObj(function(J){this.selectDate(this.stringToDate(A(J.target).parent().attr("date")));this.hide();return false}));A("td[date="+this.dateToString(new Date())+"]",this.tbody).addClass("today")},selectDate:function(C){if(typeof (C)=="undefined"){C=this.stringToDate(this.input.val())}if(C){this.selectedDate=C;this.selectMonth(C);var D=this.dateToString(C);A("td[date="+D+"]",this.tbody).addClass("selected");if(this.input.val()!=D){this.input.val(D).change()}}else{this.selectMonth(new Date())}},show:function(){this.rootLayers.css("display","block");this.setPosition();this.input.unbind("focus",this.show);A([window,document.body]).click(this.hideIfClickOutside)},hide:function(){this.rootLayers.css("display","none");A([window,document.body]).unbind("click",this.hideIfClickOutside);this.input.focus(this.show)},hideIfClickOutside:function(C){if(C.target!=this.input[0]&&!this.insideSelector(C)){this.hide()}},stringToDate:function(C){var D;if(D=C.match(/^(\d{1,2}) ([^\s]+) (\d{4,4})$/)){return new Date(D[3],this.shortMonthNum(D[2]),D[1])}else{return null}},dateToString:function(C){return C.getDate()+" "+this.short_month_names[C.getMonth()]+" "+C.getFullYear()},setPosition:function(){var C=this.input.offset();this.rootLayers.css({top:C.top+this.input.outerHeight(),left:C.left});if(this.ieframe){this.ieframe.css({width:this.dateSelector.outerWidth(),height:this.dateSelector.outerHeight()})}},moveMonthBy:function(C){this.selectMonth(new Date(this.currentMonth.setMonth(this.currentMonth.getMonth()+C)))},prevMonth:function(){this.moveMonthBy(-1);return false},nextMonth:function(){this.moveMonthBy(1);return false},monthName:function(C){return this.month_names[C.getMonth()]},insideSelector:function(C){var D=this.dateSelector.offset();D.right=D.left+this.dateSelector.outerWidth();D.bottom=D.top+this.dateSelector.outerHeight();return C.pageY<D.bottom&&C.pageY>D.top&&C.pageX<D.right&&C.pageX>D.left},bindToObj:function(D){var C=this;return function(){return D.apply(C,arguments)}},bindMethodsToObj:function(){for(var C=0;C<arguments.length;C++){this[arguments[C]]=this.bindToObj(this[arguments[C]])}},indexFor:function(E,D){for(var C=0;C<E.length;C++){if(D==E[C]){return C}}},monthNum:function(C){return this.indexFor(this.month_names,C)},shortMonthNum:function(C){return this.indexFor(this.short_month_names,C)},shortDayNum:function(C){return this.indexFor(this.short_day_names,C)},daysBetween:function(D,C){D=Date.UTC(D.getFullYear(),D.getMonth(),D.getDate());C=Date.UTC(C.getFullYear(),C.getMonth(),C.getDate());return(C-D)/86400000},changeDayTo:function(F,C,D){var E=D*(Math.abs(C.getDay()-F-(D*7))%7);return new Date(C.getFullYear(),C.getMonth(),C.getDate()+E)},rangeStart:function(C){return this.changeDayTo(this.start_of_week,new Date(C.getFullYear(),C.getMonth()),-1)},rangeEnd:function(C){return this.changeDayTo((this.start_of_week-1)%7,new Date(C.getFullYear(),C.getMonth()+1,0),1)},isFirstDayOfWeek:function(C){return C.getDay()==this.start_of_week},isLastDayOfWeek:function(C){return C.getDay()==(this.start_of_week-1)%7},adjustDays:function(E){var D=[];for(var C=0;C<E.length;C++){D[C]=E[(C+this.start_of_week)%7]}return D}};A.fn.date_input=function(C){return this.each(function(){new B(this,C)})};A.date_input={initialize:function(C){A("input.date_input").date_input(C)}};return B})(jQuery)

$.extend(DateInput.DEFAULT_OPTS, {
  stringToDate: function(string) {
    var matches;
    if (matches = string.match(/^(\d{2,2})\/(\d{2,2})\/(\d{4,4})$/)) {
      return new Date(matches[3], matches[1] - 1, matches[2]);
    } else {
      return null;
    };
  },

  dateToString: function(date) {
    var month = (date.getMonth() + 1).toString();
    var dom = date.getDate().toString();
    if (month.length == 1) month = "0" + month;
    if (dom.length == 1) dom = "0" + dom;
    return month + "/" + dom + "/" + date.getFullYear();
  },
  start_of_week: 0
});