﻿(function () {
    var f = { init: function () {
        var c = document.getElementsByTagName("input"), e = [], g, h;
        for (a = 0; a < c.length; a++) {
            if ((c[a].type == "checkbox" || c[a].type == "radio") && c[a].className == "styled") {
                e[a] = document.createElement("span");
                e[a].className = c[a].type;
                if (c[a].checked == true) {
                    position = c[a].type == "checkbox" ? "0 -" + "25" * 2 + "px" : "0 -" + "25" * 2 + "px";
                    e[a].style.backgroundPosition = position
                }
                c[a].parentNode.insertBefore(e[a], c[a]);
                c[a].onchange = f.clear;
                if (c[a].getAttribute("disabled")) {
                    e[a].className = e[a].className += " disabled"
                } else {
                    e[a].onmousedown = f.pushed;
                    e[a].onmouseup = f.check
                }
            }
        }
        c = document.getElementsByTagName("select");
        for (a = 0; a < c.length; a++) {
            if (c[a].className == "styled") {
                h = c[a].getElementsByTagName("option");
                g = h[0].childNodes[0].nodeValue;
                g = document.createTextNode(g);
                for (b = 0; b < h.length; b++) {
                    if (h[b].selected == true) {
                        g = document.createTextNode(h[b].childNodes[0].nodeValue)
                    }
                }
                e[a] = document.createElement("span");
                e[a].className = "styledselect";
                e[a].id = "select" + c[a].name;
                e[a].appendChild(g);
                c[a].parentNode.insertBefore(e[a], c[a]);
                if (c[a].getAttribute("disabled")) {
                    c[a].previousSibling.className = c[a].previousSibling.className += " disabled"
                } else {
                    c[a].onchange = f.choose
                }

                $(c[a]).change(function () {
                    $(e[a]).text($(this).val());
                });
            }
        }
        document.onmouseup = f.clear
    }, pushed: function () {
        element = this.nextSibling;
        this.style.backgroundPosition = element.checked == true && element.type == "checkbox" ? "0 -" + "25" * 3 + "px" : element.checked == true && element.type == "radio" ? "0 -" + "25" * 3 + "px" : element.checked != true && element.type == "checkbox" ? "0 -25px" : "0 -25px"
    }, 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 -" + "25" * 2 + "px"
            } else {
                this.style.backgroundPosition = "0 -" + "25" * 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 c = 0; c < inputs.length; c++) {
            if (inputs[c].type == "checkbox" && inputs[c].checked == true && inputs[c].className == "styled") {
                inputs[c].previousSibling.style.backgroundPosition = "0 -" + "25" * 2 + "px"
            } else {
                if (inputs[c].type == "checkbox" && inputs[c].className == "styled") {
                    inputs[c].previousSibling.style.backgroundPosition = "0 0"
                } else {
                    if (inputs[c].type == "radio" && inputs[c].checked == true && inputs[c].className == "styled") {
                        inputs[c].previousSibling.style.backgroundPosition = "0 -" + "25" * 2 + "px"
                    } else {
                        if (inputs[c].type == "radio" && inputs[c].className == "styled") {
                            inputs[c].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
            }
        }
    }
    };
    CustomFormInputs = f
})();
