/*

CUSTOM FORM ELEMENTS

Created by Ryan Fait
www.ryanfait.com

The only things you may need to change in this file are the following
variables: checkboxHeight, radioHeight and selectWidth (lines 24, 25, 26)

The numbers you set for checkboxHeight and radioHeight should be one quarter
of the total height of the image want to use for checkboxes and radio
buttons. Both images should contain the four stages of both inputs stacked
on top of each other in this order: unchecked, unchecked-clicked, checked,
checked-clicked.

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

The value of selectWidth should be the width of your select list image.

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

*/

/*
var checkboxWidth = "20";
var checkboxHeight = "19";
var radioWidth = "20";
var radioHeight = "19";
var selectWidth = "63";
*/

/* No need to change anything after this */


//document.write('<style type="text/css">input.styled { display: none; } select.styled { position: relative; width: ' + selectWidth + 'px; opacity: 0; filter: alpha(opacity=0); z-index: 5; } .disabled { opacity: 0.5; filter: alpha(opacity=50); }</style>');
//document.write('<style type="text/css">input.'+CustomElement.selectorName+' { display: none; } select.'+CustomElement.selectorName+' { position: relative; width: ' + CustomElement.selectWidth + 'px; opacity: 0; filter: alpha(opacity=0); z-index: 5; } .'+CustomElement.className+'disabled { opacity: 0.5; filter: alpha(opacity=50); }</style>');
CustomElement = {
    checkboxWidth: "20",
    checkboxHeight: "19",
    radioWidth: "20",
    radioHeight: "19",
    selectWidth: "63",
    selectorName: "styled",
    className: "custom",
    inited: false,
    objects: [],
    objectSpans: [],
    init: function()
    {
        if(CustomElement.inited) { CustomElement.rescan(); return; }
        CustomElement.objects = Array();
        CustomElement.objectSpans = Array();
        CustomElement.inited = true;
        CustomElement.rescan();
    },
    rescan: function()
    {
        var inputs = $$("input");
        for(var i=0;i<inputs.length;i++)
        {
            if(CustomElement.objects.indexOf(inputs[i])==-1)
            {
                // New element, add it
                if((inputs[i].type == "checkbox" || inputs[i].type == "radio") && inputs[i].className == CustomElement.selectorName) {
                    var span = document.createElement("span");
                    span.className = CustomElement.className+"-"+inputs[i].type;

                    if(inputs[i].checked == true) {
                        if(inputs[i].type == "checkbox") {
                            position = "0 -" + (CustomElement.checkboxHeight*2) + "px";
                            span.style.backgroundPosition = position;
                        } else {
                            position = "0 -" + (CustomElement.radioHeight*2) + "px";
                            span.style.backgroundPosition = position;
                        }
                    }
                    inputs[i].parentNode.insertBefore(span, inputs[i]);
                    Event.observe(inputs[i],"change", CustomElement.clear, false);
                    if(!inputs[i].getAttribute("disabled")) {
                        Event.observe(span,"mousedown",CustomElement.pushed, false);
                        Event.observe(span,"mouseup",CustomElement.check, false);
                    } else {
                        span.className = span.className += " "+CustomElement.className+"disabled";
                    }
                    CustomElement.objects.push(inputs[i]);
                    CustomElement.objectSpans.push(span);
                }
            }
        }
        // Select
        //inputs = $$("select");
        inputs = [];
        for(i=0;i<inputs.length;i++)
        {
            if(CustomElement.objects.indexOf(inputs[i])==-1)
            {
                if(inputs[i].className.indexOf(CustomElement.selectorName)!=-1) {
                    // Stupid IE hidden hack ...
                    if($(inputs[i]).offsetWidth == 0) continue;
                    option = inputs[i].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);
                        }
                    }
                    var span = document.createElement("span");
                    span.className = CustomElement.className+"-select";
                    span.id = "select" + inputs[i].name;
                    span.appendChild(textnode);
                    $(inputs[i]).insert({before: span});
                    if(!$(inputs[i]).hasAttribute("disabled")) {
                        Event.observe($(inputs[i]),"change", CustomElement.choose, false);
                    } else {
                        inputs[i].previousSibling.className = inputs[i].previousSibling.className += " "+CustomElement.className+"disabled";
                    }
                    CustomElement.objects.push(inputs[i]);
                    CustomElement.objectSpans.push(span);
                }
            }
        }
        CustomElement.updateDisplay();
    },
    updateDisplay: function() {
        if(!CustomElement.inited) CustomElement.init();
        for(var i in CustomElement.objectSpans)
        {
            if($(CustomElement.objectSpans[i]).nextSibling != null)
            {
                if($(CustomElement.objectSpans[i]).nextSibling.getWidth()!=0) {
                    if($(CustomElement.objectSpans[i]).hasClassName(CustomElement.className+"-select")) {
                        $(CustomElement.objectSpans[i]).style.width = parseInt($(CustomElement.objectSpans[i]).nextSibling.getWidth()-4)+"px";
                        CustomElement.handleDisabled($(CustomElement.objectSpans[i]));
                    }
                    if($(CustomElement.objectSpans[i]).hasClassName(CustomElement.className+"-checkbox")) {
                        $(CustomElement.objectSpans[i]).style.width = CustomElement.checkboxWidth;
                        CustomElement.handleDisabled($(CustomElement.objectSpans[i]));
                        //CustomElement.checkInline($(CustomElement.objectSpans[i]));
                    }
                    if($(CustomElement.objectSpans[i]).hasClassName(CustomElement.className+"-radio")) {
                        $(CustomElement.objectSpans[i]).style.width = CustomElement.radioWidth;
                        CustomElement.handleDisabled($(CustomElement.objectSpans[i]));
                        //CustomElement.checkInline($(CustomElement.objectSpans[i]));
                    }
                }
            }
        }
    },
    handleDisabled: function(el) {
        var remove = -1;
        if(el.nextSibling.hasAttribute("disabled"))
        {
            if(!el.hasClassName(CustomElement.className+"disabled"))
            {
                el.addClassName(" "+CustomElement.className+"disabled");
                remove = true;
            }
        } else {
            if(el.hasClassName(CustomElement.className+"disabled"))
            {
                el.removeClassName(" "+CustomElement.className+"disabled");
                remove = false;
            }
        }
        //alert(el.className+" - "+el.nextSibling.id+" - "+remove);
        if(remove!=-1)
        {
            if(remove==true)
            {
                if(el.className == (CustomElement.className+"-select"))
                {
                    Event.stopObserving(el.nextSibling,"change", CustomElement.choose, false);
                } else {
                    Event.stopObserving(el,"mousedown",CustomElement.pushed, false);
                    Event.stopObserving(el,"mouseup",CustomElement.check, false);
                }
            } else {
                if(el.className == (CustomElement.className+"-select"))
                {
                    Event.observe(el.nextSibling,"change", CustomElement.choose, false);
                } else {
                    Event.observe(el,"mousedown",CustomElement.pushed, false);
                    Event.observe(el,"mouseup",CustomElement.check, false);
                }
            }
        }
    },
    pushed: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + CustomElement.checkboxHeight*3 + "px";
		} else if(element.checked == true && element.type == "radio") {
			this.style.backgroundPosition = "0 -" + CustomElement.radioHeight*3 + "px";
		} else if(element.checked != true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + CustomElement.checkboxHeight + "px";
		} else {
			this.style.backgroundPosition = "0 -" + CustomElement.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 -" + CustomElement.checkboxHeight*2 + "px";
			} else {
				this.style.backgroundPosition = "0 -" + CustomElement.radioHeight*2 + "px";
				group = this.nextSibling.name;
				inputs = document.getElementsByTagName("input");
				for(var a = 0; a < inputs.length; a++) {
					if(inputs[a].name == group && inputs[a] != this.nextSibling) {
						inputs[a].previousSibling.style.backgroundPosition = "0 0";
					}
				}
			}
			element.checked = true;
		}
        if(element.hasAttribute("onclick"))
        {
            var f = element.getAttribute("onclick");
            if(Prototype.Browser.IE) {
                if(Prototype.Browser.IE8)
                {
                    eval(f);
                } else {
                    f.call();
                }
            } else {
                eval(f);
            }
            //alert(element.getAttribute("onclick"));
            //eval(element.getAttribute("onclick"));
        }
        CustomElement.rescan();
	},
	clear: function() {
		inputs = document.getElementsByTagName("input");
		for(var b = 0; b < inputs.length; b++) {
			if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == CustomElement.selectorName) {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + CustomElement.checkboxHeight*2 + "px";
			} else if(inputs[b].type == "checkbox" && inputs[b].className == CustomElement.selectorName) {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			} else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == CustomElement.selectorName) {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + CustomElement.radioHeight*2 + "px";
			} else if(inputs[b].type == "radio" && inputs[b].className == CustomElement.selectorName) {
				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;
			}
		}
        if(this.hasAttribute("onclick"))
        {
            eval(this.getAttribute("onclick"));
        }
        CustomElement.rescan();
	}
}

//document.write('<style type="text/css">input.'+CustomElement.selectorName+' { display: none; } select.'+CustomElement.selectorName+' { position: relative; width: ' + CustomElement.selectWidth + 'px; opacity: 0; filter: alpha(opacity=0); z-index: 5; } .'+CustomElement.className+'disabled { opacity: 0.5; filter: alpha(opacity=50); }</style>');
document.write('<style type="text/css">input.'+CustomElement.selectorName+' { display: none; } .'+CustomElement.className+'disabled { opacity: 0.5; filter: alpha(opacity=50); }</style>');
/*
Custom = {
    isInited: false,
    spanObjects: null,
    initedElements: null,
	init: function() {
		if(Custom.isInited) return;
        Custom.spanObjects = Array();
        Custom.initedElements = Array();
        var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
		for(a = 0; a < inputs.length; a++) {
			if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
				span[a] = document.createElement("span");
				span[a].className = "custom-"+inputs[a].type;

				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;
					}
				}
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				inputs[a].onchange = Custom.clear;
				if(!inputs[a].getAttribute("disabled")) {
					span[a].onmousedown = Custom.pushed;
					span[a].onmouseup = Custom.check;
				} else {
					span[a].className = span[a].className += " disabled";
				}
			}
		}
		inputs = document.getElementsByTagName("select");
		for(a = 0; a < inputs.length; a++) {
			if(inputs[a].className.indexOf("styled")!=-1) {
				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] = document.createElement("span");
				span[a].className = "custom-select";
				span[a].id = "select" + inputs[a].name;
                var tmpObj = $(inputs[a]);
                //span[a].style.width = (parseInt($(inputs[a]).getWidth())-4)+"px";
				span[a].appendChild(textnode);
                alert(inputs[a].id);
                $(inputs[a]).insert({before: span[a]});
				//inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				if(!inputs[a].getAttribute("disabled")) {
					//inputs[a].onchange = Custom.choose;
                    //inputs[a].addEventListener("change", Custom.choose, false);
                    Event.observe($(inputs[a]),"change", Custom.choose, false);
				} else {
					inputs[a].previousSibling.className = inputs[a].previousSibling.className += " disabled";
				}
                Custom.spanObjects.push(span[a]);
			}
		}
		document.onmouseup = Custom.clear;
        Custom.isInited = true;
        Custom.updateDisplay();
	},
    updateDisplay: function() {
        if(!Custom.isInited) return;
        alert(Custom.spanObjects.length);
        for(var i in Custom.spanObjects)
        {
            if($(Custom.spanObjects[i]).nextSibling != null)
            {
                if($(Custom.spanObjects[i]).nextSibling.getWidth()!=0) {
                    $(Custom.spanObjects[i]).style.width = parseInt($(Custom.spanObjects[i]).nextSibling.getWidth()-4)+"px";
                }
            }
        }
    },
	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] != this.nextSibling) {
						inputs[a].previousSibling.style.backgroundPosition = "0 0";
					}
				}
			}
			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 && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else if(inputs[b].type == "checkbox" && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			} else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
			} else if(inputs[b].type == "radio" && inputs[b].className == "styled") {
				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;
			}
		}
	}
}
*/
var BFCustom = CustomElement;
window.onload = BFCustom.init;