/* Copyright 2012 Virtual Interconnect Software, LLC.  All rights reserved. */
function FXCalendarMonthAnimateGetElementsByClassName(el, classNames) {
				if(el.getElementsByClassName)
				{
					return el.getElementsByClassName(classNames);
				}
				else
				{
					var names = classNames.split(" ");
					var allEls = el.getElementsByTagName("*");
					var outEls = [];
					for(var e = 0; e < allEls.length; ++e)
					{
						if(allEls[e].className)
						{
							var noMatch = false;
							for(var c = 0; c < names.length && !noMatch; ++c)
							{
								if((" " + allEls[e].className + " ").indexOf(" " + names[c] + " ") <= 0)
								{
									noMatch = true;
								}
							}
							if(!noMatch)
							{
								outEls.push(allEls[e]);
							}
						}
					}
					return outEls;
				}
			}

function FXCalendarMonthAnimateGetContainingCalendar(el)
{
	var curEl = el;
	while(curEl && (" " + curEl.className + " ").indexOf(" Calendar ") < 0)
	{
		curEl = curEl.parentNode;
	}
	return curEl;
}

function FXCalendarMonthAnimateMove()
{
	this.animationTimerFunc();
}

function FXCalendarMonthAnimateMoveLeft()
{
	//calculate how far to move
	var movePixels = 1;
	var rightest = null;
	for(var c = 0; c < this.childNodes.length; ++c)
	{
		if(this.childNodes[c].style)
		{
			if(!rightest || this.childNodes[c].offsetLeft > rightest.offsetLeft)
			{
				rightest = this.childNodes[c];
			}
		}
	}
	var phase = (this.parentNode.clientWidth - rightest.offsetLeft) * 3.14159265 / this.parentNode.clientWidth;
	movePixels = Math.sin(phase) * 50 + Math.cos(phase / 2) * 15 + 1;
	
	//move
	for(var c = 0; c < this.childNodes.length; ++c)
	{
		if(this.childNodes[c].style)
		{
			this.childNodes[c].style.left = (this.childNodes[c].offsetLeft - movePixels) + "px";
		}
	}
	
	//see if we're done moving
	if(rightest && rightest.offsetLeft <= 0)
	{
		rightest.style.position = "relative";
		rightest.style.left = 0;
		rightest.style.top = 0;
		for(var c = 0; c < this.childNodes.length; ++c)
		{
			if(this.childNodes[c] != rightest)
			{
				this.removeChild(this.childNodes[c]);
				--c;
			}
		}
		var calendarEl = FXCalendarMonthAnimateGetContainingCalendar(rightest);
		calendarEl.parentNode.style.height = "auto";
		calendarEl.parentNode.style.overflow = "visible";
		this.animationTimerFunc = null;
	}
}

function FXCalendarMonthAnimateMoveRight()
{
	//calculate how far to move
	var movePixels = 1;
	var leftest = null;
	for(var c = 0; c < this.childNodes.length; ++c)
	{
		if(this.childNodes[c].style)
		{
			if(!leftest || this.childNodes[c].offsetLeft < leftest.offsetLeft)
			{
				leftest = this.childNodes[c];
			}
		}
	}
	var phase = (this.parentNode.clientWidth + leftest.offsetLeft) * 3.14159265 / this.parentNode.clientWidth;
	movePixels = Math.sin(phase) * 50 + Math.cos(phase / 2) * 15 + 1;
	
	//move
	for(var c = 0; c < this.childNodes.length; ++c)
	{
		if(this.childNodes[c].style)
		{
			this.childNodes[c].style.left = (this.childNodes[c].offsetLeft + movePixels) + "px";
		}
	}
	
	//see if we're done moving
	if(leftest && leftest.offsetLeft >= 0)
	{
		leftest.style.position = "relative";
		leftest.style.left = 0;
		leftest.style.top = 0;
		for(var c = 0; c < this.childNodes.length; ++c)
		{
			if(this.childNodes[c] != leftest)
			{
				this.removeChild(this.childNodes[c]);
				--c;
			}
		}
		var calendarEl = FXCalendarMonthAnimateGetContainingCalendar(leftest);
		calendarEl.parentNode.style.height = "auto";
		calendarEl.parentNode.style.overflow = "visible";
		this.animationTimerFunc = null;
	}
}

function FXCalendarMonthAnimateFetchMonth()
{
	//figure out which month to fetch
	var linkEl = this;
	RequestXML(linkEl.href + "&Format=month&FX=MonthAnimate", function(xmlRoot) {
				//add the new month to the end of the display for the current month
				var calendarEl = FXCalendarMonthAnimateGetContainingCalendar(linkEl);
				var containerEl = calendarEl.parentNode;
				var divEl = xmlRoot.firstChild.firstChild;
				
				//save the container size so it doesn't change when we add the new month
				containerEl.style.height = containerEl.clientHeight + "px";
				containerEl.style.overflow = "hidden";
				
				//import the new month into the current document
				var newMonthEl = calendarEl.parentNode.appendChild(document.importNode(divEl.removeChild(divEl.firstChild), true));
				
				//apply FX
				if(FXCalendarStackEvents)
				{
					FXCalendarStackEvents();
				}
				if(FXCalendarEventPreview)
				{
					FXCalendarEventPreview();
				}
				if(FXCalendarDayDetail)
				{
					FXCalendarDayDetail();
				}
				
				//activate the animation system on the new buttons
				FXCalendarMonthAnimate();
				
				//position the new month correctly in relation to this month
				var leftRight = ((" " + linkEl.className + " ").indexOf("PrevMonth") >= 0 ? -1 : 1);
				calendarEl.style.position = "absolute";
				calendarEl.style.left = 0;
				newMonthEl.style.position = "absolute";
				if(leftRight < 0)
				{
					newMonthEl.style.left = -containerEl.clientWidth + "px";
					containerEl.animationTimerFunc = FXCalendarMonthAnimateMoveRight;
				}
				else
				{
					newMonthEl.style.left = containerEl.clientWidth + "px";
					containerEl.animationTimerFunc = FXCalendarMonthAnimateMoveLeft;
				}
				
				if(!containerEl.animationTimer)
				{
					containerEl.animationTimer = setInterval(function() { if(containerEl.animationTimerFunc) { containerEl.animationTimerFunc(); } }, 40);
				}
				
				//remove the current month
				//calendarEl.parentNode.removeChild(calendarEl);
			});
	return false;
}

function FXCalendarMonthAnimate()
{
	//apply a "left/right one month animates the actual calendar without reloading the page" action to the left/right buttons on the calendar
	//getElementsByClassName is used quite extensively since we're processing a microformat, so make sure we have a consistent function to use
	var getElementsByClassName = FXCalendarMonthAnimateGetElementsByClassName;
	
	//first, locate all calendars that we can apply this effect to
	var calendars = getElementsByClassName(document, "CalendarMonth");
	
	//for each calendar, apply the behavior to the left/right buttons
	for(var c = 0; c < calendars.length; ++c)
	{
		var prevMonths = getElementsByClassName(calendars[c], "PrevMonth");
		for(var p in prevMonths)
		{
			prevMonths[p].onclick = FXCalendarMonthAnimateFetchMonth;
		}
		var nextMonths = getElementsByClassName(calendars[c], "NextMonth");
		for(var n in nextMonths)
		{
			nextMonths[n].onclick = FXCalendarMonthAnimateFetchMonth;
		}
	}
}

RegisterInit("FXCalendarMonthAnimate();");

