/**
* Copyright (c) 2008 Kelvin Luck (http://www.kelvinluck.com/)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*
* Heavily modified by mschmidt @ Cuban Council.
* .
* $Id: jquery.datePicker.js 102 2010-09-13 14:00:54Z kelvin.luck $
**/

(function($){

	$.fn.extend({

		renderCalendar : function(s) {
		    		
			var dc = function(a) {
				return document.createElement(a);
			};
		
			s = $.extend({}, s);
			var year = s.year || today.getFullYear();
			
			// loop through months
			x = 0;
			for (x = 0; x < 12; x++) {
			
				// get any category parameter
				curCat = 'cat_id';
				curCat = curCat.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");            
				var regexS = "[\\?&]"+curCat+"=([^&#]*)";
				var regex = new RegExp( regexS );
				var results = regex.exec( window.location.href );
				
				if (results) {
					curCat = 'cat_id='+results[1]+'&';
				} else {
					curCat = '';
				}
		
			    var calendarTable = $(dc('a')).addClass('cal-box-in');
			    var selMonth = s.month == undefined ? 1 : s.month;
			    if ((selMonth-1) == x) {
			        // selected month
			        calendarTable.addClass('cal-selmonth');
			    }
		
    			var today = (new Date()).zeroTime();
    			today.setHours(12);
		
    			var month = x;
    			calendarTable.append('<h4>' + Date.abbrMonthNames[month] + '</h4>');
    			calendarTable.attr('href',pageUrl + '?'+curCat+'m=' + (1 + month) +'&y=' + year);
			
    			var currentDate = (new Date(year, month, 1, 12, 0, 0));
			
    			// check to see if this is an inactive month
    			if (currentDate.getTime() > today.getTime()) {
    			    calendarTable.addClass('cal-inactive');
    			}
		
    			var firstDayOffset = Date.firstDayOfWeek - currentDate.getDay() + 1;
    			if (firstDayOffset > 1) firstDayOffset -= 7;
    			var weeksToDraw = Math.ceil(( (-1*firstDayOffset+1) + currentDate.getDaysInMonth() ) /7);
    			currentDate.addDays(firstDayOffset-1);

    			var w = 0;
    			while (w++<weeksToDraw) {
    				var r = jQuery(dc('div'));
    				var firstDayInBounds = s.dpController ? currentDate > s.dpController.startDate : false;
    				for (var i=0; i<7; i++) {
    					var thisMonth = currentDate.getMonth() == month;
    					var d = $(dc('span'))
    								.text('')
    								.addClass((thisMonth ? 'current-month ' : 'other-month ') +
    													(currentDate.isWeekend() ? 'weekend ' : 'weekday ') +
    													(thisMonth && currentDate.getTime() == today.getTime() ? 'today ' : '')
    								)
    								.data('datePickerDate', currentDate.asString())
    							;
    					r.append(d);
    					
    					// add popularity markers
    					var dayPopFound = dayPops[currentDate.asString()];    
                        if (dayPopFound) {
                    		d.addClass('pop-' + dayPopFound);
                    	}

    					// set the time to midday to avoid any weird timezone issues??
    					currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate()+1, 12, 0, 0);
    				}
    				calendarTable.append(r);
    			}
		
    			$(this).append(calendarTable);
    		}
		
		},
		
		testCallback : function($span, thisDate, month, year) {
		    var dayPopFound = dayPops[thisDate.asString()];    
            if (dayPopFound) {
        		$span.addClass('pop-' + dayPopFound);
        	}
		}
	
	});

})(jQuery);
