(function($) { 
	var opts = {},appuntamentiArray,el;
	
	$.fn.appuntamenti = function(settings) {
		opts.fileAppuntamenti='/appuntamenti_calendario.php',
		opts.fileAppuntamentiDettaglio='/appuntamenti_calendario_dettaglio.php'
		
		opts.settings = $.extend({}, $.fn.appuntamenti.defaults, settings);
		opts.el = $(this);
		
		$.fn.appuntamenti.getAppuntamentiCalendario();
	}
	
	$.fn.appuntamenti.getAppuntamentiCalendario = function() {
		$.getJSON(
			opts.fileAppuntamenti,
			appuntamentiCalendarioCallback
		);
	} 
		
	
	function appuntamentiCalendarioCallback(data){
//		console.log(data);
		
		opts.appuntamentiArray=data;
	
		opts.el.datepicker({
			prevText: "", 
			nextText: "", 
			beforeShowDay: appuntamentiCalendarioAccendi,
//			onChangeMonthYear: attivaqTip,
			onSelect: apriEvento
		}); 
	}
	
	function attivaqTip(day){
		var elems = $('#datepicker .ui-state-active a');

		// Store our title attribute and remove it so we don't get browser tooltips showing up
		
		var anno=$('.ui-datepicker-year').text();
		
		var meseTesto=$('.ui-datepicker-month').text();
		mese=$.datepicker.regional['it']['monthNames'].indexOf(meseTesto)+1;
		
		elems.each(function(i) {
			$.attr(this, 'tooltip', $(this).text());
		})
//		console.log(elems);
		// Create the tooltip on a dummy div since we're sharing it between targets
		$('<div />').qtip({
			content: {
				text: ' ',
				title: {
					button: 'Close'
				}
			},
			position: {
				target: 'event', // Use the triggering element as the positioning target
				effect: false ,  // Disable default 'slide' positioning animation
				my: 'bottom right',
				at: 'top right'
			},
			show: {
				target: elems
			},
			hide: 'unfocus',
			events: {
				show: function(event, api) {
					// Update the content of the tooltip on each show
					var target = $(event.originalEvent.target);
					
					if(target.length) {
						api.set('content.ajax.url', opts.fileAppuntamentiDettaglio+'?date='+target.attr('tooltip')+'/'+mese+'/'+anno);
					}
				}
			}
		}).show();
	}
	
	function apriEvento(dateText,inst){
//		console.log(dateText);
		
		$.getJSON(
			opts.fileAppuntamentiDettaglio,
			{date:dateText},
			apriEventoCallback
		);
	}

	function apriEventoCallback(data){

		html='';
		
		html+='<ul class="corsoDaCalendario">';
		html+='	<div class="ico"><img width="51" height="51" alt="icona calendario" src="img/ico_calendario.png"></div>';
		html+='	<div class="header scheda"><p>Corsi attivi il '+data[0]['data_corso'].replace(/([0-9]{4})-([0-9]{2})-([0-9]{2})/,'$3/$2/$1').replace(/ 00:00:00/,'')+'</p></div>';
		html+='	<ul>';
		
		for(i in data){
			html+='	<li>';
			
			dataInizio=data[i]['data_inizio'].replace(/([0-9]{4})-([0-9]{2})-([0-9]{2})/,'$3/$2/$1');
			dataInizio=dataInizio.replace(/ 00:00:00/,'');
			
			dataFine=data[i]['data_fine'].replace(/([0-9]{4})-([0-9]{2})-([0-9]{2})/,'$3/$2/$1');
			dataFine=dataFine.replace(/ 00:00:00/,'');
			
			html+='			<div>';
			html+='				<div class="dataCorso">Dal '+dataInizio+' al '+dataFine+'</div>';
			html+='				<div class="luogoCorso"><a class="schede fancybox.ajax" href="schedaCorso.php?id='+data[i]['id']+'">'+data[i]['nome']+'</a>';
			html+='			</div>';			
			html+='		</li>';
		}
		
		html+='	</ul>';
				
//		console.log(html);
		$.fancybox(
			html,
			{
				'onComplete':function(){
					$('.schede').fancybox({
						closeClick:false
					});
				},
				'autoSize':false,
				'width':460,
				'maxHeight':400,
				closeClick:false
			}
		);
	}
	
	function chiudiEvento(){
		$('#agenda').slideUp();
	}

	function appuntamentiCalendarioAccendi(date) {
		if(opts.appuntamentiArray[date.getFullYear()]){
			if(opts.appuntamentiArray[date.getFullYear()][(date.getMonth()+1)]){
				if(opts.appuntamentiArray[date.getFullYear()][(date.getMonth()+1)][date.getDate()]){
					return [true, 'ui-state-highlight ui-state-active'];  
				}
			}
		} 
		return [false, ""];  
	} 

})(jQuery);
