// credits and kuddos go to: David Dorward; <http://dorward.me.uk/>. Jeffrey Zeldman; the cookie script came from his JS file at <http://www.zeldman.com/>. The kind people at #javascript on EFNet IRC
// this script is free to use and improve
// NOTE: if the XHTML documents get served as application/xhtml+xml, the script will fail on the innerHTML directive. 

var dom, tag, cookies, iniClass, panelState, fontScale, newWindow;
ua = navigator.appVersion; if(ua.indexOf("MSIE") != -1 &&  ua.indexOf("Macintosh") != -1) isIEmac = 1; else isIEmac = 0;
var isIE = (document.all) ? true : false;
curURL = document.location.href.replace("http://","");
domain = curURL.substring(0,curURL.indexOf("/"));
panelName = "eigen";
panel_cookie = "panel_t";
win_cookie = "newwindow_t";
font_cookie = "fontscale_t";

if (document.getElementById) dom = true;
if (document.getElementsByTagName) tag = true;
//if (document.cookie) cookies = true;

// on loading document
window.onload = function() { 
	if (dom && tag) {
		//getCookies();
		//iniClass = document.getElementsByTagName("body").item(0).className;
		//scale(fontScale);
		//setNewWindow(newWindow);
		//createPanel();
	}
	if (isIE) {
		//styleAbbr();
	}
}

// retrieve relevant cookies
function getCookies() {
	// panel state
	panelState = readCookie(panel_cookie);
	if(!panelState) panelState = "open";
	// font scale
	fontScale = readCookie(font_cookie);
	if(!fontScale) fontScale = "medium";
	// new window
	newWindow = (readCookie(win_cookie) == "true");
}

// create panel
function createPanel() {
	ref = document.getElementById(panelName);
	if(ref) ref.className = panelState; else return false;
	
	if(!isIEmac && document.createElement && !isIE) {
		// for DOM capable browsers

		// panel state
		a = document.createElement("h3");
		
		b = document.createElement("a");
		b.setAttribute("id","instellingen");
		if(panelState == "closed") {
			b.setAttribute("title","Open Eigen instellingen");
			b.setAttribute("href","javascript:openPanel();");
		} else {
			b.setAttribute("title","Sluit Eigen instellingen");
			b.setAttribute("href","javascript:closePanel();");
		}
		a.appendChild(b);
		c = document.createElement("img");
		c.setAttribute("class","ext");
		c.setAttribute("width","11"); c.setAttribute("height","11");
		if(panelState == "closed") {
			c.setAttribute("src","/img/arrow_closed.gif");
			c.setAttribute("alt","Open");
		} else {
			c.setAttribute("src","/img/arrow_open.gif");
			c.setAttribute("alt","Sluit");
		}
		b.appendChild(c);
		d = document.createTextNode(" Eigen instellingen");
		b.appendChild(d);

		e = document.createElement("form");
		e.setAttribute("action","#"); e.setAttribute("method","post");
		e.setAttribute("onsubmit","return false;");
		f = document.createElement("fieldset");
		e.appendChild(f);

		// font switch
		g = document.createElement("label");
		f.appendChild(g);
		h = document.createTextNode("Tekstgrootte ");
		g.appendChild(h);

		i = document.createElement("select");
		i.setAttribute("size","1");
		i.setAttribute("onchange","setFontScale(this.value);");
		g.appendChild(i);
		
		j = document.createElement("option");
		j.setAttribute("value","small");
		if(fontScale=="small") j.setAttribute("selected","selected");
		i.appendChild(j);
		k = document.createTextNode("klein");
		j.appendChild(k);
		
		j = document.createElement("option");
		j.setAttribute("value","medium");
		if(fontScale=="medium") j.setAttribute("selected","selected");
		i.appendChild(j);
		k = document.createTextNode("medium");
		j.appendChild(k);

		j = document.createElement("option");
		j.setAttribute("value","large");
		if(fontScale=="large") j.setAttribute("selected","selected");
		i.appendChild(j);
		k = document.createTextNode("groot");
		j.appendChild(k);

		j = document.createElement("option");
		j.setAttribute("value","huge");
		if(fontScale=="huge") j.setAttribute("selected","selected");
		i.appendChild(j);
		k = document.createTextNode("enorm");
		j.appendChild(k);

		// window switch
		k2 = document.createElement("br");
		f.appendChild(k2);
		l = document.createElement("label");
		f.appendChild(l);
		m = document.createElement("input");
		m.setAttribute("onclick","newWinChck(this.checked);");
		m.setAttribute("class","checkbox"); m.setAttribute("type","checkbox");
		if(newWindow) m.setAttribute("checked","checked");
		l.appendChild(m);
		n = document.createTextNode(" Open externe links automatisch in een nieuw venster.");
		l.appendChild(n);

		// add it or replace it
		gh = ref.childNodes;
		if(gh.length > 1) {
			ref.replaceChild(a, gh[gh.length-2]);
			ref.replaceChild(e, gh[gh.length-1]);		
		} else {
			ref.appendChild(a);
			ref.appendChild(e);
		}

	} else {
		// for IE5/Mac specifically

		// panel state
		if(panelState == "closed") tggle = "<a href=\"javascript:openPanel();\" title=\"Open Eigen instellingen\" name=\"instellingen\" id=\"instellingen\"><img src=\"/img/arrow_closed.gif\" alt=\"Open \" width=\"11\" height=\"11\" class=\"ext\" /> Eigen instellingen</a>"; else tggle = "<a href=\"javascript:closePanel();\" title=\"Sluit Eigen instellingen\" name=\"instellingen\" id=\"instellingen\"><img src=\"/img/arrow_open.gif\" alt=\"Sluit \" width=\"11\" height=\"11\" class=\"ext\" /> Eigen instellingen</a>";
	
		// fontscale switch
		fontSwitch = "<label>Tekstgrootte <select size=\"1\" onchange=\"setFontScale(this.value);\"><option value=\"small\"" + (fontScale == "small" ? " selected=\"selected\"" : "") + ">klein</option><option value=\"medium\"" + (fontScale == "medium" ? " selected=\"selected\"" : "") + ">medium</option><option value=\"large\"" + (fontScale == "large" ? " selected=\"selected\"" : "") + ">groot</option><option value=\"huge\"" + (fontScale == "huge" ? " selected=\"selected\"" : "") + ">enorm</option></select></label><br />";
	
		// window switch
		if(newWindow) h = "checked=\"checked\""; else h = "";
		winSwitch = "<label><input onclick=\"newWinChck(this.checked);\" class=\"checkbox\" type=\"checkbox\" " + h + "/> Open externe links automatisch in een nieuw venster.</label>";
	
		// put it all together
		ref.innerHTML = "<h3>" + tggle + "</h3><form action=\"#\" method=\"post\" onsubmit=\"return false;\"><fieldset>" + fontSwitch + " " + winSwitch + "</fieldset></form>";
	
	}
}

// close the panel
function closePanel() {
	ref = document.getElementById(panelName);
	ref.className = "closed";
	createCookie(panel_cookie, "closed", 60);
	getCookies();
	createPanel();
}

// open the panel
function openPanel() {
	ref = document.getElementById(panelName);
	ref.className = "open";
	createCookie(panel_cookie, "open", 60);
	getCookies();
	createPanel();
}

// modify scale
function setFontScale (val) {
	scale(val);
	createCookie(font_cookie, val, 60);
}

function scale(val) {
	r = document.getElementsByTagName("body").item(0);
	r.className = iniClass + " " + val;
}

// execute panel switch
function newWinChck (state) {
	if(state == true) setNewWindow(true); else setNewWindow(false);
	createCookie(win_cookie, state, 60);
}

// set preference for external links
function setNewWindow(sw) {
	tree = document.getElementsByTagName("a");
	for (i = 0; i < tree.length; i++) {
		elm = tree.item(i);
		ref = elm.href; 
		if(ref) {
			if (ref.indexOf("http://") >= 0 && ref.indexOf(domain) == -1) {
				if (sw == true) {
					evnt = "return pop('" + ref + "')";
						func = new Function("e", evnt);
						elm.onclick = func;
				} else elm.onclick = "";
			}
		}
	}
}

// create a cookie
function createCookie(name,value,days) {
	if (days) {
		var date=new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires="; expires="+date.toGMTString();
	}
	else expires="";
	document.cookie=name+"="+value+expires+"; path=/";
}

// read a cookie
function readCookie(name) {
	var nameEQ=name + "="; 
	var ca=document.cookie.split(';');
	for (var i=0;i < ca.length;i++) {
		var c=ca[i];
		while (c.charAt(0)==' ') c=c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

// window pop
function pop(url) {
	if(window.open) {
		s = window.open(url,"","scrollbars=yes,resizable=yes,toolbar=yes,directories=yes,location=yes,menubar=yes,status=yes");
		return false;
	} else return true;
}

// ABBR support
function styleAbbr() {
	oldBodyText = document.body.innerHTML;
	reg = /<ABBR([^>]*)>([^<]*)<\/ABBR>/g;
	newBodyText = oldBodyText.replace(reg, '<abbr $1><span class=\"abbr\" $1>$2</span></abbr>');
	document.body.innerHTML = newBodyText;
}
