// import other useful files
document.write('<script src="js/cookies.js" type="text/javascript"></script>');
document.write('<script src="js/searchtoggle.js" type="text/javascript"></script>');
document.write('<script src="js/popups.js" type="text/javascript"></script>');
document.write('<script src="js/adbanner.js" type="text/javascript"></script>');

function init() {
	if (arguments.callee.done) return; // quit if this function has already been called
	arguments.callee.done = true; // flag this function so called only once
	// do stuff
	// set up tabs
	initTabs();
	// set up user's search preference
	initSearch();
	// preload the nav+funder graphics
	preloadNav();
	// preload the calendar graphics
	// preloadCal();
	// replacement calendar (must .start() before popup init)
	ARCalendar.start(todaysdate,'replacement_cal');
	// set up any popup links on the page
	initPopups();
	// put rollovers on funder images
	initFunders();
	// initialize subnav rolovers (uses jquery)
	initSubnav();
	
	this.name = "ar_main";
}

// for Mozilla based browsers
if (document.addEventListener) {
   document.addEventListener("DOMContentLoaded", init, null);
}

// for Internet Explorer
/*@cc_on @*/
/*@if (@_jscript)
   document.write('<script type="text/javascript" defer src="/wgbh/roadshow/js/iedomloaded.js"></script>');
/*@end @*/

/* for other browsers */
window.onload = init;


function preloadNav() {
	if (document.images) {
		var navImgs = new Array();
		for (var i=0; i< 7; i++) {
			navImgs[i] = new Image();
		}
	navImgs[0].src = "homeimages/nav_ontv_over.jpg";
	navImgs[1].src = "homeimages/nav_ontheroad_over.jpg";
	navImgs[2].src = "homeimages/nav_online_over.jpg";
	navImgs[3].src = "homeimages/nav_resources_over.jpg";
	navImgs[4].src = "homeimages/header_libertymutual_over.jpg";
	navImgs[5].src = "homeimages/header_subaru_over.jpg";
	navImgs[6].src = "homeimages/header_ancestry_over.jpg";
	}
}

function preloadCal() {
	if (document.images) {
		var calImgs = new Array();
		for (var i=0; i<8; i++) {
			calImgs[i] = new Image();
		}
	calImgs[0].src = "homeimages/calendar_200801.gif";
	calImgs[1].src = "homeimages/calendar_200802.gif";
	calImgs[2].src = "homeimages/calendar_200803.gif";
	calImgs[3].src = "homeimages/calendar_200804.gif";
	calImgs[4].src = "homeimages/calendar_200805.gif";
	calImgs[5].src = "homeimages/calendar_200806.gif";
	calImgs[6].src = "homeimages/calendar_200807.gif";
	calImgs[7].src = "homeimages/calendar_200808.gif";
	}
}


function initTabs() {
	var tabs = new Array();
	tabs = document.getElementById("tabs").getElementsByTagName("li");
	for (var i=0; i< tabs.length; i++) {
		tabs[i].onclick = function() { showTab(this); return false; }
	}
}
/*
function showTab(obj) {
	var tabs = new Array();
	tabs = document.getElementById("tabs").getElementsByTagName("li");
	for (var i=0; i< tabs.length; i++ ) {
		var hour = "hour" + (i+1);
		if (tabs[i].id == obj.id) {
			tabs[i].setAttribute((document.all ? 'className' : 'class'), 'active');
			document.getElementById(hour).style.display = "block";
		} else {
			tabs[i].setAttribute((document.all ? 'className' : 'class'), '');
			document.getElementById(hour).style.display = "none";
		}
	}
}
*/

// 20091001: addded fade transition (ought to rebuild function w/ jQuery)
// msmith
function showTab(obj) {
	var tabs = new Array();
	tabs = jQuery("#tabs").find("li");
	for (var i=0; i< tabs.length; i++ ) {
		var hour = "#hour" + (i+1);
		if (tabs[i].id == obj.id) {
			tabs[i].setAttribute((document.all ? 'className' : 'class'), 'active');
			$(hour).fadeIn('slow');
		} else {
			tabs[i].setAttribute((document.all ? 'className' : 'class'), '');
			$(hour).fadeOut('fast');
		}
	}
}

function initFunders() {
	var funders = new Array();
	funders = document.getElementsByTagName("img");
	for (var i=0; i<funders.length; i++) {
		if (funders[i].className == 'funder') {
			funders[i].onmouseover = function() { this.src = this.src.replace(/\.jpg/,"_over.jpg").replace(/\.gif/,"_over.gif");
			};
			funders[i].onmouseout = function() { this.src = this.src.replace(/_over/,''); };
		}
	}
}

function swapCal(direction) {
	var months = new Array();
	var currDate;
	var newDate;
	months = document.getElementById("leftcol").getElementsByTagName("div");
	for ( var i=0; i < months.length; i++) {
		if (months[i].className == "now") {
			currDate = months[i].id.substr(9);
		}
	}
	if (direction == "next") {   // this is the date math section.  it will not successfully deal with year boundaries right now.
		newDate = Number(currDate) + 1;
	} else if (direction == "prev") {
		newDate = Number(currDate) - 1;
	}
	document.getElementById("calendar_" + newDate).setAttribute((document.all ? 'className' : 'class'), 'now');
	document.getElementById("calendar_" + currDate).setAttribute((document.all ? 'className' : 'class'), 'calbox');
}

function hideSubnav() {
$('#navlist > li').each(function(){
		$(this).removeClass('sfhover');
	});
}

function initSubnav() {
	 // hide subnav as mouse leaves header
	 $('#header').bind('mouseleave', function() {
	 		hideSubnav();
	 	});

	 // toggle the appropriate subnav list
	 $('#nav_ontv').bind('mouseover', function() {
	   hideSubnav();
	   $('#navlist > li:eq(0)').addClass('sfhover');
	 });
	 $('#nav_ontheroad').bind('mouseover', function() {
	   hideSubnav();
	   $('#navlist > li:eq(1)').addClass('sfhover');
	 });
	 $('#nav_online').bind('mouseover', function() {
	   hideSubnav();
	   $('#navlist > li:eq(2)').addClass('sfhover');
	 });
	 $('#nav_resources').bind('mouseover', function() {
	   hideSubnav();
	   $('#navlist > li:eq(3)').addClass('sfhover');
	 });
}