var currentPage = 0;
var pages = 0;
var limit = 3;

var pages;
var fromYear;
var fromMonth;
var currentDay;
var fromDate;

var current = 0;
var date = new Date();

$(document).ready(function() {
	
	fromYear	= date.getFullYear(); 	
	fromMonth 	= date.getMonth();	
	currentDay 	= date.getDay();
	fromDate 	= date.getDate();
	
	var offset = currentPage*limit;
	
	getCalendar(date.getMonth()+1,date.getFullYear(),function(){getAgendaMonth(offset);});
	
	// paginator
	$('.gotoNextPage').click(function(){
		if(currentPage<pages){
			getAgendaMonth(currentPage);
		}
	});
	
	$('.gotoPrevPage').click(function(){
		if(currentPage>1){
			currentPage = currentPage-2;
			getAgendaMonth(currentPage);
		}	
	});
	
	$('.gotoPage').click(function(){
			var gotoPage = parseInt($(this).html());
			if(!isNaN(gotoPage)){
				getAgendaMonth(--gotoPage);
			}
	});
	
	// calendar
	$("#prevMonth").click(function(){
		with(date){setMonth(getMonth()-1);}
		fromMonth = date.getMonth();
		fromDate=1;
		getCalendar(date.getMonth()+1,date.getFullYear(),function(){getAgendaMonth(0);});
	});
	
	$("#nextMonth").click(function(){
		with(date){setMonth(getMonth()+1);}
		fromMonth = date.getMonth();
		fromDate=1;
		getCalendar(date.getMonth()+1,date.getFullYear(),function(){getAgendaMonth(0);});
	});
});

function getAgendaMonth(offset){
	
	fromYear = date.getFullYear(); // error josef
	
	var listurl ="data/getMedia.php?type=4&l="+limit+"&o="+offset+"&ord=date&long=1&from="+fromYear+"-"+(fromMonth+1)+"-"+fromDate+"&yr="+fromYear+"&mo="+(fromMonth+1);
	$("#agendaItems").html("<div class='ajaxloader'><img alt='loading' src='img/ajax-loader-home.gif'/></div>");
	
	$.ajax({
		type: "POST",
		url: listurl,
		dataType: "xml",
		error: function(data){
			// TODO: define error message
			$(targetDiv).html("Error");
		},
		success: function(xml) {
			$(xml).find('data').each(function(){
				pages = parseInt($(this).attr('pages'));
				$("#pager_pages").val(pages);
				currentPage = parseInt($(this).attr('page'));
				$("#pager_page").val(currentPage-1);
			});
			$("#agendaMonth h2").html(month_names[fromMonth]);
			if(pages==0){$("#agendaItems").html("<div class='agendaItem noresult' style='text-align:center'>No se han encontrado resultados...</div>");return;}
			
			
			updatePgr();
			$("#agendaItems").html("");
			var i =0;
			$(xml).find('media').each(function(){
				var name = $(this).find('name').text();
				var desc = $(this).find('description').find('long').text();
				var url = $(this).find('url').text();
				var fulldate = parse_date($(this).attr('date'));
				var day = day_names[fulldate.getDay()];
				var date = fulldate.getDate();
				var month = month_names[fulldate.getMonth()];
				var frequency = $(this).find('frequency').text();
				var htmlString ="";
				
				if(i++>0){
					htmlString = "<div class='agendaItem withSeparator'>";
				} else {
					htmlString = "<div class='agendaItem'>";
				}
				htmlString = htmlString+ 
								"	<div class='agendaItemDate'>"+
								"		<div class='date'>"+
								"			<span>"+
								"				<span class='agendaItemDay'>"+day+"</span>"+
								"				<span class='agendaItemNumber'>"+date+"</span>"+
								"				<span class='agendaItemMonth'>"+month+"</span>"+
								"			</span>"+
								"		</div>"+
								"		<div class='agendaThumbnail'>"+
								"			<img width='163px' height='108px' src='"+url+"' alt=''>"+
								"		</div>"+
								"	</div>"+
								"	<div class='agendaRight'>"+
								"		<div class='agendaFrequency'>"+frequency+"</div>"+
								"		<div class='agendaTitle'>"+name+"</div>"+
								"		<div class='agendaDescription'>"+desc+"</div>"+
								"	</div>"+
								"</div>"; // end of agendaItem
				$("#agendaItems").append(htmlString);
				
			});
			
			// over description
			$('.agendaItem').hover(function(){
				$(".agendaDescription",this).css("overflow","auto");
				$(".agendaDescription",this).css("height","auto");
			},function(){
				$(".agendaDescription",this).css("overflow","hidden");
				$(".agendaDescription",this).css("height","58px");
			});
		}
	});
}



