//var theCalendar = new homeCalendar();
var monthArray = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
//constructor function for a new calendar object
function getMonth(name) {
	monthObj = new Object;
		monthObj['January'] = 0;
		monthObj['February'] = 1;
		monthObj['March'] = 2;
		monthObj['April'] = 3;
		monthObj['May'] = 4;
		monthObj['June'] = 5;
		monthObj['July'] = 6;
		monthObj['August'] = 7;
		monthObj['September'] = 8;
		monthObj['October'] = 9;
		monthObj['November'] = 10;
		monthObj['December'] = 11;
	return monthObj[name];
}

//scan the currently loaded home calendar and convert all onclick events into actualy event listeners
theCells = document.getElementById('homeCalendar').getElementsByTagName('TABLE')[0].getElementsByTagName('TD');
for(i=0;i<theCells.length;i++) {
	setListener = function(num) {
	if(theCells[num].className && theCells[num].className == 'event') {
		var thisAnchor = theCells[num].firstChild;
		if(thisAnchor.addEventListener) {
			var theListenerString = thisAnchor.getAttribute('onclick').match(/(.*)\((.*)\);?/);
			var funcName = theListenerString[1];
			var funcArgs = theListenerString[2];
			
			var theFunctionString = funcName + "(e," + funcArgs + ");";
			thisAnchor.addEventListener('click',function(e) { eval(theFunctionString); },false);
			thisAnchor.removeAttribute('onclick');
			
		} else {
			var theListenerString = String(thisAnchor.getAttribute('onclick')).replace(new RegExp( "\\n", "g" ),'');
			theListenerString = theListenerString.replace('function onclick(){','');
			var theListenerString = theListenerString.match(/(.*)\((.*)\);?/);
			var funcName = theListenerString[1];
			var funcArgs = theListenerString[2];
			
			var theFunctionString = funcName + "(e," + funcArgs + ");";
			thisAnchor.attachEvent('onclick',function(e) { eval(theFunctionString); });
		}
	}
	}(i);
}

function homeCalendar() {
	var scope = this;
	this.calElem = document.getElementById('homeCalendar').getElementsByTagName('TABLE')[0];	
	this.monthElem = document.getElementById('homeCalendar').getElementsByTagName('SPAN')[1];
	this.yearElem = document.getElementById('homeCalendar').getElementsByTagName('SPAN')[2];
	this.month = getMonth(this.monthElem.innerHTML);
	this.year = Number(this.yearElem.innerHTML);
	this.animateMonth; //intervals for changing
	this.animateYear; //the month and the year
	this.animateCalendar; //and the calendar
	this.calBody = document.getElementById('homeCalendarBody');
	this.animating = false;
	this.next = false;
	this.prev = false;
	this.type = document.getElementById('homeCalendar').getElementsByTagName('A')[0].href.match(/type\=([a-z]+)#/)[1];
	
	this.loadMonth = function(which,curMonth,curYear) {
		var queryString = new Object();
		queryString['which'] = which;
		
		if(which == 'next') {
			if(curMonth == 11) {
				queryString['month'] = 1;
				queryString['year'] = curYear+1;
			} else {
				queryString['month'] = curMonth+2; //php and javascript start at different indices
				queryString['year'] = curYear;
			}
		} else if(which == 'prev') {
			if(curMonth == 0) {
				queryString['month'] = 12;
				queryString['year'] = curYear-1;
			} else {
				queryString['year'] = curYear;
				queryString['month'] = curMonth; //php and javascript start months at different indices
			}
		}
		
		
		queryString['type'] = this.type;
		
		var ajaxCalReq = GetXmlHttpObject();
		url = prepareURL('php/scripts/calendar.php',queryString,true);
		ajaxCalReq.open('GET',url,true);
		ajaxCalReq.onreadystatechange = function() {
			if(ajaxCalReq.readyState == 4 && ajaxCalReq.status == 200) {
				xml = ajaxCalReq.responseXML.documentElement;
				which = xml.attributes[0].nodeValue;
				var thisTbody = document.createElement('TBODY');
				thisTbody.id = which+'HomeCalendarBody';
				convertToHtml(xml,thisTbody);
				document.getElementById('homeCalendarBody').parentNode.appendChild(thisTbody);
			}
		}
		ajaxCalReq.send(null);
	}
	
	this.loadMonth('next',scope.month,scope.year,scope.type);
	setTimeout(function() { scope.loadMonth('prev',scope.month,scope.year); },1000); //this is crucial (at least for localhost) - 2 very fast calls to the server screws it up
	
	//change the necessary style properties
	this.monthElem.parentNode.style.height = this.monthElem.parentNode.offsetHeight + 'px';
	if(this.calBody.addEventListener) {
		this.calBody.parentNode.style.opacity = '1';
	} else {
		this.calBody.parentNode.style.filter = 'alpha(opacity=100)';
	}
	
	//add event listeners to the next and previous links
	allCalLinks = document.getElementById('homeCalendar').getElementsByTagName('A');
	for(i=0;i<allCalLinks.length;i++) {
		if(allCalLinks[i].className) {
			if(allCalLinks[i].className == 'next' || allCalLinks[i].className == 'prev') {
				if(allCalLinks[i].addEventListener) {
					allCalLinks[i].addEventListener('click',function(e) { scope.changeMonth(e,this.innerHTML.toLowerCase()); },false);
				} else {
					if(allCalLinks[i].className == 'next') {
						allCalLinks[i].attachEvent('onclick',function(e) { scope.changeMonth(e,'next'); });
					} else {
						allCalLinks[i].attachEvent('onclick',function(e) { scope.changeMonth(e,'previous'); });
					}
				}
			} else if(allCalLinks[i].className == 'current') {
				this.currentAnchor = allCalLinks[i];
			}
		}
	}
}

homeCalendar.prototype.changeMonth = function(e,which) {
	myPreventDefault(e);
	
	var scope = this;
	if(this.animating == false) {
	
		if(which == 'previous') {
			this.prev = true;
			if(this.month == 0) {
				this.month = 11;
				this.year = this.year-1;
				this.yearElem.innerHTML = this.year + '<br>' + this.yearElem.innerHTML;
				this.yearElem.style.top = -this.yearElem.offsetHeight/2+'px';
				this.prev = true;
				this.animateYear = setInterval(function() { scope.animateYearSpan(0,scope.yearElem,scope.animateYear,scope.year); },10);
			} else {
				this.month--;
			}
			this.monthElem.style.top = -this.monthElem.offsetHeight+'px';
			this.monthElem.innerHTML = monthArray[this.month]+'<br>'+this.monthElem.innerHTML;
			this.animateMonth = setInterval(function() { scope.animateMonthSpan(0,scope.monthElem,scope.animateMonth,monthArray[scope.month]); },10);
		} else {
			this.next = true;
			if(this.month == 11) {
				this.month = 0;
				this.year = this.year+1;
				yearEndPos = -this.yearElem.offsetHeight;
				this.yearElem.innerHTML += '<br>'+this.year;
				this.yearElem.style.top = '0';
				this.next = true;
				this.animateYear = setInterval(function() { scope.animateYearSpan(yearEndPos,scope.yearElem,scope.animateYear,scope.year); },10);
			} else {
				this.month++;		
			}
			monthEndPos = -this.monthElem.offsetHeight;
			this.monthElem.innerHTML += '<br>'+monthArray[this.month];
			this.monthElem.style.top = '0';
			this.animateMonth = setInterval(function() { scope.animateMonthSpan(monthEndPos,scope.monthElem,scope.animateMonth,monthArray[scope.month]); },10);
		}
		
		
		//fade the calendar
		this.animateCalendar = setInterval(function() { scope.animateCalendarArea('out',scope.animateCalendar); },10);
		
	}
}

homeCalendar.prototype.animateMonthSpan = function(endPos,element,interval,finalHTML) {
	this.animating = true;
	//move it in a negative direction
	if(endPos < 0) {
		if(parseInt(element.style.top) <= endPos) {
			clearInterval(interval);
			this.animating = false;
			element.innerHTML = finalHTML;
			element.style.top = 0;
		} else {
			element.style.top = parseInt(element.style.top)-0-1+'px';
		}
	} else {
	//move it in a positive direction
		if(parseInt(element.style.top) >= endPos) {
			clearInterval(interval);
			this.animating = false;
			element.innerHTML = finalHTML;
		} else {
			element.style.top = parseInt(element.style.top)-0+1+'px';
		}
	
	}
}

homeCalendar.prototype.animateYearSpan = function(endPos,element,interval,finalHTML) {
	this.animating = true;
	//move it in a negative direction
	if(endPos < 0) {
		if(parseInt(element.style.top) <= endPos) {
			clearInterval(interval);
			this.animating = false;
			element.innerHTML = finalHTML;
			element.style.top = 0;
		} else {
			element.style.top = parseInt(element.style.top)-0-1+'px';
		}
	} else {
	//move it in a positive direction
		if(parseInt(element.style.top) >= endPos) {
			clearInterval(interval);
			this.animating = false;
			element.innerHTML = finalHTML;
		} else {
			element.style.top = parseInt(element.style.top)-0+1+'px';
		}
	
	}
}

homeCalendar.prototype.animateCalendarArea = function(direction,interval) {
	this.animating = true;
	var scope = this;
	
	opacityFix = this.calBody.parentNode;
	
	if(opacityFix.addEventListener) {
		//W3C compliant
		if(direction == 'in') {
			if(opacityFix.style.opacity >= 1) {
				clearInterval(interval);
				this.animating = false;
			} else {
				opacityFix.style.opacity = opacityFix.style.opacity-0+0.1;
			}
		} else {
			if(opacityFix.style.opacity == 0) {
				//fade out, set the id of this element to nextHomeCalendarBody or prevHomeCalendarBody depending on which link was pushed
				if(this.next == true) {
					//the next link was pushed so
					//1 - remove the current 'prevHomeCalendarBody'
					//2 - set this.calBody.id = 'prevHomeCalendarBody'
					//3 - set this.calBody = document.getElementById('nextHomeCalendarBody')
					//4 - set this.calBody.id = 'homeCalendarBody';
					//5 - do this.loadMonth('next');
					this.loadMonth('next',scope.month,scope.year);
					this.calBody.parentNode.removeChild(document.getElementById('prevHomeCalendarBody'));
					this.calBody.id = 'prevHomeCalendarBody';
					this.calBody = document.getElementById('nextHomeCalendarBody');
					this.next = false;
				} else {
					//the prev link was pushed so we need to load up a new previous month
					//1 - remove the current 'nextHomeCalendarBody'
					//2 - set this.calBody.id = 'nextHomeCalendarBody'
					//3 - set this.calBody = document.getElementById('prevHomeCalendarBody');
					//4 - set this.calBody.id = 'homeCalendarBody';
					//5 - do this.loadMonth('prev')
					this.calBody.parentNode.removeChild(document.getElementById('nextHomeCalendarBody'));
					this.loadMonth('prev',scope.month,scope.year);
					this.calBody.id = 'nextHomeCalendarBody';
					this.calBody = document.getElementById('prevHomeCalendarBody');
					this.prev = false;
				}
				clearInterval(interval);
				this.animating = false;
				this.calBody.id = 'homeCalendarBody';
				this.animateCalendar = setInterval(function() { scope.animateCalendarArea('in',scope.animateCalendar); },10);
			} else {
				opacityFix.style.opacity-=0.1;
			}
		}
	} else {
		//IE
		currentFilterOpacity = parseInt(opacityFix.style.filter.split('=')[1]);
		if(direction == 'in') {
			if(currentFilterOpacity >= 100) {
				clearInterval(interval);
				this.animating = false;
			} else {
				newFilterOpacity = currentFilterOpacity + 10;
				opacityFix.style.filter = 'alpha(opacity='+newFilterOpacity+')';
			}
		} else {
			if(currentFilterOpacity == 0) {
				//fade out, set the id of this element to nextHomeCalendarBody or prevHomeCalendarBody depending on which link was pushed
				if(this.next == true) {
					//the next link was pushed so
					//1 - remove the current 'prevHomeCalendarBody'
					//2 - set this.calBody.id = 'prevHomeCalendarBody'
					//3 - set this.calBody = document.getElementById('nextHomeCalendarBody')
					//4 - set this.calBody.id = 'homeCalendarBody';
					//5 - do this.loadMonth('next');
					this.loadMonth('next',scope.month,scope.year);
					this.calBody.parentNode.removeChild(document.getElementById('prevHomeCalendarBody'));
					this.calBody.id = 'prevHomeCalendarBody';
					this.calBody = document.getElementById('nextHomeCalendarBody');
					this.next = false;
				} else {
					//the prev link was pushed so we need to load up a new previous month
					//1 - remove the current 'nextHomeCalendarBody'
					//2 - set this.calBody.id = 'nextHomeCalendarBody'
					//3 - set this.calBody = document.getElementById('prevHomeCalendarBody');
					//4 - set this.calBody.id = 'homeCalendarBody';
					//5 - do this.loadMonth('prev')
					this.calBody.parentNode.removeChild(document.getElementById('nextHomeCalendarBody'));
					this.loadMonth('prev',scope.month,scope.year);
					this.calBody.id = 'nextHomeCalendarBody';
					this.calBody = document.getElementById('prevHomeCalendarBody');
					this.prev = false;
				}
				clearInterval(interval);
				this.animating = false;
				this.calBody.id = 'homeCalendarBody';
				this.animateCalendar = setInterval(function() { scope.animateCalendarArea('in',scope.animateCalendar); },10);
			} else {
				newFilterOpacity = currentFilterOpacity - 10;
				opacityFix.style.filter = 'alpha(opacity='+newFilterOpacity+')';
			}
		}
	}
}