/**
 * @author jasonroy
 */
var highlight_array = [];

$(function()
{
    initializeFocus();
	setupCollapsible();
})
function initializeFocus()
{
    var fields = $('.inputElements input, .inputElements select');
    fields.each(function(i, domElement)
    {
        if ($(domElement).attr('type') == 'radio' || $(domElement).attr('type') == 'checkbox') 
        {
            $(domElement).click(function()
            {
                clearSafariRadios();
                $(this).parents('li').addClass("highlighted");
            });
            $(domElement).focus( function()
            {
                clearSafariRadios();
                $(this).parents('li').addClass("highlighted");
            });
            highlight_array.splice(highlight_array.length, 0, domElement);
        }
        else if ($(domElement).attr('type') != 'button' && $(domElement).attr('type') != 'submit') 
        {
            $(domElement).focus(function()
            {
                clearSafariRadios();
                $(this).parents('li').addClass("highlighted");
            })
            $(domElement).blur(function()
            {
                $(this).parents('li').removeClass("highlighted");
            })
        }
    })
}

function clearSafariRadios()
{
    $.each(highlight_array, function(i, item)
    {
        $(item).parents('li').removeClass('highlighted')
    })
}

function setupCollapsible()
{
    $('.collapsible label:first').each(function(i, domElement)
    {
        $(domElement).toggle(function()
        {
            $(domElement).siblings('.inputElements').slideDown(400);
            $(domElement).parent().addClass("open");
        }, function()
        {
            $(domElement).siblings('.inputElements').slideUp(400);
            $(domElement).parent().removeClass("open");
        })
    })
    
}
