// 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 sabbathcheck(){
	var today = new Date();
	var day = today.getDay();
	if(day==0){
		window.location="http://solascripturapublishing.com/sabbath.html"
	}
}
function standardheadpackage(){
	sabbathcheck();
}
standardheadpackage();

// 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://www.adobe.com/products/acrobat/readstep2.html");
		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");
		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] === 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);
		}
	}
}

// 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);
					emailaddy = emailaddy.replace(/-at-cox-period-net-but-with-out-dashes-and-replace-at-cox-period-net-with\[at\]cox\.net/,"@"+ maildomain);
					params = ahref.substring(parampos);
					anchors[i].setAttribute("href","mailto:" + emailaddy + params);
				}else{
					emailaddy = ahref.replace(/mailto:/, "");
					emailaddy = emailaddy.replace(/-at-cox-period-net-but-with-out-dashes-and-replace-at-cox-period-net-with\[at\]cox\.net/,"@" + 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].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 distinguishes non-web document files so that users know they area about to launch plug-ins/other applications
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;
		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);
			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 h1s = document.getElementsByTagName("h1");
		var mainhead = h1s[0];
		var h1parentnode = mainhead.parentNode;
		h1parentnode.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(marknonwebdocumentlinks);
addonloadfunction(maketoc);
addonloadfunction(exposelinks);
addonloadfunction(sabbathcheck);
// addonloadfunction(addpubliclink);
// addonloadfunction(addfooterlinks);
