(function($){
	
	$.fn.archive_calendar = function(opts) {
		
		var md = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 30, 31);
		var curdate = new Date();
		
		var options = {
			month: curdate.getMonth(),
			year: curdate.getYear(),
			base_uri: '/news/city/archive',
			cb_prev: function(pm, url) {
				//var pm = new Date(options.year, options.month - 1, 1); 
				return '<a href="#" onclick="javascript:news_archive_calendar(' + pm.getMonth() + ', ' + pm.getFullYear() + ', \'' + options.base_uri + '\');"><</a> ';
			}, 
			cb_next: function (nm, url) {
				//var nm = new Date(options.year, options.month + 1, 1);
				return '<a href="#" onclick="javascript:news_archive_calendar(' + nm.getMonth() + ', ' + nm.getFullYear() + ', \'' + options.base_uri + '\');">></a> ';
			}
		};
		
		if (opts) {
			$.extend(options, opts);
		}
					
		function isLeapYear(year) {						
		    if(year % 4 == 0) {
		        if(year % 100 == 0) {
		            if(year % 400 == 0) {
		                return true;
		            } else
		                return false;
		        } else
		            return true;
		    }
		    return false;
		}
		
		this.each(function(){
			var i, j, d, b = false;
			var shtml = '';
			var fd = (new Date(options.year, options.month, 1)).getDay();
			
			if (isLeapYear(options.year)) 
				md[1] = 29;
			if (fd == 0)
				fd = 6;
			
			var pm = new Date(options.year, options.month - 1, 1); 
			var nm = new Date(options.year, options.month + 1, 1);
			
			var p = options.cb_prev(pm, options.base_uri);		
			var n = options.cb_next(nm, options.base_uri);
			
			shtml += '<table class="calendar">';			
			shtml += '<thead><tr>';

			i = 0;
			j = Date.firstDayOfWeek;
			do {
				shtml += '<th' + ((j == 6 || j == 0) ? ' class="we"' : '') + '>';
				shtml += Date.abbrDayNames[j] + '</th>';
				j++;
				if (j >= 7) 
					j = 0;
				i++;
			} while (i < 7);
			shtml += '</tr></thead>';
			
			shtml += '<tbody>';
			d = 1;
			for(i = 0; i < 6; i++) {
				shtml += '<tr>';				
				for(j = 0; j < 7; j++) {
					if (i == 0) {						
						if (j < fd - 1) {
							shtml += '<td>&nbsp;</td>';							
							continue;
						}
					}
					shtml += '<td class="d' + d + ((j == 5 || j == 6) && (d <= md[options.month]) ? ' we' : '') + '">';
					shtml += (d <= md[options.month] ? d : '&nbsp;') + '</td>';					
					d++;
				}
				shtml += '</tr>';
			}
			
			shtml += '</tbody><tfoot><tr><td align="center" colspan="7">';
			
			//shtml += '<a href="#" onclick="javascript:news_archive_calendar(' + pm.getMonth() + ', ' + pm.getFullYear() + ', \'' + options.base_uri + '\');"><</a> ';
			shtml += p; 
			shtml += Date.monthNames[options.month] + ' ';
			//shtml += '<a href="#" onclick="javascript:news_archive_calendar(' + nm.getMonth() + ', ' + nm.getFullYear() + ', \'' + options.base_uri + '\');">></a> ';
			shtml += options.year + ' ';
			shtml += n;
			
			shtml += '</td></tr></tfoot></table>';
			
			//var c = $(shtml);
			$(this).html(shtml);
		});
		
		return this;
	};
	
	$.fn.month_releases_days = function(opts) {
		var curdate = new Date();
		
		var options = {
			month: curdate.getMonth(),
			year: curdate.getYear(),
			base_uri: '/news/city/archive',
			cbClickReleaseUrl: function(year, month, day) {
				return options.base_uri + '/' + (year + '-' + month + '-' + day);
			},
			mode: 'p'
		};
		
		if (opts) {
			$.extend(options, opts);
		}
		
		function leading_zero(s) {
			if (s.length == 1) {
				s = '0' + s;
			}
			return s;
		}
		
		this.each(function(){
			
			$.getJSON(options.base_uri + '/releases', {m: options.month, y: options.year, r: options.mode}, function(data){				
				$.each(data.days, function (i, day){			
					$('td.d' + day).addClass('release').click(function(){
						window.location = options.cbClickReleaseUrl(options.year, leading_zero((options.month + 1) + ''), leading_zero(day + ''));
					});
				});
			});
			
			return this;
		});
	};
	
})(jQuery);
