var EventCalendar = {

	VariableName: 'EventCalendar',
	value:new Date(),
	events:new Array(),
	infoBox:null,
	temp: '',
	serviceName: 'event/EventLogic',
	msg: {
		url: '/event/',
		title: 'Eseménynaptár',
		days:['H','K','SZ','CS','P','SZ','V'],
		months:['január','február','március','április','május','június','július','augusztus','szeptember','október','november','december'],
		prev_year:'&nbsp;',
		next_year:'&nbsp;',
		prev_month:'&nbsp;',
		next_month:'&nbsp;'
	},
	data:{
		list:[],
		index:[]
	},

	action:'doSearch',
	
	show: function(name, info) {
		this.element = $(name);
		this.infoBox = $(info);
		this.temp = this.infoBox.innerHTML;
		this.year = this.value.getFullYear();
		this.month = this.value.getMonth();
		this.day = this.value.getDate();
		this.parse();
	},

	hide: function() {
		this.element.style.display='none';
	},

	date: function(time) {
		var date = new Date();
		date.setTime(parseInt(time+'000'));
		var text =
			date.getFullYear()+'. '+
			this.msg.months[date.getMonth()]+' '+
			date.getDate();
		return text;
	},
	
	serviceParams: null,
	
	doClick: function(day) {
		var html = '';
		var index = this.data.index[day] ? this.data.index[day] : [];
		for (var i=0; i<index.length; i++) {
			var item = this.data.list[index[i]];
			var date = new Date();
			html += '<a href="'+this.msg.url+item.event_id+'">'+item.title+'</a><br />';
			html += ''+item.place+'<br/>';
			html += this.date(item.date_start)+' -tól<br/>';
			html += this.date(item.date_end)+' -ig<br/>';
		}
		this.infoBox.innerHTML = html;
		this.infoBox.style.display = 'block';
	},

	stepMonth: function(num) {
		this.infoBox.style.display = 'none';
		this.infoBox.innerHTML = this.temp;
		this.visible = true;
		this.month += num;
		while (this.month < 0) {
			this.month += 12;
			this.year -= 1;
		}
		while (this.month > 11) {
			this.month -= 12;
			this.year += 1;
		}
		this.data = Service.execute(
			this.serviceName,
			'GetCalendar',
			[this.year, this.month+1,null,this.serviceParams],
			false
		);
		this.parse();
	},

	getWeekday: function(time) {
		var value = new Date(time);
		if(value.getDay() == 0) return 6;
		return value.getDay()-1;
	},

	doSearch: function(year, month, day)
	{
		window.location = "/events/0/search?Date_Year="+year+"&Date_Month="+month+"&Date_Day="+day+"&search=";
	},
	doRedirect: function(year, month, day)
	{
		if (this.data.index[day].length>1)
		{
			window.location = "/rolunk_irtak_meg/0/search?Date_Year="+year+"&Date_Month="+month+"&Date_Day="+day+"&search=";
		}
		else {
			window.location = this.data.list[this.data.index[day][0]].link;
		}
	},
	
	parse: function() {
		var html = 
			'<table>' + "\n" +
			'<caption>' + "\n" +
			'<a href="#" class="year_left" onclick="'+this.VariableName+'.stepMonth(-12); return false;">'+this.msg['prev_year']+'</a>&nbsp;' +
			'<a href="#" class="month_left" onclick="'+this.VariableName+'.stepMonth(-1); return false;">'+this.msg['prev_month']+'</a>&nbsp;' +
			'<span>'+this.year+'. '+this.msg.months[this.month]+'' + '</span>&nbsp;' +
			'<a href="#" class="month_right" onclick="'+this.VariableName+'.stepMonth(1); return false;">'+this.msg['next_month']+'</a>&nbsp;' +
			'<a href="#" class="year_right" onclick="'+this.VariableName+'.stepMonth(12); return false;">'+this.msg['next_year']+'</a>&nbsp;' +
			'</caption>' + "\n" +
			'<thead>' + "\n" +
			'<tr>' + "\n";
		for(var i=0; i<this.msg.days.length-1; i++) {
			html += '<th>'+this.msg.days[i]+'</th>' + "\n";
		}
		html += '<th class="sunday">'+this.msg.days[i]+'</th>' + "\n" +
			'</tr>' + "\n" +
			'</thead>' + "\n" +
			'<tbody>' + "\n";
		var dayTime = 24 * 3600 * 1000;
		var first = new Date(this.year, this.month, 1, 12,0,0).getTime();
		var last = new Date(this.year, this.month+1, 1,12,0,0).getTime();
		var today = new Date();
		var todayTime = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 12,0,0).getTime();
		var valueTime = new Date(this.value.getFullYear(), this.value.getMonth(), this.value.getDate(),12,0,0).getTime();
		var weekday = this.getWeekday(first);
		var time = first - (weekday * dayTime) ;			
		while(time < last) {
			html += '<tr>' + "\n";
			for(var i=0; i<7; i++) {
				var date = new Date(time);
				if(date.getMonth() == this.month) {
					var style = '';
					if(date.getDay() == 0) style = "sunday";
					// alert(time + '---' + todayTime);
					if(new Date(time).getDate() == new Date(todayTime).getDate()) style += (style == '') ? 'today' : ' today';
					html += '<td'+(style != '' ? ' class="'+style+'"' : '')+'>';
					if(this.data.index[date.getDate()]) {
						html += '<a href="#" class="event"';
							switch(this.action)
							{
							case 'doRedirect':
								// html += ' onclick="'+this.VariableName+'.doRedirect('+this.data.list[this.data.index[date.getDate()][0]][0]['link']+'); return false;"';
								html += ' onclick="'+this.VariableName+'.doRedirect('+date.getFullYear()+', '+
								(date.getMonth()+1)+', '+date.getDate()+'); return false;"';
								break;
							case 'doClick':
								html += ' onclick="'+this.VariableName+'.doClick('+date.getDate()+'); return false;"';
								break;
							default:
								html += 'onclick="'+this.VariableName+'.doSearch('+date.getFullYear()+', '+
									(date.getMonth()+1)+', '+date.getDate()+'); return false;"';
								break;
							}
							html += '>'+date.getDate()+'</a>';
					} else {
						html += date.getDate();
					}
					html += '</td>' + "\n";	
				} else {
					html += '<td>&nbsp;</td>' + "\n";
				}
				time += dayTime;
			}
			html += '</tr>' + "\n";
		}	
		html += '</tbody>' + "\n";
		html += '</table>' + "\n";
		this.element.innerHTML = html;
	}
}

// cloning object
var EventCalendar2 = {
		value: 		EventCalendar.value,
		events:		EventCalendar.events,
		infoBox:	EventCalendar.infobox,
		temp:		EventCalendar.temp,
		msg:		EventCalendar.msg,
		data:		EventCalendar.data,
		action:		EventCalendar.action,
		show:		EventCalendar.show,
		hide:		EventCalendar.hide,
		date:		EventCalendar.date,
		doClick:	EventCalendar.doClick,
		doSearch:	EventCalendar.doSearch,
		doRedirect:	EventCalendar.doRedirect,
		stepMonth:	EventCalendar.stepMonth,
		getWeekday:	EventCalendar.getWeekday,
		parse:		EventCalendar.parse,
		VariableName: 'EventCalendar2',
		action:'doRedirect',
		serviceName: 'content/ContentLogic',
		serviceParams: null
}