/*
 * common.js
 * required: jquery.js + jquery.easing.js
 * 
 */

$.extend($.expr[':'], {

	// 外部リンク
	external: function(a,i,m) {
		if (!a.href) {
			return false;
		}
		return a.hostname && a.hostname !== window.location.hostname;
	}
	
});

$.fn.extend({

	// 新規ウィンドウを開く
	openWindow: function() {
		$(this).click(function() {
			window.open(this.href);
			return false;
		});
	}
	
});

$(function() {

	$('body')
		.addClass('js-enabled')
		.find(':first-child').addClass('first-child').end()
		.find(':last-child').addClass('last-child').end()
		.find('*').hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		});

	
	// ブラウザ判定
	var browserName;
	var browserVer;
	if ($.browser.msie) {
		browserName = 'msie';
		switch ($.browser.version) {
			case '6.0': browserVer = 'v6'; break;
			case '7.0': browserVer = 'v7'; break;
			case '8.0': browserVer = 'v8'; break;
		}
		$('body').addClass(browserVer);
	}
	$('body').addClass(browserName);
		
	// フォーム
	$('input').each(function() {
		$(this).addClass($(this).attr('type'));
	});
	$('input[type="text"]').parent().addClass('has-input-text');

	// リンク
	$('a:external:not([href^="http://www.espayroll.jp/"], [href^="https://x126.secure.ne.jp/~x126041/"])').addClass('external').openWindow();

	// ロールオーバー
	$('.btn').each(function() {
		this.defaultSrc = $(this).attr('src');
		this.hoverSrc = this.defaultSrc.replace(/(\.gif|\.jpg|\.png)/, '-h$1');
		this.hoverImg = new Image;
		this.hoverImg.src = this.hoverSrc;
	}).hover(function() {
		$(this).attr('src', this.hoverSrc);
	}, function() {
		$(this).attr('src', this.defaultSrc);
	});

	// スクロール
	if (!$.browser.opera) {
		$('.back-to-top a').click(function() {
			$('html, body').animate({scrollTop: 0}, 600, 'easeOutExpo');
			return false;
		});
		$('[href^="#"]:not(.back-to-top a)').click(function() {
			var targetOffset = $(this.hash).offset().top;
			var documentHeight = $(document).height();
			var windowHeight = $(window).height();
			if ((documentHeight - targetOffset) < windowHeight) {
				targetOffset = (documentHeight - windowHeight);
			}
			$('html, body').animate({scrollTop: targetOffset}, 600, 'easeOutExpo');
		});
	}
	
	// service/
	$('#service .article .nav')
		.find('ul li:has(.current)').addClass('has-current').end()
		.find('h2, li:has(a)').click(function() {
			location.href = $(this).find('a').attr('href');
		});
		
	$('#service #index #consulting ul.group li:nth-child(odd)').addClass('odd');

	$('#service #payrollservice #feature-02 .section')
		.find('.section:even').addClass('odd').end()
		.find('.section:odd').addClass('even').each(function() {
			var prevHeight = $(this).prev().find('h4').height();
			var thisHeight = $(this).find('h4').height();
			if (prevHeight > thisHeight) {
				$(this).css('paddingTop', prevHeight - thisHeight);
			} else {
				$(this).prev().css('paddingTop', thisHeight - prevHeight);
			}
		});
	
	// casestudy
	$('#introduction tr:nth-child(even)').addClass('even');
	
	// 道順
	$('#company #accessmap #direction ol')
		.find('li > :first-child').addClass('head').end()
		.find('li').each(function(i) {
			i ++;
			$(this).addClass('item-' + i).find('.title').prepend('(' + i + ') ');
		}).end()
		.each(function() {
			var tallest = 0;
			$(this).find('li').each(function() {
				var thisHeight = $(this).height();
				if (thisHeight > tallest) {
					tallest = thisHeight;
				}
			}).height(tallest);
		});
	
	// グループ企業
	$('#company #group .section .section').each(function() {
		var sectionHeight = $(this).height();
		$(this).find('h3').css('paddingTop', (sectionHeight - 90) / 2);
	});
	
	// ニュースリリース
	$('#news td:not(:has(a))').addClass('not-has-a').wrapInner('<span></span>');
	
	// サイトマップ
	$('#sitemap .column li:has(ul)').addClass('has-ul');
	
	// inline-link for IE6/7
	if ($.browser.msie && $.browser.version <= 7.0) {
		$('a.inline-link').append('<span>&nbsp;</span>');
	}
	
	// ローカルナビ
	$('#local-nav')
		.find('li:has(ul)').addClass('has-ul').end()
		.find('li:has(.current)').addClass('has-current');
	
});
