// Google Search (CSE) Stuff
// theirs

var googleSearchIframeName = "cse-search-results";
var googleSearchFormName = "cse-search-box";
var googleSearchFrameWidth = 575;
var googleSearchDomain = "www.google.com";
var googleSearchPath = "/cse";
var googleSearchResizeIframe = true;

// site-specific
var searchboxformdivinputcxvalue = "002805293366787106831:14eiiie6xua";

// mine
var searchpageid = "search_page"; // id of body of search page where search results should be loaded
var searchplaceid = "welcome"; // id of element after which to place search box
var searchdivid = "site_search"; // proposed id (and class) of site search div
var searchaction = "/county/search.asp"; // page location in site
var postsearchelemid = "welcome"; // post-search element id

// local variables

var jscl = -1;
var classAttrib = "";
var thehead = document.getElementsByTagName("head")[0];

// site variables are declared in site_var.js

function addonloadfunction(addlfunction) {
	var onloadfunctions = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = addlfunction;
	} else {
		if (onloadfunctions) {
			window.onload = function () {
				onloadfunctions();
				addlfunction();
			};
		} else {
			window.onload = function () {
				addlfunction();
			};
		}
	}
}

function compliancecheck(){
	if(document.body.getAttribute("class")){
		classAttrib = "class";
	}else if(document.body.getAttribute("className")){
		classAttrib = "className";
	}
	if(document.body.getAttribute(classAttrib)){
		for (var i = 0; i < 5; i++) {
			if(document.body.getAttribute(classAttrib).indexOf("jscl"+String(i)) > -1){
				jscl = i;
			}
		}
	}
}

function openinnewwindow(passed_href) {
	window.open(passed_href, domainshortname + "_document");
}

function addsearch(){
	var searchplace = document.getElementById(searchplaceid);

	// append script to head to display search form
	searchboxscript = document.createElement("script");
	searchboxscript.setAttribute("src","http://www.google.com/afsonline/show_afs_search.js");
	searchboxscript.setAttribute("type","text/javascript");
	thehead.appendChild(searchboxscript);

	var searchdiv = document.getElementById("Layer2");
	var searchboxform = document.createElement("form");
	searchboxform.setAttribute("action",searchaction);
	searchboxform.setAttribute("id",googleSearchFormName);
	var searchboxformdiv = document.createElement("div");
	searchboxformdivinputcx = document.createElement("input");
	searchboxformdivinputcx.setAttribute("name","cx");
	searchboxformdivinputcx.setAttribute("type","hidden");
	searchboxformdivinputcx.setAttribute("value",searchboxformdivinputcxvalue);
	searchboxformdivinputcof = document.createElement("input");
	searchboxformdivinputcof.setAttribute("name","cof");
	searchboxformdivinputcof.setAttribute("type","hidden");
	searchboxformdivinputcof.setAttribute("value","FORID:11");
	var searchboxformdivinputq = document.createElement("input");
	searchboxformdivinputq.setAttribute("name","q");
	searchboxformdivinputq.setAttribute("type","text");
	searchboxformdivinputq.setAttribute("size","25");
	var searchboxformdivinputsa = document.createElement("input");
	searchboxformdivinputsa.setAttribute("name","sa");
	searchboxformdivinputsa.setAttribute("type","submit");
	searchboxformdivinputsa.setAttribute("value","Search");
	
	searchboxformdiv.appendChild(searchboxformdivinputcx);
	searchboxformdiv.appendChild(searchboxformdivinputcof);
	searchboxformdiv.appendChild(searchboxformdivinputq);
	searchboxformdiv.appendChild(searchboxformdivinputsa);

	searchboxform.appendChild(searchboxformdiv);
	searchdiv.appendChild(searchboxform);


	if(document.getElementById(searchpageid) !== null){
		// append script to head to display search query results
		searchresultsscript = document.createElement("script");
		searchresultsscript.setAttribute("src","http://www.google.com/coop/cse/brand?form=cse-search-box&lang=en");
		searchresultsscript.setAttribute("type","text/javascript");
		thehead.appendChild(searchresultsscript);

		var mainhead = document.getElementsByTagName("h1")[0];
		var mainheadparentnode = mainhead.parentNode;
		searchresultsdiv = document.createElement("div");
		searchresultsdiv.setAttribute("id",googleSearchIframeName);
		mainheadparentnode.insertBefore(searchresultsdiv, mainhead.nextSibling);
	}
}

// this function distinguishes non-web document files so that users know they area about to launch plug-ins/other applications
addonloadfunction(compliancecheck);
addonloadfunction(addsearch);
