/*
	ARTz Shorthand Legend

	d = document (object)
	w = window (object)
	o,n = object/node reference (object)
	os, ns = collection of objects/nodes (object array)
	e = event
	t = HTML tag (e.g., 'div', 'ul')
	id = CSS id (string)
	s = string variable
	i,j = integer variables
	c = CSS class (string)
	f() = document fragment
	r = return value
	l = length of array


artz.update()
This function gets all artz-enabled modules (div elements containing 'artz-' prefix in their class name), and proceeds to loop through all them enabling or disabling them based on the user's cookie setting.

artz.toggle()
This function will toggle the accessibility setting, which will call the artz_update function and update the modules to the correct state.

artz.toggle.init()
This function initializes all switches (anchor elements containing the 'artz-switch' class), which involves attaching the artz_switch() function to their click, the correct text.

*/
var d = document;
var w = window;
function ge(id){return d.getElementById(id)}
function gt(t,o){o=o?o:d;return o.getElementsByTagName(t)}
function gc(c,t,s,o){var r=new Array();var os=gt(t,o);for(var i=0,j=0,l=os.length;i<l;i++){var sc = s + os[i].className + s;if(sc.indexOf(s+c+s)!=-1){r[j] = os[i];j++;}}return r;} // New method for getElementsByTagName + ClassName; string "s" allows pinpointing exact class or if it exists in the string
function gte(e){if (w.event && w.event.srcElement) r = w.event.srcElement;else if (e && e.target) r = e.target;if (!r) return null;if(r.nodeType == 3)r=r.parentNode; /* Safari 1.3 */ return r;}
function ce(t){return d.createElement(t)}
function cf(){return d.createDocumentFragment()}
function ct(s){return d.createTextNode(s)}
function ac(p,c){p.appendChild(c)}
function ib(p,c,e){p.insertBefore(c,e)}
function ae(o,et,fn){if(o.addEventListener)o.addEventListener(et,fn,false);else if(o.attachEvent)o.attachEvent("on"+et,fn);}
function re(o,et,fn){if(o.removeEventListener)o.removeEventListener(et,fn,false);else if(o.detachEvent)o.detachEvent("on"+et,fn);}
//function ad(e,t){while (e.nodeName.toLowerCase() != t && e.nodeName.toLowerCase() != 'html') e=e.parentNode; return (e.nodeName.toLowerCase()=='html') ? null : e;}
function scookie(name, value, days, path, domain) {
	var expires = -1;
	if(typeof days == "number" && days >= 0) {
		var dt = new Date();
		dt.setTime(dt.getTime()+(days*24*60*60*1000));
		expires = dt.toGMTString();
	}
	value = escape(value);
	document.cookie = name + "=" + value + ";"
		+ (expires != -1 ? " expires=" + expires + ";" : "")
		+ (path ? "path=" + path : "")
		+ (domain ? "; domain=" + domain : "")
}
function gcookie(name) {
	var idx = d.cookie.indexOf(name+'=');
	if(idx == -1) { return null; }
	var value = d.cookie.substring(idx+name.length+1);
	var end = value.indexOf(';');
	if(end == -1) { end = value.length; }
	value = value.substring(0, end);
	value = unescape(value);
	return value;
}

if (artz == null || typeof(artz) != "object") var artz = new Object();

artz = {

	on: function() {
		if (gcookie('artz')=='1')
			return true;
		else
			return false;
	},

	sr: function() {
		return false;
	},

	init: function() {
		if (artz.sr() && !artz.on()) artz.enable();
	},

	update: function() {
		var m,s,a;
		artz.on()?m='exit':m='init';
		a = gc('artz-','div',''),l=a.length;
		for (var i=0;i<l;i++) {
			s=' ' + a[i].className + ' ';
			if (s.indexOf(' artz ')!=-1 && m == 'init') {
				// module is already initialized; do nothing
			} else if (s.indexOf(' artz ')==-1 && m=='exit') {
				// module is already exited; do nothing
			} else {
				s = a[i].className;
				s = s.replace(' artz','');
				s = s.replace('artz-','');
				eval('artz.'+s+'.'+m+'(a[i].id)');
			}
		}
	},

	enable: function() {
		scookie('artz','1',365,'/','artzstudio.com');
		artz.update();
	},

	disable: function() {
		scookie('artz','0',365,'/','artzstudio.com');
		artz.update();
	}
}

// 1. Create toggle with tooltip callout, link to our site.
// 2. Add support code for init function; break out if no support for key DOM functions.
// 3. Strip whitespace on init.
// 4. Consider initializing / exiting modules without id.
// 5. Plan for other class names tagged on to module.
// 6. Add screen reader detect.

if (artz.toggle == null || typeof(artz.toggle) != "object") artz.toggle = new Object();

artz.toggle = {

	init: function () {
		var on='Enable AOL Accessibility', off='Disable AOL Accessibility';
		var s,sw;
		artz.on()?s=off:s=on;
		sw = gc('artz-switch','a',' ');
		for (var i=0,l=sw.length;i<l;i++) {
			re(sw[i],'click',artz.toggle.set);
			ae(sw[i],'click',artz.toggle.set);
			!sw[i].hasChildNodes()?ac(sw[i],ct(s)):sw[i].firstChild.data=s;
		}
	},

	set: function () {
		artz.on()?artz.disable():artz.enable();
		artz.toggle.init();
	}
}

if (artz.tb == null || typeof(artz.toggle) != "object") artz.tb = new Object();

artz.tb = {

	init: function(id) {
		if (artz.on()) return;
		var tb = ge(id);
		tb.parentNode.className+=' artz';
		tb.className+=' artz';
		var tabs = gc('tabs','ul',' ',tb)[0];
		var h3s = gt('h3',tabs);
		var f = cf();
		var c = "on";
		var l = h3s.length;
		for (var i=0;i<l;i++){
			var li=ce('li');
			li.className = c;
			c='';
			ac(li,ct(h3s[i].firstChild.data));
			ae(li,'click',artz.tb.set);
			ac(f,li);
		}
		var e = ce('ul');
		e.className = 'dtabs';
		ac(e,f);
		ib(tb,e,tabs);
	},

	set: function(e) {
		var o=gte(e);
		var dtabs = gt('li',o.parentNode);
		var h3s = gt('h3',o.parentNode.parentNode);
		var l = dtabs.length;
		for (var i=0;i<l;i++){
			if (o==dtabs[i]) {
				dtabs[i].className = 'on';
				h3s[i].parentNode.className = 'tab on';
			} else {
				dtabs[i].className = '';
				h3s[i].parentNode.className = 'tab';
			}
		}
	},

	exit: function(id) {
		var tb = ge(id);
		tb.parentNode.className = tb.parentNode.className.replace( ' artz','');
		tb.className = tb.className.replace(' artz','');
		tb.removeChild(gc('dtabs','ul',' ',tb)[0]);
	}
}



