var CustomControls = {
	_settings : new Array(),

	init : function(s) {
		var n, im, u = navigator.userAgent;

		this._settings = {
			checkbox_on_img : 'checkbox_on.png',
			checkbox_off_img : 'checkbox_off.png',
			radio_on_img : 'radio_on.png',
			radio_off_img : 'radio_off.png',
			checkbox_selector : 'customCheckbox',
			radio_selector : 'customRadio'
		};

		for (n in s)
			this._settings[n] = s[n];

		s = this._settings;

		for (n in s) {
			if (n.indexOf('_img') != -1 && s[n].indexOf) {
				im = new Image();
				im.src = s[n];
			}
		}

		this.isOpera = u.indexOf('Opera') != -1;
		this.isSafari = u.indexOf('Safari') != -1;

		this.addEvent(window, "load", CustomControls.onLoad);
		//Running onLoad twice causes multiple checkboxes/radios to appear if browser != IE
		//this.addEvent(window, 'DOMContentLoaded', CustomControls.onLoad);
	},

	getFileName : function(s) {
		return s.substring(s.lastIndexOf('/') + 1);
	},

	toggle : function(p, t) {
		var s = this._settings, f = this.getFileName(p);

		if (t == "checkbox")
			return f == this.getFileName(s.checkbox_on_img) ? s.checkbox_off_img : s.checkbox_on_img;

		if (t == "radio")
			return f == this.getFileName(s.radio_on_img) ? s.radio_off_img : s.radio_on_img;
	},

	onLoad : function(e) {
		if (!CustomControls.onLoadDone) {
			CustomControls.convertElements();
			CustomControls.onLoadDone = true;
		}
	},

	addEvent : function(o, n, h) {
		if (o.attachEvent)
			o.attachEvent("on" + n, h);
		else
			o.addEventListener(n, h, false);
	},

	checkboxClick : function(e) {
		if (window.event && !CustomControls.isOpera && !CustomControls.isSafari) {
			CustomControls.targetElm = e.srcElement;

			window.setTimeout(function() {
				CustomControls._checkboxClick(CustomControls.targetElm);
			});
		} else
			return CustomControls._checkboxClick(e.target);
	},

	_checkboxClick : function(le) {
		var im, fe;

		if (le.nodeName == "IMG")
			le = le.parentNode;

		fe = le.nextSibling;
		if (!fe.disabled) {
			im = le.firstChild;
			im.src = CustomControls.toggle(im.src, 'checkbox');
			fe.checked = !fe.checked;
			try { fe.onchange(); } catch (ex) { }
			try { fe.onclick(); } catch (ex) { }
		}
	},

	radioClick : function(e) {
		if (window.event && !CustomControls.isOpera && !CustomControls.isSafari) {
			CustomControls.targetElm = e.srcElement;

			window.setTimeout(function() {
				CustomControls._radioClick(CustomControls.targetElm);
			});
		} else
			return CustomControls._radioClick(e.target);
	},

	_radioClick : function(le) {
		var im, nl, re, i, s = CustomControls._settings;

		if (le.nodeName == "IMG")
			le = le.parentNode;

		im = le.firstChild;
		re = le.nextSibling;
		if (!re.disabled) {
			re.checked = true;
			try { re.onchange(); } catch (ex) { }
			try { re.onclick(); } catch (ex) { }
			im.src = s.radio_on_img;

			// Update all other radios
			nl = re.form.elements;
			for (i=0; i<nl.length; i++) {
				if (nl[i] != re && nl[i].type == "radio" && nl[i].name == re.name) {
					im = nl[i].previousSibling.firstChild;
					im.src = s.radio_off_img;
					nl[i].checked = false;
				}
			}
		}
	},

	labelClick : function(e) {
		if (window.event && !CustomControls.isOpera && !CustomControls.isSafari) {
			CustomControls.targetElm = e.srcElement;

			window.setTimeout(function() {
				CustomControls._labelClick(CustomControls.targetElm);
			});
		} else
			CustomControls._labelClick(e.target);

		CustomControls.cancelEvent(e);
		return false;
	},

	_labelClick : function(elm) {
		var im, lfor, te, i, nl, s = CustomControls._settings;

		if ((lfor = elm.getAttribute("for")) || (lfor = elm.htmlFor)) {
			if ((te = document.getElementById(lfor))) {
				if (!te.disabled) {
					im = te.previousSibling.firstChild;

					if (te.type == "radio") {
						te.checked = true;
						try { te.onchange(); } catch (ex) { }
						im.src = te.checked ? s.radio_on_img : s.radio_off_img;
					}

					if (te.type == "checkbox") {
						te.checked = !te.checked;
						try { te.onchange(); } catch (ex) { }
						im.src = te.checked ? s.checkbox_on_img : s.checkbox_off_img;
					}

					if (te.type == "radio") {
						for (i=0, nl = te.form.elements; i<nl.length; i++) {
							if (nl[i] != te && nl[i].type == "radio" && nl[i].name == te.name) {
								im = nl[i].previousSibling.firstChild;
								im.src = s.radio_off_img;
								nl[i].checked = false;
							}
						}
					}
				}
			}
		}
	},

	update : function(f) {
		var nl = f.elements, i, im, s = CustomControls._settings;

		for (i=0; i<nl.length; i++) {
			if (new RegExp('custom', 'gi').test(nl[i].className)) {
				im = nl[i].previousSibling.firstChild;

				if (nl[i].type == "radio")
					im.src = nl[i].checked ? s.radio_on_img : s.radio_off_img;

				if (nl[i].type == "checkbox")
					im.src = nl[i].checked ? s.checkbox_on_img : s.checkbox_off_img;
			}
		}
	},

	cancelEvent : function(e) {
		if (!e.preventDefault) {
			e.returnValue = false;
			e.cancelBubble = true;
		} else
			e.preventDefault();
	},

	getCheckedElement : function(nl, n) {
		var i;

		for (i=0; i<nl.length; i++) {
			if (nl[i].type == "radio" && nl[i].name == n && nl[i].checked)
				return nl[i];
		}

		return null;
	},

	convertElements : function() {
		var i, x, nl, d = document, f = d.forms, im, ne, s = CustomControls._settings, cn;

		for (i=0; i<f.length; i++) {
			for (x=0, nl = f[i].elements; x<nl.length; x++) {
				cn = nl[x].className;

				if (cn.indexOf(s.checkbox_selector) != -1 || cn.indexOf(s.radio_selector) != -1) {
					nl[x].style.display = 'none';
					ne = d.createElement('a');
					ne.className = cn;
					ne.href = "javascript:void(0);";
					ne.setAttribute("onmousedown", "return false;");

					if (nl[x].type == "checkbox") {
						im = nl[x].checked ? s.checkbox_on_img : s.checkbox_off_img;
						this.addEvent(ne, 'click', CustomControls.checkboxClick);
					} else {
						im = nl[x].checked ? s.radio_on_img : s.radio_off_img;
						this.addEvent(ne, 'click', CustomControls.radioClick);
					}

					ne.innerHTML = '<img border="0" src="' + im + '" />';

					nl[x].parentNode.insertBefore(ne, nl[x]);
				}
			}
		}

		nl = d.getElementsByTagName("label");
		for (i=0; i<nl.length; i++) {
			if ((x = nl[i].getAttribute("for")) || (x = nl[i].htmlFor)) {
				ne = d.getElementById(x);
                try
                {
				if (new RegExp('custom', 'gi').test(ne.className))
					this.addEvent(nl[i], 'click', CustomControls.labelClick);
				}
				catch (err){}
			}
		}
	}
};
