(function () {
    
function populate_specialties() {
    if (typeof window.Resurgens == "undefined")
    {
        window.setTimeout(populate_specialties, 250);
        return;
    }
    // populate the fields in the physicians overlay
    var specs_field = $('form.select .by-spec select');
    specs_field.each(function () {
        var specs = window.Resurgens.specialties_by_id;
        var sel = $(this).get(0);
        var i = 0;
        for (id in specs)
        {
            i++;
            sel.options[i] = new Option(specs[id].name, id);
        }
    });
    update_phys_matches();
}
    
function populate_locations() {
    if (typeof window.Resurgens == "undefined")
    {
        window.setTimeout(populate_locations, 250);
        return;
    }
    // populate the fields in the physicians overlay
    var locs_field = $('form.select .by-loc select');
    locs_field.each(function () {
        var locs = window.Resurgens.locations_by_id;
        var sel = $(this).get(0);
        var i = 0;
        for (id in locs)
        {
            i++;
            sel.options[i] = new Option(locs[id].name, id);
        }
    });
    update_phys_matches();
}

function update_phys_matches()
{
    var spec_map = window.Resurgens.physicians_by_specialty;
    var loc_map = window.Resurgens.physicians_by_location;
    $('form.select .by-spec select').each(function () {
        var f = $(this).closest('form');
        var spec_field = f.find('.by-spec select');
        var loc_field = f.find('.by-loc select');
        var res = f.find('div.results');
        if (!spec_field.val() && !loc_field.val())
        {
            res.html('');
            return;
        }
        var matches_spec = false;
        var spec_sel = false;
        var matches_loc = false;
        var loc_sel = false;
        var matches = [];
        if (spec_field.val())
        {
            var val = spec_field.val();
            if (spec_map[val])
            {
                matches_spec = spec_map[val].members;
                spec_sel = spec_map[val].name;
            }
        }
        if (loc_field.val())
        {
            var val = loc_field.val();
            if (loc_map[val])
            {
                matches_loc = loc_map[val].members;
                loc_sel = loc_map[val].name;
            }
        }
        if (matches_spec && matches_loc)
        {
            for (var i = 0; i < matches_spec.length; i++)
            {
                if ($.inArray(matches_spec[i], matches_loc) != -1)
                {
                    matches.push(matches_spec[i]);
                }
            }
        }
        else
        {
            if (matches_spec)
            {
                matches = matches_spec;
            }
            if (matches_loc)
            {
                matches = matches_loc;
            }
        }
        if (matches.length == 0)
        {
            res.html('<span class="regret">Sorry, no matches.</span>');
        }
        else
        {
            var l = '/staff/physicians?';
            var vars = {};
            if (spec_sel)
            {
                l = l+'spec='+encodeURIComponent(spec_sel);
                if (loc_sel)
                {
                    l = l+'&';
                }
            }
            if (loc_sel)
            {
                l = l+'loc='+encodeURIComponent(loc_sel);
            }
            res.html('<a>View '+(matches.length)+' matching physician'+(matches.length != 1 ? 's' : '')+' →</a>');
            res.find('a').attr('href', l);
        }
    });
}

function reset_physicians(phys_field)
{
    if (typeof phys_field == 'undefined')
    {
        phys_field = $('form.select .by-name select');
    }
    var phys = window.Resurgens.physicians_by_id;
    phys_field.each(function () {
        var sel = $(this).get(0);
        var i = 0;
        sel.options.length = 1;
        for (id in phys)
        {
            if (phys[id].phys == false)
            {
                continue;
            }
            i++;
            sel.options[i] = new Option(phys[id].name, phys[id].name);
        }
        $(this).find('option').eq(0).text('Physicians ('+i+')');
    });
}
    
$(document).ready(function () {
    
    // search box
    if ($('#header .search form').attr('action') != 'http://www.google.com/cse')
    {
        $('#header .search form').submit(function () {
            var q_field = $(this).find('.field input');
            if (!q_field.val() || q_field.val() == q_field.attr('placeholder'))
            {
                e.preventDefault();
                return;
            }
            var q = q_field.val();
            q_field.removeAttr('name');
            var real_q = $('<input type="hidden" name="q" />');
            real_q.val(q+' site:www.resurgens.com');
            $(this).prepend(real_q);
        });
    }
    
    // nav overlays
    var nav_overlays = $('.nav-overlay');
    var ol_hovering = false;
    var ol_showing = false;
    var ol_focused = false;
    
    function open_overlay($li)
    {
        $li.addClass('hover '+$li.data('hoverclass'));
        ol_showing = $li;
    }
    
    function close_overlay($li)
    {
        $li.removeClass('hover '+$li.data('hoverclass'));
        if (ol_showing && ol_showing.get(0) == $li.get(0))
        {
            ol_showing = false;
        }
    }
    
    function mark_focused()
    {
        ol_focused = $(this).closest('li.nav-link');
    }
    
    function maybe_close()
    {
        ol_focused = false;
        var $li = $(this).closest('li.nav-link');
        window.setTimeout(function () {
            if ((ol_focused && ol_focused.get(0) == $li.get(0)) ||
                (ol_hovering && ol_hovering.get(0) == $li.get(0)))
            {
                return;
            }
            close_overlay($li);
        }, 250);
    }
    
    $('#nav .self a').each(function () {
        var hoverclass = 'nav-'+$(this).data('navname')+'-hover';
        if ($(this).closest('li').hasClass('active'))
        {
            hoverclass = hoverclass+' nav-'+$(this).data('navname')+'-active-hover';
        }
        $(this).closest('li').data('hoverclass', hoverclass);
        if ($(this).hasClass('has-overlay'))
        {
            var which = $('#nav-'+($(this).data('navname'))+'-overlay');
            $(this).closest('li').append(which);
        }
    }).closest('li').hover(function () {
        ol_hovering = $(this);
        if (ol_focused)
        {
            return;
        }
        open_overlay($(this));
    }, function () {
        if (ol_hovering && ol_hovering.get(0) == $(this).get(0))
        {
            ol_hovering = false;
        }
        if (ol_focused && ol_focused.get(0) == $(this).get(0))
        {
            return;
        }
        if (ol_showing && ol_showing.get(0) != $(this).get(0))
        {
            return;
        }
        close_overlay($(this));
    }).find('.nav-overlay input, .nav-overlay select')
        .bind('focus', mark_focused)
        .bind('blur', maybe_close);
    
    // specialty centers rollbar
    $('#nav-specialty-overlay .left-side a').hover(function () {
        var $a = $(this);
        if ($a.attr('href') == '#')
        {
            $a.data('name', $a.text()).text('Coming soon!').addClass('coming-soon');
        }
    }, function () {
        var $a = $(this);
        if ($a.hasClass('coming-soon'))
        {
            $a.text($a.data('name')).removeClass('coming-soon');
        }
    }).click(function (e) {
        if ($(this).attr('href') == '#')
        {
            e.preventDefault();
        }
    });
    
    // specialty centers in footer
    $('#footer .network li a').hover(function () {
        var $a = $(this);
        if ($a.attr('href') == '#')
        {
            $a.data('name', $a.text()).text('Coming soon!').addClass('coming-soon');
        }
    }, function () {
        var $a = $(this);
        if ($a.hasClass('coming-soon'))
        {
            $a.text($a.data('name')).removeClass('coming-soon');
        }
    }).click(function (e) {
        if ($(this).attr('href') == '#')
        {
            e.preventDefault();
        }
    });
    
    // add .focus to focused inputs
    $('input[type="text"]')
        .each(function () {
            var ph = $(this).attr('placeholder');
            if (ph && $(this).attr('value') == '')
            {
                $(this).attr('value', ph);
            }
            if (ph)
            {
                var i = $(this);
                var f = $(this).closest('form').submit(function (e) {
                    if (i.attr('value') == ph)
                    {
                        i.attr('value', '');
                    }
                });
            }
        })
        .focus(function () {
            $(this).addClass('focus');
            var ph = $(this).attr('placeholder');
            if (ph && $(this).attr('value') == ph)
            {
                $(this).attr('value', '');
            }
        })
        .blur(function () {
            $(this).removeClass('focus');
            var ph = $(this).attr('placeholder');
            if (ph && $(this).attr('value') == '')
            {
                $(this).attr('value', ph);
            }
        });

    // physicians overlay
    $('form.select .by-spec select').bind('keyup change', update_phys_matches);
    $('form.select .by-loc select').bind('keyup change', update_phys_matches);
});
$(window).load(populate_specialties);
$(window).load(populate_locations);
})();
