var f_month=0;
var f_year=0;
var accomodation_id=0;
function show_prev(year,month,id) {
	clean();
	var url='index.php?option=com_rjaccomodation&controller=accomodations&task=get_occupations_xml';
	var nmonth=month-1;
	if(nmonth==0) {
		nmonth=12;
	}
	f_month=month;
	f_year=year;
	accomodation_id=id;
	var params='from='+year+"-"+month+'&to='+(year+1)+"-"+nmonth+'&id='+id;
	processAjax(params,url,render);
	
}

function show_next(year,month,id) {
	clean();
	var nmonth=month-1;
	if(nmonth==0) {
		nmonth=12;
	}
	var url='index.php?option=com_rjaccomodation&controller=accomodations&task=get_occupations_xml';
	var params='from='+year+"-"+month+'&to='+(year+1)+"-"+nmonth+'&id='+id;
	f_month=month;
	f_year=year;
	accomodation_id=id;
	processAjax(params,url,render);
}

function clean() {
	for(var i=document.getElementById('calendar').childNodes.length;i>0;i--) {
		document.getElementById('calendar').removeChild(document.getElementById('calendar').childNodes[i-1]);
	}
}

function render(ajax) {
	var xml=ajax.responseXML;
	var cal=document.getElementById('calendar');
	var month=f_month;
	var year=f_year;
	
	for(var i=0;i<12;i++) {
		if(month==13) {
			month=1;
			year++;
			
		}
		var div_m=document.createElement('div');
		div_m.setAttribute('class','month');
		
		var div_mn=document.createElement('div');
		div_mn.setAttribute('class','month_name');
		div_mn.appendChild(document.createTextNode(xml.getElementsByTagName('months')[0].getElementsByTagName('m'+month)[0].firstChild.nodeValue+" "+year));
		div_m.appendChild(div_mn);
		
		var t=document.createElement('table');
		t.setAttribute('width',"100%");
		var tr=t.insertRow(0);
		for(var j=0;j<days.length;j++) {
			var th=document.createElement('th');
			th.appendChild(document.createTextNode(days[j]));
			tr.appendChild(th);
		}
		
		var day=1;
		var week=1;
		var firstday=xml.getElementsByTagName('first_days')[0].getElementsByTagName('m'+month)[0].firstChild.nodeValue;
		var days_in_month=xml.getElementsByTagName('days_in_month')[0].getElementsByTagName('m'+month)[0].firstChild.nodeValue;
		
		while(day+7<=days_in_month) {
			var tr=t.insertRow(t.rows.length);
			if(week==1) {
				if(firstday-1>0) {
					var c=0;
					var td=tr.insertCell(c++);
					td.setAttribute('colspan',firstday-1);
					for(var j=firstday-1;j<7;j++) {
						var td=tr.insertCell(c++);
						td.appendChild(document.createTextNode(day));
						getOccupied(td,year,month,day,xml);
						day++;
					}
				} else {
					for(var j=0;j<7;j++) {
						var td=tr.insertCell(j);
						td.appendChild(document.createTextNode(day));
						getOccupied(td,year,month,day,xml);
						day++;
					}
					
				}
			} else {
				for(var j=0;j<7;j++) {
					var td=tr.insertCell(j);
					td.appendChild(document.createTextNode(day));
					getOccupied(td,year,month,day,xml);
					day++;
				}
			}
			week++;
		}
		var tr=t.insertRow(t.rows.length);
		var c=0;
		while(day<=days_in_month) {
			var td=tr.insertCell(c++);
			td.appendChild(document.createTextNode(day));
			getOccupied(td,year,month,day,xml);
			day++;
		}
		
		month++;
		div_m.appendChild(t);
		cal.appendChild(div_m);
		if(i>0 && i%4==3) {
			var div_c=document.createElement('div');
			div_c.setAttribute('style','clear:both');
			cal.appendChild(div_c);
		}	
	}
	
	document.getElementById('prev').onclick=function() {
		show_prev(f_year-1,f_month,accomodation_id);
	}
	
	document.getElementById('next').onclick=function() {
		show_next(f_year+1,f_month,accomodation_id);
	}
}

function getOccupied(td,year,month,day,xml) {
	var occupation=xml.getElementsByTagName('occupation');
	if(month<10) {
		month="0"+month;
	}
	if(day<10) {
		day="0"+day;
	}
	var date=year+"-"+month+"-"+day;
	var found=false;
	
	for(var i=0;i<occupation.length;i++) {
		if(occupation[i].getElementsByTagName('date')[0].firstChild.nodeValue==date) {
			if(occupation[i].getElementsByTagName('type')[0].firstChild.nodeValue==1) {
				td.setAttribute('class','obsazenost_obsazeno');
				found=true;
			} else {
				td.setAttribute('class','obsazenost_rezervovano');
				found=true;
			}
		}
	}
	
	if(found==false) {
		td.setAttribute('class','obsazenost_volno');
		td.onclick=function() {
			window.location="index.php?option=com_rjaccomodation&view=reserve&layout=form&accomodation_id="+accomodation_id+"&year="+year+"&month="+month+"&day="+day;
		}
	}
}
