/* -------------------------------------------------------------------
	* 共通関数実行
	* init
 ------------------------------------------------------------------- */
$(function() {

	/* for IE6 background image flicker */
	if (jQuery.browser.msie && jQuery.browser.version == 6) {
		try {
			document.execCommand("BackgroundImageCache", false, true);
		} catch(err) {}
	}

	/* set RolloverImages */
	var roi = new RolloverImages('rollover', 'on');
	
	/* popup */
	$('.popupLink').popup();

	/* set SmoothScroll */
	$().SmoothScroll();
	
	/* fixColumnWidth */
	$('.fixColumnWidth').fixColumnWidth();
	
	/* fixColumnHeight */
	$('.fixColumnHeight').fixColumnHeight();
	
	/* closeBtn */
	$('#closeBtn').closeBtn();
	
	
	/* -------------------------------------------------------------------
	* 文字サイズ監視
	 ------------------------------------------------------------------- */
	$('body').append('<div id="e">fontsize change event</div>');
	$('#e').css('visibility','hidden').css('position','absolute').css('top','0');
	var defHeight = $('#e').height();

	checkBoxSize = function(){
		if(defHeight != $('#e').height()){
			
			// if fontsize was changed
			if($('#sitetop').length){masonrySet();} //for sitetop
			$('.fixColumnHeight').fixColumnHeight();
			
			defHeight= $('#e').height();
		};
	};
	setInterval(checkBoxSize,500);
	
	
	/* -------------------------------------------------------------------
	* グロナビドロップダウンメニュー
 	------------------------------------------------------------------- */
	//サブメニューを消す
    $('ul.subMenu').hide();
	
	//ホバー処理
    $('li.mainMenu').hover(
		function(){
			var target = $('img:first',this);
			var t = $(this)
			if(t.is('.gnavOn') == false){
				onSrc = $('img:first',this).attr('src').replace(/(\.gif)$/, 'on'+'.gif');
				$('img:first',this).attr('src',onSrc);
			}
			$('ul:not(:animated)',this).animate(
				{height:'show'},
				{duration: 200, easing: 'linear'}
			)
        },
        function(){
			var target = $('img:first',this);
			var t = $(this)
			if(t.is('.gnavOn') == false){
				offSrc = $('img:first',this).attr('src').replace(/(\on.gif)$/, '.gif');
				$('img:first',this).attr('src',offSrc);
			}
            $('ul',this).animate(
				{height:'hide'},
				{duration: 200, easing: 'linear'}
			)
    	}
	);
	
});



/* -------------------------------------------------------------------
	* 共通関数定義
	* @function
 ------------------------------------------------------------------- */
(function($){

	/*
	 * リンクのtarget指定の設定・上書き	(初期値：pdfリンクを別窓にする)
	 * @function
	 */
	$.fn.LinkTargetSet = function(config){
		if(this.length == 0) return false;
		c = jQuery.extend({
					reg	:	new RegExp("(\.pdf$|\.pdf#.*)", "i"),
				 	target	:	'_blank'
			 },config);
		$(this).each(function(){
			var t = this;
			var $t = $(t);
			var href = $t.attr('href');
			if (href.match(c.reg)) {
				$t.attr('target',c.target);
			}
		});
	};

	/*
	 * ポップアップ
	 * @function
	 */
	$.fn.popup = function (op) {
		settings = {
			height: 800, // 高さ設定
			width: 960, // 幅設定
			toolbar: 0, // ツールバー 1 (YES) or 0 (NO)
			scrollbars: 1, // スクロールバー 1 (YES) or 0 (NO)
			status: 0, // ステータスバー 1 (YES) or 0 (NO)
			resizable: 1, // リサイズ 1 (YES) or 0 (NO)
			left:0, // ポジション（left）
			top:0, // ポジション（top）
			center: 1 // 中央表示 1 (YES) or 0 (NO)
		};
		
		if (settings.center == 1) {
			settings.top = (screen.height - settings.height) / 2;
			settings.left = (screen.width - settings.width) / 2;
		}
		
		$.extend(settings,op || {});
		
		return this.each(function () {
	
			parameters = "height=" + settings.height + ",width=" + settings.width + ",toolbar=" + settings.toolbar + ",scrollbars=" + settings.scrollbars + ",status=" + settings.status + ",resizable=" + settings.resizable + ",left=" + settings.left + ",screenX=" + settings.left + ",top=" + settings.top + ",screenY=" + settings.top;
	
			$(this).bind("click", function () {
				var name = "POPUP";
				winObj = window.open(this.href, name, parameters);
				winObj.focus();
				return false;
			});
		});
	};

	/*
	 * スムーススクロール
	 * @function
	 */
	$.fn.SmoothScroll	= function(options){
		if(this.length == 0) return false;
		var scroller = (function() {
			var c = $.extend({
							easing:100,
							step:30,
							fps:60,
							fragment:''
						}, options);
				
			c.ms = Math.floor(1000/c.fps);
			var timerId;
			var param = {
					stepCount:0,
					startY:0,
					endY:0,
					lastY:0
			};
				
				// fnc
				function move() {
					if (param.stepCount == c.step) {
						// scroll end
						setFragment(param.fragment);
						
						window.scrollTo(getCurrentX(), param.endY);
					} else if (param.lastY == getCurrentY()) {
						// scroll
						param.stepCount++;
						window.scrollTo(getCurrentX(), getEasingY());
						param.lastY = getEasingY();
						timerId = setTimeout(move, c.ms); 
					} else {
						// error	
						if (getCurrentY()+getViewportHeight() == getDocumentHeight()) {
							setFragment(param.fragment);
						}
					}
				}
				function setFragment(fragment){
					location.href = location.href.split('#')[0] + fragment;
				}

				function getEasingY() {
					return Math.floor(getEasing(param.startY, param.endY, param.stepCount, c.step, c.easing));
				}
				function getEasing(start, end, stepCount, step, easing) {
					var s = stepCount / step;
					return (end - start) * (s + easing / (100 * Math.PI) * Math.sin(Math.PI * s)) + start;
				}
				function targetOffsetTop(target){
					return target.offset().top;
				}
				return {
					set: function(options) {
						this.stop();
						if (options.startY == undefined) options.startY = getCurrentY();
						param = $.extend(param, options);						
						param.lastY = param.startY;
						param.fragment = options.fragment;
						timerId = setTimeout(move, c.ms); 
					},
					stop: function(){
						clearTimeout(timerId);
						param.stepCount = 0;
					}
				};
			})();
			
			function getCurrentY() {
					return document.body.scrollTop  || document.documentElement.scrollTop;
			}
			function getCurrentX() {
					return document.body.scrollLeft  || document.documentElement.scrollLeft;
			}
			function getDocumentHeight(){
					return (document.compatMode && document.compatMode != "BackCompat")?document.documentElement.scrollHeight:document.body.scrollHeight;
			}
			function getViewportHeight(){
					return (!$.browser.safari && !$.browser.opera) ? document.documentElement.clientHeight || document.body.clientHeight || document.body.scrollHeight : window.innerHeight;
			}
			// init
			$('a[href^=#], area[href^=#]').not('a[href=#], area[href=#], a.no-scroll, area.no-scroll').click(function(){

				var fragment = $(this).attr('href');
				var target = $(fragment);
				if (target.length == 0) target = $('a[name='+fragment+']');
				
				if (target.length) {
					scroller.set({
						endY: ((getDocumentHeight() - target.offset().top )) < getViewportHeight() ? getDocumentHeight() - getViewportHeight() : target.offset().top,
						fragment: fragment,
						_target :	target
					});

					return false;
				}
			});
	};
	
	
	/* ------------------------------------------
		fixColumnWidth
	------------------------------------------ */
	$.fn.fixColumnWidth = function (config) {
		if (this.length == 0) return false;
		
		$(this).each(function () {
			imgArr = [];
			targetImg = $('img',this);
			
			targetImg.each(function(){ //画像の数だけ画像幅を格納
				
				tmpWidth = $(this).width();
				imgArr.push(tmpWidth);
				
			});
			imgArr.sort(function(a, b) {return a - b});
			maxWidth = imgArr[imgArr.length - 1]; //配列の中から一番大きいものを取り出す
			$(this).width(maxWidth);

		});
	};
	
	
	
	/* ------------------------------------------
		fixColumnHeight
	------------------------------------------ */
	$.fn.fixColumnHeight = function (config) {
		if (this.length == 0) return false;
		
		$(this).each(function () {
			columnArr = [];
			targetColumn = $('.leftContents,.centerContents,.rightContents,.indexBlock01',this);
			
			targetColumn.each(function(){ //カラムの数だけ高さを格納
				
				$(this).height('auto');
				tmpHeight = $(this).height();
				columnArr.push(tmpHeight);
				
			});
			
			columnArr.sort(function(a, b) {return a - b});
			maxHeight = columnArr[columnArr.length - 1]; //配列の中から一番大きいものを取り出す
			targetColumn.height(maxHeight);

		});
	};
	
	
	
	/* ------------------------------------------
		closeBtn
	------------------------------------------ */
	$.fn.closeBtn = function () {
		if (this.length == 0) return false;
		
		$(this).show();
		var method = function(){
			window.close();
		};
		
		$(this).click(method);
		
	};
	
	
})(jQuery)
