(function($) {
$.fn.jCal = function (opt) {
$.jCal(this, opt);
}
$.jCal = function (target, opt) {
opt = $.extend({
day: new Date(), // date to drive first cal
days: 1, // default number of days user can select
showMonths: 1, // how many side-by-side months to show
dCheck: function (day) { return true; }, // handler for checking if single date is valid or not
callback: function (day, days) { return true; }, // callback function for click on date
selectedBG: 'rgb(0, 143, 214)', // default bgcolor for selected date cell
defaultBG: 'rgb(255, 255, 255)', // default bgcolor for unselected date cell
dayOffset: 0, // 0=week start with sunday, 1=week starts with monday
forceWeek: false, // true=force selection at start of week, false=select days out from selected day
dow: ['S', 'M', 'T', 'W', 'T', 'F', 'S'], // days of week - change this to reflect your dayOffset
ml: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
ms: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
_target: target // target DOM element - no need to set extend this variable
}, opt);
opt.day = new Date(opt.day.getFullYear(), opt.day.getMonth(), opt.day.getDate());
$(target).stop().empty();
for (var sm=0; sm < opt.showMonths; sm++)
$(target).append('
');
opt.cID = 'c' + $('.jCalMo').length;
$('.jCalMo', target).each(
function (ind) {
drawCalControl($(this), $.extend( {}, opt, { 'ind':ind,
'day':new Date( new Date( opt.day.getTime() ).setMonth( new Date( opt.day.getTime() ).getMonth() + ind ) ) }
));
drawCal($(this), $.extend( {}, opt, { 'ind':ind,
'day':new Date( new Date( opt.day.getTime() ).setMonth( new Date( opt.day.getTime() ).getMonth() + ind ) ) }
));
});
$('.jCal', target).each(
function () {
$('.month', this).css('width', $(this).parent().width() - ( $('.left', this).width() || 0 ) - ( $('.right', this).width() || 0 ) + 'px')
});
}
function drawCalControl (target, opt) {
$(target).append(
'' +
( (opt.ind == 0) ? '

' : '' ) +
'
' + opt.ml[opt.day.getMonth()] + ' ' + opt.day.getFullYear() + '' +
( (opt.ind == ( opt.showMonths - 1 )) ? '

' : '' ) +
'
');
$(target).find('.jCal .left').bind('click',
function (e) {
document.getElementById('calTwoResult').innerHTML = '';
if ($('.jCalMask', opt._target).length > 0) return false;
var mD = { w:0, h:0 };
$('.jCalMo', opt._target).each( function () {
mD.w += $(this).width() + parseInt($(this).css('padding-left')) + parseInt($(this).css('padding-right'));
var cH = $(this).height() + parseInt($(this).css('padding-top')) + parseInt($(this).css('padding-bottom'));
mD.h = ((cH > mD.h) ? cH : mD.h);
} );
$(opt._target).prepend('');
opt.day = new Date( $('div[id*=' + opt.cID + 'd_]:first', opt._target).attr('id').replace(opt.cID + 'd_', '').replace(/_/g, '/') );
opt.day.setDate(1);
opt.day.setMonth( opt.day.getMonth() - 1 );
drawCalControl($('.jCalMo:first', opt._target), opt);
drawCal($('.jCalMo:first', opt._target), opt);
if (opt.showMonths > 1) {
$('.right', opt._target).clone(true).appendTo( $('.jCalMo:eq(1) .jCal', opt._target) );
$('.left:last, .right:last', opt._target).remove();
}
$(opt._target).append('');
$('.jCalMo', opt._target).wrapAll(
'');
$('.jCalMove', opt._target).css('margin-left', ( ( mD.w / opt.showMonths ) * -1 ) + 'px').css('opacity', 0.5).animate({ marginLeft:'0px' }, 'veryfast',
function () {
$(this).children('.jCalMo:not(:last)').appendTo( $(opt._target) );
$('.jCalSpace, .jCalMask', opt._target).empty().remove();
if ( typeof $(opt._target).data('day') == 'object' )
reSelectDates(opt._target, $(opt._target).data('day'), $(opt._target).data('days'), opt);
});
});
$(target).find('.jCal .right').bind('click',
function (e) {
document.getElementById('calTwoResult').innerHTML = '';
if ($('.jCalMask', opt._target).length > 0) return false;
var mD = { w:0, h:0 };
$('.jCalMo', opt._target).each( function () {
mD.w += $(this).width() + parseInt($(this).css('padding-left')) + parseInt($(this).css('padding-right'));
var cH = $(this).height() + parseInt($(this).css('padding-top')) + parseInt($(this).css('padding-bottom'));
mD.h = ((cH > mD.h) ? cH : mD.h);
} );
$(opt._target).append('');
opt.day = new Date( $('div[id^=' + opt.cID + 'd_]:last', opt._target).attr('id').replace(opt.cID + 'd_', '').replace(/_/g, '/') );
opt.day.setDate(1);
opt.day.setMonth( opt.day.getMonth() + 1 );
drawCalControl($('.jCalMo:last', opt._target), opt);
drawCal($('.jCalMo:last', opt._target), opt);
if (opt.showMonths > 1) {
$('.left', opt._target).clone(true).prependTo( $('.jCalMo:eq(1) .jCal', opt._target) );
$('.left:first, .right:first', opt._target).remove();
}
$(opt._target).append('');
$('.jCalMo', opt._target).wrapAll(
'');
$('.jCalMove', opt._target).css('opacity', 0.5).animate({ marginLeft:( ( mD.w / opt.showMonths ) * -1 ) + 'px' }, 'veryfast',
function () {
$(this).children('.jCalMo:not(:first)').appendTo( $(opt._target) );
$('.jCalSpace, .jCalMask', opt._target).empty().remove();
if ( typeof $(opt._target).data('day') == 'object' )
reSelectDates(opt._target, $(opt._target).data('day'), $(opt._target).data('days'), opt);
});
});
}
function reSelectDates (target, day, days, opt) {
var fDay = new Date(day.getTime());
var sDay = new Date(day.getTime());
for (var fC = false, di = 0, dC = days; di < dC; di++) {
var dF = $(target).find('div[id*=d_' + (sDay.getMonth() + 1) + '_' + sDay.getDate() + '_' + sDay.getFullYear() + ']');
if ( dF.length > 0 ) {
dF.stop().addClass('selectedDay');
fC = true;
}
sDay.setDate( sDay.getDate() + 1 );
}
if ( fC && typeof opt.callback == 'function' ) opt.callback( day, days );
}
function drawCal (target, opt) {
var events = new Array();
events[1] = new Object(); events[1]['title'] = 'ABRaS Mina 10k Series - Race 1';events[1]['date'] = '2008-10-17';events[1]['url'] = 'http://www.premiermarathons.comevents/abras_mina_10k_series__race_1_157.html';events[2] = new Object(); events[2]['title'] = 'ABRaS Mina 10k Series - Race 2';events[2]['date'] = '2009-3-27';events[2]['url'] = 'http://www.premiermarathons.comevents/abras_mina_10k_series__race_2_171.html';events[3] = new Object(); events[3]['title'] = 'ABRaS Mina 10k Series - Race 3';events[3]['date'] = '2009-5-15';events[3]['url'] = 'http://www.premiermarathons.comevents/abras_mina_10k_series__race_3_172.html';events[4] = new Object(); events[4]['title'] = 'Airtel Delhi Half Marathon';events[4]['date'] = '2008-11-9';events[4]['url'] = 'http://www.premiermarathons.comevents/airtel_delhi_half_marathon_165.html';events[5] = new Object(); events[5]['title'] = 'BLOM Beirut Marathon 08';events[5]['date'] = '2008-11-30';events[5]['url'] = 'http://www.premiermarathons.comevents/blom_beirut_marathon_08_164.html';events[6] = new Object(); events[6]['title'] = 'Christmas Pursuit Race';events[6]['date'] = '2008-12-13';events[6]['url'] = 'http://www.premiermarathons.comevents/christmas_pursuit_race_168.html';events[7] = new Object(); events[7]['title'] = 'Dubai Road Runners 10km Race';events[7]['date'] = '2009-1-3';events[7]['url'] = 'http://www.premiermarathons.comevents/dubai_road_runners_10km_race_169.html';events[8] = new Object(); events[8]['title'] = 'Dubai Tri Club Aquathlon';events[8]['date'] = '2008-9-27';events[8]['url'] = 'http://www.premiermarathons.comevents/dubai_tri_club_aquathlon_144.html';events[9] = new Object(); events[9]['title'] = 'Dubai Tri Club Half Ironman Distance Race';events[9]['date'] = '2009-1-30';events[9]['url'] = 'http://www.premiermarathons.comevents/dubai_tri_club_half_ironman_distance_race_170.html';events[10] = new Object(); events[10]['title'] = 'Dubai Tri Club Olympic Triathlon';events[10]['date'] = '2009-3-6';events[10]['url'] = 'http://www.premiermarathons.comevents/dubai_tri_club_olympic_triathlon_173.html';events[11] = new Object(); events[11]['title'] = 'Dubai Tri Club Triathlon';events[11]['date'] = '2008-11-28';events[11]['url'] = 'http://www.premiermarathons.comevents/dubai_tri_club_triathlon_163.html';events[12] = new Object(); events[12]['title'] = 'Johnson Arabia Dubai Creek Striders Half Marathon';events[12]['date'] = '2008-12-5';events[12]['url'] = 'http://www.premiermarathons.comevents/johnson_arabia_dubai_creek_striders_half_marathon_146.html';events[13] = new Object(); events[13]['title'] = 'RAK Half Marathon';events[13]['date'] = '2009-2-20';events[13]['url'] = 'http://www.premiermarathons.comevents/rak_half_marathon_143.html';events[14] = new Object(); events[14]['title'] = 'RAK Half Marathon';events[14]['date'] = '2010-2-12';events[14]['url'] = 'http://www.premiermarathons.comevents/rak_half_marathon_178.html';events[15] = new Object(); events[15]['title'] = 'Standard Chartered DUBAI Marathon';events[15]['date'] = '1970-1-1';events[15]['url'] = 'http://www.premiermarathons.comevents/standard_chartered_dubai_marathon_167.html';events[16] = new Object(); events[16]['title'] = 'Standard Chartered MUMBAI Marathon';events[16]['date'] = '2009-1-18';events[16]['url'] = 'http://www.premiermarathons.comevents/standard_chartered_mumbai_marathon_166.html';
for (var ds in opt.dow)
$(target).append('' + opt.dow[ds] + '
');
var fd = new Date( new Date( opt.day.getTime() ).setDate(1) );
var ldlm = new Date( new Date( fd.getTime() ).setDate(0) );
var ld = new Date( new Date( new Date( fd.getTime() ).setMonth( fd.getMonth() + 1 ) ).setDate(0) );
var copt = {fd:fd.getDay(), lld:ldlm.getDate(), ld:ld.getDate()};
var offsetDayStart = ( ( copt.fd < opt.dayOffset ) ? ( opt.dayOffset - 7 ) : 1 );
var offsetDayEnd = ( ( ld.getDay() < opt.dayOffset ) ? ( 7 - ld.getDay() ) : ld.getDay() );
var i = 1;
var css_class = 'day';
var current_day = 0;
for ( var d = offsetDayStart, dE = ( copt.fd + copt.ld + ( 7 - offsetDayEnd ) ); d < dE; d++) {
if(d <= ( copt.fd - opt.dayOffset)) {
current_day = (copt.lld - ( ( copt.fd - opt.dayOffset ) - d )); current_month = fd.getMonth();}
else if(d > ( ( copt.fd - opt.dayOffset ) + copt.ld)) {
current_day = (d - ( ( copt.fd - opt.dayOffset ) + copt.ld)); current_month = fd.getMonth() + 2;}
else
{
current_day = ( d - ( copt.fd - opt.dayOffset)); current_month = fd.getMonth() + 1;}
current_year = fd.getFullYear();
current_date = current_year + '-' + current_month + '-' + current_day;
for (var j in events) {
if(typeof(events[j]) != 'undefined') {
if(current_date == events[j]['date']) {
css_class = 'dayevent'; break;
} else { css_class = 'day'; }
}
}
$(target).append(
(( d <= ( copt.fd - opt.dayOffset ) ) ?
'' + ( copt.lld - ( ( copt.fd - opt.dayOffset ) - d ) ) + '
'
: ( ( d > ( ( copt.fd - opt.dayOffset ) + copt.ld ) ) ?
'' + ( d - ( ( copt.fd - opt.dayOffset ) + copt.ld ) ) + '
'
: '' + ( d - ( copt.fd - opt.dayOffset ) ) + '
'
)
)
);
i++;
}
$(target).find('div[id^=' + opt.cID + 'd]:first, div[id^=' + opt.cID + 'd]:nth-child(7n+2)').before( '
' );
$(target).find('div[id^=' + opt.cID + 'd_]:not(.invday)').bind("mouseover mouseout click", function(e){
if ($('.jCalMask', opt._target).length > 0) return false;
var osDate = new Date ( $(this).attr('id').replace(/c[0-9]{1,}d_([0-9]{1,2})_([0-9]{1,2})_([0-9]{4})/, '$1/$2/$3') );
if (opt.forceWeek) osDate.setDate( osDate.getDate() + (opt.dayOffset - osDate.getDay()) );
var sDate = new Date ( osDate.getTime() );
if (e.type == 'click')
$('div[id*=d_]', opt._target).stop().removeClass('selectedDay').removeClass('overDay').css('backgroundColor', '');
for (var di = 0, ds = opt.days; di < ds; di++) {
var currDay = $(opt._target).find('#' + opt.cID + 'd_' + ( sDate.getMonth() + 1 ) + '_' + sDate.getDate() + '_' + sDate.getFullYear());
if ( currDay.length == 0 || $(currDay).hasClass('invday') ) break;
if ( e.type == 'mouseover' ) $(currDay).addClass('overDay');
else if ( e.type == 'mouseout' ) $(currDay).stop().removeClass('overDay').css('backgroundColor', '');
else if ( e.type == 'click' ) $(currDay).stop().addClass('selectedDay');
sDate.setDate( sDate.getDate() + 1 );
}
if (e.type == 'mouseover') {
opt.callback( osDate, di );
}
if (e.type == 'click') {
opt.day = osDate;
opt.callback( osDate, di );
$(opt._target).data('day', opt.day).data('days', di);
}
});
}
})(jQuery);