﻿
var tweets = [];

/* Tweet Model */
function Tweet(user, text, created) {
    this.user = user;
    this.text = text;
    this.created = created;
}

function initSocial() {
    // set social icon mouse events
    $('.roll-call img').each(function () {
        $(this).hover(
            // mouseover
            function () {
                this.src = this.src.replace('_icon.png', '_icon_blue.png');
            },
            // mouseout
            function () {
                this.src = this.src.replace('_icon_blue.png', '_icon.png');
            }
        );
    });

    findTweetsByUser('matthew_cooke', 3);
}

function findTweetsByUser(username, count) {
    $.ajax({
        url: 'http://api.twitter.com/1/statuses/user_timeline.json/',
        type: 'GET',
        dataType: 'jsonp',
        data: {
            screen_name: username,
            include_rts: true,
            count: count,
            include_entities: true
        },
        success: function (data, textStatus, xhr) {
            // create tweets
            for (var index = 0; index < data.length; index++) {
                var tweet = new Tweet(data[index].screen_name, data[index].text, new Date(data[index].created_at));

                // add tweet to tweets
                tweets.push(tweet);
            }

            // load tweets
            loadTweets();
        }

    });
}

function loadTweets() {
    // check if tweets are done loading...
    if (tweets.length != 6) {
        findTweetsByUser('alicebybee', 3);
        return false;
    }

    // sort tweets by date
    tweets.sort(function (a, b) {
        var tweet_a = a.created; tweet_b = b.created;
        if (tweet_b < tweet_a) return -1;
        else if (tweet_b > tweet_a) return 1;
        else return 0;
    });

    // append 3 latest tweets to .feeds
    $.each(tweets, function (index, tweet) {
        if (index == 3) return false;

        $('#tweet-tmpl').tmpl({
            created: tweet.created.toString('MMMM dd, yyyy'),
            text: JQTWEET.ify.clean(tweet.text)
        }).appendTo('.feeds');
    });
}

JQTWEET = {

    /**
    * The Twitalinkahashifyer!
    * http://www.dustindiaz.com/basement/ify.html
    * Eg:
    * ify.clean('your tweet text');
    */
    ify: {
        link: function (tweet) {
            return tweet.replace(/\b(((https*\:\/\/)|www\.)[^\"\']+?)(([!?,.\)]+)?(\s|$))/g, function (link, m1, m2, m3, m4) {
                var http = m2.match(/w/) ? 'http://' : '';
                return '<a class="twtr-hyperlink" target="_blank" href="' + http + m1 + '">' + ((m1.length > 25) ? m1.substr(0, 24) + '...' : m1) + '</a>' + m4;
            });
        },

        at: function (tweet) {
            return tweet.replace(/\B[@＠]([a-zA-Z0-9_]{1,20})/g, function (m, username) {
                return '<a target="_blank" class="twtr-atreply" href="http://twitter.com/intent/user?screen_name=' + username + '">@' + username + '</a>';
            });
        },

        list: function (tweet) {
            return tweet.replace(/\B[@＠]([a-zA-Z0-9_]{1,20}\/\w+)/g, function (m, userlist) {
                return '<a target="_blank" class="twtr-atreply" href="http://twitter.com/' + userlist + '">@' + userlist + '</a>';
            });
        },

        hash: function (tweet) {
            return tweet.replace(/(^|\s+)#(\w+)/gi, function (m, before, hash) {
                return before + '<a target="_blank" class="twtr-hashtag" href="http://twitter.com/search?q=%23' + hash + '">#' + hash + '</a>';
            });
        },

        clean: function (tweet) {
            return this.hash(this.at(this.list(this.link(tweet))));
        }
    } // ify
};

