<div id="twitter-feed"></div>
<script type="text/javascript">
    $(document).ready(function () {
        var displaylimit = {{ pttweet_limit }};
        var screenname = "{{ heading_title }}";
        var showdirecttweets = false;
        var showretweets = true;
        var showtweetlinks = true;

        var headerHTML = '';
        var loadingHTML = '';
        headerHTML += '<div class="tweet-title"><h3>' + screenname + '</h3></div>';
        loadingHTML += '<div id="loading-container"><img src="{{ base_url }}image/plaza/ajax-loader.gif" width="32" height="32" alt="tweet loader" /></div>';

        $('#twitter-feed').html(headerHTML + loadingHTML);

        var twitter_json_url = '{{ base_url }}twitter/get-tweets.php?twitteruser={{ pttweet_user }}&consumerkey={{ pttweet_consumer_key }}&consumersecret={{ pttweet_consumer_secret }}&accesstoken={{ pttweet_access_token }}&accesstokensecret={{ pttweet_access_token_secret }}';

        $.getJSON(twitter_json_url,
                function(feeds) {
                    //alert(feeds);
                    var feedHTML = '<div class="tweet-content">';
                    var displayCounter = 1;
                    for (var i=0; i<feeds.length; i++) {
                        var tweetscreenname = feeds[i].user.name;
                        var tweetusername = feeds[i].user.screen_name;
                        var profileimage = feeds[i].user.profile_image_url_https;
                        var status = feeds[i].text;
                        var isaretweet = false;
                        var isdirect = false;
                        var tweetid = feeds[i].id_str;

                        //If the tweet has been retweeted, get the profile pic of the tweeter
                        if(typeof feeds[i].retweeted_status != 'undefined'){
                            profileimage = feeds[i].retweeted_status.user.profile_image_url_https;
                            tweetscreenname = feeds[i].retweeted_status.user.name;
                            tweetusername = feeds[i].retweeted_status.user.screen_name;
                            tweetid = feeds[i].retweeted_status.id_str
                            isaretweet = true;
                        };


                        //Check to see if the tweet is a direct message
                        if (feeds[i].text.substr(0,1) == "@") {
                            isdirect = true;
                        }

                        //console.log(feeds[i]);

                        if (((showretweets == true) || ((isaretweet == false) && (showretweets == false))) && ((showdirecttweets == true) || ((showdirecttweets == false) && (isdirect == false)))) {
                            if ((feeds[i].text.length > 1) && (displayCounter <= displaylimit)) {
                                if (showtweetlinks == true) {
                                    status = addlinks(status);
                                }

                                if (displayCounter == 1) {
                                    feedHTML += headerHTML;
                                }

                                feedHTML += '<div class="twitter-article">';
                                feedHTML += '<div class="twitter-pic"><a href="https://twitter.com/' + tweetusername + '" ><img src="' + profileimage + '"images/twitter-feed-icon.png" width="42" height="42" alt="twitter icon" /></a></div>';
                                feedHTML += '<div class="twitter-text"><span class="tweetprofilelink"><strong><a href="https://twitter.com/' + tweetusername + '" >' + tweetscreenname + '</a></strong> <a href="https://twitter.com/' + tweetusername + '" >@' + tweetusername + '</a></span><br/>' + status + '<br/>';
                                {% if show_time %}
                                feedHTML += '<span class="tweet-time"><a href="https://twitter.com/' + tweetusername + '/status/' + tweetid + '">' + relative_time(feeds[i].created_at) + '</a></span>';
                                {% endif %}
                                feedHTML += '</div></div>';
                                displayCounter++;
                            }
                        }
                    }
                    feedHTML += '</div>';
                    $('#twitter-feed').html(feedHTML);
                });

        function addlinks(data) {
            //Add link to all http:// links within tweets
            data = data.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
                return '<a href="' + url + '" >' + url + '</a>';
            });

            data = data.replace(/\B@([_a-z0-9]+)/ig, function(reply) {
                return '<a href="http://twitter.com/' + reply.substring(1) + '" style="font-weight:lighter;" >' + reply.charAt(0) + reply.substring(1) + '</a>';
            });
            return data;
        }

        function relative_time(time_value) {
            var values = time_value.split(" ");
            time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
            var parsed_date = Date.parse(time_value);
            var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
            var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
            var shortdate = time_value.substr(4,2) + " " + time_value.substr(0,3);
            delta = delta + (relative_to.getTimezoneOffset() * 60);

            if (delta < 60) {
                return '1m';
            } else if(delta < 120) {
                return '1m';
            } else if(delta < (60*60)) {
                return (parseInt(delta / 60)).toString() + 'm';
            } else if(delta < (120*60)) {
                return '1h';
            } else if(delta < (24*60*60)) {
                return (parseInt(delta / 3600)).toString() + 'h';
            } else if(delta < (48*60*60)) {
                //return '1 day';
                return shortdate;
            } else {
                return shortdate;
            }
        }
    });
</script>