/*============================================================================ 
jquery.function.js
Copyright (C) C-brains Corporation All rights reserved.
============================================================================ */

/**********************************************************
setClassBtn
--------
指定の要素のclass属性に"btn"を設定
@param / @return
**********************************************************/
function setClassBtn() {
	$("div#header dl dd img, ul#headerNav li img, div#nav ul li img, div#unitSegment ul li img").each(function() {
		$(this).addClass("btn");
	});
}

/**********************************************************
highlightCurrentNav
--------
現在のカテゴリナビゲーションをハイライト
@param / @return
**********************************************************/
function highlightCurrentNav() {
	$("div#nav li > a, ul#headerNav > li > a").each(function() {
        var homepathes, naviPath;
        homepathes = ['/', '', 'index.php', '/index.php'];
        // ホームかどうか
        if ($.inArray(this.pathname, homepathes) != -1) {
            if ($.inArray(location.pathname, homepathes) != -1) {
                $('img', this).attr('src', $('img', this).attr('src').replace(/\.gif/, '_d.gif'));
            }
            return;
        }
        // それ以外のカテゴリかどうか
        naviPath = this.pathname;
        // IEではpathnameが'/'から開始されない対策
        if (!(naviPath.match(/^\//))) {
            naviPath = '/' + this.pathname;
        }
        if (location.pathname.match(new RegExp('^' + naviPath))) {
            $('img', this).attr('src', $('img', this).attr('src').replace(/\.gif/, '_d.gif'));
        }
	});
}


/**********************************************************
initRollOverImages
--------
マウスオーバーで画像を切換
@param / @return
**********************************************************/
function initRollOverImages() {
	var image_cache = new Object();
	$("a img.btn,input[type=image].btn").not("[src*='_o.'],[src*='_d.']").each(function(i) {
		var imgsrc = this.src;
		var dot = this.src.lastIndexOf('.');
		var imgsrc_on = this.src.substr(0, dot) + '_o' + this.src.substr(dot, 4);
		image_cache[this.src] = new Image();
		image_cache[this.src].src = imgsrc_on;
		$(this).hover(
			function() { this.src = imgsrc_on; },
			function() { this.src = imgsrc; }
		);
	});
}


/**********************************************************
pageScroll
--------
ページをスムーズにスクロール（ href属性値が#で始まるものが対象 ）
@param / @return
**********************************************************/
function pageScroll() {
	$("a[href*=#]").click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 500);
				return false;
			}
		}
	});
}

/**********************************************************
toggleSubNaviSeminar
--------
サブナビゲーションをトグルさせる(親liをクリックしたら子ulを展開する)
@param / @return
**********************************************************/
function toggleSubNaviSeminar() {
    //セミナーコンテンツ
    if (location.pathname.match(/^\/?seminar\//)) {
        // 一旦子要素を全て非表示
        $('div#sub > ul > li > ul').hide();
        // 親要素が選択されていたら子要素を展開
        $('div#sub > ul > li.current > ul').show();
        // 子要素の中が選択されていたら親要素を展開
        $('div#sub > ul > li > ul > li.current').parent().show();
    }
}
/**********************************************************
toggleSubNaviInformation
--------
サブナビゲーションをトグルさせる(親liをクリックしたら子ulを展開する)
@param / @return
**********************************************************/
function toggleSubNaviInformation() {
    var toggleBotton, currentCategory;
    //セミナーコンテンツ
    if (location.pathname.match(/^\/?information\//)) {
        toggleBotton = $('div#sub > ul > li.toggle');
        // まずカテゴリを全て非表示
        toggleBotton.hide();
        
        // 人材マネジメントセミナーが選択されていたら
        // 子のメニューを表示
        if ($('div#sub > ul > li.seminar').hasClass('current')) {
            toggleBotton.show();
        }
        // 個々のエントリーが選択されていても表示
        currentCategory = toggleBotton.filter(':has(li.current)');
        if (currentCategory.length > 0) {
            toggleBotton.show();
        }


        // li.toggle > aはさらに子のulを開閉するためのボタン
        toggleBotton.find('> a').click(function(){
            var target;
            target = $(this).parent().find('ul');
            if (target.css('display') === 'none') {
                target.css('display', 'block');
            } else {
                target.css('display', 'none');
            }
            return false;
        }).click(); // 初期状態：閉じる

        // が、選択されているエントリがあったら開く
        currentCategory.find('> a').click();


    }
}


/**********************************************************
実行処理
**********************************************************/
$(document).ready(setClassBtn);
$(document).ready(highlightCurrentNav);
$(document).ready(initRollOverImages);
$(document).ready(pageScroll);
$(document).ready(toggleSubNaviSeminar);
$(document).ready(toggleSubNaviInformation);



