var DatePicker=new Class({Implements:[Options],options:{dayChars:2,dayNames:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],daysInMonth:[31,28,31,30,31,30,31,31,30,31,30,31],format:"dd-mm-yyyy",monthNames:["January","February","March","April","May","June","July","August","September","October","November","December"],startDay:7,yearOrder:"asc",yearRange:15,yearStart:(new Date().getFullYear())},initialize:function(b,a){this.setOptions(a);if(b.get("value")!=""){this.then=new Date(b.value);this.today=new Date()}else{this.then=this.today=new Date()}this.oldYear=this.year=this.then.getFullYear();this.oldMonth=this.month=this.then.getMonth();this.oldDay=this.then.getDate();this.nowYear=this.today.getFullYear();this.nowMonth=this.today.getMonth();this.nowDay=this.today.getDate();this.options.monthNames=(this.options.monthNames&&this.options.monthNames.length==12?this.options.monthNames:this.options.monthNames)||this.options.monthNames;this.options.daysInMonth=(this.options.daysInMonth&&this.options.daysInMonth.length==12?this.options.daysInMonth:this.options.daysInMonth)||this.options.daysInMonth;this.options.dayNames=(this.options.dayNames&&this.options.dayNames.length==7?this.options.dayNames:this.options.dayNames)||this.options.dayNames;this.dp=b;this.dp.set("id",b.get("name"));this.container=this.calendar=this.active=false;this.dp.addEvents({click:function(){this.create()}.bind(this),focus:function(){this.create()}.bind(this)})},create:function(e){if(this.calendar){return false}e=($type(e)=="boolean")?e:true;if(Browser.Engine.trident&&Browser.Engine.version<=5){e=false}if(Browser.Engine.trident&&Browser.Engine.version<=4){$$("select").addClass("dp_hide")}this.container=new Element("div",{"class":"dp_container"}).injectBefore(this.dp);if(e){this.container.fade("hide")}this.calendar=new Element("div",{"class":"dp_cal"}).injectInside(this.container);var b=new Date();if(this.month&&this.year){b.setFullYear(this.year,this.month,1)}else{this.month=b.getMonth();this.year=b.getFullYear();b.setDate(1)}this.year%4==0?this.options.daysInMonth[1]=29:this.options.daysInMonth[1]=28;var a=(1-(7+b.getDay()-this.options.startDay)%7);monthSel=new Element("select",{id:this.dp.get("id")+"_monthSelect"});this.options.monthNames.each(function(h,g){monthSel.options[g]=new Element("option",{value:g});monthSel.options[g].set("text",h);if(this.month==g){monthSel.options[g].set("selected","selected")}}.bind(this));yearSel=new Element("select",{id:this.dp.get("id")+"_yearSelect"});var d=new Array();if(this.options.yearOrder=="desc"){for(var f=this.options.yearStart;f>(this.options.yearStart-this.options.yearRange-1);f--){d.include(f)}}else{for(var f=this.options.yearStart;f<(this.options.yearStart+this.options.yearRange+1);f++){d.include(f)}}d.each(function(h,g){yearSel.options[g]=new Element("option",{value:h});yearSel.options[g].set("text",h);if(this.year==h){yearSel.options[g].set("selected","selected")}}.bind(this));calTable=new Element("table");calTableThead=new Element("thead");calSelRow=new Element("tr");calSelCell=new Element("th",{colspan:"7"});monthSel.injectInside(calSelCell);yearSel.injectInside(calSelCell);calSelCell.injectInside(calSelRow);calSelRow.injectInside(calTableThead);calTableTbody=new Element("tbody");calDayNameRow=new Element("tr");for(var c=0;c<this.options.dayNames.length;c++){calDayNameCell=new Element("th");calDayNameCell.appendText(this.options.dayNames[(this.options.startDay+c)%7].substr(0,this.options.dayChars));calDayNameCell.injectInside(calDayNameRow)}calDayNameRow.injectInside(calTableTbody);while(a<=this.options.daysInMonth[this.month]){calDayRow=new Element("tr");for(c=0;c<7;c++){if((a<=this.options.daysInMonth[this.month])&&(a>0)){calDayCell=new Element("td",{"class":this.dp.get("id")+"_calDay",axis:this.year+"|"+(parseInt(this.month)+1)+"|"+a}).appendText(a).injectInside(calDayRow)}else{calDayCell=new Element("td",{"class":"dp_empty"}).appendText(" ").injectInside(calDayRow)}if((a==this.oldDay)&&(this.month==this.oldMonth)&&(this.year==this.oldYear)){calDayCell.addClass("dp_selected")}if((a==this.nowDay)&&(this.month==this.nowMonth)&&(this.year==this.nowYear)){calDayCell.addClass("dp_today")}a++}calDayRow.injectInside(calTableTbody)}calTableThead.injectInside(calTable);calTableTbody.injectInside(calTable);calTable.injectInside(this.calendar);if(e){this.container.fade("in")}$$("td."+this.dp.get("id")+"_calDay").each(function(g){g.addEvents({mouseover:function(){g.addClass("dp_roll")},mouseout:function(){g.removeClass("dp_roll")},click:function(){ds=g.axis.split("|");this.dp.value=this.formatValue(ds[0],ds[1],ds[2]);this.remove()}.bind(this)})}.bind(this));[monthSel,yearSel].each(function(g){g.addEvents({focus:function(){this.active=true}.bind(this),change:function(){this.month=monthSel.value;this.year=yearSel.value;this.remove(false);this.create(false);this.active=false;this.dp.focus()}.bind(this)})}.bind(this));this.dp.addEvent("blur",function(){(function(){if(!this.active){this.remove()}}.bind(this)).delay(500)}.bind(this))},formatValue:function(c,d,b){var a="";if(b<10){b="0"+b}if(d<10){d="0"+d}a=this.options.format.replace(/dd/i,b).replace(/mm/i,d).replace(/yyyy/i,c);this.month=this.oldMonth=""+(d-1)+"";this.year=this.oldYear=c;this.oldDay=b;return a},remove:function(a){a=($type(a)=="boolean")?a:true;if(Browser.Engine.trident&&Browser.Engine.version<=5){a=false}if($type(this.container)=="element"){if(a!=false){this.container.fade("out")}else{this.container.dispose()}}this.active=this.container=this.calendar=false;$$("select.dp_hide").removeClass("dp_hide")}});Element.implement({DatePicker:function(a){new DatePicker(this,a);return this}});window.addEvent("domready",function(){$$("input.DatePicker").DatePicker()});
