﻿/*
======== table of content. =================================

Description: load timeline and show
Update: 2010/06/23-
Author: Japan Electronic Industrial Arts Co.Ltd.
        http://jeia.co.jp/

============================================================
*/

new function() {
	
	var parmaURL = 'http://twitter.com/jeia3/status/';
	var queryURL = 'http://twitter.com/statuses/user_timeline/jeia3.json';
	var timelineSelector = '#twitline .timeline';
	var loadNum = 10; // tweets
	var linespan = 3000; // msec
	var framespan = 120; // msec
	var speed = 5; // px/msec
	var que = [];
	var key = 0;
	
	var now = null;
	var $timeline = null;
	var $scroll = null;
	
	
	if ( typeof jQuery == 'undefined' ) {
		return;
	}
	
	$(document).ready( function() {
		
		$timeline = jQuery( timelineSelector );
		
		request();
	});
	
	/**
	 * request
	 */
	function request() {
		
		var data = {
			'rpp': loadNum
		};
		
		jQuery.ajax({
			'type': 'GET',
			'url': queryURL,
			'dataType': 'jsonp',
			'cache': false,
			'data': data,
			'success': function( d ) {
				onSuccess( d );
			},
			'error': function( d1, d2, d3 ) {
				onError( d1, d2, d3 );
			}
		});
	}
	
	/**
	 * onSuccess
	 */
	function onSuccess( _data ) {
		
		if ( $timeline.length == 0 ) {
			return;
		}
		
		now = new Date();
		
		if ( _data.length > 0 ) {
			for ( var i = 0; _data[ i ]; i ++ ) {
				que.push( getLine( _data[ i ] ) );
			}
			
			showNext();
		}
	}
	
	/**
	 * onError
	 */
	function onError( _XMLHttpRequest, _textStatus, _errorThrown ) {
	}
	
	/**
	 * showNext
	 */
	function showNext() {
		
		$timeline.html( que[ key ] );
		$scroll = $timeline.find( 'p' ).eq(0);
		
		if ( $scroll.length > 0 ) {
			if ( $scroll.width() > $timeline.width() ) {
				
				$scroll.css({
					'left': $timeline.width() + 'px',
					'top': '0px'
				});
				
				floatTimeline();
			}
			else {
				showTimeline();
			}
		}
		
		key ++;
		
		if ( key >= que.length ) {
			key = 0;
		}
	}
	
	/**
	 * link text
	 */
	function getLine( _data ) {
		
		var text = _data[ 'text' ];
		var href = parmaURL + _data[ 'id' ];
		var time = getTimeText( _data[ 'created_at' ] );
		var line = [
			'<p style="display:block; position:absolute; white-space:nowrap;">',
			'<a href="',href,'" target="_blank" title="つぶやきを見る" style="display:inline-block; white-space:nowrap;">',
				time, ': ', text,
			'</a>',
			'</p>'
		].join('');
		
		return line;
	}
	
	/**
	 * time text
	 */
	function getTimeText( _time ) {
		
		var time = '';
		var timeArray = _time.split( ' ' );
		var timeStr = [
			timeArray[1], ' ',
			timeArray[2], ', ',
			timeArray[5], ' ',
			timeArray[3]
		].join('');
		
		var createdTime = new Date( timeStr  );
		createdTime.setTime( createdTime.getTime() - now.getTimezoneOffset() * 60 * 1000 );
		
		var span = now - createdTime;
		
		time = Math.floor( span / 1000 );
		
		if ( time < 60 ) {
			return time + '秒前';
		}
		
		time = Math.floor( time / 60 );
		
		if ( time < 60 ) {
			return time + '分前';
		}
		
		time = Math.floor( time / 60 );
		
		if ( time < 24 ) {
			return time + '時間前';
		}
		
		time = Math.floor( time / 24 );
		
		if ( time < 30 ) {
			return time + '日前';
		}
		
		if ( time < 365 ) {
			return Math.floor( time / 30 ) + 'ヶ月前';
		}
		
		return Math.floor( time / 365 ) + '年前';
	}
	
	
	/**
	 * showTimeline
	 */
	function showTimeline() {
		
		$scroll.css({
			'left': '0px',
			'top': - $scroll.height() + 'px',
			'opacity': 0
		}).
		animate({
			'top': '0px',
			'opacity': 1
		}, 600, function() {
			setTimeout( hideTimeline, linespan );
		});
	}
	
	/**
	 * hideTimeline
	 */
	function hideTimeline() {
		
		$scroll.animate({
			'top': $scroll.height() + 'px',
			'opacity': 0
		}, 600, function() {
			showNext();
		});
	}
	
	/**
	 * floatTimeline
	 */
	function floatTimeline() {
		
		var x = $scroll.offset().left - $timeline.offset().left;
		
		if ( $scroll.width() + x > 0 ) {
			$scroll.css({
				'left': ( x - speed ) + 'px'
			});
			setTimeout( floatTimeline, framespan );
		}
		else {
			showNext();
		}
	}
}
