var PB = {};

PB.TorrentList = function() {
    this.bindCallbacks = function() {
        $('#result table tr[class^=row]').each(function() {
            $(this).find('a.nfo').toggle(
                function(e) {
                    var row = $(this).parent().parent();
                    row.find('td[class=name]').css({
                        'border-bottom-color': 'transparent',
                        'background-color': '#FCFCFC'
                    });
                    var description = row.next('tr.description');
                    var info = description.find('span');
                    if (info.text().length == 0) {
                        info.text('Loading...');
                        info.load('/ajax/nfo/' + row.attr('id'));
                    }
                    jQuery.each(jQuery.browser, function(i) {
                        if ($.browser.msie) {
                            description.css('display', 'block');
                        } else {
                            description.css('display', 'table-row');
                        }
                    });
                    info.animate({height: '600px'});
                    
                    e.preventDefault();
                },
                function(e) {
                    var row = $(this).parent().parent();
                    var description = row.next('tr.description');
                    var info = description.find('span');
                    info.slideUp(function() {
                        jQuery.each(jQuery.browser, function(i) {
                           if (! $.browser.msie) {
                                row.find('td[class=name]').css({
                                    'border-bottom-color': '#D8D8D8',
                                    'background-color': 'inherit'
                                });
                           }
                        });
                        
                        info.css({height: '0px'});
                        description.hide();
                    });
                    
                    e.preventDefault();
                }
            );
        });
    }
    
    this.bindCallbacks();
};

PB.Twitter = function() {
    var head = $('head').get(0);
    var script1 = document.createElement('script');
    var script2 = document.createElement('script');
    script1.type = script2.type = 'text/javascript';
    script1.src= 'http://twitter.com/javascripts/blogger.js';
    head.appendChild(script1);
    script2.src="http://twitter.com/statuses/user_timeline/publicbits.json?callback=twitterCallback2&count=1";
    head.appendChild(script2);
};

PB.Feedback = function() {
    this.shown = false;
    
    this.init = function() {
        $("#feedback h1").bind('click', this.onClick);
        $("#feedback form").bind('submit', this.onSubmit);
    };
    
    this.onSubmit = function(e) {
        if ($("#feedback form [name=message]").val().length > 0) {
            $.post("/ajax/feedback", {
                'email': $("#feedback form [name=email]").val(),
                'type': $("#feedback form [name=type]").val(),
                'message': $("#feedback form [name=message]").val()
            });
            $("#feedback form [name=message]").removeClass('invalid');
            $("#feedback h1").text('Thank you!');
            $("#feedback h1").click();
            $("#feedback form input,textarea").val('');
            
        } else {
            $("#feedback form [name=message]").focus();
            $("#feedback form [name=message]").addClass('invalid');
            $('#feedback form input,textarea').val('')
        }
        
        e.preventDefault();
    };
    
    this.onClick = function(e) {
        if (! this.shown) {
            $("#feedback").animate({width: '300px', height: '200px'}, 250, function() {
                $("#feedback form").fadeIn(200, function() {
                    $("#feedback form input").focus();
                });
            });
        } else {
            $("#feedback form").fadeOut(200, function() {
                $("#feedback").animate({width: '100px', height: '20px'}, 250);
            });
        }
        
        this.shown = !this.shown;
    };
    
    this.init();
}

$(document).ready(function() {
    PB.TorrentList();
    PB.Feedback();
    PB.Twitter();
});
