// jen alias pro console.log(...)
	function mcl($var) {
		//console.log($var) ;
	}
// rozsireni objeku Date o metodu vracejici string ve formatu yyyy-mm-dd
	Date.prototype.toYmd = function() {
		return  this.getFullYear().toString() +
			"/" +
			(this.getMonth()+1 < 10 ? "0" + (this.getMonth()+1).toString() : (this.getMonth()+1).toString()) +
			"/" +
			(this.getDate() < 10 ? "0" + this.getDate().toString() : this.getDate().toString()) ;
	}
	Date.prototype.toCz = function() {
		return this.getDate().toString() +
			"." +
			(this.getMonth()+1).toString() +
			"." +
			this.getFullYear().toString() ;
	}
	
	
$(function() {
	
// global udalosti
	var $events = {};

 
// datepicker
    $("#datepicker").datepicker({
		showOtherMonths: true,
		dateFormat: "yy/mm/dd",
		onSelect: popup,
        beforeShowDay: highlightDay,
        onChangeMonthYear: fetchEvents
    });

// datepicker popup
	function popup(dateText, inst) {
	// konkretni den ve var events
		var $dayEvents = $events[ new Date(dateText).toYmd() ];
	// kdyz existuje ....
		if ($dayEvents !== undefined) {
			var $popup = '' ;
			$.each($dayEvents, function(index, $event) {
				$ds = new Date($event.date_start).toCz() ;
				$de = new Date($event.date_end).toCz() ;
				if($ds === $de) {
					$d = $ds ;
				} else {
					$d = $ds + ' - ' + $de ;
				}
				$popup += '<div class="day_event"><div class="day_event_title">' + $event.title + '</div><div class="day_event_date">' + $d + '</div>' + $event.imgx + $event.sd + '<a href="' + $event.url +'">detail</a></div>' ;
				//alert($event.imgx);
			});
			$('#day_events_popup').html($popup) ;
		}
	}
	
// mouseover

	
	var $popup_hide = true ;
	
	$('#datepicker').delegate('.dayEvents', 'mouseover', function() {
		var $pos = $(this).position();
		$popup_hide = false ;
		//mcl($pos);
		$('#day_events_popup').css({
			'left'	:	$pos.left - 110,
			'top'	:	$pos.top +20
		}).show() ;
		popup( $(this).attr('title') ) ;
	});
	$('#datepicker').delegate('.dayEvents', 'mouseout',function() {
		$popup_hide = true;
		var $hide = setTimeout(function() {
			if ($popup_hide) {
				$('#day_events_popup').hide();	
			}
		}, 500);
		
	});
	$('#day_events_popup').hover(function(){
		$popup_hide = false;
	}, function(){
		$popup_hide = true;
		var $hide = setTimeout(function() {
			if ($popup_hide) {
				$('#day_events_popup').hide();	
			}
		}, 500);
	});

// nacteni udalosti pro mesic
	function fetchEvents(year, month) {
		var $url = 'igra_cal/cal_ajax_fetchEvents.php?year=' + year + '&month=' + month + '&lang=' + $lang + '' ;
		mcl($url);
		var $monthEvents = '' ;
	    $.getJSON($url, function(data){
	        $events = data ;
		    $("#datepicker").datepicker('refresh'); // prekresleni kalendare
			$('#day_events_popup').hide();	
	    });
	}

// zvyrazneni dne
	function highlightDay(date) {
		var $dateStr = date.toYmd() ;
		if( $events[$dateStr] === undefined ) {
			return [true, ''];
		} else {
			return [true, 'dayEvents', $dateStr] ;
		}	
	}
	
// prvotni nacteni udalosti
	fetchEvents();
	
});	
