var newsCalendar = {
	objectName: '',
	viewYear: 0,
	viewMonth: 0,
	firstDay: 1,
	dayNames: ['Nd','Pn','Wt','Śr','Cz','Pt','Sb'],
	monthNames: ['styczeń','luty','marzec','kwiecień','maj','czerwiec','lipiec','sierpień','wrzesień','październik','listopad','grudzień'],
	ajaxRoot: '',
	redirectUrl: '',
	eventDays: Array(),

	init: function(year, month, objectName, redirectUrl, ajaxRoot, lang) {
		this.objectName = objectName;
		this.viewYear = year;
		this.viewMonth = month;
		this.redirectUrl = redirectUrl;
		this.ajaxRoot = ajaxRoot;
		switch(lang){
		case 'pl':
			this.dayNames = ['ND','PN','WT','ŚR','CZ','PT','SO'];
			this.monthNames = ['Styczeń','Luty','Marzec','Kwiecień','Maj','Czerwiec','Lipiec','Sierpień','Wrzesień','Październik','Listopad','Grudzień'];
			break;
		case 'en':
			this.dayNames = ['Nd','Pn','Wt','Śr','Cz','Pt','Sb'];
		this.monthNames = ['January','February','March','April','May','June','July','August','September','October','November','December'];
			break;
		case 'de':
			this.dayNames = ['Nd','Pn','Wt','Śr','Cz','Pt','Sb'];
		this.monthNames = ['Januar','Februar','März','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember'];
			break;
		case 'ru':
			this.dayNames = ['Nd','Pn','Wt','Śr','Cz','Pt','Sb'];
		this.monthNames = ['Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'];
			break;
		case 'fr':
			this.dayNames = ['Nd','Pn','Wt','Śr','Cz','Pt','Sb'];
		this.monthNames = ['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre'];
			break;
		default :
			this.dayNames = ['Nd','Pn','Wt','Śr','Cz','Pt','Sb'];
		this.monthNames = ['styczeń','luty','marzec','kwiecień','maj','czerwiec','lipiec','sierpień','wrzesień','październik','listopad','grudzień'];
			break;
		
		}
		//$('#groupCount').keyup(function() {eval(objectName + '.build()');});
	},

	reBuild: function(year, month) {
		this.viewYear = year;
		this.viewMonth = month;
		this.build();
	},

	build: function() {
		this.getEventDays();

		html = '<table class="newsCalendar" cellspacing="0">';
		prevMonth = ((this.viewMonth - 1) >= 0) ? (this.viewMonth - 1) : 11;
		prevYear = (prevMonth == 11) ? this.viewYear - 1 : this.viewYear;

		nextMonth = ((this.viewMonth + 1) <= 11) ? (this.viewMonth + 1) : 0;
		nextYear = (nextMonth == 0) ? this.viewYear + 1 : this.viewYear;

		html += '<thead><tr class="yearRow">' +
			'<td class="pointer" colspan="7">' + 
				'<div class="arrow" style="margin-left:26px;"><span onclick="' + this.objectName +'.reBuild(' + prevYear + ',' + prevMonth + ' )">&lt;</span></div>' +
				'<div class="monthBlock">'+this.monthNames[this.viewMonth] +' '+ this.viewYear +'</div><div class="arrow"><span onclick="' + this.objectName +'.reBuild(' + nextYear + ',' + nextMonth + ' )">&gt;</span></div>' +			 
//				'<div class="arrow"><img onclick="'+ this.objectName + '.reBuild(' + (this.viewYear - 1) + ',' + this.viewMonth + ' )" src="/img/arrow-red-left.gif" alt="arrow-left" border="0"/></div>' +
//				'<div class="yearBlock">' + this.viewYear + '</div><div class="arrow"><img src="/img/arrow-red-right.gif" alt="arrow-right" border="0" onclick="' + this.objectName +'.reBuild(' + (this.viewYear + 1) + ',' + this.viewMonth + ' )"/></div>' +
			'</td></tr><tr class="titleRow">';
		for (var dow = 0; dow < 7; dow++) {
			html += '<td '+ ((dow + this.firstDay) % 7 == 0 ? ' class="weekEndCell"' : '') +
			'>' + this.dayNames[(dow + this.firstDay) % 7] + '</td>';
		}
		html += '</tr></thead><tbody>';
		var daysInMonth = this.getDaysInMonth(this.viewYear, this.viewMonth);
		var leadDays = (this.getFirstDayOfMonth(this.viewYear, this.viewMonth) - this.firstDay + 7) % 7;
		var printDate = new Date(this.viewYear, this.viewMonth, 1 - leadDays);
		var numRows = Math.ceil((leadDays + daysInMonth) / 7);
		var now = new Date();
		var today = new Date(now.getFullYear(),now.getMonth(),now.getDate());
		var nextMonth = 0;
		for (var row = 0; row < numRows; row++) {
			html += '<tr class="daysRow">';
			for (var dow = 0; dow < 7; dow++) {
				var otherMonth = (printDate.getMonth() != this.viewMonth);
				var dayEvent =  ((printDate.getMonth() == this.viewMonth) && (this.eventDays[printDate.getDate()]));
				var weekEnd = ((dow + this.firstDay + 6) % 7 >= 6);
				(printDate.getMonth()==this.viewMonth) && (printDate.getDate() == daysInMonth) ? nextMonth=1 : ''; 
				html += '<td class="daysCell' + 
					(weekEnd ? ' weekEndCell' : '') +
					(otherMonth ? ' otherMonth' : '') + 
					(dayEvent ? ' dayEvent' : '') +
					(printDate.getTime() == today.getTime() && (printDate.getMonth()==this.viewMonth) ? '" id="currentDay"' : '"') +
					(nextMonth==1 ? 'style="border:none;"' : '') +
					'>' + printDate.getDate() + '</td>';					
					printDate.setDate(printDate.getDate() + 1);
			}
			html += '</tr>';
		}		
		html += '</tbody></table>';
		$('#calendar_div_main').hide().empty().append(html).show();
//		var redirectUrl = this.redirectUrl + '/?date=' + this.viewYear + '-' + (this.viewMonth + 1) + '-'; 
//		$('td.dayEvent').click(function() {
//			eval(location.href = redirectUrl + $(this).html());
//		});
		var redirectUrl = this.redirectUrl;
		var ajaxUrl = this.ajaxRoot + '/o_muzeum/events/getevents/?date=' + this.viewYear + '-' + (this.viewMonth + 1) + '-';
		var show_event_list = this.ajaxRoot + '/o_muzeum/events/list/month/' + (this.viewMonth + 1) + '/year/' + this.viewYear + '/';
		var eventUrlLink = redirectUrl + '/?date=' + this.viewYear + '-' + (this.viewMonth + 1) + '-';
		$(".show-event-list").attr("href",show_event_list);
		$('td.dayEvent').click(function() {			
			$.getJSON(ajaxUrl + $(this).html(),
		        function(data){
					var htm = ' ';
					$.each(data, function(i,item){
						var eventUrl = redirectUrl + '/?date=' + item.event_date;
						htm += '<span>'+item.event_date_show+'</span> ' + item.event_name + ' <a href="'+ eventUrl +'">&gt;&nbsp;więcej</a><br /><br />';
					});					
					$("#event-list").html(htm);					
					$("#show-event-list-bottom").css("display","block");
					$("#event-list").slideDown();
					 $("#show-event-list-top").css("background-image","url(/img/arrow-red-up-pink.gif)");
					
		    });
			
			$(".show-event-list").attr("href",eventUrlLink+$(this).html());
								
		});
	},

	getEventDays: function() {
		var path = this.ajaxRoot + '/o_muzeum/events/geteventdays/';
		var eventDays = Array();

		var year = this.viewYear;
		var month = this.viewMonth + 1;
		$.ajax({
			type: 'GET',
			url: path,
			data: {year: year, month: month},
			dataType: 'json',
			async: false,
			success: function(data) {
				$.each(data, function(i,item) {
					eventDays[i] = item;
				});
			},
			error: function() {
			}
		});
		this.eventDays = eventDays;
	},

	getDaysInMonth: function(year, month) {
		return 32 - new Date(year, month, 32).getDate();
	},

	getFirstDayOfMonth: function(year, month) {
		return new Date(year, month, 1).getDay();
	}
}