/*
 * common.js
 * depends jquery.js (v1.3.2) + 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;
		});
	}
	
});


// ローカルナビアクティブ処理[プラグイン]
$.fn.path = function() {
	$(this).each(function() {
		if (this.href == window.location.href) {
			$(this).addClass('active');
		}
	});
	return this;
};

$.fn.globalPath = function(options) {
	var o = $.extend({
		root: '/',
		ignoreFiles: '/index\.html|index\.htm|index\.shtml|index\.php|index\.cgi/i'
	}, options);
	$(this).each(function() {
		if (this.hostname != location.hostname) {return this;}
		this.path = this.pathname.replace(o.ignoreFiles, '');
		if (!this.path.match(/^\//)) {
			this.path = '/' + this.path;
		}
		if (this.path == o.root) {
			if (this.path == location.pathname.replace(o.ignoreFiles, '')) {
				$(this).find('img').addClass('active').swapImg();
			}
		}
		else if (location.pathname.search(this.path) == 0) {
			$(this).find('img').addClass('active').swapImg();
		}
	});
	return this;
};

$.fn.swapImg = function(options) {
	var o = $.extend({
		swapImgPostfix: '_on'
	}, options);
	$(this).each(function() {
		this.swapImgSrc = $(this).attr('src').replace(/(\.gif|\.jpeg|\.jpg|\.png)/i, o.swapImgPostfix + '$1');
		$(this).attr('src', this.swapImgSrc);
	});
	return this;
};

$.fn.rollover = function(options) {
	var o = $.extend({
		swapImgPostfix: '_on'
	}, options);
	$(this).not('.active').each(function() {
		this.defaultSrc = $(this).attr('src');
		this.hoverSrc = this.defaultSrc.replace(/(\.gif|\.jpeg|\.jpg|\.png)/i, o.swapImgPostfix + '$1');
		this.hoverImg = new Image;
		this.hoverImg.src = this.hoverSrc;
	}).hover(function() {
		$(this).attr('src', this.hoverSrc);
	}, function() {
		$(this).attr('src', this.defaultSrc);
	});
	return this;
};


// タブ切り替え[プラグイン]
$.fn.tabs = function(initial) {
	$(this).each(function() {
		$(this).find('.tab-naviOn').removeClass('tab-naviOn');
		var tabsBody = $(this).children('div');
		var tabsNav = $(this).children('ul');
		var tabsNavInitial = initial? 'nth-child(' + initial + ')': 'first';
		var tabsNavSelected = $(tabsNav).children('li:' + tabsNavInitial);
		var tabsNavLink = $(tabsNav).find('[href^="#"]');
		$(tabsBody).addClass('tabs-body').hide();
		$(tabsNav).addClass('tabs-nav');
		$(tabsNavSelected).addClass('tab-naviOn').each(function() {
			$($(this).find('[href^="#"]').attr('href')).show();
		});
		$(tabsNavLink).click(function() {
			$(tabsBody).hide();
			$(tabsNav).find('.tab-naviOn').removeClass('tab-naviOn');
			$(this).parent('li').addClass('tab-naviOn');
			$($(this).attr('href')).show();
			return false;
		});
	});
};




$(function() {

	// JavaScript 有効判定
	$('body').addClass('js-enabled');
	
	// ブラウザ判別
	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;
			default: browserVer = 'v5';
		}
		
		$('body').addClass(browserVer);
	} else if ($.browser.mozilla) {
		browserName = 'mozilla';
	} else if ($.browser.safari) {
		browserName = 'safari';
	} else if ($.browser.opera) {
		browserName = 'opera';
	} else {
		browserName = 'unknown';
	}
	$('body').addClass(browserName);

	// リンク
	$('a:external').addClass('external').openWindow();
	$('a[href$=".pdf"]').addClass('pdf').openWindow();
	$('a[href$=".zip"]').addClass('zip');
	$('a[href$=".exe"]').addClass('exe');
	$('a[href^="https:"]').addClass('https');
	$('a[href^="ftp:"]').addClass('ftp');
	$('a[href^="mailto:"]').addClass('mailto');
	
	//POPUP
	$('a.popWin').click(function(){
		window.open(this.href, 'popWindow','toolbar=yes,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=no,width=550,height=820');
		return false;
	});
	$('.btnClose > a').click(function(){
		window.close();
		return false;
	});

	// スクロール
	if (!$.browser.opera) {
		$('.pageTop > a').click(function() {
			$('html, body').animate({scrollTop: 0}, 600, 'easeOutExpo');
			return false;
		});
		$('[href^="#"]:not(.pageTop > a):not(a.switcher)').click(function() {
			if ($.browser.msie && $.browser.version == 7.0) return; // IE7では処理しない*不具合がある為
			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');
		});
	}
	
	// ローカルナビのアクティブ処理
	$('#LocalNaviArea a:not([href*="#"])').path();

	// グローバルナビのアクティブ処理
	$('#GlobalNaviArea ul a').globalPath();

	//ロールオーバー
	$('.over').rollover();

	// 文字サイズ替え
	$('.switcher a').click(function() {
		switchStyle($(this).attr('title'));
		return false;
	});
	var c = $.cookie('style');
	if (c) {
		switchStyle(c);
	}
	function switchStyle(styleName) {
		$('link[rel*="stylesheet"][title]').each(function() {
			this.disabled = true;
			if ($(this).attr('title') == styleName) {
				this.disabled = false;
			};
		});
		// 選択した設定をクッキーに保存します
		$.cookie('style', styleName, {
			expires: 30, // 有効日数
			path: '/', // 有効ディレクトリ
			domain: 'example.com', // サブドメインで共有する場合に指定
			secure: true // HTTPS の場合に指定
		});
	}

	// タブ切り替え
	$('#tabChange').tabs();

});

