/* ========================================
    機能を実行
========================================= */

jQuery(function($){

    var $main = $('#main'),
        $sub = $('#sub');

    // jQuery プラグインを実行
    (function(){
        // ブラウザが IE6 か IE7 であるか判別
        var isIE6or7;
        if (!$.support.opacity && !jQuery.support.style) {
            isIE6or7 = true;
        }
        // 画像ロールオーバー
        $('img, input[type="image"]').set_rollovers();
        
        // 要素の高さ揃え
        if (!isIE6or7) {
            $('.indexList01 > li', $main).set_height({ items_per_row:2, group_by_parent: 'ul' });
        } else {
            $('.indexList01 > li:nth-child(2n+1)').css('clear', 'both'); // IE6, 7 では高さ揃え時に表示不具合があるため、clear:left; を使って擬似的に頭を揃える
        }
        $('.indexList02 > li', $main).set_height({ items_per_row:3, group_by_parent: 'ul' });
        $('.indexList04 > li', $main).set_height({ items_per_row:3, group_by_parent: 'ul' });
        $('.indexList05 .nav li a', $main).set_height({ items_per_row:2, group_by_parent: 'ul' });
        $('.indexList06 > li', $main).set_height({ items_per_row:5, group_by_parent: 'ul' });
        $('.indexList07 .body', $main).set_height({ items_per_row:3, group_by_parent: 'ul' });
        $('.indexList08 > li', $main).set_height({ items_per_row:3, group_by_parent: 'ul' });
		$('.designersList > li', $main).set_height({ items_per_row:3, group_by_parent: 'ul' });
        $('.indexListForm > li', $main).set_height({ items_per_row:3, group_by_parent: 'ul' });
        $('.indexListForm > li dt', $main).set_height({ items_per_row:3, group_by_parent: 'ul' });
        
        // ポップアップ表示
        $.set_links({
            ".popup" : { width:550, height: 550, toolbar: "no", scrollbars: "yes", status: "yes" ,directories: "no", menubar: "no", resizable: "yes" },
			".popup02" : { width:800, height: 715, toolbar: "no", scrollbars: "yes", status: "yes" ,directories: "no", menubar: "no", resizable: "yes" }
        });
    })();

    // グローバルナビゲーション現在地表示
    (function(){
        // 変数定義
        var pageClass = $('#page').attr('class');
        var condSrc = /_o(\.[a-z]{3})$/;
        var condCategory = /(^.+?gn_)(.+?)(_o\.[a-z]{3}$)/;
        $('#globalNav img').each(function(){
            var obj = $(this);
            var category = obj.attr('src').replace(condCategory, '$2');
            // 一致するメニューがあればアクティブ化
            if (pageClass === category) {
                obj.attr('src', obj.attr('src').replace(condSrc, '_c$1'));
            }
        });
    })();

    // グローバルナビゲーションプルダウンメニュー表示
    (function(){
        $('#globalNav .nav > li').hover(
            function(){
                $(this).addClass('hover');
            },
            function(){
                $(this).removeClass('hover');
            }
        );
    })();

    // ローカルナビゲーション現在地表示
    (function() {
        if (!$('#page').hasClass('nav-local-nocheck')) {
            var condIndex = /\/index\.html?/;
            var path = location.pathname.replace(condIndex, '/');
            var checks = [path];
            // 文字列末尾から 1 ディレクトリずつ削り、URL を収集
            while (path && '/' !== path) {
                path = path.replace(/[^/]*\/?$/, '');
                checks.push(path);
            }
            // メニューの href 属性値をすべて取得
            var hrefs = $('#localNav ul a').map(function(){
                return $(this).attr('href').replace(condIndex, '/');
            });
            // URL と href 属性値の一致を判定
            checkStart : for (var i = 0; i<checks.length; i++) {
                for (var j = 0; j<hrefs.length; j++) {
                    if (checks[i] == hrefs[j]) {
                        $('#localNav ul a').eq(j)  // 一致した a 要素の
                            .parent('span').addClass('current')  // 親要素の span 要素に current を付与
                            .parents('li').addClass('selected');  // その親であるの li 要素すべてに selected を付与
                        break checkStart;
                    }
                }
            }
        }
    })();

    // 画像とテキストの横並び
    (function(){
        // 回りこみなしモジュールの幅設定
        $('.imageAndText', $main).each(function(){
            var obj = $(this);
            $('.imageBlock img:first, .fleft img, .fright img', obj).each(function(){
                var img = $(this);
                var imgWidth = img.attr('width');  // 内包画像の幅を取得
                if (img.hasClass('imageFrame')) {
                    imgWidth += 8;  // imageFrame がある場合はボーダーの幅を含めてセット
                } else if (img.hasClass('aquraNewsThumb')) {
                    imgWidth += 14;  // アキュラ通信のサムネイル画像用のボーダー幅を含めてセット
                }
                // 回りこみの有無で画像幅をセットする要素を変更
                if (obj.hasClass('imagePosition01') || obj.hasClass('imagePosition02')) {
                    img.parents('.imageBlock').css('width', imgWidth);
                } else if (obj.hasClass('imagePosition03')) {
                    img.parent().css('width', imgWidth);
                }
            });
        });
    })();

    // リンク先種別ごとにアイコンを付与
    $('a:not(:has(img))', $main).each(function(){
        var obj = $(this);
        if (obj.attr('href').match(/\.pdf/)) {
            // PDF ファイルへのリンクは PDF アイコンを付与
            obj.append('<img src="/common/images/icon_pdf_01.gif" class="blank" alt="PDFファイルを新しいウィンドウで開きます" />');
        } else if (obj.attr('target') == '_blank') {
            // 新しいウィンドウで開くリンクはブランクアイコンを付与
            obj.append('<img src="/common/images/icon_blank_01.gif" class="blank" alt="新しいウィンドウで開きます" />');
        }
    });
    //$('a[target="_blank"]:not(:has(img))', $main).append('<img src="/common/images/icon_blank_01.gif" class="blank" alt="新しいウィンドウで開きます" />');

    // 特定のブロック内に登場する最後の要素のマージンを 0 に
    $('.relatedInfo > dl > dd > :last-child', $main).css('margin-bottom', '0');

    // .listImageAndText をストライプ化
    $('.listImageAndText01 .item:nth-child(2n)', $main).addClass('even');

    // 沿革用テーブルをストライプ化
    $('#history tr:nth-child(2n+1)', $main).addClass("odd");
    $('#history tr:nth-child(2n)', $main).addClass("even");

    // .navPage01 の最初と最後の要素のマージンを変更するためクラスを付与
    $('.navPage01 > li:first-child', $main).addClass('first');
    $('.navPage01 > li:last-child', $main).addClass('last');

    // お問い合わせ用カタログインデックスのチェックによる背景制御
    $('.indexListForm input[type="checkbox"]').change(function(){
        var obj = $(this);
        if (obj.attr('checked') == true) {
            obj.parents('li').addClass('checked');
        } else {
            obj.parents('li').removeClass('checked');
        }
    });

});


/* ========================================
    jQuery プラグインをロード
========================================= */
/* _____ 画像ロールオーバー _____ */
(function(a){jQuery.fn.set_rollovers=function(b){var c={extension:"_o.",hover_extension:"_h.",menu:{}};if(b){jQuery.extend(c,b)}var e=document.location.pathname;var d=false;a.each(c.menu,function(f){if(e.match(new RegExp("^"+f))){d=c.menu[f]}});return this.filter(function(f){return a(this).attr("src").match(c.extension)}).each(function(){var f=new Image;f.src=this.src.replace(c.extension+".",c.hover_extension+".");if(this.src.match(/[^\/]+$/)[0]==d){this.src=this.src.replace(new RegExp(c.extension+"(.[a-z]+)$"),c.hover_extension+"$1")}else{if(this.parentNode.nodeName.match(/a/i)){a(this).parent().focus(function(){a(this).find(">img").trigger("mouseover")}).blur(function(){a(this).find(">img").trigger("mouseout")})}a(this).mouseover(function(){this.src=this.src.replace(new RegExp(c.extension+"(.[a-z]+)$"),c.hover_extension+"$1")}).mouseout(function(){this.src=this.src.replace(c.hover_extension,c.extension)})}})}})(jQuery);

/* _____ 要素の高さ揃え _____ */
(function(a){a.fn.set_height=function(q){var g={items_per_row:false,delay:1000,group_by_parent:false};if(q){jQuery.extend(g,q)}if(a("#js_etalon").length){var o=a("#js_etalon").get(0)}else{var o=a("body").append('<span id="js_etalon_wrapper" style="height:0px;overflow:hidden;display:block;"><span id="js_etalon">m</span></span>').find("#js_etalon").get(0)}var e=function(t,s){for(var r=0;r<t.length;r++){if(t[r]==s){return}}t.push(s)};var b=this;var f=[];var m=[];var l=[];var k=function(){for(var t=0;t<f.length;t++){var s=0;var u=f[t][0].currentStyle?parseInt(f[t][0].currentStyle.paddingTop)+parseInt(f[t][0].currentStyle.paddingBottom):parseInt(document.defaultView.getComputedStyle(f[t][0],null).getPropertyValue("padding-top"))+parseInt(document.defaultView.getComputedStyle(f[t][0],null).getPropertyValue("padding-bottom"));for(var r=0;r<f[t].length;r++){f[t][r].style.height="auto";s=Math.max(f[t][r].offsetHeight-u,s)}for(var r=0;r<f[t].length;r++){f[t][r].style.height=s+"px"}}};var n=function(){var z=0;var x=0;var r=null;var u=null;f[z]=[];var A=false;for(var v=0;v<b.length;v++){if(g.group_by_parent){r=a(b[v]).parents(g.group_by_parent)[0];if(v>0&&r!=u&&!A){f[++z]=[];x=0}u=r}if(g.items_per_row){b[v].className+=" nb"+parseInt(x%g.items_per_row+1);if(!(x%g.items_per_row)&&x>0){f[++z]=[];x=0;A=true}}f[z][x++]=b[v];A=false}for(var v=0;v<f.length;v++){if(!f[v][0].offsetHeight){var y=f[v][0];while(y.style.display!="none"){y=y.parentNode}m.push(y)}for(var t=0;t<f[v].length;t++){var w=f[v][t].getElementsByTagName("img");for(var s=0;s<w.length;s++){e(l,w[s].src)}}}};if(b.length){n();var d=o.offsetHeight;var c=setInterval(function(){var t=o.offsetHeight;if(t!=d){d=t;k()}for(var s=0;s<m.length;s++){if(m[s].style.display!="none"){k();m=[];for(var r=0;r<f.length;r++){if(!f[r][0].offsetHeight){var u=f[r][0];while(u.style.display!="none"){u=u.parentNode}m.push(u)}}}}},g.delay);k();if(l.length){var h=[];var p=0;for(var j=0;j<l.length;j++){h[j]=document.createElement("img");h[j].onload=function(){p++;if(p==l.length){k()}};h[j].src=l[j]}}}return this}})(jQuery);

/* _____ ポップアップ表示 _____ */
(function(a){a.set_links=function(b){a("a[rel=popup],a[rel=external],area[rel=popup],area[rel=external]").click(function(){if(this.rel=="external"){window.open(this.href,"_blank");return false}var j="";var d=0;var c="";for(i in b){if(i.match(">>")){data=i.split(">>");if(String(window.location).match(data[0])&&this.href.match(data[1])){c=i;break}}if(i.match(/^\.(.*)/)){var e=new RegExp("\\b"+RegExp.$1+"\\b");if(this.className.match(e)){for(param in b[i]){if(param=="name"){continue}j+=param+"="+b[i][param]+","}if(this.href.match(/width=(\d+).*height=(\d+)/)){var f=RegExp.$1;var h=RegExp.$2;if(j.match("width")&&j.match("height")){j=j.replace(/width=\d+/,"width="+f);j=j.replace(/height=\d+/,"height="+h)}else{j+="width="+f+",height="+h}}var g=b[i]["name"]?b[i]["name"]:"popup";window.open(this.href,g,j.split(/,$/)[0]).focus();this.blur();return false}}else{if(this.href.match(i)&&i.length>d){c=i;d=i.length}}}for(param in b[c]){if(param=="name"){continue}j+=param+"="+b[c][param]+","}var g=b[c]["name"]?b[c]["name"]:"popup";window.open(this.href,g,j.split(/,$/)[0]).focus();this.blur();return false});return this}})(jQuery);
