jQuery(function($){
    $('a.comment-reply-link').each(function(i,item){
        $(this).click(function(){
            var name, comment, parent_id;
            name = $(this).parents('div.listing-item').find('div.byline div.element.name').text();
            comment = $(this).parents('div.listing-item').find('div.content p:first-child').text();
            parent_id = $(this).parents('div.listing-item').find('input[type=hidden]').val();
            
            $('form.comments-form p.in-response-to').show()
                .find('strong.name').text(name)
                .siblings('strong.comment').text(comment)
                .parents('form.comments-form').find('#StoryCommentParentId').val(parent_id);
        });
    });
    
    if ( typeof gallery != 'undefined' ) {
        $(gallery).each(function(i,item){
            
            $('div.gallery-thumbnails').append(
                $('<div />').addClass('thumbnail').append(
                    $('<a />').attr('href',item.full).append(
                        $('<img />').attr('src',item.thumb)
                    ).click(function(event){
                        event.preventDefault();
                        
                        var currentPhoto = $('div.gallery-photo img').add('div.gallery-photo div');
                        currentPhoto.css('z-index',10);
                        
                        if ( currentPhoto.attr('src') == item.full ) return;
                        
                        var newPhoto = $('<div />').hide().addClass('wrapper').append(
                            $('<div />').addClass('canvas').append(
                                $('<img />').attr('src', item.full )
                            )
                        ).append(
                            $('<div />').addClass('photo-caption').text( item.caption )
                        ).append(
                            $('<div />').addClass('photo-credit').text( item.credit )
                        );
                        $('div.gallery-photo').append(newPhoto);
                        
                        currentPhoto.fadeOut(1000);
                        newPhoto.fadeIn(1000, function(){ currentPhoto.remove(); });
                        
                    })
                )
            );
        });
    }
    
    $('td.stories-gallery div.thumbnail a').click(function(event){
    });
    
    $('td.stories-sidebar select.sorter').change(function(){
        $('td.stories-sidebar div.sidebar-listing').hide().filter('.'+$(this).val()).show();
    }).change();
    
    $('form.stories-form textarea').bind( 'change keyup blur mouseup', function( event ) {
        
        var limit = 20000;
        $('form.stories-form .textarea .caption span.count').text( $(this).val().length );
        if ( $(this).val().length >= limit ) {
            $(this).addClass( 'too-long' );
            $('form.stories-form  .submit input').attr('disabled','disabled');
            $('form.stories-form  .textarea .caption span.too-long').show();
        } else {
            $(this).removeClass( 'too-long' );
            $('form.stories-form  .submit input').attr('disabled',false);
            $('form.stories-form  .textarea .caption span.too-long').hide();
        }
    }).change();
    
    $('.stories-edit div.thumbnail a.delete').click(function(event){
        event.preventDefault();
        
        var actor = $(this);
        $.get($(this).attr('href'),{},function(json){
            if ( json.success == true ) {
                $(actor).parent('div.thumbnail').fadeOut('slow',function(){$(this).remove()});
            } else {
                alert( json.message );
            }
        },'json');
        
        return false;
    });
    
    $('.stories-edit div.thumbnail').hover(function(event){
        $('div.edit-photo',this).show();
    },function(event){
        $('div.edit-photo').hide();        
    });
    $('.stories-edit div.thumbnail form').submit(function(event){
        event.preventDefault();
        
        var f = $(this);
        
        $('input[type=submit]', f).attr('disabled',true).before(
                $('<div />').addClass('wait')
            ).add('[type=text]').filter('[type=text]').attr('readonly',true);
        $.post( $(f).attr('action'), $(f).serialize(), function(json) {
            $('input', f).attr('disabled',false).attr('readonly',false).siblings('div.wait').remove();
        }, 'json' );
    });
    
    (function(){
        
        if (!$.plot) return;
        
        var storyVisitsSeries = {
            color: '#0066DD',
            data: visits,
            label: "Business Avg.",
            shadowSize: 0
        };
        
        var xMin = (function(){
            var aYearAgo = (new Date()).setDate( (new Date()).getDate()-365 );
            if (visits[0]) return visits[0][0] > aYearAgo ? visits[0][0] : aYearAgo;
            else return null;
        })();
        var options = {
            legend: { show: false },
            yaxis: { tickDecimals: 0 },
            xaxis: {
                mode: "time",
                min: xMin,
                minTickSize: [1, "day"]
            },
            grid: { color: "#999999", borderWidth: 1 }
        };
        
        if ( $("div.graph").length > 0 )
            $.plot($("div.graph"), [storyVisitsSeries], options);
    })();
;});

