/*
 * jGetTwitter: a jQuery plugin, version: 1.0.0 (2011-08-21)
 * @requires jQuery v1.5.1 or later
 *
 * jGetTwitter is a jQuery plugin that gets latest tweets from users' twitter
 * account.
 *
 * For usage and examples, visit:
 * http://www.zafarsaleem.info/plugins/
 *
 * Licensed under the MIT:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright (c) 2011, Zafar Saleem ([zafarsaleem3] - [at] - gmail [dot] com)
 */
(function($){
    $.fn.jGetTwitter = function(user_options) {
        var default_settings = {
            "username" : '',
            "NoOfTweets" : 5,
            "ProfilePhoto" : true,
            "TweetTime" : true,
            "FollowerDisplay" : true,
            "TotalNoOfTweets" : true,
            "theme" : 'golden',
            "width" : 400
        };
        
        if(user_options){
            $.extend(default_settings, user_options);
        }
        
        var targetContainer = '#' + $(this).attr('id');
        var userName = default_settings['username'];
        var NoOfTweets = default_settings['NoOfTweets'];
        var profilePhoto = default_settings['ProfilePhoto'];
        var followerDisplay = default_settings['FollowerDisplay'];
        var TotalNoOfTweets = default_settings['TotalNoOfTweets'];
        var tweetTime = default_settings['TweetTime'];
        var theme = default_settings['theme'];
        var width = default_settings['width'];
        var tweet;
        var info;
        
        var url = 'http://api.twitter.com/1/statuses/user_timeline/' + userName + '.json?callback=?';
        
        $.getJSON(url, function (tweets) {
          /*  info = "<div class='tweetContainer'>";
            info += "<ul>";
            info += "<li>" + userName + "</li>";
            if(followerDisplay == true) {
                info += "<li>Followers: " + tweets[0].user.followers_count + "</li>";
            }
            if(TotalNoOfTweets == true) {
                info += "<li>Total Tweets: " + tweets[0].user.statuses_count + "</li>";
            }
            info += "</ul>";
            info += "</div>";
            */
            for (var i = 0; i < NoOfTweets; i++) {   
                tweet = "<a href='http://twitter.com/#!/dnmendelson' class='twitt' target='_blank'><div class='tweetContainer'>";
                if(profilePhoto == true) {
                    tweet += "<div class='profile_img'><img src='" + tweets[i].user.profile_image_url + "' /></div>";
                }
                tweet += "<div>" + tweets[i].text + "</div>";
                if(tweetTime == true) {
                    tweet += "<div class='timeago'>" + timeAgo(tweets[i].created_at) + "</div>";
                }
                tweet += "<div class='no_float'></div>";
                tweet += "</div></a>";
                
                $(targetContainer).append(tweet).fadeIn(1000);
            }
            
            $(targetContainer).prepend(info);
            themeColor(theme);
        });
        
        //function to display time in timeago format
        function timeAgo(d) {
			d=d.replace('+0000','');
            var currentDate = Math.round(+new Date()/1000);
            var tweetDate = Math.round(+new Date(d)/1000); 
            
            var diffTime = currentDate - tweetDate;
            //alert(currentDate+' - '+tweetDate+' = '+diffTime);
            if (diffTime < 59) return 'less than a minute ago';
            else if(diffTime > 59 && diffTime < 120) return 'about a minute ago';
            else if(diffTime >= 121 && diffTime <= 3600) return (parseInt(diffTime / 60)).toString() + ' minutes ago';
            else if(diffTime > 3600 && diffTime < 7200) return '1 hour ago';
            else if(diffTime > 7200 && diffTime < 86400) return (parseInt(diffTime / 3600)).toString() + ' hours ago';
            else if(diffTime > 86400 && diffTime < 172800) return '1 day ago';
            else if(diffTime > 172800 && diffTime < 604800) return (parseInt(diffTime / 86400)).toString() + ' days ago';
            else if(diffTime > 604800 && diffTime < 12089600) return '1 week ago';
            else if(diffTime > 12089600 && diffTime < 2630880) return (parseInt(diffTime / 604800)).toString() + ' weeks ago';
            else if(diffTime > 2630880 && diffTime < 5261760) return '1 month ago';
            else if(diffTime > 5261760 && diffTime < 31570560) return (parseInt(diffTime / 2630880)).toString() + ' months ago';
            else if(diffTime > 31570560 && diffTime < 63141120) return '1 year ago';
            else return (parseInt(diffTime / 31570560)).toString() + ' years ago';
        }
        
        //function to add CSS styles on div tags
        function addStyles() {
		   $('.tweetContainer').css('padding','5px')
                                .css('width',width)
                                .css('margin-bottom','3px')
                                .css('font-size','11px')
								.css('color','#01a9e7')
                                .css('border-bottom','solid 1px #d2d7da');
                                		   
           $('.tweetContainer ul').css('list-style','none')
                                  .css('padding','5px')
                                  .css('margin','2px 0 0px 0px')
                                  .css('font-weight','bold')
                                  .css('color','#01a9e7')
                                  .css('font-size','11px');
                                  
            $('.tweetContainer ul li:first-child').css('font-size','18px');
            
            $('.timeago').css('color','#6f8b96')
						.css('text-decoration','none')
                         .css('font-size','10px');
            
            $('.no_float').css('clear','both');
			
			$('a.twitt').live('mouseover',function(){
				$(this).css('color','#01a9e7')
					   .css('text-decoration','underline');	
				$('.timeago').css('text-decoration','none');
			});
			
			$('a.twitt').live('mouseout',function(){
				$(this).css('color','#01a9e7')	
					   .css('text-decoration','none');	
				$('.timeago').css('text-decoration','none');
			});        
		}
        
        //theme for jGetTwitter.
        function themeColor(theme) {
            var gradientBg;
            var borderBg;
            addStyles();
            $('.tweetContainer').css("background",gradientBg)
                                .css("border",borderBg);
        } //end of function
    }
})(jQuery);
