$(function() {
	var m, a;
	var mesi_ita = new Array('gennaio', 'febbraio', 'marzo', 'aprile', 'maggio', 'giugno', 'luglio', 'agosto', 'settembre', 'ottobre', 'novembre', 'dicembre');
	
	var getCalendario = function(mese, anno) {
		
		var today = new Date;
	
		m = ( mese == null ) ? today.getMonth()+1 : mese;
		a = ( anno == null ) ? today.getFullYear() : anno;
		
		var eventCallback = function($td, thisDate, month, year) {	
			
			var cur_day = thisDate.getDate();
			var count = 0;
			
			$(".link-eventi").attr("href", "/eventi/" + (month+1) + "/" + year);
			$(".link-offerte").attr("href", "/offerte/" + (month+1) + "/" + year);
			
			$.each(eventi, function(day, value){
				if ( day == cur_day && $td.is('.current-month') )
				{
					if ( value['eventi'] && value['offerte'] )
					{
						$td.addClass('has-bothEvent');
						$("#clone-container").append('<div class="approfondimento" id="tooltip-both-'+day+'">');
						
						$.each(value['eventi'], function(k, evento){
							$("#tooltip-both-"+day).append('<div class="zoom-locandina"><img src="'+evento['image']+'" /></div>');
							$("#tooltip-both-"+day).append('<div class="contenuti-aggiuntivi"><strong class="offerName">'+evento['name']+'</strong></div>');
							$("#tooltip-both-"+day).append('<div class="pulizia"></div>');
						});
						$("#tooltip-both-"+day).append('<a href="/eventi/'+(month+1) + "/" + year+'" class="vedi-tutti">Vai agli eventi di questo mese</a>');
						
						$("#tooltip-both-"+day).append('<div class="pulizia"></div>');

						count = 0;
						$.each(value['offerte'], function(k, offerta){
							if ( count < 3 )
							{
								$("#tooltip-both-"+day).append('<div class="zoom-locandina"><img src="'+offerta['image']+'" /></div>');
								$("#tooltip-both-"+day).append('<div class="contenuti-aggiuntivi"><strong class="offerName">'+offerta['name']+'</strong><br />Negozio: <strong>'+offerta['shop_name']+'</strong></div>');
								$("#tooltip-both-"+day).append('<div class="pulizia"></div>');
							}
						});
						$("#tooltip-both-"+day).append('<a href="/offerte/'+(month+1) + "/" + year+'" class="vedi-tutti">Consulta le offerte del mese</a>');
						
						$td.attr("rel", "#tooltip-both-"+day);
					}
					else if ( value['eventi'] )
					{
						$td.addClass('has-event');
						$("#clone-container").append('<div class="approfondimento" id="tooltip-eventi-'+day+'">');

						$.each(value['eventi'], function(k, evento){
							$("#tooltip-eventi-"+day).append('<div class="zoom-locandina"><img src="'+evento['image']+'" /></div>');
							$("#tooltip-eventi-"+day).append('<div class="contenuti-aggiuntivi"><strong class="offerName">'+evento['name']+'</strong></div>');
							$("#tooltip-eventi-"+day).append('<div class="pulizia"></div>');
						});

						$("#tooltip-eventi-"+day).append('<a href="/eventi/'+(month+1) + "/" + year+'" class="vedi-tutti">Vai agli eventi di questo mese</a>');
						$td.attr("rel", "#tooltip-eventi-"+day);
					}
					else
					{
						$td.addClass('has-lastEvent');
						$("#clone-container").append('<div class="approfondimento" id="tooltip-offerte-'+day+'">');

						count = 0;
						$.each(value['offerte'], function(k, offerta){
							if ( count < 3 )
							{
								$("#tooltip-offerte-"+day).append('<div class="zoom-locandina"><img src="'+offerta['image']+'" /></div>');
								$("#tooltip-offerte-"+day).append('<div class="contenuti-aggiuntivi"><strong class="offerName">'+offerta['name']+'</strong><br />Negozio: <strong>'+offerta['shop_name']+'</strong></div>');
								$("#tooltip-offerte-"+day).append('<div class="pulizia"></div>');
								
								count++;
							}
						});
						
						$("#tooltip-offerte-"+day).append('<a href="/offerte/'+(month+1) + "/" + year+'" class="vedi-tutti">Consulta le offerte del mese</a>');
						$td.attr("rel", "#tooltip-offerte-"+day);
					}

					$td.attr("title", day + "/" + (month+1) + "/" + year);
					$td.cluetip({
						local:true,
						cursor: 'pointer',
						activation: 'click',
						arrows: false,
						closePosition: 'top',
						closeText: '<div class="close-tooltip"></div>',
						sticky: true,
						showTitle: true,
						dropShadow: true
					});
				}
			})
		};
		
		$.ajax({
			url: '/events/get_event_json.json?m=' + m + '&y=' + a,
			cache: false,
			success: function (ret){
				ret += "\n";
				eventi = eval('('+ret+')');
				
				$('#calendario').renderCalendar({month:(m-1), year:anno, renderCallback:eventCallback, showHeader: $.dpConst.SHOW_HEADER_SHORT});
			}
		});
	}
	
	$("#nextMonth").click(function(){
		var next_m, next_a;

		if ( m == 12 )
		{
			next_m = 1;
			next_a = a+1;
		}
		else if ( m == 1 )
		{
			next_m = m+1;
			next_a = a;
		}
		else
		{
			next_m = m+1;
			next_a = a;
		}

		getCalendario(next_m, next_a);
		
		$("#nome_mese").html(mesi_ita[m-1]);
		$("#anno").html(a);
		
		return false;
	});
	
	$("#prevMonth").click(function(){
		
		var prev_m, prev_a;
		
		if ( m == 12 )
		{
			prev_m = m-1;
			prev_a = a;
		}
		else if ( m == 1 )
		{
			prev_m = 12;
			prev_a = a-1;
		}
		else
		{
			prev_m = m-1;
			prev_a = a;
		}

		getCalendario(prev_m, prev_a);

		$("#nome_mese").html(mesi_ita[m-1]);
		$("#anno").html(a);

		return false;
	});
	
	getCalendario();
});
