// calendar.js for event/lesson calendar MAXIM 1.0.0
// Copyright (c) 2009 Michael Daum,  visit http://webdaum.de/

if(document.getElementById) {
	
	with (window.document) {
	 writeln('<script type="text/javascript" src="js/xmlhttp.js"></script>');
	}

	c_weekdays    = c_weekdays.split(',');
	c_months      = c_months.split(',');
	c_short_months= c_short_months.split(',');
	c_short_days  = c_short_days.split(',');

	// todays date:
	var c_todays_date = new Date();
	var c_year  = c_todays_date.getFullYear();
	var c_month = c_todays_date.getMonth();

	// usefull ones:
	var c_month_length = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	
	// handle GET query for a specific date:
	if(
	 location.search &&
	 location.search.indexOf("year"))
	{
		var _GET = new Array();

		var params = unescape(location.search.substring(1,location.search.length)).split("&");
		for(var i=0;i<params.length;i++) {
			param = params[i].split("=");
			_GET[param[0]] = param[1];
		}

		c_year = parseInt(_GET['year']);	
		if(_GET['month']) {
			c_month = parseInt(_GET['month'])-1;
		}
	}

}

// february needs some special treatment:
function calendar_check_feb() {
	
	// is leap year?
	if(c_year%4==0) {
	
		// add a day:
		c_month_length[1] = 29;
		
	}
}

// create calendar's years navigation:
function calendar_build_nav_year() {
	var a = document.createElement("A");
	var year = document.getElementById('c_year_span');
	a.setAttribute('href','?section=events&year='+c_year);
	a.innerHTML = c_year;
	year.innerHTML = '';
	year.appendChild(a);

}

// create calendar's months navigation:
function calendar_build_nav_month() {

	var a = document.createElement("A");
	var month = document.getElementById('c_month_span');
	a.innerHTML = c_short_months[c_month];
	a.setAttribute('title',c_months[c_month]);
	a.setAttribute('href','?section=events&year='+c_year+'&month='+(c_month+1));
	month.innerHTML = '';
	month.appendChild(a);

}

// display a specific date, this is the main function:
function calendar_show_date(my_year,my_month) {
  
	// check if ajax request is running already:
	if(window.autocomplete) {
		clearTimeout(autocomplete);
	}
	// date auto preselection:
	c_year  = my_year  || c_todays_date.getFullYear();
	c_month = my_month || c_todays_date.getMonth();

	if(my_month === 0) c_month = 0; 

	// initialize today's date:
	c_show_date = new Date(c_year,c_month);

	// rebuild year's navigation:
	calendar_build_nav_year();  

	// rebuild month's navigation:
	calendar_build_nav_month(c_year);

	// Debug:
	//alert (c_show_date);
	//alert(i+"-"+j+"-"+k);
	
	// load calendar table data:
	if(window.document.getElementById("calendar_table")) {
		calendar_ajax_load("calendar_table","calendar_table");
	}
}

// forward - backward  navigation month/year
function calendar_turn(direction,period) {

	if(!document.getElementById) {
		return false;
	}
	
	if(period=='year' && c_year+direction > 1970) {
	
		c_year +=direction;
		
	} else {
	
		calendar_check_feb();

		//check:
		switch (direction) {
		  
			case -1:
				if( c_month > 0) {
					c_month -= 1;
				} else {
					c_month = 11;
					c_year -= 1;
				}
				break;
				
			case 1: 
			
				
				if( c_month < 11) {
					c_month += 1;
				} else {
					c_month = 0;
					c_year+= 1;
				}
				break;
		  
		}
	}
	calendar_show_date(c_year,c_month);
}

function calendar_url() {

	var url = "?year="+c_year+"&month="+(c_month+1);
	return url;
	
}
function calendar_ajax_load(resource,send_to) {

	//send_to = send_to || "image-description";  
	var url=calendar_url()+"&ajax="+resource; 

	try { 

		// run ajax request in IE:
		if(document.all&& !window.opera) {
			xmlhttp.open("GET",url,true);
		}
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState == 4 && xmlhttp.responseText) { 
				var result = xmlhttp.responseText;
				if(xmlhttp.status == 200) {
					try {
						document.getElementById(send_to).innerHTML = result;
					} catch(e) {
						document.getElementById(send_to).innerText = '';
						document.getElementById(send_to).innerText = result;
						//alert(result);
					}
				}
			} 
		}
		// run ajax request in firefox/opera/safari:
		if(!document.all || window.opera) {
			xmlhttp.open("GET",url,true);
		}
		xmlhttp.send(null);		

	} catch (e) { 

		var index = send_to=="tbody_calendar_month"?0:1;

		if(!window.frames[index]) {
			iframe= '<iframe frameborder="0" src="about:blank"></iframe>';  		
			document.getElementById(send_to).innerHTML = iframe;	
		}
		window.frames[index].location.href=url+"&iframe=1";
	}
	return false;	
}


