/*

CUSTOM FORM ELEMENTS

Created by Ryan Fait
www.ryanfait.com

The only thing you need to change in this file is the following
variables: checkboxHeight, radioHeight and selectWidth.

Replace the first two numbers with the height of the checkbox and
radio button. The actual height of both the checkbox and radio
images should be 4 times the height of these two variables. The
selectWidth value should be the width of your select list image.

You may need to adjust your images a bit if there is a slight
vertical movement during the different stages of the button
activation.

Visit http://ryanfait.com/ for more information.

*/

var checkboxHeight = "21";
var radioHeight = "21";

/* No need to change anything after this */
if (window.XMLHttpRequest) { 
	document.write('<style type="text/css">input.styled { display: none; } span.for-styled-select { position: absolute; } select.styled { position: relative; opacity: 0; filter: alpha(opacity=0); z-index: 5; }</style>');
} else {
	document.write('<style type="text/css">input.styled { display: none; }</style>');
}

var Custom = {
	init: function() {
		var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
/* 		var oldselects = document.getElementsByClassName('select');
		var oldcheckboxes = document.getElementsByClassName('checkbox');
		var oldradios = document.getElementsByClassName('radio');
		oldelms = oldelms.push(oldselects.join());
		oldelms = oldelms.push(oldcheckboxes.join());
		oldelms = oldelms.push(oldradios.join()); */
		
		/* for(i = 0; i < oldelms.length; i++) {
			console.log(oldelms[i]);
		} */
		for(a = 0; a < inputs.length; a++) {
			if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && (/styled/.test(inputs[a].className)) && !(/doneStyling/.test(inputs[a].className))) {
				span[a] = document.createElement("span");
				span[a].className = inputs[a].className.replace(inputs[a].type,'').replace('styled','for-styled-'+inputs[a].type);
				if(inputs[a].id) {
					span[a].id = 's_'+inputs[a].id;
				}

				if(inputs[a].checked == true) {
					if(inputs[a].type == "checkbox") {
						position = "0 -" + (checkboxHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					} else {
						position = "0 -" + (radioHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					}
				}
				
				//re added a check for disabled inputs
				if(!inputs[a].getAttribute("disabled")) {
					inputs[a].className = inputs[a].className + " doneStyling";
					inputs[a].onchange = Custom.clear;
					span[a].onmousedown = Custom.pushed;
					span[a].onmouseup = Custom.check;
					document.onmouseup = Custom.clear;
				} else {
					inputs[a].className = inputs[a].className + " disabled doneStyling";
					span[a].className = span[a].className += " disabled";
				}
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
			}
			
			//for inputs type=text, wraps the input with two spans, two because of IE7, so no styling is needed for input itself
			if((inputs[a].type == 'text' || inputs[a].type == 'password') && (/wrapped/.test(inputs[a].className)) && !(/doneWrapping/.test(inputs[a].className))) {
				span[a] = new Array();
				span[a][0] = document.createElement("span");
				span[a][1] = document.createElement("span");
				
				span[a][0].className = inputs[a].className.replace('text','').replace('wrapped','for-wrapped-input');
				span[a][1].className = 'for-wrapped-input-inner';
				inputs[a].className = inputs[a].className + " doneWrapping";
				
				inputs[a].parentNode.insertBefore(span[a][0], inputs[a]);
				span[a][0].appendChild(span[a][1]);
				span[a][1].appendChild(inputs[a]);
				
				if(inputs[a].value.length) {
					inputs[a].defaultValue = inputs[a].value;
					inputs[a].className = inputs[a].className + ' isDefault';
					inputs[a].onfocus = function() {
						if(this.value == this.defaultValue) {
							this.select();
							this.className = this.className.replace(/isDefault/, '');
							
						}
					};
					// has a problem if i want to delete the default value
					/*inputs[a].onblur = function() {
						if(this.value == '' || this.value == this.defaultValue) {
							this.value = this.defaultValue;
							this.className = /isDefault/.test(this.className) ? this.className : this.className + ' isDefault';
						}
					};*/
					inputs[a].onmouseup = function() {
						//if(typeof(this.preventDefault) == 'function') this.preventDefault();
						return false;
					};
				}
			}
		}
		if (!window.XMLHttpRequest) { return; }
		
		inputs = document.getElementsByTagName("select");
		for(a = 0; a < inputs.length; a++) {
			if((/styled/.test(inputs[a].className)) && !(/doneStyling/.test(inputs[a].className))) {
				
				option = inputs[a].getElementsByTagName("option");
				active = option[0].childNodes[0].nodeValue;
				textnode = document.createTextNode(active);
				for(b = 0; b < option.length; b++) {
					if(option[b].selected == true) {
						textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
					}
				}
				span[a] = new Array();
				span[a][0] = document.createElement("span");
				span[a][1] = document.createElement("span");
				span[a][0].className = inputs[a].className.replace('styled','for-styled-select');
				span[a][1].className = 'for-styled-select-inner';
				span[a][1].id = "select" + inputs[a].name;
				span[a][0].style.left = inputs[a].offsetLeft+'px';
				span[a][0].style.top = inputs[a].offsetTop+'px';
				span[a][0].appendChild(span[a][1]);
				span[a][1].appendChild(textnode);
				
				inputs[a].className = inputs[a].className + " doneStyling";
				inputs[a].parentNode.insertBefore(span[a][0], inputs[a]);
				inputs[a].onchange = Custom.choose;
			}
		}
	},
	pushed: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight*3 + "px";
		} else if(element.checked == true && element.type == "radio") {
			this.style.backgroundPosition = "0 -" + radioHeight*3 + "px";
		} else if(element.checked != true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
		} else {
			this.style.backgroundPosition = "0 -" + radioHeight + "px";
		}
	},
	check: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 0";
			element.checked = false;
		} else {
			if(element.type == "checkbox") {
				this.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else {
				this.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
				group = this.nextSibling.name;
				inputs = document.getElementsByTagName("input");
				for(a = 0; a < inputs.length; a++) {
					if(inputs[a].name == group && inputs[a].name == "radio" && inputs[a] != this.nextSibling) {
						inputs[a].previousSibling.style.backgroundPosition = "0 0";
						inputs[a].checked = false;
					}
				}
			}
			element.checked = true;
		}
	},
	clear: function() {
		inputs = document.getElementsByTagName("input");
		for(var b = 0; b < inputs.length; b++) {
			if(inputs[b].type == "checkbox" && inputs[b].checked == true && /styled/.test(inputs[b].className)) {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else if(inputs[b].type == "checkbox" && /styled/.test(inputs[b].className)) {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			} else if(inputs[b].type == "radio" && inputs[b].checked == true && /styled/.test(inputs[b].className)) {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
			} else if(inputs[b].type == "radio" && /styled/.test(inputs[b].className)) {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			}
		}
	},
	choose: function() {
		option = this.getElementsByTagName("option");
		for(d = 0; d < option.length; d++) {
			if(option[d].selected == true) {
				document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
			}
		}
	}
}
//window.onload = Custom.init;
