var activeMonth = "oct08";
var months_in_order = new Array('oct08', 'nov08', 'dec08', 'jan09', 'feb09', 'mar09', 'apr09', 'may09', 'jun09', 'jul09', 'aug09', 'sep09', 'oct09', 'nov09', 'dec09', 'jan10', 'feb10');

/* Internet Explorer does not implement indexOf, so if you don't have it, here it is. */
if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function(elt /*, from*/)  {
		var len = this.length >>> 0;
		var from = Number(arguments[1]) || 0;
		from = (from < 0)
			? Math.ceil(from)
			: Math.floor(from);
		if (from < 0)
		from += len;

		for (; from < len; from++) {
			if (from in this &&
				this[from] === elt)
			return from;
		}
		return -1;
	};
}


function moveCurrentMonthPointer() {
	//find the index of the current month
	currentMonthIndex = months_in_order.indexOf(activeMonth);
	//use the monthly array to go from 0 to that index
	for (i=0; i<currentMonthIndex; i++) {
		old_image_name = get(months_in_order[i] + "_month").getElementsByTagName("img")[0].src;
		new_image_name = old_image_name.replace(/month_[a-z]*.gif/, 'month_past.gif');
		get(months_in_order[i] + "_month").getElementsByTagName("img")[0].src = new_image_name;
	}

	//replace the current month
	old_image_name = get(months_in_order[currentMonthIndex] + "_month").getElementsByTagName("img")[0].src;
	new_image_name = old_image_name.replace(/month_[a-z]*.gif/, 'month_current.gif');
	get(months_in_order[currentMonthIndex] + "_month").getElementsByTagName("img")[0].src = new_image_name;

	//replace the rest of them (future months, unless our current month is february '10
	if (currentMonthIndex!=16) {
		for (i=currentMonthIndex+1; i<17; i++) {
			old_image_name = get(months_in_order[i] + "_month").getElementsByTagName("img")[0].src;
			new_image_name = old_image_name.replace(/month_[a-z]*.gif/, 'month_future.gif');
			get(months_in_order[i] + "_month").getElementsByTagName("img")[0].src = new_image_name;
		}
	}
}

function showHoverArrow(arrow_side) {
	if (!(activeMonth=='oct08' && arrow_side=='left') && !(activeMonth=='feb10' && arrow_side=='right')) {
		old_image_name = get(arrow_side + "_ha").src;
		new_image_name = old_image_name.replace("arrow_" + arrow_side + '.gif', "arrow_" + arrow_side + "_hover.gif");
		get(arrow_side + "_ha").src = new_image_name;
	}
}

function hideHoverArrow(arrow_side) {
	old_image_name = get(arrow_side + "_ha").src;
	new_image_name = old_image_name.replace("arrow_" + arrow_side + "_hover.gif", "arrow_" + arrow_side + '.gif');
	get(arrow_side + "_ha").src = new_image_name;
}

function switchActiveMonth(id) {
	//alert("active month:" + activeMonth);
	if (id!=activeMonth) {
		hide(activeMonth);
		if (id!="") {
			show(id);
			activeMonth = id;
			moveCurrentMonthPointer();
			//if you continue to hover over the arrow after panning over, disable the appropriate arrow
			if (activeMonth=='oct08') { hideHoverArrow('left'); }
			if (activeMonth=='feb10') { hideHoverArrow('right'); }
		}
	}
}

function backOneMonth() {
	if ((activeMonth) != 'oct08') {
		nextMonth = months_in_order.indexOf(activeMonth) - 1;
		switchActiveMonth(months_in_order[nextMonth]);
	}
}

function aheadOneMonth() {
	if ((activeMonth) != 'feb10') {
		nextMonth = months_in_order.indexOf(activeMonth) + 1;
		switchActiveMonth(months_in_order[nextMonth]);
	}
}

function startCalendar() {
	//provide the prefix to the month that we're going to start with.
	var m = new Array("jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec");
	var d = new Date();
	currentMonth = m[d.getMonth()] + (d.getFullYear()+"").substr(2,2);
	switchActiveMonth(currentMonth);
}
//this encapsulates the call to show the hover element. It retrieves the text from that month's <h2> tag and sends it to the hover function.
function showMonthHoverElement(event, monthToLoad) { 
	if (monthToLoad != "") {
		var hovertext = "";
		if (monthToLoad == 'special') {
			hovertext = "Special news this month.";
		} else {
			hovertext = document.getElementById(monthToLoad + "title").innerHTML;
		}
		elementAsHover.doHoverByEvent(event, 'calHov', hovertext);
	}
}
