
// 初期実行の記述
$(function() {
	globalNaviStyle('globalNavigation');
	DropDownNavigation();
	ScrollSkipLink(false);
});

/**
 * DropDownNavigation
 * マウスオーバーで下層のナビゲーションを表示させる
 */
function DropDownNavigation() {
	// 基本設定
	var config = {
		// 各id・class名関連
		headlineIdPrefix : "ddn-headline-",
		contentIdPrefix : "ddn-content-",
		headlineActiveClass : "ddnActive",
		// ナビ表示位置を固定するか（true/false）
		positionFixed : true,
		// ナビ表示位置を固定する場合、基準となる要素（要素の左下が基準となる）
		positionTarget : ".globalNavigation",
		// 表示位置補正（ボーダーなどでズレが出る場合に指定）
		revisionTop : 10,
		revisionLeft : 0,
		// 意味無し
		dummy : true
	};
	// ナビの背景を使用する場合に指定、使用しない場合は"false"。
	//var bgConfig = false;
	var bgConfig = {
		idPrefix : "ddn-background-",
		opacity : 0.95,
		css : {
			background : "#292929",
			"-moz-border-radius" : "4px",
			"-webkit-border-radius" : "4px",
			"border-radius" : "4px",
			position : "absolute"
		}
	};
	// 状態の初期値
	var status = {
		mouseOn : false,
		mouseOnNum : 0,
		timer : "",
		status : true
	};

	// 位置情報の取得関連
	if (config.positionFixed == true) {
		status.positionTop = $(config.positionTarget).position().top + $(config.positionTarget).innerHeight() + config.revisionTop;
		status.positionLeft = $(config.positionTarget).position().left + config.revisionLeft;
		// ウィンドウリサイズ時に位置情報の再取得を設定
		$(window).resize(function(){
			status.positionTop = $(config.positionTarget).position().top + $(config.positionTarget).innerHeight() + config.revisionTop;
			status.positionLeft = $(config.positionTarget).position().left + config.revisionLeft;
		});
	}

	// ヘッド：マウスオーバーの機能を付加
	$("[id^='" + config.headlineIdPrefix + "']").bind("mouseover", function() {
		var num = this.id.match(/[0-9]+$/);
		var position = getPosition(this);
		var content = $("#" + config.contentIdPrefix + num);
		var contentCss = {
			position : "absolute",
			top : position.top,
			left : position.left,
			zIndex : 200,
			display : "block"
		};

		clearRefreshTimer();
		status.mouseOn = true;
		if (String(num) != String(status.mouseOnNum)) {
			refreshAll();
			status.mouseOnNum = num;
			$(this).addClass(config.headlineActiveClass);
			// 背景の作成、コンテンツを表示状態に
			if (bgConfig != false) {
				appendBackground(num);
				$("#" + bgConfig.idPrefix + num).animate({ opacity: bgConfig.opacity, height: content.innerHeight() }, 200, function() {
					content.css(contentCss);
				});
			} else {
				content.css(contentCss);
			}
		}

		return false;
	});

	// ヘッド：マウスアウトの機能を付加
	$("[id^='" + config.headlineIdPrefix + "']").bind("mouseout", function() {
		status.mouseOn = false;
		setRefreshTimer();
	});

	// コンテンツ：マウスオーバーの機能を付加
	$("[id^='" + config.contentIdPrefix + "']").bind("mouseover", function(){
		clearRefreshTimer();
		status.mouseOn = true;
	});

	// コンテンツ：マウスアウトの機能を付加
	$("[id^='" + config.contentIdPrefix + "']").bind("mouseout", function(){
		status.mouseOn = false;
		setRefreshTimer();
	});

	/**
	 * 時間を置いてアクティブのものを非アクティブに戻す
	 * ヘッド・コンテンツからのマウスアウト時に実行される
	 */
	function setRefreshTimer() {
		status.timer = setInterval(function (){
			if (status.mouseOn == false) {
				refreshOne(status.mouseOnNum);
				clearInterval(status.timer);
			}
		}, 300);
	}

	/**
	 * "setRefreshTimer"でセットしたインターバルのクリア
	 */
	function clearRefreshTimer() {
		clearInterval(status.timer);
	}

	/**
	 * 指定のものを非アクティブ状態に戻す
	 * @param num 識別番号（id="ddn-headline-1" id末尾の番号）
	 */
	function refreshOne(num) {
		status.mouseOnNum = 0;
		var headlineId = config.headlineIdPrefix + num;
		var contentId = config.contentIdPrefix + num;
		if (bgConfig != false) {
			var bgId = bgConfig.idPrefix + num;
			$("#" + bgId).stop();
			$("#" + bgId).animate({ opacity: 0, height: 0 }, 200, function() {
				$("#" + bgId).remove();
			});
			/*if (config.positionFixed == false) {
				$("#" + bgId).animate({ opacity: 0, height: 0 }, 200, function() {
					$("#" + bgId).remove();
				});
			} else {
				$("#" + bgId).remove();
			}*/
		}
		$("#" + headlineId).removeClass(config.headlineActiveClass);
		$("#" + contentId).css("display", "none");
	}

	/**
	 * 全てのものを非アクティブ状態に戻す
	 */
	function refreshAll() {
		status.mouseOnNum = 0;
		if (bgConfig != false) {
			$("[id^='" + bgConfig.idPrefix + "']").stop();
			$("[id^='" + bgConfig.idPrefix + "']").remove();
		}
		$("[id^='" + config.headlineIdPrefix + "']").removeClass(config.headlineActiveClass);
		$("[id^='" + config.contentIdPrefix + "']").css("display", "none");
	}

	function appendBackground(num) {
		var id = bgConfig.idPrefix + num;
		var position = getPosition("#" + config.headlineIdPrefix + num);
		$("body").append('<div id ="' + id + '"></div>');
		$("#" + id).css(bgConfig.css);
		$("#" + id).css({
			opacity: 0,
			top : position.top,
			left : position.left,
			zIndex : 100,
			width : $("#" + config.contentIdPrefix + num).innerWidth() + "px",
			// 上から下へ伸びるアニメーションを付けるため"height"は"0"を指定
			//height: $("#" + config.contentIdPrefix + num).innerHeight() + "px"
			height : 0
		});
	}

	function getPosition(target) {
		if (config.positionFixed == true) {
			var result = {
				top : status.positionTop,
				left : status.positionLeft
			};
		} else {
			var result = {
				top : $(target).position().top + $(target).innerHeight() + config.revisionTop,
				left : $(target).position().left + config.revisionLeft
			};
		}
		return result;
	}
}

/**
 * ScrollSkipLink
 *
 * 『#pagetop』のようなページ内リンクを
 * スクロールのアニメーション使って所定位置まで移動させる。
 * rel属性に『scroll-skip-link』が指定されたアンカーにイベントを付加する。
 *
 * @param boolean opening ページが開かれた時に反応するか（する：true）
 * @return false
 */
function ScrollSkipLink(opening) {

	var targetRelValue = "scroll-skip-link";
	var animationRate  = 600;

	$("a[rel='" + targetRelValue + "']").live("click", function() {
		var where = $(this).attr("href").match(/[^#]+$/);
		if ($("a[name='" + where + "']").size()) {
			$("html, body").animate(
					{ scrollTop : $("a[name='" + where + "']").offset().top }, animationRate);
		}
		return false;
	});

	// ページが開かれた時のページ内リンクにも
	if (opening) {
		var anchorLabel = location.href.match(/#([^#]+)$/);
		if (anchorLabel) {
			var where = anchorLabel[1];
			if ($("a[name='" + where + "']").size()) {
				$("html, body").animate(
						{ scrollTop : $("a[name='" + where + "']").offset().top }, animationRate);
			}
		}
	}
	return false;
}


/*
 * グローバルナビゲーションのアクティブスタイル管理
 * アクティブ用のスタイルとして該当する"li"にクラス"act"を追加する
 * @param targetUl 対象となるulのid
 */
function globalNaviStyle(targetUl) {
	var targetId = "#" + targetUl;
	var urlArr = window.location.href.split('/');
	var currentDir = urlArr[(urlArr.length) - 2];
	
	$(targetId).children("li").each(function() {
		var itemId = $(this).attr('id').replace("gn_", "");
		if (itemId == currentDir) {
			$(this).addClass('act');
		}
	});
}

/*
 * サービス紹介のローカルナビゲーションのアクティブスタイル管理
 * ※機能は"globalNaviStyle()"と同じ
 */
function serviceNaviStyle(targetUl) {
	var targetId = "#" + targetUl;
	var urlArr = window.location.href.split('/');
	var currentUrl = urlArr[(urlArr.length) - 1].replace(".html", "");

	$(targetId).children("li").each(function() {
		var itemId = $(this).attr('id').replace("s_", "");
		var f = itemId.indexOf(currentUrl);
		if (f != -1) {
			$(this).addClass('act');
		}
	});
}

/*
 * 会社情報（会社案内等）のローカルナビゲーションのアクティブスタイル管理
 * ※機能は"globalNaviStyle()"と同じ
 */
function aboutusNaviStyle(targetUl){
	var targetId = "#" + targetUl;
	var urlArr = window.location.href.split('/');
	var currentUrl = urlArr[(urlArr.length) - 1].replace(".html", "");
	var currentDir = urlArr[(urlArr.length) - 2];
	
	$(targetId + " li").each(function(){
		var itemHref = $(this).children("a").attr("href");
		var itemId = $(this).attr('id').replace("s_", "");
		var f = itemId.indexOf(currentUrl);
		var d = itemId.indexOf(currentDir);
		if(d != -1){
			$(this).parent("ul").children("li").removeClass("act");
			$(this).addClass('act');
		}else if(f != -1){
			$(this).addClass('act');
		}
	});
}

/*
 * テーブルに偶数行ごとにクラスを追加する
 * 偶数行のtrの子要素にクラス"even"を追加する
 * @param targetTableClass 対象となるtableのclass
 */
function resultTableStyle(targetTableClass) {
	$('.' + targetTableClass).each(function() {
		$(this).children("tbody").children("tr:even").addClass('even');
	});
}


//IE互換用
if(!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}
if(!Array.indexOf){
	Array.prototype.indexOf = function(object){
		for(var i = 0; i < this.length; i++){
			if(this[i] == object){
				return i;
			}
		}
		return -1;
	}
}
// / IE互換用


