jQuery(function()
{
    jQuery('body').set_thickbox_links();
    
    jQuery('#navigation li').hover(
        function()
        {
            jQuery(this).addClass('hover');
            jQuery(this).children('ul').addClass('hover');
            jQuery(this).siblings('li').find('ul.hover').removeClass('hover');
        },
        function()
        {
            jQuery(this).removeClass('hover');
            
            jQuery(this).children('ul').oneTime(1000, function()
            {
                if (jQuery(this).parent().hasClass('hover'))
                {
                    return;
                }
                
                jQuery(this).removeClass('hover');
            });
        }
    );
    
    jQuery('#login_form_bouncer').submit(function()
    {
        if (jQuery('#login_form_wrapper').css('display') == 'block')
        {
            jQuery('#login_form_wrapper').css('display', 'none');
            jQuery('input[name="f_cancel"]').css('display', 'none');
            jQuery('input[name="f_submit"]').css('display', 'block');
        }
        else
        {
            jQuery('#login_form_wrapper').css('display', 'block');
            jQuery('input[name="f_cancel"]').css('display', 'block');
            jQuery('input[name="f_submit"]').css('display', 'none');
        }
        
        return false;
    });
    
    jQuery('#content-left div').each(function()
    {
        jQuery(this).find('h1:first').addClass('first');
    });
    
    jQuery('.ajax_loader thead').ajax_links();
    
    if (jQuery('#up_label').find('input[type="hidden"]').size() > 0)
    {
        jQuery('#up_label').css('display', 'none');
    }
});

jQuery.fn.ajax_links = function()
{
    jQuery(this).find('a').each(function(i)
    {
        jQuery(this).click(function()
        {
            var href = jQuery(this).attr('href');
            
            if (href.match(/(\?|&)ajax/))
            {
            }
            else if (href.match(/\?/))
            {
                href += '&ajax';
            }
            else
            {
                href += '?ajax';
            }
            
            jQuery(this).parents('.ajax_loader').load(
                href,
                {},
                function()
                {
                    jQuery('.ajax_loader a.thickbox').each(function()
                    {
                        tb_init(jQuery(this));
                    });
                    
                    jQuery('.ajax_loader thead').ajax_links();
                    jQuery('.ajax_loader').set_thickbox_links();
                }
            );
            
            return false;
        });
    });
}

jQuery.fn.set_thickbox_links = function()
{
    jQuery(this).find('a.thickbox').each(function()
    {
        var href = jQuery(this).attr('href');
        
        if (!href)
        {
            return;
        }
        
        if (href.match(/\?/))
        {
            href += '&ajax';
        }
        else
        {
           href += '?ajax';
        }
        
        jQuery(this).attr('href', href);
    });
}

jQuery.fn.ie_margins = function()
{
    jQuery(this).find('ul.level-1 > li > ul').each(function()
    {
        var width = jQuery(this).parent().width();
        jQuery(this).css('margin-left', '-' + width + 'px');
    });
}
