$(document).ready(function() {

    initDropdowns();

});


/*=========================================
    Custom Dropdown Fields
=========================================*/
function initDropdowns() {

    $('.customDropdown').addClass('scriptEnabledDropdown');
    
    
    // Update label
    $('.customDropdown').bind('updateState', function() {

        temp = $('select option:selected', this).text();
        $('.dropdownValue', this).html(temp);

    });
    
    
    // Update initial value
    $('.customDropdown').each(function() {

        tempW = $('select', this)[0].offsetWidth - 18;
        $('.dropdownValue', this).css('width', tempW + 'px');

        temp = $('select', this).parent().parent().trigger('updateState');

    });
    
    
    //Bind events 
    $('.customDropdown select').change(function() {
        $(this).parent().parent().trigger('updateState');
    });

    $('.customDropdown select.park').change(function() {
        $(this).parent().parent().trigger('updateState');
        $(this).next().next().val($(this).val());
    });

    $('.customDropdown select').blur(function() {
        $(this).parent().parent().trigger('updateState');
    });

    $('.customDropdown select').focus(function() {
        $(this).parent().parent().trigger('updateState');
    });

    $('.customDropdown').hover(function() {
        $(this).addClass('customDropdownHover');
    }, function() {
        $(this).removeClass('customDropdownHover');
    })
    

    // Disabled
    $('.customDropdown select[disabled=disabled]').each(function() {
        $(this).parents('.formItem').addClass('fieldDisabled');
    });


    // Error
    $('.customDropdown select.fieldError').each(function() {
        $(this).parents('.formItem').addClass('fieldError');
    });
}
