(function($) {

	"use strict";

	var options = {
		events_source: 'calendar/event.php',
		view: 'month',
		tmpl_path: 'calendar/tmpls/',
		tmpl_cache: false,
		day: 'now',
		onAfterEventsLoad: function(events) {
			console.log('Events loaded:', events);
			console.log('Events count:', events ? events.length : 0);
			if(!events || events.length === 0) {	
				$('#eventlist').html('<li>No hay anuncios</li>');
				return;
			}
			var list = $('#eventlist');
			list.html('');

			$.each(events, function(key, val) {
				console.log('Event:', val);
				$(document.createElement('li'))
					.html('<b>' + val.title + '</b> - ' + new Date(val.start))
					.appendTo(list);
			});
		},
		onAfterViewLoad: function(view) {
			$('.span9 h3').text(this.getTitle());
			$('.btn-group button').removeClass('active');
			$('button[data-calendar-view="' + view + '"]').addClass('active');
		},
		classes: {
			months: {
				general: 'label'
			}
		}
	};

	var calendar = $('#calendar').calendar(options);

	$('.btn-group button[data-calendar-nav]').each(function() {
		var $this = $(this);
		$this.click(function() {
			calendar.navigate($this.data('calendar-nav'));
		});
	});

	$('.btn-group button[data-calendar-view]').each(function() {
		var $this = $(this);
		$this.click(function() {
			calendar.view($this.data('calendar-view'));
		});
	});
}(jQuery));