/** Based off of the jQuery plugin - Tweetable  **/

(function ($) {
    
    $.fn.tweets = function (options) {
        //specify the plugins defauls
        var defaults = {
            limit: 5,                       // number of tweets to show
            username: 'fryinsights',         // @username tweets to display
            user: false,                    // display username in front of tweet
            time: true,                    // display date
            replies: false,                 // filter out @replys
            position: 'append'              // append position
        };
        //overwrite the defaults
        var options = $.extend(defaults, options);
        //loop through each instance
        return this.each(function (options) {
            //assign our initial vars
            var act = $(this);
            var $tweetList;
            var tweetMonth = '';
            var shortMonths = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
            var tweetUser = defaults.user ? '<span class="username">' + defaults.username + ": " + '</span>' : '';
            var api = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=";
            var count = "&count=";


            //do a JSON request to twitters API
            $.getJSON(api + defaults.username + count + defaults.limit + "&callback=?", act, function (data) {
                //loop through twitters response
                $.each(data, function (i, item) {
                    //check for the first loop
                    if (i == 0) {
                        //create an unordered list to store tweets in
                        $tweetList = $('<ul class="tweetList">')[defaults.position.toLowerCase() + 'To'](act);
                    }
                    //handle @reply filtering if required
                    if (defaults.replies === false) {
                        //{"error":"Rate limit exceeded. Clients may not make more than 150 requests per hour.","request":"\/1\/statuses\/user_timeline.json?screen_name=fryinc"}
                        if (item.in_reply_to_status_id === null) {
                            $tweetList.append('<li class="tweet_content_' + i + '"><span class="twitterName">@ ' + defaults.username + '</span><p class="tweet_link_' + i + '">' + tweetUser + item.text.replace(/#(.*?)(\s|$)/g, '<span class="hash">#$1 </span>').replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, '<a href="$&">$&</a> ').replace(/@(.*?)(\s|\(|\)|$)/g, '<a href="http://twitter.com/$1">@$1 </a>$2') + '</p></li>');
                        }
                    } else {
                        $tweetList.append('<li class="tweet_content_' + i + '"><p class="tweetBody tweet_link_' + i + '">' + tweetUser + item.text.replace(/#(.*?)(\s|$)/g, '<span class="hash">#$1 </span>').replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, '<a href="$&">$&</a> ').replace(/@(.*?)(\s|\(|\)|$)/g, '<a href="http://twitter.com/$1">@$1 </a>$2') + '</p></li>');
                    }
                    //display the time of tweet if required
                    if (defaults.time == true) {
                        for (var iterate = 0; iterate <= 12; iterate++) {
                            if (shortMonths[iterate] == item.created_at.substr(4, 3)) {
                                tweetMonth = shortMonths[iterate]

                            }
                        }
                        $('.tweet_content_' + i).append('<span class="twitterDate"> ' + item.created_at.substr(8, 2) + ' ' + tweetMonth + ' via ' + item.source + '</span>');
                    }
                });
                //close the unordered list
            });
        });
    }
})(jQuery);
