// This file is referenced on every page -except- the homepage

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


// Initialization routine for all pages with blown up subnav
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
	initSubNav();
	// if fts index page, initialize image grid
	if (document.getElementById("index") && window.location.pathname.search(/fts/) != -1 || document.getElementById("index") && window.location.pathname.search(/tips/) != -1) {
		initImageGrid();
	}
	// if yourstories index page, initialize tabs
	if (document.getElementById("index") && window.location.pathname.search(/yourstories/) != -1) {
		initStories();
	}
	// if teachers guide page, initialize subnav
	if (window.location.pathname.search(/teachers_/) != -1) {
		initRoll();
	}
	// set up user's search preference
	initSearch();
	// set up any popup links on the page
	initPopups();
	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;


// Initialization routine for 4 sub-index pages
function initSubIndex() {
	initSubNav();
	// set up user's search preference
	initSearch();
	//set up any popup links on the page
	initPopups();
	this.name = "ar_main";
	// if it's the teacher's index, initialize the left nav
	if (window.location.pathname.search(/teachers.html/) != -1) {
		initRoll();
	}
	// if it's the on the road page, preload the map graphics
//	if (window.location.pathname.search(/ontheroad.html/) != -1) {
//		initMapRoll();
//	}
}


function initSubNav() {
	// put rollover on archive image
	if(document.getElementById("archive")) {
		var archive = document.getElementById("archive");
		archive.onmouseover = swap;
		archive.onmouseout = swap;
	}
	// set up subnav "stickiness"
	if(document.getElementById("navlist")) {
		var list = document.getElementById("navlist");
			list.onmouseover = clearNavState;
			list.onmouseout = setNavState;
	}
	// initialize subnav
	sfHover();
	setNavState();
}




function setNavState() {
	if(section != "") {
		var livesection = document.getElementById(section);
		var livesection_bg = "background:url('/wgbh/roadshow/images/subnav_" + section + "_over.gif');";
	//	livesection.setAttribute('style', livesection_bg);
		livesection.style.cssText = livesection_bg;
		var livesection_nav = document.getElementById("sub_" + section);
	//	livesection_nav.setAttribute('style','left:0px;');	
		livesection_nav.style.cssText = "left:0px;";
	}
}

function clearNavState() {
	if(section != "") {
		var livesection = document.getElementById(section);
	//	livesection.removeAttribute('style');
		livesection.style.cssText = "";
		var livesection_nav = document.getElementById("sub_" + section);
	//	livesection_nav.removeAttribute('style');
		livesection_nav.style.cssText = "";
	}
}

function swap() {
	var pat = /_over/;
	var found = this.src.match(pat);
	if(found) {
		this.src = this.src.replace(/_over\.gif/,".gif");
	} else { 
		this.src = this.src.replace(/\.gif/,"_over.gif");
	}
}


/**	nav for ie 	**/
/** cribbed from dbulli **/
function sfHover() {
	if (document.getElementById("navlist")) {
		var sfEls = document.getElementById("navlist").getElementsByTagName("li");
		for (var i=0; i<sfEls.length; i++) {
			if (sfEls[i].className != "leftbracket" && sfEls[i].className != "rightbracket") {
				sfEls[i].onmouseover=function() {
					this.className+=" sfhover";
				}
				sfEls[i].onmouseout=function() {
					this.className=this.className = "";
				}
			}
		}
	}
// added to deal with "share" control
	if(document.getElementById("share")) {
		var share = document.getElementById("share");
		share.onmouseover= function() { this.className+=" sfhover"; }
		share.onmouseout=function() { this.className=this.className = ""; }
	}
}


// tips/fts page font-size controls
function resize(size) {
	switch(size)
	{
	case "small":
		document.getElementById("main").className = "";
		break;
	case "med":
		document.getElementById("main").className = "med";
		break;
	case "large":
		document.getElementById("main").className = "large";
		break;
	default:
		break;
	}
}


// tips/fts index right column category display
function initImageGrid() {
	var cat_links = document.getElementById("catcol").getElementsByTagName("a");
	for(var i=0;i<cat_links.length;i++) {
		cat_links[i].onclick = function() { displayCategory(this.id.replace(/cat_/,"")); return false; }
	}
	document.getElementById('picks').setAttribute((document.all ? 'className' : 'class'),'boxed');	
}	
	
function displayCategory(catname) {
	var displaycol;
	if (window.location.pathname.search(/fts/) != -1) {
		displaycol = "pickscol";
	} else if (window.location.pathname.search(/tips/) != -1) {
		displaycol = "tipscol";
	}
	var cat_div = document.getElementById(displaycol).getElementsByTagName("div");
	for(var i=0; i<cat_div.length; i++) {
		document.getElementById(cat_div[i].id).setAttribute((document.all ? 'className' : 'class'),'');
		if (cat_div[i].id == catname) {
			document.getElementById(cat_div[i].id).setAttribute((document.all ? 'className' : 'class'),'boxed');
		}
	}
}


// your stories tab initialization
var active; // = the currently displayed chunk (i.e. cityname + digit from 1 to 3)
function initStories() {
	var tabs = document.getElementById("tabbar").getElementsByTagName("li"); // get all the tabs
	// figure out which tab is active
	for (var j=0; j< tabs.length; j++) {
		if (tabs[j].className == "active") {
			active = tabs[j].id.substr(tabs[j].id.search(/_/) + 1) + "1";
		}		
	}
	var divs = document.getElementById("storychunk").getElementsByTagName("div"); // get all the divs
	var chunks = new Array(); 
	// pull the hourly divs out of the array of all divs
	for (var i=0; i< divs.length; i++) {
		if (divs[i].className == "six") {
			// hide the hourly divs that are not active
			if(divs[i].id != active) {
				document.getElementById(divs[i].id).setAttribute((document.all ? 'className' : 'class'),'six hidden');
			}
			// add all relevant divs to the chunks array
			chunks.push(divs[i]);
		}
	}
	// hide the previous button
	document.getElementById("prev").setAttribute((document.all ? 'className' : 'class'),'hidden');
	// if there's a next page, show the next button
	if (document.getElementById(active.substr(0,active.search(/\d/)) + "2")) {
		document.getElementById("next").setAttribute((document.all ? 'className' : 'class'),'');
	} else {
		document.getElementById("next").setAttribute((document.all ? 'className' : 'class'),'hidden');
	}
}


function morestories(dir) {
	if (dir == "next" && active.match(/1/)) {
		document.getElementById(active).setAttribute((document.all ? 'className' : 'class'),'six hidden');
		active = active.substr(0,active.search(/\d/)) + "2";
		document.getElementById(active).setAttribute((document.all ? 'className' : 'class'),'six');
		if (document.getElementById(active.substr(0,active.search(/\d/)) + "3")) {
			document.getElementById("prev").setAttribute((document.all ? 'className' : 'class'),'');
			document.getElementById("next").setAttribute((document.all ? 'className' : 'class'),'');
		} else {
			document.getElementById("prev").setAttribute((document.all ? 'className' : 'class'),'');
			document.getElementById("next").setAttribute((document.all ? 'className' : 'class'),'hidden');
		}
	} else if ( dir == "next" && active.match(/2/)) {
		document.getElementById(active).setAttribute((document.all ? 'className' : 'class'),'six hidden');
		active = active.substr(0,active.search(/\d/)) + "3";
		document.getElementById(active).setAttribute((document.all ? 'className' : 'class'),'six');
		document.getElementById("prev").setAttribute((document.all ? 'className' : 'class'),'');
		document.getElementById("next").setAttribute((document.all ? 'className' : 'class'),'hidden');
	} else if ( dir == "prev" && active.match(/2/)) {
		document.getElementById(active).setAttribute((document.all ? 'className' : 'class'),'six hidden');
		active = active.substr(0,active.search(/\d/)) + "1";
		document.getElementById(active).setAttribute((document.all ? 'className' : 'class'),'six');
		document.getElementById("prev").setAttribute((document.all ? 'className' : 'class'),'hidden');
		document.getElementById("next").setAttribute((document.all ? 'className' : 'class'),'');
	} else if ( dir == "prev" && active.match(/3/)) {
		document.getElementById(active).setAttribute((document.all ? 'className' : 'class'),'six hidden');
		active = active.substr(0,active.search(/\d/)) + "2";
		document.getElementById(active).setAttribute((document.all ? 'className' : 'class'),'six');
		document.getElementById("prev").setAttribute((document.all ? 'className' : 'class'),'');
		document.getElementById("next").setAttribute((document.all ? 'className' : 'class'),'');
	}
}

function showstories(city) {
	var citytabs = new Array;
	var activetab;
	citytabs = document.getElementById('tabbar').getElementsByTagName('li'); // determine which tab is active
	for(var i=0; i<citytabs.length; i++) {
		if(citytabs[i].className == "active") {
			activetab = citytabs[i].id.substr(4);
		}
		citytabs[i].removeAttribute((document.all ? 'className' : 'class')); // make all tabs inactive
	}
	if (document.getElementById(activetab + "1")) {
		document.getElementById(activetab + "1").setAttribute((document.all ? 'className' : 'class'),'six hidden');
	}
	if (document.getElementById(activetab + "2")) {
		document.getElementById(activetab + "2").setAttribute((document.all ? 'className' : 'class'),'six hidden');
	}
	if (document.getElementById(activetab + "3")) {
		document.getElementById(activetab + "3").setAttribute((document.all ? 'className' : 'class'),'six hidden');
	}
	document.getElementById("tab_" + city).setAttribute((document.all ? 'className' : 'class'),'active');
	document.getElementById(city + "1").setAttribute((document.all ? 'className' : 'class'),'six');
	document.getElementById("prev").setAttribute((document.all ? 'className' : 'class'),'hidden');
	if (document.getElementById(city + "2")) {
		document.getElementById("next").setAttribute((document.all ? 'className' : 'class'),'');
	} else {
		document.getElementById("next").setAttribute((document.all ? 'className' : 'class'),'hidden');
	}	
	active = city + "1";
}



// city page right hand column tab controls
function showappraisals(hour) {
	var hours = new Array;
	for(var i=1; i<=3; i++) {
		var tempname = "hour" + i;
		hours[i-1] = document.getElementById(tempname);
	}
	var tabs = new Array;
	tabs = document.getElementById('appraisalcol').getElementsByTagName('li');

	for(var j=0; j<tabs.length; j++) {
		hours[j].style.display = "none";
		tabs[j].removeAttribute((document.all ? 'className' : 'class'));
	}
	document.getElementById("hour" + hour).style.display = "block";
	document.getElementById("tab" + hour).setAttribute((document.all ? 'className' : 'class'),'active');
}

function initRoll() {
	if(document.getElementById('tgnav')) {
		var liArr = new Array;
		liArr = document.getElementById('tgnav').getElementsByTagName('li');
		for(var i=0; i< liArr.length; i++) {
			if (liArr[i].className == "on") {
				;
			} else {
				liArr[i].onmouseover = function() { this.firstChild.firstChild.src = this.firstChild.firstChild.src.replace(/.jpg/,"_over.jpg"); }
				liArr[i].onmouseout = function() { this.firstChild.firstChild.src = this.firstChild.firstChild.src.replace(/_over.jpg/,".jpg"); }
			}
		}
	}
}


// ontheroad.html tour map rollovers
function initMapRoll() {
	var mapArray = new Array();
	mapArray[0] = new Image();
	mapArray[1] = new Image();
	mapArray[2] = new Image();
	mapArray[3] = new Image();
	mapArray[4] = new Image();
	mapArray[5] = new Image();

	mapArray[0].src = "images/ontheroad_splash_dallas.jpg";
	mapArray[1].src = "images/ontheroad_splash_palmsprings.jpg";
	mapArray[2].src = "images/ontheroad_splash_wichita.jpg";
	mapArray[3].src = "images/ontheroad_splash_chattanooga.jpg";
	mapArray[4].src = "images/ontheroad_splash_hartford.jpg";
	mapArray[5].src = "images/ontheroad_splash_grandrapids.jpg";
}

function maprollover(city) {
	var mapImg = document.getElementById('splashmap');
	mapImg.src = "images/ontheroad_splash_" + city + ".jpg";
}

function maprollout() {
	var mapImg = document.getElementById('splashmap');
	mapImg.src = "images/ontheroad_splash.jpg";
}