// jQuery closure
(function($, window, undefined){
    // domready
    $(function(){
        // colorbox url-from-rel re-bind
        $('a.cbox[href]').filter('.url-from-rel').each(function(){
            $.extend($(this).data('colorbox'), $.colorbox.defaultSettings, { href: $(this).attr('rel') });
        });
        
        // arrow key nav for articles
        $(document).keyup(function(e){
            if($('#colorbox').is(':hidden')){
                if(e.which == 37){
                    $('#main_content').find('li.pagination-prev a').each(function(){
                        window.location.href=$(this).attr('href');
                    });
                }else if(e.which == 39){
                    $('#main_content').find('li.pagination-next a').each(function(){
                        window.location.href=$(this).attr('href');
                    });
                }
            }
        });
        
        // content footer - tweets
        if($('#footer_tweets').length){
            $('#footer_tweets').JSONFeed($.extend({}, $().JSONFeed.presets.twpDjangoModeration, {
                avatar_width: 32,
                avatar_height: 32,
                url: 'http://projects.washingtonpost.com/moderation/twitter-feed/topsecretamerica/recent.json',
                appendCallback: function(){
                    $().JSONFeed.utils.hovercardsWithUsernames(this);
                    $(this).find('.wp-jf-timestamp').each(function(){
                        var date = new Date( Date.parse($(this).text()) );
                        date = new Date(date.getTime()-3600000);
                        $(this).text(date.toUTCString()).relativeDate();
                    });
                    $(this).children().hide().each(function(idx, el){
                        setTimeout(function(){
                            $(el).fadeIn('slow');
                        }, idx*50);
                    });
                }
            }));
        }
        
        // state profile expander
        $('#description_remainder').next('#description_show').remove();
        $('.expanded-text, #description_remainder').after('<a href="#" class="expanded-text-trigger">Read full description &raquo;</a>')
            .next().click(function(e){
                $(this).prev('.expanded-text, #description_remainder').slideToggle('fast');
                ($(this).html(($(this).text().match('Read full description'))?' Close description':'Read full description &raquo;'));
                return false;
            });
        
        // state profile quotes
        $('.state-profile-quotes').cycle({
            timeout: 30000,
            pause: true,
            next: 'blockquote'
        });
        
        // state profile table cycles
        $('table.callout-striped.pager').each(function(){
            var self = $(this);
            self.find('thead').eq(0).append('<tr class="even-secondary"><td colspan="9999"><div class="pager"><a class="prev" href="#">&laquo; Previous</a> <a class="next" href="#">Next &raquo;</a></div></td></tr>');
            self.find('tbody').not(':first').hide();
            self.find('.pager a').click(function(e){
                e.preventDefault();
                var slides = $(this).parents('table').find('tbody'),
                    curr = slides.filter(':visible').eq(0);
                    idx = slides.index(curr);
                curr.hide();
                if($(this).is('.next')){
                    slides.eq((idx == slides.length-1)?0:idx+1).show();
                }else{
                    slides.eq((idx==0)?slides.length-1:idx-1).show();
                }
            });
        });
        
        $('table.callout-striped.multipage').each(function(){
            $(this).find('tbody').not(':first').hide();
            var cols = $(this).find('thead tr').eq(0).find('th').length;
            $(this).append('<tfoot><tr><td colspan="'+cols+'" style="text-align:center"><a href="#" class="more">Show 20 more &hellip;</a> &nbsp; &nbsp; <a href="#" class="all">Show all &raquo;</a></td></tr></tfoot>')
                .find('a.more').click(function(e){
                    e.preventDefault();
                    var last = $(this).parents('table').find('tbody:visible').last();
                    last.next('tbody').show();
                    if(!last.next('tbody').next('tbody').length){
                        $(this).parents('tfoot').remove();
                    }
                }).end().find('a.all').click(function(e){
                    e.preventDefault();
                    $(this).parents('table').find('tbody').show()
                        .end().end().parents('tfoot').remove();
                });
        });
        
        // search fields...
        $('input[type=text]').focus(function(){
            var val = $(this).val();
            if(val == '(e.g. New York)' || val == '(e.g. General Dynamics)'){
                $(this).data('val-label', $(this).val())
                    .val('');
            }
        }).blur(function(){
            if($(this).val() == ''){
                $(this).val($(this).data('val-label'))
            }
        });
        
        // firefox...
        $('iframe').each(function(){
            $(this).attr('src', $(this).attr('src'));
        });
        
        $('.simple-tabs li a').click(function(e){
            e.preventDefault();
            $(this).parent('li').addClass('active').siblings().removeClass('active')
            $($(this).attr('href')).show().siblings('.panel').hide();
        });
        
        $('#state_profile_list').find('#searchResultsTable table').each(function(){
            $(this).tablesorter()
            .bind('sortEnd', function(){
                $(this).find('tbody tr').each(function(idx, el){
                    if(idx % 2){
                        $(this).addClass('shadedrow');
                    }else{
                        $(this).removeClass('shadedrow');
                    }
                });
            });
        });
        
    });
})(jQuery, window)

