$(function(){
    
    // If a good/decent browser
    if ( ($.browser.msie && $.browser.version > 6) || !$.browser.msie )
    {   
        $('#nav_main li').hover(function(){
            $(this).children('.nav_main_sub').stop(true, true).slideDown(300);
        }, function(){
            $(this).children('.nav_main_sub').stop(true, true).slideUp('fast');
        });
        
        $('ul.features li').hoverIntent(function(){
            var self = $(this);
            var count = self.attr('rel');

            self.siblings().find('.overlay').stop(true, true).fadeIn(200);
            $('.summaries .summary'+count).stop(true, true).fadeIn(200);
            
        }, function(){
            var self = $(this);
            var count = self.attr('rel');
            
            self.siblings().find('.overlay').stop(true, true).fadeOut(200);
            $('.summaries .summary'+count).stop(true, true).fadeOut(200);
        });
    } 
    else
    {
        $('#nav_main li').hover(function(){
            $(this).children('.nav_main_sub').show();
        }, function(){
            $(this).children('.nav_main_sub').hide();
        });
    }
    
    //setCloudHeight();
    
    $('ul.fields label, .nav_categories label').autoWidth({padding: 10});
    
    // align the drop downs to the middle of the nav link
    $('#nav_main > li').each(function(i){
        if( $('body.home').length == 1 && i == 0 ) {
            // So the first item on the home page is left aligned. Everything else is centered.
        } else {
            var width = $(this).width();
            var sub = $(this).children('.nav_main_sub');
            var sub_width = sub.width();
            var position = (sub_width / 2) - (width / 2);
            sub.css('left', parseInt('-' + position));
        }
    });
    
    $('a[title="Edit this item"]').addClass('ee_edit_icon');
    
    $('.listing .title').not('.news .title').toggle(function(){
        //setCloudHeight();
        $(this).parents('li').addClass('active').children('.body').slideDown('fast');
    }, function(){
        $(this).parents('li').removeClass('active').children('.body').slideUp('fast');
    });
    
    $('a.clear').click(function(){
        $('.nav_categories select').attr('selectedIndex', 0);
    });
    
});


// function setCloudHeight() {
//  $('.cloud').height( $(document).height() );
// }








jQuery.fn.autoWidth = function(options) 
{
    var settings = {
        minWidth : false,
        limitWidth : false,
        padding : 0,
        ignore : ''
    }
    
    if(options) {
        jQuery.extend(settings, options);
    };

    var maxWidth = 0;

    this.not(settings.ignore).each(function(){
        if ($(this).width() > maxWidth){
            if(settings.limitWidth && maxWidth >= settings.limitWidth) {
                maxWidth = settings.limitWidth;
            } 
            else if(settings.minWidth && maxWidth <= settings.limitWidth)
            {
                maxWidth = settings.minWidth + settings.padding;
            } 
            else 
            {
                maxWidth = $(this).width() + settings.padding;
            }
        }
    });  
    
    this.not(settings.ignore).width(maxWidth);
}

