
var interaction = window.interaction = window.interaction || {};

interaction.Calendar = function()
{
	this.$calendar = $('.calendar').eq(0);
	this.$monthPaging = $('.month-paging').eq(0);
	this.monthNames = ['Leden', 'Únor', 'Březen', 'Duben', 'Květen', 'Červen', 'Červenec', 'Srpen', 'Září', 'Říjen', 'Listopad', 'Prosinec'];
	this.ajaxUrl = BASE_HREF+'ajax-calendar/';
	this.ajaxUrl = 'ajax-calendar/';
	this.ajaxUrlEvents = 'ajax-events/';
}

interaction.Calendar.prototype.init = function()
{
	this.$loader = $('<div class="calendar-loader"/>');
	this.$monthPaging.find('.prev').bind('click', $.proxy(function()
	{
		this.prev();
		return false;
	}, this));
	this.$monthPaging.find('.next').bind('click', $.proxy(function()
	{
		this.next();
		return false;
	}, this));
	
	this.$calendar.find('.calendar-table').delegate('a', 'click', $.proxy(this.loadDayEvents, this));
	
}

interaction.Calendar.prototype.prev = function()
{
	var data = {
		month: this.$calendar.attr('data-month'),
		year: this.$calendar.attr('data-year')
	};
	
	if(data.month == 1)
	{
		data.month = 12;
		data.year--;
	}
	else
	{
		data.month--;
	}	
	this.ajaxLoad(data);
}

interaction.Calendar.prototype.next = function()
{
	var data = {
		month: this.$calendar.attr('data-month'),
		year: this.$calendar.attr('data-year')
	};
	
	if(data.month == 12)
	{
		data.month = 1;
		data.year++;
	}
	else
	{
		data.month++;
	}	
	this.ajaxLoad(data);
}


interaction.Calendar.prototype.ajaxLoad = function(data)
{
	this.showLoader();
	
	$.ajax({
		url: this.ajaxUrl, 
		data: data,
		dataType: 'html',
		success: $.proxy(function(response)
		{
			var $monthName = this.$monthPaging.find('.month-name');
			
			this.$calendar.attr('data-month', data.month);
			this.$calendar.attr('data-year', data.year);
			
			$monthName.html(this.monthNames[data.month - 1] + ' ' + data.year);
			
			this.$calendar.find('.calendar-table').empty().append(response);
			
			this.hideLoader();		
        }, this)
	});
};

interaction.Calendar.prototype.loadDayEvents = function(event)
{
	this.showLoader();

	var data = {
		day: $(event.currentTarget).text(),
		month: this.$calendar.attr('data-month'),
		year: this.$calendar.attr('data-year')
	};
	
	$.ajax({
		url: this.ajaxUrlEvents, 
		data: data,
		dataType: 'html',
		success: $.proxy(function(response)
		{
			$('.col-calendar-b').html(response);			
			
			this.hideLoader();		
        }, this)
	});
	
	return false;
};


interaction.Calendar.prototype.showLoader = function()
{
	this.$calendar.append(this.$loader);
}

interaction.Calendar.prototype.hideLoader = function()
{
	this.$loader.detach();
}

