var defaultTheme = "Sky and Space/default";
var cookieNameForTheme = "kidsBuzzTheme=";
var searchTermHilightColorIndex = 0;

/* Used to replace radio buttons with different graphics */
var radioHeight = "25";
var radioClass = "kidsSearchRadioButton";

var radioButton = {
	init: function() {
		var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
		for(a = 0; a < inputs.length; a++) {
			if(inputs[a].type == "radio" && inputs[a].className == radioClass) {
				span[a] = document.createElement("span");
				span[a].className = radioClass;

				if(inputs[a].checked == true) {
					position = "0 -" + (radioHeight*2) + "px";
					span[a].style.backgroundPosition = position;
				}
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				inputs[a].onchange = radioButton.clear;

				span[a].onmousedown = radioButton.pushed;
				span[a].onmouseup = radioButton.check;
			}
		}
		document.onmouseup = radioButton.clear;
	},
	pushed: function() {
		element = this.nextSibling;
		if(element.checked == true ) {
			this.style.backgroundPosition = "0 -" + radioHeight*3 + "px";
		} else {
			this.style.backgroundPosition = "0 -" + radioHeight + "px";
		}
	},
	check: function() {
		element = this.nextSibling;
		this.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
		group = this.nextSibling.name;
		inputs = document.getElementsByTagName("input");
		for(a = 0; a < inputs.length; a++) {
			if(inputs[a].name == group && inputs[a] != this.nextSibling) {
				inputs[a].previousSibling.style.backgroundPosition = "0 0";
			}
		}
		element.checked = true;
	},
	clear: function() {
		inputs = document.getElementsByTagName("input");
		for(var b = 0; b < inputs.length; b++) {
			if(inputs[b].checked == true && inputs[b].className == radioClass) {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
			} else if(inputs[b].className == radioClass) {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			}
		}
	},
	choose: function() {
		option = this.getElementsByTagName("option");
		for(d = 0; d < option.length; d++) {
			if(option[d].selected == true) {
				document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
			}
		}
	}
}

var lampPost = {  /* Mouse over effects for the lamp post flashing on and off */
	over: function() {
		document.getElementById("kidsHomeStreetLamp").style.backgroundImage = 'url("/images/lightPoleTop_.png")';
		document.getElementById("kidsHeading").style.backgroundImage = 'url("/images/kidsHeading_.png")';
	},
	out: function() {
		document.getElementById("kidsHomeStreetLamp").style.backgroundImage = 'url("/images/lightPoleTop.png")';
		document.getElementById("kidsHeading").style.backgroundImage = 'url("/images/kidsHeading.png")';
	},
	init: function() {
		var lampPostElement = new Array(); var e;
		lampPostElement[0] = document.getElementById("kidsHomeStreetLamp");
		lampPostElement[1] = document.getElementById("kidsHeading");
		for(e = 0; e < lampPostElement.length; e++) {
			lampPostElement[e].onmouseover = lampPost.over;
			lampPostElement[e].onmouseout  = lampPost.out;
		}
	}
}

var kidsBuzzTheme = {
	init: function() {
		var cookieStart = document.cookie.indexOf(cookieNameForTheme) + cookieNameForTheme.length
		var theme = defaultTheme;
		if ( cookieStart >= cookieNameForTheme.length) { // if cookie has been set
			cookieStop = document.cookie.indexOf(";",cookieStart);
			if ( cookieStop == -1 ) { cookieStop = document.cookie.length; }
			theme = unescape(document.cookie.substring(cookieStart,cookieStop));
		}
		var linkTag = document.getElementsByTagName("link");
		var l;
		for (l=0; l<linkTag.length; l++) { // disable all alternate links for IE
			if ((linkTag[l].rel.indexOf("alt") != -1) && linkTag[l].title) {
				linkTag[l].disabled = true;
			}
		}
	},
	swap: function(themeName) {
		var linkTag = document.getElementsByTagName("link");
		var l;
		for (l=0; l<linkTag.length; l++) {
			if ((linkTag[l].rel.indexOf("alt") != -1) && linkTag[l].title) {
				linkTag[l].disabled = true;
				if (linkTag[l].title == themeName) { linkTag[l].disabled = false; }
			}
		}
		var cookieExpire=new Date();
		cookieExpire.setDate(cookieExpire.getDate() + "1");
		document.cookie=cookieNameForTheme + escape(themeName) + ";expires=" + cookieExpire.toGMTString() +"; path=/;";
	},
	matchLinkColor: function(elementHandle,handleType,style) { //change color of given style of a element ID/class to match the link color
		var themedLinkColor;
		var themedLink = document.getElementById('kidsMainContent').getElementsByTagName('a')[0];
		if (themedLink.currentStyle)
			themedLinkColor = themedLink.currentStyle['color'];
		else if (window.getComputedStyle)
			themedLinkColor = document.defaultView.getComputedStyle(themedLink,null).getPropertyValue('color');
		if ( handleType == 'id' )	
			eval('document.getElementById(elementHandle).style.' + style + '=themedLinkColor');
		if ( handleType == 'class') {
			var classItems = document.getElementById('kidsMainContent').getElementsByTagName('*');
			for (var e = 0; e < classItems.length; e++) {
				if ( classItems[e].className == elementHandle ) { 
					eval('classItems[e].style.' + style + '=themedLinkColor');
				}
 			}
		}
	}
}

var kidsBuzzSearchTermHighlight = { // based on nsftools SearchAndHighlight
	init: function() {
		// examine queryString for presense of 'mark' value
		var hilightTerms;
		var queryString = window.location.search.substring(1);
		var keyValuePairs = queryString.split('&');
		for (var k=0; k < keyValuePairs.length; k++) {
			var keyValuePair = keyValuePairs[k].split('=');
			if ( keyValuePair[0] == 'mark') { hilightTerms = unescape(keyValuePair[1]).replace(/[ ]+/g,"+"); }
		}
		if (hilightTerms) {
			var hilightTerm = hilightTerms.split('+');
			for (var q=0; q < hilightTerm.length; q++) {
				kidsBuzzSearchTermHighlight.hilight(hilightTerm[q]);
			}
		}
	},
	hilight: function(searchTerm) { 
		// given a term, hilight it in the specified div
		
		searchTermHilightColorIndex++;  //increments global variable to change the hilight color to one of 8 different colors
		if (searchTermHilightColorIndex > 6) { searchTermHilightColorIndex = 1; }
		
		var highlightOpenTag  = '<span class="kidsSearchTerm' + searchTermHilightColorIndex +'">';
		var highlightCloseTag = '</span>';
		
		var contentHTML = document.getElementById('kidsMainContent').innerHTML; // hilight only terms in this div
		
		var newHTML = "";
		var i = -1;
		var lcSearchTerm  = searchTerm.toLowerCase();
		var lcContentHTML = contentHTML.toLowerCase();
	    
		while (contentHTML.length > 0) {
			i = lcContentHTML.indexOf(lcSearchTerm, i+1);
			if (i < 0) {
				newHTML += contentHTML;
				contentHTML = "";
			} else {
				// skip anything inside an HTML tag
				if (contentHTML.lastIndexOf(">", i) >= contentHTML.lastIndexOf("<", i)) {
					// skip anything inside a <script> block
					if (lcContentHTML.lastIndexOf("/script>", i) >= lcContentHTML.lastIndexOf("<script", i)) {
						newHTML += contentHTML.substring(0, i) + highlightOpenTag + contentHTML.substr(i, searchTerm.length) + highlightCloseTag;
	          				contentHTML = contentHTML.substr(i + searchTerm.length);
						lcContentHTML = contentHTML.toLowerCase();
						i = -1;
					}
				}
			}
		}
		document.getElementById('kidsMainContent').innerHTML = newHTML;
	}
}

function expand(param) { // Joke page - expand div layer to show the answer
	param.style.display=(param.style.display=="none"?"":"none");
}

var kidsBuzzWhatAreYouReading = { // Detect if this is the What Are You Reading page and run the appropriate script onLoad
	init: function() {
		if (document.getElementById("ctl00_ContentArea_kidsWhatAreYouReadingForm")) { kidsBuzzWhatAreYouReadingForm.init(); }
	}
}

function onLoadWindow () { // Put all necessary functions needed to run when the window loads
	radioButton.init();
	lampPost.init();
	kidsBuzzTheme.init();
	kidsBuzzSearchTermHighlight.init();
	kidsBuzzWhatAreYouReading.init();
}
window.onload = onLoadWindow;


