/*
Created by: Eric Goodloe
 
Inspired by: Ryan Fait at http://ryanfait.com/.

*/
var optHeight = "22";
var rblHeight = "22";
var ddlWidth = "190";
var joy = true;
var styletext = 'input.customCtrlLarge { display: none; } select.customCtrlLarge { position: relative; width: ' + ddlWidth + 'px; opacity: 0; filter: alpha(opacity=0); z-index: 5; } .disabled { opacity: 0.5; filter: alpha(opacity=50); }';
try
{ document.write('<style type="text/css">' + styletext + '</style>'); }
catch(err)
{ joy = false; }


function getImagePosition(imageHeight, imageNum){
   if (imageNum == 0){return "0 -1px";}
   else {return "0 -" + (((imageHeight * imageNum) + (imageNum * 2)) + 1) +  "px";}
}

// Setup Namespace
var DynamicControls = {
    initialize: function() {
        noSelect();
        if (!joy) {
            var headID = document.getElementsByTagName("head")[0];
            var styles = document.createElement('style');
            styles.setAttribute('type', 'text/css');
            var newStyle = document.createTextNode(styletext);
            styles.appendChild(newStyle);
            headID.appendChild(styles);
        }
        if (document.getElementById("outerframe")) {
            var outer = document.getElementById("outerframe")
            outer.className = outer.className.replace("BrandedRblEnabled-JS", "BrandedRblEnabled");
            outer.className = outer.className.replace("BrandedOptEnabled-JS", "BrandedOptEnabled");
        }

        var inputs = document.getElementsByTagName("input"), span = Array();
        var imageHeight;
        var a;

        // Loop through all the inputs located.
        for (a = 0; a < inputs.length; a++) {
            if (inputs[a].className.indexOf("customCtrlLarge") > -1) {

                if (inputs[a].type == "radio") { imageHeight = rblHeight; }
                if (inputs[a].type == "checkbox") { imageHeight = optHeight; }

                span[a] = document.createElement("span");
                span[a].className = inputs[a].type + "Branded";

                if (inputs[a].checked == true) { span[a].style.backgroundPosition = getImagePosition(imageHeight, 3); }
                else { span[a].style.backgroundPosition = getImagePosition(imageHeight, 0); }

                inputs[a].parentNode.insertBefore(span[a], inputs[a]);
                AddEventObject(span[a], 'change', DynamicControls.refresh);
                AddEventObject(inputs[a], 'change', DynamicControls.refresh);

                if (!inputs[a].getAttribute("disabled")) {
                    AddEventObject(span[a], 'mousedown', DynamicControls.clickDown);
                    AddEventObject(span[a], 'click', DynamicControls.clickUp);

                    AddEventObject(span[a], 'mouseover', DynamicControls.hoverIn);
                    AddEventObject(span[a], 'mouseout', DynamicControls.hoverOut);
                } else {
                    span[a].className = span[a].className += " disabled";
                }
            }
        }

        // If this is a drop down
        inputs = document.getElementsByTagName("select");
        var textnode, option, active;

        // Loop through all the selects
        for (a = 0; a < inputs.length; a++) {
            if (inputs[a].className.indexOf("customCtrlLarge") > -1) {
                var option = inputs[a].getElementsByTagName("option");

                var textnode = document.createTextNode(option[0].childNodes[0].nodeValue);
                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 = "selectBranded";
                span[a].id = "selectSpan" + inputs[a].name;
                span[a].appendChild(textnode);
                inputs[a].parentNode.insertBefore(span[a], inputs[a]);

                if (inputs[a].getAttribute("disabled")) { inputs[a].previousSibling.className = inputs[a].previousSibling.className += " disabled"; }
                else { inputs[a].onchange = DynamicControls.choose; }
            }
        }

    },

    clickDown: function(e) {
        var THIS = currentObjectWithInEvent(this);
        var element = THIS.nextSibling;
        if (element.type == "radio") {
            if (element.checked == true) { THIS.style.backgroundPosition = getImagePosition(rblHeight, 5); }
            else { THIS.style.backgroundPosition = getImagePosition(rblHeight, 2); }
        } else if (element.type == "checkbox") {
            if (element.checked == true) { THIS.style.backgroundPosition = getImagePosition(optHeight, 5); }
            else { THIS.style.backgroundPosition = getImagePosition(optHeight, 2); }
        }
    },

    clickUp: function(e) {
        var THIS = currentObjectWithInEvent(this);

        var element = THIS.nextSibling;

        if (element.type == "radio") {
            element.click();
        } else {
            if (element.type == "checkbox")
                element.click();
        }

        var inputs = document.getElementsByTagName("input");
        // Loop through all inputs
        for (var b = 0; b < inputs.length; b++) {
            if (inputs[b].className.indexOf("customCtrlLarge") > -1) {
                if (inputs[b].type == "radio") {
                    if (element == inputs[b]) {
                        if (inputs[b].checked == true) { inputs[b].previousSibling.style.backgroundPosition = getImagePosition(rblHeight, 4); }
                        else { inputs[b].previousSibling.style.backgroundPosition = getImagePosition(rblHeight, 1); }
                    } else {
                        if (inputs[b].checked == true) { inputs[b].previousSibling.style.backgroundPosition = getImagePosition(rblHeight, 3); }
                        else { inputs[b].previousSibling.style.backgroundPosition = getImagePosition(rblHeight, 0); }
                    }
                } else if (inputs[b].type == "checkbox") {
                    if (element == inputs[b]) {
                        if (inputs[b].checked == true) { inputs[b].previousSibling.style.backgroundPosition = getImagePosition(optHeight, 4); }
                        else { inputs[b].previousSibling.style.backgroundPosition = getImagePosition(optHeight, 1); }
                    } else {
                        if (inputs[b].checked == true) { inputs[b].previousSibling.style.backgroundPosition = getImagePosition(optHeight, 3); }
                        else { inputs[b].previousSibling.style.backgroundPosition = getImagePosition(optHeight, 0); }
                    }
                }
            }
        }
    },

    refresh: function() {
        // Load array of all elements
        var inputs = document.getElementsByTagName("input");
        // Loop through all inputs
        for (var b = 0; b < inputs.length; b++) {
            if (inputs[b].className.indexOf("customCtrlLarge") > -1) {
                if (inputs[b].type == "radio") {
                    if (inputs[b].checked == true) { inputs[b].previousSibling.style.backgroundPosition = getImagePosition(rblHeight, 3); }
                    else { inputs[b].previousSibling.style.backgroundPosition = getImagePosition(rblHeight, 0); }
                } else if (inputs[b].type == "checkbox") {
                    if (inputs[b].checked == true) { inputs[b].previousSibling.style.backgroundPosition = getImagePosition(optHeight, 3); }
                    else { inputs[b].previousSibling.style.backgroundPosition = getImagePosition(optHeight, 0); }
                }
            }
        }
    },

    // Define choose function
    choose: function(e) {
        var THIS = currentObjectWithInEvent(this);
        var option = THIS.getElementsByTagName("option");

        for (d = 0; d < option.length; d++) {
            if (option[d].selected == true) { document.getElementById("selectSpan" + THIS.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue; }
        }
    },

    hoverIn: function(e) {
        var THIS = currentObjectWithInEvent(this);
        var element = THIS.nextSibling;
        if (element.className.indexOf("customCtrlLarge") > -1) {
            if (element.type == "radio") {
                if (element.checked == true) { THIS.style.backgroundPosition = getImagePosition(rblHeight, 4); }
                else { THIS.style.backgroundPosition = getImagePosition(rblHeight, 1); }
            } else if (element.type == "checkbox") {
                if (element.checked == true) { THIS.style.backgroundPosition = getImagePosition(optHeight, 4); }
                else { THIS.style.backgroundPosition = getImagePosition(optHeight, 1); }
            }
        }
    },

    hoverOut: function(e) {
        var THIS = currentObjectWithInEvent(this);
        var element = THIS.nextSibling;

        if (element.className.indexOf("customCtrlLarge") > -1) {
            if (element.type == "radio") {
                if (element.checked == true) { THIS.style.backgroundPosition = getImagePosition(rblHeight, 3); }
                else { THIS.style.backgroundPosition = getImagePosition(rblHeight, 0); }
            } else if (element.type == "checkbox") {
                if (element.checked == true) { THIS.style.backgroundPosition = getImagePosition(optHeight, 3); }
                else { THIS.style.backgroundPosition = getImagePosition(optHeight, 0); }
            }
        }
    }
}




/***********************************************
* Disable select-text script- © Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for full source code
* Modified here to exclude form tags properly and cross browser by jscheuer1
***********************************************/
//form tags to omit:
var omitformtags = ["input", "textarea", "select"]

function disableselect(e) {
    for (var i = 0; i < omitformtags.length; i++)
        if (omitformtags[i] == (e.target.tagName.toLowerCase()))
        return null;
    return false
}

function reEnable() {
    return true
}
function noSelect() {
    if (typeof document.onselectstart != "undefined") {
        document.onselectstart = new Function("return false")
        if (document.getElementsByTagName) {
            tags = document.getElementsByTagName('*')
            for (var j = 0; j < tags.length; j++) {
                for (var i = 0; i < omitformtags.length; i++)
                    if (tags[j].tagName.toLowerCase() == omitformtags[i]) {
                    tags[j].onselectstart = function() {
                        document.onselectstart = new Function('return true')
                    }
                    if (tags[j].onmouseup !== null) {
                        var mUp = tags[j].onmouseup.toString()
                        mUp = 'document.onselectstart=new Function (\'return false\');\n' + mUp.substr(mUp.indexOf('{') + 2, mUp.lastIndexOf('}') - mUp.indexOf('{') - 3);
                        tags[j].onmouseup = new Function(mUp);
                    }
                    else {
                        tags[j].onmouseup = function() {
                            document.onselectstart = new Function('return false')
                        }
                    }
                }
            }
        }
    }
    else {
        document.onmousedown = disableselect
        document.onmouseup = reEnable
    }
}
// All functions have been rendedered to page.  Initialize the procedure
addLoadEvent(DynamicControls.initialize);
