var tweetArray = new Array();
var tweetIdArray = new Array();
var arrayCountMax = 0;
var displayMaxRec = 10;
var timelineScreenName="ow_outward";
var timelineReloadIntervalId = "";
var timelineGetIntervalId    = "";
var timelineTargetCount = 0;

(function(){
    jQuery.fn.getTimelineTweet = function(){
        getJsonData2();
        function getJsonData2(){
			var timeline_url = 'http://search.twitter.com/search.json?rpp='+displayMaxRec+'&q=from%3A'+timelineScreenName;
            $.getJSON(timeline_url+"&callback=?",function(json){create_tweet_box2(json)});
        }
        function create_tweet_box2(resultData){
			if(!resultData.error){
				jsonObj=resultData.results;
	            var regObj = new RegExp(/(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])/g);


	            for (var i = jsonObj.length -1 ;i >= 0;i--) {
	                var obj = jsonObj[i];
					var tmp = obj.created_at.split(' ');
					var updated = new Date(obj.created_at);
	                text = obj.text;
	                if(text.match(regObj)){
	                    var match_ptt = text.match(regObj);
						for(var link_key=0;link_key < match_ptt.length;link_key++){
		                    var link = match_ptt[link_key];
		                    text = text.replace(link, '<a href="' + link + '" target="_blank" >' + link + '</a>');
						}
	                }
	                text = text.replace('RT @','');
	                var tweet_tmp = $(document.createElement('div'));
					tweet_tmp.append('<div class="twImg"><img src="' + obj.profile_image_url + '" border="0" width="48"></div>');
	                var tweet_body = $(document.createElement('div'));
					tweet_body.attr('class','twBody');
	                tweet_body.append('<div class="twName"><a href="http://twitter.com/' + obj.from_user + '" target="_blank">' + obj.from_user + '</a></div> ');
	                tweet_body.append('<div class="twText">' + text + '</div>');

					mm = "00"+updated.getMinutes();
					mm = mm.substr(mm.length-2,2); 
	                tweet_body.append('<div class="twDate">' + updated.getFullYear() + '/' + (updated.getMonth() + 1) + '/' + updated.getDate() + ' (' + updated.getHours() + ':' + mm + ')' + '</div>');
					tweet_tmp.append(tweet_body);
	                tweet_tmp.append('<div style="clear:both;"></div>');

	                if($.inArray(obj.id, tweetIdArray) == -1){
	                    tweetArray.push(tweet_tmp.html());
	                    tweetIdArray.push(obj.id);
	                    arrayCountMax++;
	                }
	            }
				timelineTargetCount = tweetArray.length;
				for(ii=0;ii<tweetArray.length;ii++){
					var tweet_main_box = $(document.createElement('div'));
					tweet_main_box.addClass("box01");
					tweet_main_box.append(tweetArray[ii]);
					tweet_main_box.css('display','none');
					$('.tweetBox').prepend(tweet_main_box);
					$('.tweetBox .box01').eq(0).slideToggle();
					if($('.tweetBox .box01').length > displayMaxRec){
						$('.tweetBox .box01').eq(10).remove();
					}
				}
			}
            timelineReloadIntervalId = setInterval(function(){hoge();},1000);
			timelineGetIntervalId    = setInterval(function(){getTweetData();},100000);
        };
    }
})(jQuery);

function getTweetData(){
	var timeline_url = 'http://search.twitter.com/search.json?rpp='+displayMaxRec+'&q=from%3A'+timelineScreenName;
    $.getJSON(timeline_url+"&callback=?",function(resultData){
		if(!resultData.error){
			jsonObj=resultData.results;
	        var regObj = new RegExp(/(http|https):\/\/([\w-]+\.)+[\w-]{2,4}(:\d+)?(\/[\w %=@&~,:+!{}`_\-\.\/\?\[\]]*)?/);
	        for (var i = jsonObj.length -1 ;i >= 0;i--) {
	            var obj = jsonObj[i];
				var tmp = obj.created_at.split(' ');
				var updated = new Date(obj.created_at);
	            text = obj.text;
	            if(text.match(regObj)){
	                var link = text.match(regObj)[0];
	                text = text.replace(regObj, '<a href="' + link + '" target="_blank" >' + link + '</a>');
	            }
                text = text.replace('RT @','');
                var tweet_tmp = $(document.createElement('div'));
				tweet_tmp.append('<div class="twImg"><img src="' + obj.profile_image_url + '" border="0" width="48"></div>');
                var tweet_body = $(document.createElement('div'));
				tweet_body.attr('class','twBody');
                tweet_body.append('<div class="twName"><a href="http://twitter.com/' + obj.from_user + '" target="_blank">' + obj.from_user + '</a></div> ');
                tweet_body.append('<div class="twText">' + text + '</div>');
mm = "00"+updated.getMinutes();
mm = mm.substr(mm.length-2,2);
				tweet_body.append('<div class="twDate">' + updated.getFullYear() + '/' + (updated.getMonth() + 1) + '/' + updated.getDate() + ' (' + updated.getHours() + ':' + mm + ')' + '</div>');
				tweet_tmp.append(tweet_body);
                tweet_tmp.append('<div style="clear:both;"></div>');

	            if($.inArray(obj.id, tweetIdArray) == -1){
	                tweetArray.push(tweet_tmp.html());
	                tweetIdArray.push(obj.id);
	                arrayCountMax++;
	            }
	        }
			hoge();
		}
	});
};
    function hoge(){
        if(timelineTargetCount < arrayCountMax){
			var tweet_main_box = $(document.createElement('div'));
			tweet_main_box.addClass("box01");
			tweet_main_box.append(tweetArray[timelineTargetCount]);
			tweet_main_box.css('display','none');
			$('.tweetBox').prepend(tweet_main_box);
			$('.tweetBox .box01').eq(0).slideToggle();
			timelineTargetCount++;
			if($('.tweetBox .box01').length > displayMaxRec){
				$('.tweetBox .box01').eq(10).remove();
			}
        }
    }


$(function(){
    $('body').getTimelineTweet();
});


