$(function() {
	// twitter
	$('#li_twitter a').hover(
		function() { $(this).find('img').attr('src', 'img/common/twitter_on.png'); },
		function() { $(this).find('img').attr('src', 'img/common/twitter.png'); }
	);

	// myspace
	$('#li_myspace a').hover(
		function() { $(this).find('img').attr('src', 'img/common/myspace_on.png'); },
		function() { $(this).find('img').attr('src', 'img/common/myspace.png'); }
	);

	// facebook
	$('#li_facebook a').hover(
		function() { $(this).find('img').attr('src', 'img/common/facebook_on.png'); },
		function() { $(this).find('img').attr('src', 'img/common/facebook.png'); }
	);

	// 同意してお問い合わせする
	$('img.agree_btn').hover(
		function() { $(this).attr('src', 'img/btn_agree_on.png'); },
		function() { $(this).attr('src', 'img/btn_agree.png'); }
	);

	// 他の実績を見る
	$('img.portfolio_btn').hover(
		function() { $(this).attr('src', 'img/btn_portfolio_on.png'); },
		function() { $(this).attr('src', 'img/btn_portfolio.png'); }
	);


	// ページ内リンク時にスクロール
	$('a[href^=#]').click(function() {
		var $target = $($(this).attr('href'));
		if ($target) {
			$(this).blur();
			$('html,body').stop().animate({scrollTop: Math.ceil($target.offset().top)}, {easing: 'easeInOutQuad', duration: 2000});
		}
		return false;
	});


	// スクロール時にメニューと連動
	var ids = [];
	$('a[href^=#]').each(function() {
		ids.push($(this).attr('href'));
	});

	$(window).scroll(function() {
		var scroll_top = $(window).scrollTop();

		for (var i = 0; i < ids.length; i ++) {
			var id = ids[i];

			var $menu    = $('a[href="' + id + '"] img');
			var $section = $(id);

			var top    = $section.offset().top;
			var height = $section.outerHeight();

			if (top <= scroll_top && scroll_top < top + height) {
				$menu.attr('src', $menu.attr('src').replace(/^(.+?)(_on)?(\.[a-z]+)$/, '$1_on$3'));
			} else {
				$menu.attr('src', $menu.attr('src').replace(/^(.+?)(_on)?(\.[a-z]+)$/, '$1$3'));
			}
		}
	});

	$(window).trigger('scroll');
});

