");
for (var i=0; i<7; i++) {
var weekday = (_firstDayOfWeek + i) % 7;
var atts = {'class':(weekday == 0 || weekday == 6 ? 'weekend ' : 'weekday ')};
if (curDay < 0 || curDay >= lastDay) {
dayStr = ' ';
} else if (firstMonth && curDay < firstDate-1) {
dayStr = curDay+1;
atts['class'] += 'inactive';
} else if (finalMonth && curDay > lastDate-1) {
dayStr = curDay+1;
atts['class'] += 'inactive';
} else {
d.setDate(curDay+1);
var dStr = _dateToStr(d);
dayStr = jQuery("").attr({'href':'javascript:;', 'rel':dStr}).html(curDay+1).click(function(e)
{
jQuery.datePicker.selectDate(jQuery.attr(this, 'rel'), this);
return false;
})[0];
if (_selectedDate && _selectedDate==dStr) {
jQuery(dayStr).attr('class','selected');
}
}
if (thisMonth && curDay+1 == todayDate) {
atts['class'] += 'today';
}
thisRow.append(jQuery("").attr(atts).append(dayStr));
curDay++;
}
tBody.append(thisRow);
}
jCalDiv.append(
jQuery("").attr('cellspacing',2).append("")
.find("thead").append(headRow).parent().append(tBody.children())
).append(navLinksDiv);
if (jQuery.browser.msie) {
// we put a styled iframe behind the calendar so HTML SELECT elements don't show through
var iframe = [ ''].join('');
jCalDiv.append(document.createElement(iframe));
}
jCalDiv.css({'display':'block'});
return jCalDiv[0];
};
var _draw = function(c)
{
// explicitly empty the calendar before removing it to reduce the (MASSIVE!) memory leak in IE
// still not perfect but a lot better!
// Strangely if you chain the methods it reacts differently - when chained opening the calendar on
// IE uses a bunch of memory and pressing next/prev doubles this memory. When you close the calendar
// the memory is freed. If they aren't chained then pressing next or previous doesn't double the used
// memory so only one chunk of memory is used when you open the calendar (which is also freed when you
// close the calendar).
jQuery('div.popup-calendar a', _openCal[0]).unbind();
jQuery('div.popup-calendar', _openCal[0]).empty();
jQuery('div.popup-calendar', _openCal[0]).remove();
jQuery('div.popup-calendar', _openCal[0]).css({'display':'none'});
_openCal.append(c);
};
var _drawMonth = function(c)
{
jQuery('div.popup-calendar a', _openCal).unbind();
jQuery('div.popup-calendar', _openCal).empty();
_openCal.append(c);
}
var _closeDatePicker = function()
{
jQuery('div.popup-calendar a', _openCal).unbind();
jQuery('div.popup-calendar', _openCal).empty();
jQuery('div.popup-calendar', _openCal).css({'display':'none'});
jQuery(document).unbind('mousedown', _checkMouse);
delete _openCal;
_openCal = null;
};
var _handleKeys = function(e)
{
var key = e.keyCode ? e.keyCode : (e.which ? e.which: 0);
if (key == 27) {
_closeDatePicker();
}
return false;
};
var _checkMouse = function(e)
{
var target = jQuery.browser.msie ? window.event.srcElement : e.target;
var cp = jQuery(target).findClosestParent('div.popup-calendar');
if (cp.get(0).className != 'date-picker-holder') {
_closeDatePicker();
}
};
return {
getChooseDateStr: function()
{
return navLinks.b;
},
show: function()
{
if (_openCal) {
_closeDatePicker();
}
this.blur();
var input = jQuery('input', jQuery(this).findClosestParent('input')[0])[0];
_firstDate = input._startDate;
_lastDate = input._endDate;
_firstDayOfWeek = input._firstDayOfWeek;
_openCal = jQuery(this).findClosestParent('div.popup-calendar');
var d = jQuery(input).val();
if (d != '') {
if (_dateToStr(_strToDate(d)) == d) {
_selectedDate = d;
_draw(_getCalendarDiv(_strToDate(d)));
} else {
// invalid date in the input field - just default to this month
_selectedDate = false;
_draw(_getCalendarDiv());
}
} else {
_selectedDate = false;
_draw(_getCalendarDiv());
}
/*
if (jQuery.browser == "msie") {
_openCal.bind('keypress', _handleKeys);
} else {
jQuery(window).bind('keypress', _handleKeys);
}
*/
jQuery(document).bind('mousedown', _checkMouse);
},
changeYear: function(d, e)
{
_draw(_getMonthCalendarDiv(d));
},
changeMonth: function(d, e)
{
_draw(_getCalendarDiv(d));
},
selectDate: function(d, ele)
{
selectedDate = d;
var $theInput = jQuery('input', jQuery(ele).findClosestParent('input')[0]);
$theInput.val(d);
$theInput.trigger('change');
_closeDatePicker(ele);
},
changeToMonthView: function(i)
{
_drawMonth(i);
},
closeCalendar: function()
{
_closeDatePicker(this);
},
setInited: function(i)
{
i._inited = true;
},
isInited: function(i)
{
return i._inited != undefined;
},
setDateFormat: function(format,separator)
{
// set's the format that selected dates are returned in.
// options are 'dmy' (european), 'mdy' (americian) and 'ymd' (unicode)
dateFormat = format.toLowerCase();
dateSeparator = separator?separator:"/";
},
/**
* Function: setLanguageStrings
*
* Allows you to localise the calendar by passing in relevant text for the english strings in the plugin.
*
* Arguments:
* days - Array, e.g. ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
* months - Array, e.g. ['January', 'Febuary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
* navLinks - Object, e.g. {p:'Prev', n:'Next', c:'Close', b:'Choose date'}
**/
setLanguageStrings: function(aDays, aMonths, aNavLinks)
{
days = aDays;
months = aMonths;
navLinks = aNavLinks;
},
/**
* Function: setDateWindow
*
* Used internally to set the start and end dates for a given date select
*
* Arguments:
* i - The id of the INPUT element this date window is for
* w - The date window - an object containing startDate and endDate properties
* each in the current format as set in a call to setDateFormat (or in the
* default format dmy if setDateFormat hasn't been called).
* e.g. {startDate:'01/03/2006', endDate:'11/04/2006}
**/
setDateWindow: function(i, w)
{
if (w == undefined) w = {};
if (w.startDate == undefined) {
i._startDate = new Date();
} else {
i._startDate = _strToDate(w.startDate);
}
if (w.endDate == undefined) {
i._endDate = new Date();
i._endDate.setFullYear(i._endDate.getFullYear()+5);
} else {
i._endDate = _strToDate(w.endDate);
};
i._firstDayOfWeek = w.firstDayOfWeek == undefined ? 0 : w.firstDayOfWeek;
}
};
}();
jQuery.fn.findClosestParent = function(s)
{
var ele = this;
while (true) {
if (jQuery(s, ele[0]).length > 0) {
return (ele);
}
ele = ele.parent();
if(ele[0].length == 0) {
return false;
}
}
};
jQuery.fn.datePicker = function(a)
{
this.each(function() {
if(this.nodeName.toLowerCase() != 'input') return;
jQuery.datePicker.setDateWindow(this, a);
if (!jQuery.datePicker.isInited(this)) {
var chooseDate = jQuery.datePicker.getChooseDateStr();
var calBut;
if(a && a.inputClick){
calBut = jQuery(this).attr({'class':'date-picker', 'title':chooseDate});
}
else {
calBut = jQuery("").attr({'href':'javascript:;','class':'date-picker', 'title':chooseDate}).append("" + chooseDate + "");
}
jQuery(this).wrap(
''
).before(
jQuery("").attr({'class':'popup-calendar'})
).after(
calBut
);
calBut.bind('click', jQuery.datePicker.show);
jQuery.datePicker.setInited(this);
}
});
return this;
}; |