/*
 * jTwitter 1.1.1 - Twitter API abstraction plugin for jQuery
 * Copyright (c) 2009 jQuery Howto
 * Licensed under the GPL license:
 *   http://www.gnu.org/licenses/gpl.html
 * Plugin + Author URL:
 *   http://jquery-howto.blogspot.com
 */
//(function(b){b.extend({jTwitter:function(d,a,c){if(!(d=="undefined"||a=="undefined")){if(b.isFunction(a)){c=a;a=5}b.getJSON("http://twitter.com/status/user_timeline/"+d+".json?count="+a+"&callback=?",function(e){b.isFunction(c)&&c.call(this,e)})}}})})(jQuery);

(function( $ ){
	$.extend( {
		jTwitter: function( username, numPosts, fnk ) {
			var info = {};

			// If no arguments are sent or only username is set
			if( username == 'undefined' || numPosts == 'undefined' ) {
				return;
			} else if( $.isFunction( numPosts ) ) {
				// If only username and callback function is set
				fnk = numPosts;
				numPosts = 5;
			}

			var url = "https://twitter.com/status/user_timeline/"
				+ username + ".json?count="+numPosts+"&callback=?";

			/*$.getJSON( url, function( data ){
				if( $.isFunction( fnk ) ) {
					fnk.call( this, data );
				}
			});*/

			$.ajax({
				url: url,
				dataType: 'json',
				success: function(data) {
					if( $.isFunction( fnk ) ) {fnk.call( this, data );}
				},
				timeout: 5000,
				error: function() {
				        $(this).html('エラーが発生しています。。。');
				}
  			});
		}
	});
})( jQuery );

function relative_time(time_value) {
    var values = time_value.split(" "),
        parsed_date = Date.parse(values[1] + " " + values[2] + ", " + values[5] + " " + values[3]),
        date = new Date(parsed_date),
        relative_to = (arguments.length > 1) ? arguments[1] : new Date(),
        delta = parseInt((relative_to.getTime() - parsed_date) / 1000),
        r = '';

    function formatTime(date) {
        var hour = date.getHours(),
            min = date.getMinutes() + "",
            ampm = 'AM';

        if (hour == 0) {
            hour = 12;
        } else if (hour == 12) {
            ampm = 'PM';
        } else if (hour > 12) {
            hour -= 12;
            ampm = 'PM';
        }

        if (min.length == 1) {
            min = '0' + min;
        }

        return hour + ':' + min + ' ' + ampm;
    }

    function formatDate(date) {
        var ds = date.toDateString().split(/ /),
            mon = monthDict[date.getMonth()],
            day = date.getDate()+'',
            dayi = parseInt(day),
            year = date.getFullYear(),
            thisyear = (new Date()).getFullYear(),
            th = 'th';

        // anti-'th' - but don't do the 11th, 12th or 13th
        if ((dayi % 10) == 1 && day.substr(0, 1) != '1') {
            th = 'st';
        } else if ((dayi % 10) == 2 && day.substr(0, 1) != '1') {
            th = 'nd';
        } else if ((dayi % 10) == 3 && day.substr(0, 1) != '1') {
            th = 'rd';
        }

        if (day.substr(0, 1) == '0') {
            day = day.substr(1);
        }

        return mon + ' ' + day + th + (thisyear != year ? ', ' + year : '');
    }

    delta = delta + (relative_to.getTimezoneOffset() * 60);

    if (delta < 5) {
        r = '5秒以内';
    } else if (delta < 30) {
        r = '３０秒以内';
    } else if (delta < 60) {
        r = '1分以内';
    } else if (delta < 120) {
        r = '約1分前';
    } else if (delta < (45*60)) {
        r = (parseInt(delta / 60)).toString() + '分前';
    } else if (delta < (2*90*60)) { // 2* because sometimes read 1 hours ago
        r = '約1時間前';
    } else if (delta < (24*60*60)) {
        r = '約' + (parseInt(delta / 3600)).toString() + '時間前';
    } else {
        if (delta < (48*60*60)) {
            r = formatTime(date) + '昨日';
        } else {
            r = formatTime(date) + ' ' + formatDate(date);
            // r = (parseInt(delta / 86400)).toString() + ' days ago';
        }
    }

    return r;
}
