// 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 = "/search.shtm"; // page location in site
var postsearchelemid = "welcome"; // post-search element id

var localhost = "devgen"; // local dev domain
var publictest = ""; // public testing server
var httpdomain = "www.snco.us"; // http domain (w/min. nec. hostname)
var maildomain = "snco.us"; // mail server
var footerdivid = "footer_global"; // name of the footer container (should be a p)
var navdivid = "nav_local";
var domainshortname = "snco";
var publiclinks = false; // run public link builder
var spamavoid = false; // true if "-at-this-domain" is used in email addresses

// 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(){
	var jsclckskip = false;
	if(document.body.getAttribute("class")){
		classAttrib = "class";
	}else if(document.body.getAttribute("className")){
		classAttrib = "className";
	}else{
		if(document.getElementById("banner").getAttribute("class")){
			classAttrib = "class";
		}else if(document.getElementById("banner").getAttribute("className")){
			classAttrib = "className";
		}
		jsclckskip = true;
	}
	if(document.body.getAttribute(classAttrib) && jsclckskip == false){
		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 add 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);

	searchboxdiv = document.createElement("div");
	searchboxdiv.setAttribute("id",searchdivid);
	searchboxdiv.setAttribute(classAttrib,searchdivid);
	searchboxform = document.createElement("form");
	searchboxform.setAttribute("action",searchaction);
	searchboxform.setAttribute("id",googleSearchFormName);
	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");
	searchboxformdivinputq = document.createElement("input");
	searchboxformdivinputq.setAttribute("name","q");
	searchboxformdivinputq.setAttribute("type","text");
	searchboxformdivinputq.setAttribute("size","25");
	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);
	searchboxdiv.appendChild(searchboxform);

	// document.getElementById("nav_main").insertBefore(searchboxdiv, searchplace.nextSibling);
	document.getElementById("banner").insertBefore(searchboxdiv, document.getElementById(postsearchelemid).nextSibling);

	if(document.getElementById(searchpageid) !== null){
		// append script to head to add 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
function marknonwebdocumentlinks() {
	var nwdpresent = [];
	var plugindocpresent = [];
	var plugincompany = [];
	var plugindownloadsite = [];
	var pluginopener = [];
	var keyext = [];
	var keyformat = [];
	var anynwdpresent = false;
	function distinguishnwd(ext, shortname, format, filetype, company, opener, downloadsite) {
		var anchors = document.getElementsByTagName("a");
		var extlen = ext.length + 1; // length of string of extension (i.e. pdf = 3 plus one for leading dot)
		var extpos = 0;
		var ahref = "";
		var ahreftext = "";
		var hreflen;
		var newwindowimg;
		for (var i = 0;i < anchors.length; i++) {
			nwdpresent[ext] = false;
			ahref = anchors[i].getAttribute("href").toLowerCase();
			extpos = ahref.lastIndexOf("." + ext);
			hreflen = ahref.length;
			if (extpos > 0) {
				var diff = hreflen - extpos;
				if (diff === extlen) {
					if (anchors[i].getAttribute(classAttrib) === null || anchors[i].getAttribute(classAttrib) === shortname) {
						anchors[i].setAttribute(classAttrib, shortname + " plugin_required");
					} else {
						anchors[i].setAttribute(classAttrib,anchors[i].getAttribute(classAttrib) + " plugin_required " + shortname);
					}
					/*anchors[i].onclick = function() {
						openinnewwindow(this.getAttribute("href"));
						return false;
					};*/
					if (anchors[i].getAttribute("title")) {
						anchors[i].setAttribute("title",anchors[i].getAttribute("title") + " (" + format + ")");
						//anchors[i].setAttribute("title",anchors[i].getAttribute("title") + " (" + format + " → new window)");
					} else {
						anchors[i].setAttribute("title",anchors[i].firstChild.data + " (" + format + ")");
						//anchors[i].setAttribute("title",anchors[i].firstChild.data + " (" + format + " → new window)");
					}
				}
				plugindocpresent[ext] = true;
				plugincompany[ext] = company;
				plugindownloadsite[ext] = downloadsite;
				pluginopener[ext] = opener;
		
				keyext[ext] = ext;
				keyformat[ext] = format;
				anynwdpresent = true;
			}
		}
	}
	if(jscl >= 0){
		// file extension, short format name, longer format name, type of document, company name, application for opening/executing docs, href for acquisition
		distinguishnwd("pdf","PDF","Adobe PDF","Portable","Adobe","Adobe Reader","http://get.adobe.com/reader/");
		distinguishnwd("doc","Word","Microsoft Word","Word Processing","Microsoft","Word Viewer","http://office.microsoft.com/en-us/downloads/HA010449811033.aspx");
		distinguishnwd("xls","Excel","Microsoft Excel","Spreadsheet","Microsoft","Excel Viewer","http://office.microsoft.com/en-us/downloads/HA010449811033.aspx");
		distinguishnwd("ppt","PowerPoint","Microsoft PowerPoint","Presentation","Microsoft","PowerPoint Viewer","http://office.microsoft.com/en-us/downloads/HA010449811033.aspx");
		distinguishnwd("wmv","WMV","Windows Media Video","Video","Microsoft","Windows Media Player","http://www.microsoft.com/windows/windowsmedia/");
		distinguishnwd("mov","QuickTime","Apple QuickTime","Video","Apple","Apple QuickTime Player","http://www.apple.com/quicktime/download/");
		var navdiv;
		if(jscl >= 1 && anynwdpresent){
			// create nav stuff
			navdiv = document.getElementById(navdivid);
			var pluginp = document.createElement("p");
			var pluginpstrong = document.createElement("strong");
			var pluginpstrongtext = document.createTextNode("Viewers and Plug-ins");
			pluginpstrong.appendChild(pluginpstrongtext);
			pluginp.appendChild(pluginpstrong);
			pluginp.setAttribute("title","Download applications to view non-web documents.");
			var pluginul = document.createElement("ul");
			pluginul.setAttribute(classAttrib,"plugin external");
			var keyp = document.createElement("p");
			var keypstrong = document.createElement("strong");
			var keypstrongtext = document.createTextNode("Document Icon Key");
			keypstrong.appendChild(keypstrongtext);
			keyp.appendChild(keypstrong);
			keyp.setAttribute("title","An explanation of the icons on this page.");
			var keyul = document.createElement("ul");
			keyul.setAttribute(classAttrib,"key");


			var pluginli = [];
			var pluginlia = [];
			var pluginliatext = [];
			var keyli = [];
			var keyliimg = [];
			var keylitext = [];

			for (var i in nwdpresent){
				if (plugindocpresent[i] !== undefined && plugindocpresent[i] === true){
					pluginli[i] = document.createElement("li");
					pluginlia[i] = document.createElement("a");
					pluginlia[i].setAttribute("href",plugindownloadsite[i]);
					pluginlia[i].setAttribute(classAttrib,keyext[i]);
					pluginliatext[i] = document.createTextNode(pluginopener[i]);
					pluginlia[i].appendChild(pluginliatext[i]);
					pluginli[i].appendChild(pluginlia[i]);
					pluginul.appendChild(pluginli[i]);

					keyli[i] = document.createElement("li");
					// keyli[i].setAttribute("class",keyext[i]);
					keyliimg[i] = document.createElement("img");
					keyliimg[i].setAttribute("src","/image/style/nwd_"+keyext[i]+".gif");
					keyliimg[i].setAttribute("alt",keyformat[i] + " icon");
					// keyliimg[i].setAttribute("style","width:12px;height:12px;");
					keyliimg[i].setAttribute("height","13");
					keyliimg[i].setAttribute("width","11");
					keylitext[i] = document.createTextNode(" "+keyformat[i]);
					keyli[i].appendChild(keyliimg[i]);
					keyli[i].appendChild(keylitext[i]);
					keyul.appendChild(keyli[i]);
				}
			}
			navdiv.appendChild(pluginp);
			navdiv.appendChild(pluginul);
			navdiv.appendChild(keyp);
			navdiv.appendChild(keyul);
		}
	}
}


function makelistsdropinie6() {
	var menulist, node;
	if (document.all && document.getElementById) {
		menulist = document.getElementById("menu");
		for (var i = 0; i < menulist.childNodes.length; i++) {
			node = menulist.childNodes[i];
			if (node.nodeName == "LI") {
				node.onmouseover = function () {
					this.className += " over";
				};
				node.onmouseout = function () {
					this.className = this.className.replace(" over", "");
				};
			}
		}
	}
}

// this function distinguishes non-web document files so that users know they area about to launch plug-ins/other applications
function exposelinks() {
	if(jscl >= 0){
		var anchors = document.getElementsByTagName("a");
		var parampos = ""; // position of stuff like ?subject=, etc.
		var params = ""; // for gathering additional parameters
		var emailaddy = "";
		var ahref = "";
		var hreflen, mailtopos, httppos;
		var ahreftext = "";
		var emaillinktext = ""; // textnode from email link
		var eltck = ""; // ck for emailaddy in email link text
		var domains = ["http://devgen/","http://snweb/","http://www.shawneesheriff.org/","http://www.snco.us/","http://www.co.shawnee.ks.us/"];
		var httpspos, httppos;
		var sitetype = null; // for tracking internal/external sites and marking externals
		for (var i = 0;i < anchors.length; i++) {
			ahref = anchors[i].getAttribute("href");
			mailtopos = ahref.toLowerCase().indexOf("mailto:");
			httpspos = ahref.toLowerCase().indexOf("https://");
			httppos = ahref.toLowerCase().indexOf("http://");
			if(httppos === 0 || httpspos === 0){
				for (var j = 0; j < domains.length; j++) {
					var dindex = ahref.toLowerCase().indexOf(domains[j]);
					if(dindex === 0){
						sitetype = "internal";
					}
				}
				if (sitetype === null){
					sitetype = "external";
				}
			}else{
				sitetype = "internal";
			}
			
			if(mailtopos === 0){
				parampos = ahref.indexOf("?");
				hreflen = ahref.length;
				if(parampos > -1){
					emailaddy = ahref.substring(7, parampos);
					if (spamavoid === true) {
						emailaddy = emailaddy.replace(/-at-this-domain/,"@"+ maildomain);
						params = ahref.substring(parampos);
						anchors[i].setAttribute("href","mailto:" + emailaddy + params);
					};
				}else{
					emailaddy = ahref.replace(/mailto:/, "");
					if (spamavoid === true) {
						emailaddy = emailaddy.replace(/-at-this-domain/,"@" + maildomain);
						anchors[i].setAttribute("href","mailto:" + emailaddy);
					};
				}
				emaillinktext = anchors[i].childNodes[0].nodeValue;
				eltck = emaillinktext.indexOf(emailaddy);
				if(eltck == -1){
					ahreftext = document.createTextNode(" ("+emailaddy+")");
					anchors[i].appendChild(ahreftext);
				}
			}else if(sitetype === "external"){
				if(anchors[i].getAttribute(classAttrib) === null){
					anchors[i].setAttribute(classAttrib,"external");
				}else{
					anchors[i].setAttribute(classAttrib,anchors[i].getAttribute(classAttrib) + " external");
				}
				if(anchors[i].getAttribute("title") == "" || anchors[i].getAttribute("title") === null){
					anchors[i].setAttribute("title",anchors[i].childNodes[0].nodeValue + " (" + ahref + ")");
				}else{
					anchors[i].setAttribute("title",anchors[i].getAttribute("title") + " (" + ahref + ")");
				}
			}
			httpspos = -1;
			httppos = -1;
			sitetype = null;
		}
	}
}

// this function creates a table of contents by creating ids for the h2s on the page to allow you to jump to them quickly
function maketoc() {
	var countyhome = document.getElementById("county_home");
	var h2s = document.getElementsByTagName("h2");
	// (h2s[0] == undefined) ? alert("undefined") : alert("defined");
	if(jscl >= 1 && countyhome === null && h2s[1] !== undefined){
		var h2text, h2textnode, h2id, h2link, contentlink, permalink, toplink;
		var linkcollection = document.createElement("p");
		var introtextnodetext = "Contents: "; 
		var introtextnode = document.createTextNode(introtextnodetext); 
		var delimspantextnodetext = ""; 
		var delimtextnode = document.createTextNode(delimspantextnodetext);
		var delimspan = document.createElement("span");
		var bullet;
		var contentlinktext = "↑ top";
		var contentlinktextnode = document.createTextNode(contentlinktext);
		
		linkcollection.setAttribute("id","content_links");
		linkcollection.appendChild(introtextnode);
		for (var i = 0;i < h2s.length; i++) {
			h2text = h2s[i].childNodes[0].nodeValue;
			h2textnode = document.createTextNode(h2text);
			if(h2s[i].getAttribute("id") !== null){
				h2id = h2s[i].getAttribute("id");
			}else{
				h2id = h2text.replace(/ /g, "");
				h2id = h2id.replace(/%20/g, "");
				h2s[i].setAttribute("id",h2id);
			}
			h2link = document.createElement("a");
			h2link.setAttribute("href","#"+String(h2id));
			h2link.setAttribute("title","Link directly to “"+h2text+".”");
			h2link.appendChild(h2textnode);
			permalink = h2link.cloneNode(true);

			linkcollection.appendChild(delimtextnode);
			linkcollection.appendChild(h2link);

			delimspan.setAttribute(classAttrib,"delimiter");
			delimspantextnodetext = " · ";
			//delimspantextnodetext = ", ";
			delimtextnode = document.createTextNode(delimspantextnodetext);
			delimspan.appendChild(delimtextnode);
			bullet = delimspan.cloneNode(true);

			contentlinktext = "↑ top";
			contentlinktextnode = document.createTextNode(contentlinktext);
			toplink = document.createElement("a");
			toplink.setAttribute("href","#content");
			toplink.setAttribute("title","Return to top.");
			toplink.setAttribute(classAttrib,"top_link");
			toplink.appendChild(contentlinktextnode);
			h2s[i].childNodes[0].nodeValue = "";
			h2s[i].appendChild(permalink);
			h2s[i].appendChild(bullet);
			h2s[i].appendChild(toplink);
		}
		var mainhead = document.getElementsByTagName("h1")[0];
		var mainheadparentnode = mainhead.parentNode;
		mainheadparentnode.insertBefore(linkcollection, mainhead.nextSibling);
	}
}
/*
function addpubliclink() {
	if(jscl >= 0){
		var currentlocation = document.location.href;
		var devck = currentlocation.indexOf(localhost);
		if (devck > -1) {
			var globalfooter = document.getElementById(footerdivid);
			var globalfooterpubliclink = document.createElement("a");
			var globalfooterpubliclinktext = document.createTextNode("pub");

			publichref = currentlocation.replace(localhost, httpdomain);

			globalfooterpubliclink.setAttribute("href",publichref);
			globalfooterpubliclink.setAttribute(id,"bottom_link");
			globalfooterpubliclink.setAttribute(classAttrib,"public_link");
			globalfooterpubliclinkdelim = document.createTextNode(" • ");
			globalfooterpubliclink.appendChild(globalfooterpubliclinktext);
			globalfooter.appendChild(globalfooterpubliclinkdelim);
			globalfooter.appendChild(globalfooterpubliclink);
		}
	}
}

function addfooterlinks() {
	if(jscl >= 0){
		var currentlocation = document.location.href;
		var devck = currentlocation.indexOf(localhost);
		if (devck > -1) {
			var globalfooter = document.getElementById(footerdivid);
			var globalfooterpubliclink = document.createElement("a");
			var globalfooterpubliclinktext = document.createTextNode("pub");

			publichref = currentlocation.replace(localhost, httpdomain);

			globalfooterpubliclink.setAttribute("href",publichref);
			globalfooterpubliclink.setAttribute(id,"bottom_link");
			globalfooterpubliclink.setAttribute(classAttrib,"public_link");
			globalfooterpubliclinkdelim = document.createTextNode(" • ");
			globalfooterpubliclink.appendChild(globalfooterpubliclinktext);
			globalfooter.appendChild(globalfooterpubliclinkdelim);
			globalfooter.appendChild(globalfooterpubliclink);
		}
	}
}
*/
addonloadfunction(compliancecheck);
// addonloadfunction(addsearch);
addonloadfunction(makelistsdropinie6);
addonloadfunction(marknonwebdocumentlinks);
addonloadfunction(maketoc);
addonloadfunction(exposelinks);
//// addonloadfunction(addpubliclink);
//// addonloadfunction(addfooterlinks);

