var AccessConfig = {
	activeClass:'access-active-el',

	/*
	* this array includes all links whose contents we'd like to try and replace
	* with some kind of javascripting. Format is ("link id","code");
	*/

	link_arr:Array(
		Array("test","zAccess.spew('oi')"),
		Array("test2","zAccess.spew('oi two')")
	)
}

var zAccess = {
	spew:function(str) {
		alert(str);
	},
	who:function(e){
		// determine which object triggered an event. props to w3 schools!
		var targ;
		if (!e) { var e=window.event; }
		if (e.target) { targ=e.target; } else if (e.srcElement) { targ=e.srcElement; }
		if (targ.nodeType==3) { targ = targ.parentNode; }
	
		return targ;
	},
	Event:{
		add:function(el,e,f_ptr) {
			if (document.addEventListener) {
				el.addEventListener(e,f_ptr,false);
			} 
			else if (document.attachEvent) {
				el.attachEvent("on" + e,f_ptr);
			}
			else { 
				return false; 
			}
		}
	},
	Stylist:{
		sub_link:function(id, f_str) {
			var el = document.getElementById(id);
			if(el) {
				el.href = 'javascript:' + f_str;
			}
		},
		set_active:function(e) {
			var el = zAccess.who(e);
			if(el.className) {
				var arr = el.className.split(' ');
				arr.push( AccessConfig.activeClass );
				el.className = arr.join(' ');
			} 
			else {
				el.className = AccessConfig.activeClass;
			}
		},
		clear_active:function(e) {
			var el = zAccess.who(e);
			if(el.className) {
				var arr = el.className.split(' ');
				arr.pop();
				el.className = arr.join(' ');
			}
		},
		// props to Paul Sowden for these next three...
		setActiveStyleSheet:function(title) {
			var i, a, main;
			for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
				if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
					a.disabled = true;
					if(a.getAttribute("title") == title) a.disabled = false;
				}
			}
		},
		getActiveStyleSheet:function() {
			var i, a;
			for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
				if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
			}
			return null;
		},
		getPreferredStyleSheet:function() {
			var i, a;
			for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
				if(a.getAttribute("rel").indexOf("style") != -1
				 && a.getAttribute("rel").indexOf("alt") == -1
				 && a.getAttribute("title")
				) return a.getAttribute("title");
			}
			return null;
		}
	},
	Cookie:{
		set:function(name,value,days) {
			if(days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*3600*1000));
				var expires = "; expires="+date.toGMTString();
			}
			else {
				expires = "";
			}
			document.cookie = name + "=" + value + expires + "; path=/";
		},
		read:function(name) {
			var arr = document.cookie.split(';');
			name+='=';
			for(var i = 0; i < arr.length; i++) {
				var m = arr[i];
				while(m.charAt(0) == ' ') m = m.substring(1,m.length);
				if(m.indexOf(name) == 0) return m.substring(name.length,m.length);
			}
			return null;
		},
		remove:function(name) {	zAccess.Cookie.set(name,null,-1); }
	}
}

function smoke_and_mirrors() {

	var i = 0;
	var cur_link;

	while( cur_link = AccessConfig.link_arr[i++] ){
		zAccess.Stylist.sub_link(cur_link[0],cur_link[1]);
	}

	// liven things up a bit, for the keyboard users..

	var link_arr = document.getElementsByTagName('a');
	i = 0;

	while( cur_link = link_arr[i++] ) {
		zAccess.Event.add(cur_link,'focus',zAccess.Stylist.set_active);
		zAccess.Event.add(cur_link,'blur',zAccess.Stylist.clear_active);
	}

	// save user's stylistic preferences

	var cookie = zAccess.Cookie.read('style');
	var title = cookie ? cookie : zAccess.Stylist.getPreferredStyleSheet();
	zAccess.Stylist.setActiveStyleSheet(title);
}

function finale() {
	var title = zAccess.Stylist.getActiveStyleSheet();
	zAccess.Cookie.set('style', title, 365);
}

zAccess.Event.add(window,'load',smoke_and_mirrors);
zAccess.Event.add(window,'unload',finale);
