var VIZIO = {};

$(document).ready(function()
{
    $('input:radio').click(function()
    {
        var radio = $(this);
        var hiddenId = $(this).closest('dl').attr('id') + '-detail';
        var hiddenElement = $('#' + hiddenId);
        
        if(hiddenElement.length > 0)
        {
            if(radio.is(':checked') && radio.val() == '1')
            {
                $('#' + hiddenId).slideDown();
            }
            else
            {
                $('#' + hiddenId).slideUp();
            }
        }
    });
    
    //  After a failed submission, go through all the checked radios and if they 
    //  are not "no", show their corresponding textareas.
    $('input:radio:checked').each(function()
    {
        var inputValue = $(this).val();
        var hiddenId = $(this).closest('dl').attr('id') + '-detail';
        var hiddenElement = $('#' + hiddenId);
                
        if(hiddenElement.length > 0 && inputValue == '1')
        {
            hiddenElement.show();
        }
    });
    
    // Survey One
    VIZIO.s1q3Actions = function(value)
    {
        if ("wireless" == value) {
            $("#q3a_wireless_difficulty-wrapper").show();
            $("#q3b_explain_no_connection-wrapper").hide();
        } else if ("not connected" == value) {
            $("#q3b_explain_no_connection-wrapper").show();
            $("#q3a_wireless_difficulty-wrapper").hide();
        } else {
            $("#q3a_wireless_difficulty-wrapper").hide();
            $("#q3b_explain_no_connection-wrapper").hide();
        }
    };
    VIZIO.s1q5Actions = function(value)
    {
        if ("cable box" == value || "satellite" == value || "fiber optic set-top box" == value) {
            $("#q5a_program_bluetooth_remote_settop_box-wrapper").show();
            $("#q5a_opentext-wrapper").show();
            $("#q5b_satifcation_control_level_remote-wrapper").show();
        } else {
            $("#q5a_program_bluetooth_remote_settop_box-wrapper").hide();
            $("#q5a_opentext-wrapper").hide();
            $("#q5b_satifcation_control_level_remote-wrapper").hide();
        }
    };

    VIZIO.s3q6Actions = function(value)
    {
        if ("yes" == value || "not sure" == value) {
            $("#q6_opentext-wrapper").show();
        } else {
            $("#q6_opentext-wrapper").hide();
        }
    }
    
    $("input[name=q3_internet_connectivity]:radio").each(function() {
    	if ($(this).attr("checked")) {
            VIZIO.s1q3Actions($(this).val());
    	}
    });
    $("input[name=q3_internet_connectivity]:radio").change(function() {
    	VIZIO.s1q3Actions($(this).val());
    });
    
    $("input[name=q5_tv_primary_signal_receiver]:radio").each(function() {
    	if ($(this).attr("checked")) {
            VIZIO.s1q5Actions($(this).val());
    	}
    });
    $("input[name=q5_tv_primary_signal_receiver]:radio").change(function() {
    	VIZIO.s1q5Actions($(this).val());
    });
    
    // Survey Three
    $("input[name=q6_difficulties_via_dock]:radio").each(function() {
        if ($(this).attr("checked")) {
            VIZIO.s3q6Actions($(this).val());
        }
    });
    $("input[name=q6_difficulties_via_dock]:radio").change(function() {
        VIZIO.s3q6Actions($(this).val());
    });

    // Survey Six
    $("#q12_consider_purchasing_via_brand_products-4").change(function() {
        siblings = {
            0: $("#q12_consider_purchasing_via_brand_products-1"),
            1: $("#q12_consider_purchasing_via_brand_products-2"),
            2: $("#q12_consider_purchasing_via_brand_products-3")
        };

        for (i=0; 3 > i; ++i) {
            if ($(this).is(":checked")) {
                siblings[i].attr("checked", false);
                siblings[i].attr("disabled", "disabled");
            } else {
                siblings[i].attr("disabled", false);
            }
        }
    });
});

VIZIO.showElementRadio = function(value, equals, wrapperId)
{
    if (equals == value) {
        $(wrapperId).show();
    } else {
        $(wrapperId).hide();
    }
};
