var PageUpdates = {
	enabled: true,
	switchers: new Array(),
	init: function() {
		var cookies = document.cookie.split(';');

		cookies.each(function(cook) {
			if ("disableJS=true" == cook) {
				PageUpdates.enabled = false;
				throw $break;
			}
		});

		if ($('ajax_switch'))
		{
		    // sets up the switcher in the top nav
		    Event.observe(window, 'load', function() { PageUpdates.initSwitcher($('ajax_switch').firstChild); });
		}
	},
	initSwitchers: function() {
		// call this to set up additional switchers on anything with the class 'jsSwitcher' (eg: on the accessibility page)
		Event.observe(window, 'load', function() {
			document.getElementsByClassName('jsSwitcher').each(this.initSwitcher.bind(this));
		}.bind(this));
	},
	initSwitcher: function(el) {
		this.switchers.push(el);

		Event.observe(el, 'click', this.toggleJS.bind(this));
		if (!this.enabled && el.innerHTML) 
			el.innerHTML = el.innerHTML.replace(/\boff\b/, 'on');
	},
	toggleJS: function(evt) {
		this.enabled = !this.enabled;

		var expiry = new Date((new Date()).getTime() + (1000 * 60 * 60 * 24 * 30 * 3)); // semi-permenant (~3 months)

		document.cookie = "disableJS=" + (this.enabled ? "false" : "true") + ";expires=" + expiry.toUTCString() + ";path=/";
		
		this.switchers.each(function(el) {
			if (el.textContent || el.innerText) {
				if (el.textContent)
					el.textContent = el.textContent.replace(PageUpdates.enabled ? /\bon\b/ : /\boff\b/, PageUpdates.enabled ? 'off' : 'on');
				else 
					el.innerText = el.innerText.replace(PageUpdates.enabled ? /\bon\b/ : /\boff\b/, PageUpdates.enabled ? 'off' : 'on');
			}
		});

		alert("Dynamic page updates are now turned " + (this.enabled ? 'ON' : 'OFF'));

		Event.stop(evt);
	}
};

PageUpdates.init();