$(document).ready(function() {
	today = new Date();
	var output_html = Calendar.create(today.getMonth() + 1, today.getFullYear());
	if( $('#home_calendar_left').size() >= 1 ) {
		document.getElementById('home_calendar_left').innerHTML = output_html;
	}
	if( $('#calendar_holder').size() >= 1 ) {
		document.getElementById('calendar_holder').innerHTML = output_html;
	}

});


var Calendar = {
    months_long: 'January February March April May June July August September October November December'.split(' '),
	months_short: 'jan feb mar apr may jun jul aug sep oct nov dec'.split(' '),
    days_of_the_week: 'S M T W T F S'.split(' '),

    is_leap_year: function(year) {
        return (((year % 4)==0) && ((year % 100)!=0) || ((year % 400)==0));
    },

    get_number_of_days_month: function(month, year) {
        if(month==0 || month==2 || month==4 || month==6 || month==7 || month==9 || month==11)
            return 31;
        else if(month==3 || month==5 || month==8 || month==10)
            return 30;
        else if(month==1 && this.is_leap_year(year))
            return 29;
        else
            return 28;
    },

    create: function(month, year) {
        month = parseInt(month) - 1;
        year  = parseInt(year);

		var day_link;
		var output = '';
		var starting_position = new Date(year, month, 1).getDay();
		var ending_position;
        var days_in_month = Calendar.get_number_of_days_month(month, year);
		var next_month = month + 1;
		var next_year = year;
		var prev_month = month;
		var prev_year = year;
		
		
		if( month == 11 ) {
			next_month = 1;
			next_year = year + 1;
		}
		else if( month == 0 ) {
			prev_month = 12;
			prev_year = year - 1;
		}

		output += '<div id="calendar_paginate"><a href="/events/search/?start_date='+prev_year+'-'+prev_month+'-01" id="cal_left_arrow"><img src="http://media.sdreader.com/img/home_cal_left.gif" alt="Prev Mo"></a>'+year+' '+Calendar.months_long[month]+'<a href="/events/search/?start_date='+next_year+'-'+(next_month+1)+'-01" id="cal_right_arrow"><img src="http://media.sdreader.com/img/home_cal_right.gif" alt="Next Mo"></a></div>'
        output += '<div id="calendar_wrap"><table id="small_calendar" border="0" cellpadding="0" cellspacing="1"><tbody><tr>';

        for (var i = 0; i < starting_position; i++) {
            output += '<td class="empty_cell">&nbsp;</td>';
        }

        var current_day = 1;
        for (var i = starting_position; current_day <= days_in_month; i++) {
            if(i%7 == 0 && current_day != 1) {
                output += '</tr><tr>';
            }
            output += '<td';
			var myDate=new Date()
			myDate.setFullYear(year, month, current_day)
			if( myDate == Date() ) {
				output   += ' id="selected" style="background:#ece5d2; color:#c50000;" ';
				day_link  = '/events/';
			}
			else {
				day_link = '/events/search/?category=&q=&age=&cost=&start_date='+year+'-'+(month+1)+'-'+current_day+'&end_date='+year+'-'+(month+1)+'-'+current_day+'&ongoing=on&search=';
			}
			output += '><a ';
			if( myDate == Date() ) {
				output += ' id="current_day" style="background:#ece5d2; color:#c50000;" ';
			}
			output += ' href="'+day_link+'">'+current_day+'</a></td>';
            current_day++;
			ending_position = i + 1;
        }

		while((ending_position % 7) != 0) {
			output += '<td class="empty_cell">&nbsp;</td>'
			ending_position++;
		}

		output += '</tr></tbody></table></div>';
		

		output += '<p><a href="/events/search/?start_date='+next_year+'-'+(next_month+1)+'-01" class="link_with_arrow">NEXT MONTH</a></p></div>';
		return output;
    }
}