(function($) {
	var n = 'ajaxNewsCalendar-';
	var currentMonth = null;
	var calendarElement = null;
	var first = true;
	var direction = 0;
	
	var ajaxData;
	
	var monthNames = new Array("Januar","Februar","Marec","April","Maj","Junij","Julij","Avgust","September","Oktober","November","December");
	
	$.fn.ajaxNewsCalendar = function(options){
		var today = new Date();
		
		var firstDay = new Date();
		firstDay.setDate(1);
		currentMonth = firstDay;
		calendarElement = this;
		

		loadMonth(firstDay);

	};
	
	$.fn.ajaxNewsCalendar.loadPrevious = function(){
		currentMonth.setMonth( currentMonth.getMonth() - 1 );
		direction = -1;
		loadMonth(currentMonth);
	}
	
	$.fn.ajaxNewsCalendar.loadNext = function(){
		currentMonth.setMonth( currentMonth.getMonth() + 1 );
		direction = 1;
		loadMonth(currentMonth);		
	}
	
	function convertToString(month,year){
		month = '0'+month;
		return ''+year+''+month.substring(month.length-2); 
	}
	
	function leadingZero(day){
		day = '0'+day;
		return ''+day.substring(day.length-2);
	}
	
	function loadMonth(firstDay){
		var today = new Date();
		var wday = (firstDay.getDay()+7-1) % 7;
		var thisDay = new Date(firstDay);
		if (wday>0) thisDay.setDate(thisDay.getDate()-wday);
		/* Header */
		head = '<div class="'+n+'previous" onclick="$.fn.ajaxNewsCalendar.loadPrevious();"></div><div class="'+n+'monthName">'+monthNames[firstDay.getMonth()]+' '+firstDay.getFullYear()+'</div><div class="'+n+'next" onclick="$.fn.ajaxNewsCalendar.loadNext();"></div>';		
		cont = '';
		/* Fill active days */
		do {
			wday = (thisDay.getDay()+7-1) % 7;
			if (wday==0){
				cont += '<div class="'+n+'week">';
			} 
			addClass = '';
			if (thisDay.getMonth()!=currentMonth.getMonth()){
				addClass += ' '+n+'inactive';
				addId = '';
			} else addId = 'id="'+n+'day_'+thisDay.getFullYear()+'-'+leadingZero(thisDay.getMonth()+1)+'-'+leadingZero(thisDay.getDate())+'"'; 
				
			if (thisDay.toDateString()==today.toDateString())
				addClass += ' '+n+'today';
			cont += '<div class="'+n+'day '+addClass+'" '+addId+'>'+thisDay.getDate()+'</div>';
			if (wday==6)
				cont += '</div>';
			thisDay.setDate(thisDay.getDate()+1);
		} while( convertToString(thisDay.getMonth(),thisDay.getFullYear()) <= convertToString(currentMonth.getMonth(),currentMonth.getFullYear()) || wday!=6);

		/* Animation effect */
		if (first){
			calendarElement.html('<div class="'+n+'calendar" style="position:relative; overflow: hidden;"><div class="'+n+'header" style="overflow:hidden;">'+head+'</div><div class="'+n+'month">'+cont+'</div></div>');
		} else {
			$("."+n+"header",calendarElement).html(head);
			var prevMonth = $("."+n+"month",calendarElement);
			$("."+n+"calendar",calendarElement).append('<div class="'+n+'month '+n+'clone">'+cont+'</div>');
			var newMonth = $("."+n+"clone",calendarElement);
			$("."+n+"calendar",calendarElement).css('height',(newMonth.height()+$("."+n+"header",calendarElement).height()+2)+'px');
			
			if (direction>0){
				newMonth.css({'position':'absolute','left':calendarElement.width()+'px','top':$("."+n+"header",calendarElement).height()+'px'});
				prevMonth.css({'position':'absolute','top':$("."+n+"header",calendarElement).height()+'px' });
				prevMonth.animate({left:'-='+calendarElement.width()+'px'},500);
				newMonth.animate({left:'-='+calendarElement.width()+'px'},500,function(){
					prevMonth.remove();
					newMonth.css({'position':'static'});
					newMonth.removeClass(n+"clone");
					
				});				
			} else {
				newMonth.css({'position':'absolute','left':'-'+calendarElement.width()+'px','top':$("."+n+"header",calendarElement).height()+'px'});
				prevMonth.css({'position':'absolute','top':$("."+n+"header",calendarElement).height()+'px' });
				prevMonth.animate({left:'+='+calendarElement.width()+'px'},500);
				newMonth.animate({left:'+='+calendarElement.width()+'px'},500,function(){
					prevMonth.remove();
					newMonth.css({'position':'static'});
					newMonth.removeClass(n+"clone");
					
				});				
			}
		}
		/* Load ajax events */
        $.post('/asp/kzs/news2json.asp',{datum:convertToString(currentMonth.getMonth()+1,currentMonth.getFullYear())},function(data){
        	ajaxData = data;
			$.each(data,function(dateIndex,date){
				var c=0;				
				dateEl = "#"+n+"day_"+dateIndex;			
				$(dateEl).addClass(n+'active');
			});
			
			$("."+n+"active").mouseover(function(){
				/* Load conent*/
				var thisDate = $(this).attr('id').split('_')[1];
				var thisDateObj = ajaxData[thisDate];
				var tooltipContent = '';
				$.each(thisDateObj,function(index,data){
					/* POPRAVI LINK OB PRENOSU NA PRODUKCIJO */
					tooltipContent += '<div class="tooltipEvent" onclick="location.href=\'KZS,,'+data.Url+'\'">';
					tooltipContent += '<div class="tooltipTitle">'+data.Naslov+'</div>';
					tooltipContent += '<div class="tooltipContent">'+data.Podnaslov+'</div>';
					tooltipContent += '</div>';
				});
				
				/* Create tooltip */
				$("#"+n+"tooltipContainer").remove();
				$('body').append('<div id="'+n+'tooltipContainer"><div class="tooltip"><div class="tooltipPointer"></div>'+tooltipContent+'</div></div>');
				var top = $(this).offset().top - $("#"+n+"tooltipContainer").height()/2 + $(this).height()/2;
				var bgtop = $("#"+n+"tooltipContainer").height()/2 - 10;
				$("#"+n+"tooltipContainer").css({'overflow':'hidden', 'display':'none','position':'absolute','z-index':'2000','top': top+'px','left':$(this).offset().left+'px' });
				$("#"+n+"tooltipContainer .tooltipPointer").css('top',bgtop+'px');
				$("#"+n+"tooltipContainer").fadeIn();
				$("#"+n+"tooltipContainer").hover(function(){
					
				},function(){
					$(this).remove();
				});
			});
			
        },'json');		
		
		first = false;

	}

})(jQuery);
