// ==UserScript==
// @name        CoTweet
// @namespace   http://cotweet.com
// @description CoTweet and Fluid
// @include     http://cotweet.com/*
// @author      Julian Naydichev [based on work by Dain Kennison and PJ Bezilla]
// ==/UserScript==    
 
// we need fluid!
if (window.fluid) {

    function growl() {
        // make sure we're in the inbox
        var inbox = $(".filter-main li:first");
        var inInbox = (inbox.hasClass('selected') || inbox.find('.selected').length);
        
        var tweets = $(".scrollable .results li[id^=update]");
        var numTweets = tweets.length;
        fluid.dockBadge = (inInbox && numTweets) || null;
        if (inInbox && numTweets > 0) {
            
            // rrrraaaaawwwwrrrrr
            tweets.each(function() {                
                
                // get our variables
                var username = $(this).find(".avatar").children()[0].title;
                var avatar   = $(this).find(".avatar").children()[0].src;
                var text     = $(this).find("p.result_content").text();
                
                // check for some stuff
                if ($(this).hasClass('has_replies')) {
                    $(this).addClass('growl');
                }   
                
                // if we haven't been alerted before, 
                // go go go go go go go
                if (!$(this).hasClass('growl')) {

                    // if our parent has a class of 'dm',
                    // guess what we are!
                    
                    var aClass = 'reply', prefix = '';
                    if ($(this).closest('li').hasClass('dm')) {
                        var aClass = 'dm';
                        var prefix = '[DM] ';
                    }
                
                    // this is not used at the moment,
                    // but can be so that when you click a growl
                    // it brings up the reply box
                    
                    // right now, there's an error =/
                    //var aElem = $(this)
                    //            .closest('li')
                    //            .find('ul > li > a.' + aClass);
                    
                    // don't go over this again when we're updated
                    $(this).addClass('growl');
                    
                    // we need that, for when clicking on the growl!
                    var that = this;
                    
                    function growlClick() {
                        $(that).closest('li').click();
                        
                        // doesn't work =(
                        //$(aElem).click();
                    }
                    
                    // show our notification!
                    fluid.showGrowlNotification({
                        title: username,
                        description: prefix + text,
                        sticky: false,
                        icon: avatar,
                        onclick: growlClick
                    });
                    console.log("Growl: " + username + " - " + text + " " + prefix);
                }
            });
        }
    }
    
    // every 15 seconds, that's coo, yeah?
    $(function() {
        setInterval(growl, 15000);
    });
// we're done with the fluid!
}

