// ----------------------------------------------------------------------------
// DOMツリーが構築されたタイミングで実行するメソッド
// ----------------------------------------------------------------------------
$(document).ready(function(){
    $('.utility img').setRollovers();
    
    //$('div.layout>section').set_height();
    $('div.layout>section.size1of2').set_height({ items_per_row:2 });
    $('div.layout>section.size1of3').set_height({ items_per_row:3 });
    $('div.layout>section.size2of3').set_height({ items_per_row:2 });
    $('div.layout>div.unit').set_height();
    
    $('figure').setImgAndCaptionStyler();
    $('ul.image li').setImgAndCaptionStyler();
    
    $('article a').not(':has(img)').setIcons({ blank_icon: "/compo/img/icon_blank.png", pdf_icon: "/compo/img/icon_pdf.png" });
    $('nav.localNav a').setActiveLocalMenu();
});


// ----------------------------------------------------------------------------
// jQuery set_height plugin
// 渡された要素の高さをそろえる。
//
// Use:
// set_height( @options )
// 
// Attributes:
// -@options: オプションのハッシュ
//  -items_per_row: 渡された要素を設定した数ごと高さをそろえる
//  -delay: 文字サイズの変更のチェックインターバル（ms）、デフォールトで 1000 (1秒)
//  -group_by_parent: 要素を設定すると、その要素内で高さを合わせる
// 
// Examples:
// :$('ul.index-double>li').set_height({ items_per_row:2 });
// =[index-double]のクラスが付いてる ul の 「>li」 の高さをそろえます。
//
// ----------------------------------------------------------------------------
(function(A){A.fn.set_height=function(P){var G={items_per_row:false,one_height:false,delay:1000,group_by_parent:false};if(P){jQuery.extend(G,P)}if(A("#js_etalon").length){var N=A("#js_etalon").get(0)}else{var N=A("body").append('<span style="position:absolute;left:-9999px;top:0;" id="js_etalon">&nbsp;</span>').find("#js_etalon").get(0)}var E=function(S,R){for(var Q=0;Q<S.length;Q++){if(S[Q]==R){return }}S.push(R)};var B=this;var F=[];var L=[];var K=[];var J=function(){for(var S=0;S<F.length;S++){var R=0;var T=F[S][0].currentStyle?parseInt(F[S][0].currentStyle.paddingTop)+parseInt(F[S][0].currentStyle.paddingBottom):parseInt(document.defaultView.getComputedStyle(F[S][0],null).getPropertyValue("padding-top"))+parseInt(document.defaultView.getComputedStyle(F[S][0],null).getPropertyValue("padding-bottom"));for(var Q=0;Q<F[S].length;Q++){F[S][Q].style.height="auto";R=Math.max(F[S][Q].offsetHeight-T,R)}for(var Q=0;Q<F[S].length;Q++){F[S][Q].style.height=R+"px"}}};var M=function(){var Y=0;var W=0;var Q=null;var T=null;F[Y]=[];var Z=false;for(var U=0;U<B.length;U++){if(G.group_by_parent){Q=A(B[U]).parents(G.group_by_parent)[0];if(U>0&&Q!=T&&!Z){F[++Y]=[];W=0}T=Q}if(G.items_per_row){B[U].className+=" nb"+parseInt(W%G.items_per_row+1);if(!(W%G.items_per_row)&&W>0){F[++Y]=[];W=0;Z=true}}F[Y][W++]=B[U];Z=false}for(var U=0;U<F.length;U++){if(!F[U][0].offsetHeight){var X=F[U][0];while(X.style.display!="none"){X=X.parentNode}L.push(X)}for(var S=0;S<F[U].length;S++){var V=F[U][S].getElementsByTagName("img");for(var R=0;R<V.length;R++){E(K,V[R].src)}}}};if(B.length){M();var D=N.offsetHeight;var C=setInterval(function(){var S=N.offsetHeight;if(S!=D){D=S;J()}for(var R=0;R<L.length;R++){if(L[R].style.display!="none"){J();L=[];for(var Q=0;Q<F.length;Q++){if(!F[Q][0].offsetHeight){var T=F[Q][0];while(T.style.display!="none"){T=T.parentNode}L.push(T)}}}}},G.delay);J();if(K.length){var H=[];var O=0;for(var I=0;I<K.length;I++){H[I]=document.createElement("img");H[I].onload=function(){O++;if(O==K.length){J()}};H[I].src=K[I]}}}if(navigator.userAgent.match("MSIE 5.5")&&B.length){setTimeout(function(){main(B),0})}return this}})(jQuery);


// ----------------------------------------------------------------------------
// jQuery setRollovers plugin
// 画像のロールオーバーを設定する。
// 
// Attributes:
// -@options: オプションのハッシュ
//  -extension: ロールオーバーする画像の(.jpg|.gif|.png)の前の文字列、デフォルトで '_o'
//  -hover_extension: ロールオーバーしたときの画像の(.jpg|.gif|.png)の前の文字列、デフォルトで '_h'
//  -menu: メニューのパスと画像のハッシュ、設定の例 menu:{ "パス":"画像", "パス2":"画像2" }
// ----------------------------------------------------------------------------
(function(b){var a="setRollovers";b.fn[a]=function(c){var d={extension:"_o",hover_extension:"_h",menu:{}};if(c){jQuery.extend(d,c)}var e=b(this);var g=document.location.pathname;var f=false;b.each(d.menu,function(h){if(g.match(new RegExp("^"+h))){f=d.menu[h]}});return this.filter(function(h){return b(this).attr("src").match(d.extension)}).each(function(){var h=new Image;h.src=this.src.replace(d.extension+".",d.hover_extension+".");if(this.src.match(/[^\/]+$/)[0]==f){this.src=this.src.replace(new RegExp(d.extension+"(.[a-z]+)$"),d.hover_extension+"$1")}else{if(this.parentNode.nodeName.match(/a/i)){b(this).parent().focus(function(){b(this).find(">img").trigger("mouseover")}).blur(function(){b(this).find(">img").trigger("mouseout")})}b(this).mouseover(function(){this.src=this.src.replace(new RegExp(d.extension+"(.[a-z]+)$"),d.hover_extension+"$1")}).mouseout(function(){this.src=this.src.replace(d.hover_extension,d.extension)})}})}})(jQuery);


// ----------------------------------------------------------------------------
// jQuery setImgParallelStyler plugin
// 画像とキャプションの幅を揃える
// ----------------------------------------------------------------------------
(function($){
    var name_space = 'setImgAndCaptionStyler';
    $.fn[name_space] = function(options){
        var elements = $(this);
        function getImgAndCaptionStyler(){
            return function(){
                var elm = $(this);
                var img = $("img", elm);
                function resize(){
                    var imgWidth = img.width();
                    var caption = $(">span", elm)
                    if(imgWidth){
                        caption.css("width", imgWidth);
                    }
                }
                resize();
                img.load(function(){resize()});
            }
        }
        elements.each(getImgAndCaptionStyler());
        return this;
    };
})(jQuery);


// ----------------------------------------------------------------------------
// jQuery setIcons plugin
// target="_balnk"、参照先がpdfへのリンクテキストに自動でアイコンを付与する。
// 
// Attributes:
// -@options: オプションのハッシュ
//  -blank_icon: blankアイコン画像のパス
//  -pdf_icon: pdfアイコン画像のパス
// ----------------------------------------------------------------------------
(function($){
    var name_space = 'setIcons';
    $.fn[name_space] = function(options){
        var settings = $.extend({
            blank_icon : false,
            pdf_icon : false
        }, options);
        var elements = $(this);
        var icoBLANK = '<img src="'+ settings.blank_icon +'" alt="" class="icon" />';
        var icoPDF = '<img src="'+ settings.pdf_icon +'" alt="" class="icon" />';
        function get(){
            return function(){
                $(this).filter('[href$=pdf]').append(icoPDF);
                $(this).filter('[target$=blank]').not('[href$=pdf]').append(icoBLANK);
            }
        }
        elements.each(get());
        return this;
    };
})(jQuery);


// ----------------------------------------------------------------------------
// jQuery setActiveLocalMenu plugin
// ローカルナビゲーションの現在地表示とメニュー開閉の自動化処理
// ----------------------------------------------------------------------------
(function($){
    var name_space = 'setActiveLocalMenu';
    $.fn[name_space] = function(options){
        var elements = $(this);
        var pagePath = location.pathname.replace('index.html','').split('#')[0];
        function get(){
            return function(){
                var href = $(this).attr('href').replace('index.html','');
                
                // 2階層目のul要素を隠す
                if($(this).next('ul')){ $(this).next('ul').addClass('child').hide() }
                
                // リンク参照先と現在のページパスが一致する場合
                if(pagePath == href){
                    $(this).addClass('active');
                    var parent_ul = $(this).parent('li').parent('ul')
                    if(parent_ul.attr('class') != "child") {
                        $(this).next('ul').show();
                    }
                    else {
                        parent_ul.show();
                    }
                }
            };
        };
        elements.each(get());
        return this;
    };
})(jQuery);


// ----------------------------------------------------------------------------
// ページ内フラグへの位置情報の調整
// ----------------------------------------------------------------------------
_get_top = function(elem){
	var curtop = 0;
	if (elem.offsetParent){
        while (elem.offsetParent){
            curtop += elem.offsetTop;
            elem = elem.offsetParent;
        }
    }
	else if (elem.y) {
        curtop += elem.y;
    }
	return curtop;
}

window.onload = function(){
    //alert(_get_top(document.getElementById(window.location.href.split('#')[1])))
    if(window.location.href.indexOf('#') != -1) {
        window.scrollTo(0, _get_top(document.getElementById(window.location.href.split('#')[1])) );
    }
}



