//	Set up the ids for the differnt months on the calendar
var months = new Array("m012009", "m022009", "m032009", "m042009",
						  	"m052009", "m062009", "m072009", "m082009",
							"m092009", "m102009", "m112009", "m122009",
							"m012010", "m022010", "m032010", "m042010",
						  	"m052010", "m062010", "m072010", "m082010",
							"m092010", "m102010", "m112010", "m122010");

//	Set up Month Names
var monthNames = new Array("January", "Feburary", "March", "April",
							"May", "June", "July", "August",
							"September", "October", "November", "December");

//	Set up Current Date
var date = new Date();

//	Current Month
var curMonth = date.getMonth();
curMonth = curMonth + 1;

//	Current Day
var curYear = date.getYear();
curYear = curYear + '';

//	Get current year for Mozilla
//	Mozilla returns 108
if(curYear.length == 3)
	curYear = curYear.substring(1, 3);
//	Get current year for IE
//	IE Returns 2008
if(curYear.length == 4)
	curYear = curYear.substring(2, 4);

// Set up empty var for the currently selected month
var selectedMonth;

//	Get the current Month and set it up to be fed in to the function ie: m012008
var startMonthStr = "m" + curMonth + "20" + curYear;

function init(){
	setListeners();
	selectMonth(document.getElementById(startMonthStr));
}
//	Set up initial event listeners for the calendar
function setListeners(){
	
	for(var i = 0; i < months.length; i++){
		var link = document.getElementById(months[i]);
		
		//	If Mozilla addEventListener		
		if(link.addEventListener){
			link.addEventListener('mouseover', overMonth, false);
			link.addEventListener('mouseout', offMonth, false);	
			link.addEventListener('click', onMonth, false);	
		}
		//	If IE attachEvent				
		else{
			link.attachEvent('onmouseover', overMonth);
			link.attachEvent('onmouseout', offMonth);
			link.attachEvent('onclick', onMonth);
		}
	}
}

// 	Mouseover function for calendar
function overMonth(e){
	//	Makes sure the event is registered as e.  If not it forces it.
	if (!e) var e = window.event;
	//	e.target works for Mozilla
	if(e.target){
		var target = e.target;
		target.style.backgroundColor="#9dcc52";
	}
	//	e.srcElement works for IE
	else{
		var target = e.srcElement;
		target.style.backgroundColor="#9dcc52";
	}
}

//	Mouse Off function for calendar
function offMonth(e){
	//	Makes sure the event is registered as e.  If not it forces it.
	if (!e) var e = window.event;
 	//	e.target works for Mozilla
	if(e.target){
		var target = e.target;
		target.style.backgroundColor="#FFFFFF";
	}
	//	e.srcElement works for IE
	else{
		var target = e.srcElement;
		target.style.backgroundColor="#FFFFFF";
	}
}

//	onClick function for the calendar
function onMonth(e){
	//	Makes sure the event is registered as e.  If not it forces it.
	if (!e) var e = window.event;
	//	e.target works for Mozilla
	if(e.target){
		var target = e.target;
	}
	//	e.srcElement works for IE
	else{
		var target = e.srcElement;
	}
	
	selectMonth(target);	
}

//	This is fired when the onMonth function is activated.
function selectMonth(target){
	//	Get the month number and the year number from the div id that is passed.
	var monthid = target.id.substring(1, 3);
	var yearid = target.id.substring(3, 7);
	
	var targetContainerId = "div" + target.id;
	var targetConatiner = document.getElementById(targetContainerId);
	//alert(targetContainer);
	
	// jQuery does all the real work now.
	$.post("/inc/rpc.php", {queryString: ""+target.id+""}, function(data){
			if(data.length > 0) {
				//	Takes data from rpc.php and inserts it in to the month div
				$('#calendar_events').html(data);
				
				//	Changes the month name header according to the selected month
				$('#calendar_month_heading').html(monthNames[monthid - 1] + " " + yearid);
				//	Changes the background color of the selected div.
				$('#'+target.id).css('background-color', '#9dcc52');
				$('#h3'+target.id).css('color', '#ffffff');
				
				//	This is the part of the code that removed the event listeners from the selected month and adds them back to the previously selected month.
				if(document.getElementById(selectedMonth)){
					var oldLink = document.getElementById(selectedMonth);
					$('#' + selectedMonth).css('background-color', '#ffffff');
					$('#h3' + selectedMonth).css('color', '#9dcc52');
				}
				//	FOR MOZILLA	
				if(target.addEventListener){
					//	Removes listeners from current selection
					target.removeEventListener('mouseover', overMonth, false);
					target.removeEventListener('mouseout', offMonth, false);	
					target.removeEventListener('click', onMonth, false);	
					//	Adds listeners to previous selection
					if(document.getElementById(selectedMonth)){
						oldLink.addEventListener('mouseover', overMonth, false);
						oldLink.addEventListener('mouseout', offMonth, false);	
						oldLink.addEventListener('click', onMonth, false);	
					}
				}
				//	FOR IE				
				else{
					//	Removes listeners from current selection
					target.detachEvent('onmouseover', overMonth);
					target.detachEvent('onmouseout', offMonth);
					target.detachEvent('onclick', onMonth);
					//	Adds listeners to previous selection
					if(document.getElementById(selectedMonth)){
						oldLink.attachEvent('onmouseover', overMonth);
						oldLink.attachEvent('onmouseout', offMonth);
						oldLink.attachEvent('onclick', onMonth);
					}
				}
				//	Lets script know what the new selectedMonth is.
				selectedMonth = target.id;
				
			}
	});
}


$(document).ready(function() {
	init();
});




