$(document).ready(function(){

	//scroll
	$('a[href^=#]').click(function(e) {
		var href= $(this).attr("href");
		var target = $(href == "#" || href == "" ? 'html' : href);
		e.preventDefault();
		smScroll(target);
	})
	
	//topic ticker
	topicTicker();
	
	//twitetr
	//表示件数10件
	$.getJSON('http://api.twitter.com/1/statuses/user_timeline/digicre55.json?count=10&callback=?', function(data) {
		var
			prof_image = data[0].user.profile_image_url,
			user_screen_name = data[0].user.screen_name,
			user_name = data[0].user.name;
		$("#twitter").append('<div class="twitter_header">'
										+'<a href="http://twitter.com/'+user_screen_name+'" target="_blank"><img src="'+prof_image+'" width="30" height="30" alt="'+user_screen_name+'" /></a>'
										+'<h3>'+user_name+'</h3>'
										+'<h4><a href="http://twitter.com/'+user_screen_name+'" target="_blank">'+user_screen_name+'</a></h4>'
										+'</div>'
										+'<ul class="tweet"></ul>');
		$.each(data, function(i, item) {
			// URLにテキストがある場合はリンク設置
			var txt = item.text
					.replace(/(https?:\/\/[-a-z0-9._~:\/?#@!$&\'()*+,;=%]+)/ig,'<a href="$1" target="_blank">$1</a>')
					.replace(/@+([_A-Za-z0-9-]+)/ig, '<a href="http://twitter.com/$1" target="_blank">@$1</a>')
					.replace(/#+([_A-Za-z0-9-]+)/ig, '<a href="http://search.twitter.com/search?q=$1" target="_blank">#$1</a>');
			$("ul.tweet").append('<li>'
											+'<p>'+txt+'</p>'
											+'<p class="tweet_str"><a href="http://twitter.com/'+item.user.screen_name+'/status/'+item.id_str+'" target="_blank">'+relative_time(item.created_at)+'</a>・<a href="http://twitter.com/intent/tweet?in_reply_to='+item.id_str+'" target="_blank">reply</a>・<a href="http://twitter.com/intent/retweet?tweet_id='+item.id_str+'" target="_blank">retweet</a>・<a href="http://twitter.com/intent/favorite?tweet_id='+item.id_str+'" target="_blank">favorite</a></p>'
											+'</li>')
		});
		// 時間調整
		function relative_time(time_value) {
			var
				values = time_value.split(" "),
				time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3],
				parsed_date = Date.parse(time_value),
				relative_to = (arguments.length > 1) ? arguments[1] : new Date(),
				delta = parseInt((relative_to.getTime() - parsed_date) / 1000),
				delta = delta + (relative_to.getTimezoneOffset() * 60);
		
			if (delta < 60) {
				return '1分以内';
			} else if(delta < 120) {
				return '約1分前';
			} else if(delta < (60*60)) {
				return (parseInt(delta / 60)).toString() + '分前';
			} else if(delta < (120*60)) {
				return '約1時間前';
			} else if(delta < (24*60*60)) {
				return '約' + (parseInt(delta / 3600)).toString() + '時間前';
			} else if(delta < (48*60*60)) {
				return '1日前';
			} else {
				return (parseInt(delta / 86400)).toString() + '日前';
			}
		}
	})
});

// topicTicker(height roll)
function topicTicker () {
	var
		$topic = $( 'body.header ul.topic' ),
		$topic_li = $topic.find( 'li' ),
		start = count = 0,
		end = $( $topic_li , $topic ).length - 1,
		interval = 3600,
		slide_interval = 1000,
		stop_slide_interval = 2000,
		easeing = 'easeOutQuint';

	if ( $( $topic_li , $topic ).length <= 1 ) {
		$topic_li.animate({ marginTop : 2 },slide_interval , easeing )
		 return false;
	}

	$( $topic_li , $topic ).each(function(i){
		$(this).attr( 'id' , 'topic' + i );
		if( i!= 0 ) $(this).hide();
	});

	$( 'li#topic0' ).animate({ marginTop : 2 },slide_interval , easeing )
				.delay(stop_slide_interval)
				.animate({ marginTop : -20 },slide_interval , easeing )

	var slideTimer = setInterval(function(){
		$( 'li#topic' + ( start == 1 ? count == 0 ? end : count - 1 : count ) ).hide().animate({ marginTop : 20 },1);
		$( 'li#topic' + ( start == 1 ? ( ++count ) - 1 : ++count ) )
				.css({'display':'block'})
				.animate({ marginTop : 2 },slide_interval , easeing )
				.delay(stop_slide_interval)
				.animate({ marginTop : -20 },slide_interval , easeing )
		if ( start == 0 && count >= end ) {
			count = 0;
			start = 1;
		} else if ( start == 1 && count > end ) {
			count = 0;
		}
	}, interval );
}

//topicTicker2(width roll)
function topicTicker2 () {
	var
		$topic = $( 'body.header ul.topic' ),
		$topic_li = $topic.find( 'li' ),
		start = count = 0,
		end = $( $topic_li , $topic ).length - 1,
		interval = 3600,
		slide_interval = 1000,
		stop_slide_interval = 2000,
		easeing = 'easeOutQuint';

	if ( $( $topic_li , $topic ).length <= 1 ) {
		$topic_li.animate({ textIndent : 0 },slide_interval , easeing )
		 return false;
	}

	$( $topic_li , $topic ).each(function(i){
		$(this).attr( 'id' , 'topic' + i );
		if( i!= 0 ) $(this).hide();
	});

	$( 'li#topic0' ).animate({ textIndent : 0 },slide_interval , easeing )
				.delay(stop_slide_interval)
				.animate({ textIndent : 753 },slide_interval , easeing )

	var slideTimer = setInterval(function(){
		$( 'li#topic' + ( start == 1 ? count == 0 ? end : count - 1 : count ) ).hide().animate({ textIndent : -753 },1);
		$( 'li#topic' + ( start == 1 ? ( ++count ) - 1 : ++count ) )
				.css({'display':'block'})
				.animate({ textIndent : 0 },slide_interval , easeing )
				.delay(stop_slide_interval)
				.animate({ textIndent : 753 },slide_interval , easeing )
		if ( start == 0 && count >= end ) {
			count = 0;
			start = 1;
		} else if ( start == 1 && count > end ) {
			count = 0;
		}
	}, interval );
}

//scroll
function smScroll(id) {
	var
		target = $(id),
		position = target.offset().top,
		documentHeight = $(document).height(),
		scrollPosition = $(this).scrollTop(),
		windowHeight = $(window).height(),
		height = documentHeight - position;
	if ( documentHeight <= position + windowHeight ) {
		var positionScroll = position - windowHeight - scrollPosition + height;
		$('html,body').animate({scrollTop:positionScroll}, 1400, 'easeOutCubic');
		return false;
	} else {
		$('html,body').animate({scrollTop:position}, 1400, 'easeOutCubic');
		return false;
	}

}
	


