$(document).ready(function()
{
    /* adjust styling for macosx-based browsers */
    if (navigator.appVersion.indexOf('Mac') != -1 || $.browser.safari) {
        $('head').append('<link type="text/css" rel="stylesheet" media="all" href="/ui/css/macosx.css" />');
    }
    
    /* stylize drop-down menus */
    $('.browse-select p').toggle(
        function () {
            $(this).parent().focus();
            $(this).next().slideDown(100);
        },
        function () {
            $(this).parent().blur();
            $(this).next().slideUp(100);
        }
    );
    $('.browse-select li a').click(function() {
        $(this).parent().parent().prev().text($(this).text());
        $(this).parent().parent().slideUp(100);
    });
    
    if ($.browser.msie && $.browser.version == '6.0') {
        $('.badge img').attr('src', function() {
            return this.src.replace('.png', '-ie6.png');
        });
    }

    // adds functionality to customized drop-down menus
    $('.browse-select').each(function()
    {
        var thisMenu = this;
        var menuClose = function(o) { o.hide(); };
        var menuToggle = function(o) {
            // close existing drop-downs
            menuClose($('.browse-select').find('ul'));
            
            if (o.is(':hidden')) {
                o.show();
            }
            else {
                o.hide();
            }
        };
        
        // open, close from box
        jQuery(this).find('p').toggle(
            function () {
                menuToggle($(this).next());
            },
            function () {
                menuToggle($(this).next());
            }
        );
        
        // make selection
        jQuery(this).find('li a').click(function() {
            jQuery(thisMenu).find('p').text($(this).text());
            menuToggle($(this).parent());
        });
        
        // close when not in use
        jQuery().click(function() {
            menuClose(jQuery(thisMenu).find('ul'));
        });
    });

    // show second-tier navigation
    $('#primary li').hover(
        function () {
            $(this).find('a').siblings().show();
        },
        function () {
            $(this).find('a').siblings().hide();
        }
    );
    
    // Set up browse form submission.
    $('#browse-form select').change( function () {
        var val = this.options[this.selectedIndex].value;
        if (val == '') return false;
        document.location = val;
    });
    
    // onrollover button for form submit image buttons - toggle "ovr" to the filename
    $("form input[type='image'][src*='/ui/img/form_btn_submit_']").hover(
        function () {
            $(this).attr('src', function() {
                return this.src.replace('.png', '_ovr.png');
            });
        },
        function () {
            $(this).attr('src', function() {
                return this.src.replace('_ovr', '');
            });
        }
    );
    
    // most read, most emailed, best rated
    $('.block-topfive ul.links').tabs();
    // comments: convert various links to Ajax links.
    $('#comments #best-comments').bind('click', function () {
        $('#comments ul.tabs li.best').addClass('tabs-selected');
        $('#comments ul.tabs li.all').removeClass('tabs-selected');
        $('#comments li.comment-filtered').hide();
        $.get('/cms/comment/commentsfilter/ajax/1');
	return false;
    });
    $('#comments #all-comments').bind('click', function () {
        $('#comments ul.tabs li.best').removeClass('tabs-selected');
        $('#comments ul.tabs li.all').addClass('tabs-selected');
        $('#comments li.comment-filtered').show();
        $.get('/cms/comment/commentsfilter/ajax/0');
	return false;
    }); 
    // Convert links to Ajax links.
    $('a.comment_suggest_offensive').bind('click', function () {
        var href = this.href.replace(/offensive\//, 'offensive/ajax/');
        var span = $('<span class="' + this.className + '">Suggesting…</span>');
        var elem = $(this).replaceWith(span);
        $.get( href, function (data, status) {
            if (status == 'success') span.html('Suggested as offensive')
            else span.html('Could not suggest as offensive')
        });
        return false;
    });
    $('a.comment_editors_picks_suggest').bind('click', function () {
        var href = this.href.replace(/suggest\//, 'suggest/ajax/');
        var span = $('<span class="' + this.className + '">Suggesting…</span>');
        var elem = $(this).replaceWith(span);
        $.get( href, function (data, status) {
            if (status == 'success') span.html('Suggested as a best comment')
            else span.html('Could not suggest as a best comment')
        });
        return false;
    });

    // set dimensions for #slideshow
    $('#slideshow').css({
        "height": $('#slideshow .slide:first').height() + 'px'
    });
    
    $('#slideshow ul').css({
        "width": ( $('#slideshow .slide').siblings().length * $('#slideshow').width() ) + 'px'
    });
    
    // enable slideshow for photo essays
    $('.page-article-photo #slideshow').serialScroll({
        items: 'li.slide',
        prev: '.page-article-photo .photo-nav .prev a',
        next: '.page-article-photo .photo-nav .next a',
        offset: 0,
        start: 0,
        duration: 1200,
        force: true,
        stop: true,
        lock: false,
        cycle: false,
        easing: 'easeOutQuart', //use this easing equation for a funny effect
        onBefore: function (e, t) {
            $('#slideshow').css('height', $(t).height()+'px');
        },
        onAfter: function (e) {
            $('#slideshow').animate({"height": ($(e).height() + 'px')}, 100);
            var curr = $(e).prevAll().length+1;
            var total = $('#slideshow li').siblings().length;
            
            $('.page-article-photo .photo-nav .nfo .curr').html(curr);
            //$('.page-article-photo .photo-nav .nfo .total').html(total);
            
            // gray out inactive nav buttons
            if (curr == 1) {
                $('.page-article-photo .photo-nav .prev a').addClass('off');
            }
            else {
                $('.page-article-photo .photo-nav .prev a').removeClass('off');
            }
            if (curr == total) {
                $('.page-article-photo .photo-nav .next a').addClass('off');
            }
            else {
                $('.page-article-photo .photo-nav .next a').removeClass('off');
            }
        }
    });
    
    // article text resize
    $('.block-pagetools .textsize a').bind('click', function()
    {
        var article = $('.node-type-article .content, .node-type-full_article .content');
        article.attr('class', 'content'); // set to default
        
        switch ($(this).attr('class'))
        {
            case 'sm': article.addClass('sm'); break;
                
            case 'lg': article.addClass('lg'); break;
                
            case 'md':
            default: break;
        }
        
        // set active state
        $(this).addClass('active');
        $(this).siblings().removeClass('active');
    });
});

// Easing equation, borrowed from jQuery easing plugin
// http://gsgd.co.uk/sandbox/jquery/easing/
jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
    return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

