MJL.event.add(window, "load", function() {
    MJL.enable.tab("tabs");
    MJL.enable.window('popup-800', {width : 800, height : 700});
    MJL.enable.rollover("roll-active", {
        active : "active",
        switchers : {
            on  : {cond : /_o(\.[^\.]+)$/g, replace : "_ov$1"},
            off : {cond : /_ov(\.[^\.]+)$/g, replace : "_o$1"}
        }
    });
});


/* Usage:
 *  jQuery.csv()(csvtext)               returns an array of arrays representing the CSV text.
 *  jQuery.csv("\t")(tsvtext)           uses Tab as a delimiter (comma is the default)
 *  jQuery.csv("\t", "'")(tsvtext)      uses a single quote as the quote character instead of double quotes
 *  jQuery.csv("\t", "'\"")(tsvtext)    uses single & double quotes as the quote character
 */
;
jQuery.extend({
  csv: function(delim, quote, linedelim) {
    delim = typeof delim == "string" ? new RegExp( "[" + (delim || ","   ) + "]" ) : typeof delim == "undefined" ? ","  : delim;
    quote = typeof quote == "string" ? new RegExp("^[" + (quote || '"'   ) + "]" ) : typeof quote == "undefined" ? '"'  : quote;
    lined = typeof lined == "string" ? new RegExp( "[" + (lined || "\r\n") + "]+") : typeof lined == "undefined" ? "\r\n" : lined;
    function splitline (v) {
      // Split the line using the delimitor
      var arr  = v.split(delim),
        out = [], q;
      for (var i = 0, l = arr.length; i < l; i++) {
        if (q = arr[i].match(quote)) {
          for (j = i; j < l; j++) {
            if (arr[j].charAt(arr[j].length-1) == q[0]) { break; }
          }
          var s = arr.slice(i, j+1).join(delim);
          out.push(s.substr(1, s.length-2));
          i = j;
        }
        else { out.push(arr[i]); }
      }
      return out;
    }
    return function(text) {
      var lines = text.split(lined);
      for (var i = 0, l = lines.length; i < l; i++) {
        lines[i] = splitline(lines[i]);
      }
      return lines;
    };
  }
});


$(document).ready(function(){
        // --------------------------------------------------------------------
        // トップページ フキダシ位置制御
        // --------------------------------------------------------------------
        (function() {
            // フキダシと対象要素の間隔 (単位: px)
            var topAdd  = 2, // 上下
                leftAdd = -20; // 左右
            // 対象要素収集
            $('#home #homeCategoryIndex .segment li').each(function() {
                var target  = $(this),
                    outline = target.find('.outline'), // フキダシ要素
                    anchor  = target.find('a'),        // focus/blur 対象要素
                    top     = -outline.outerHeight() - topAdd,
                    left    = 0 - leftAdd;
                // 元イベント定義
                target.mouseenter(function() { // 表示
                    outline.css({ top : top, left : left });
                }).mouseleave(function() {     // 非表示
                    outline.css({ top : '', left : '' });
                });
                // 元イベントに対する発送ラッパーとして定義
                anchor.focus(function() {
                    target.trigger('mouseenter'); // 表示
                }).blur(function() {
                    target.trigger('mouseleave'); // 非表示
                });
                // 下 → 上にポインタを移動させた場合に連続表示されるようにする
                outline.mousemove(function() {
                    target.trigger('mouseleave'); // 非表示
                });
            });
        })();


        // --------------------------------------------------------------------
        // Colorbox による iframe 表示
        // --------------------------------------------------------------------
        $(".modal-link").colorbox({width:"500px", height:"400px", iframe:true});


        // --------------------------------------------------------------------
        // CSV データからの絞り込み機能生成
        // --------------------------------------------------------------------
        var refine = {
            target : '#categoryRefine', // 対象要素セレクタ
            catL : '/onken/search_report/csv/l_category.csv', // 大カテゴリ csv ファイルパス
            catS : '/onken/search_report/csv/s_category.csv' // 小カテゴリ csv ファイルパス
        };
        $.get(refine.catL, function(data1) {
            var cnst = {
                cls : {
                    L : 'refine-select',
                    S : 'refine-checkbox'
                },
                name : 'NAME' // 小カテゴリが乗る input 要素の name 属性値
            };
            var csv1 = $.csv('\t')(data1); // csv を配列化
            csv1.shift(); // 最初の1行は無効
            $.get(refine.catS, function(data2) {
                var csv2 = $.csv('\t')(data2); // csv を配列化
                csv2.shift(); // 最初の1行は無効
                var html_ = [];
                $(csv2).each(function() { // 小カテゴリを html 断片化
                    html_[this[1]] += '<li><label><input type="checkbox" name="keyword'+ this[0] +'" value="1" />'+ this[2] +'</label></li>\n'	
                });
                var html2 = [];
                $(html_).each(function(key) {
                    html2[key] = this.toString().replace('undefined', '');
                });
                var html1 = '<form action=""><div class="pBox01"><h3><span class="type01">絞り込み検索</span></h3><p>以下のカテゴリ、キーワードより絞り込むことが可能です。</p><p class="form-select-01"><strong>カテゴリで絞り込む：</strong><select class="'+ cnst.cls.L +'">'; // csv のデータ入れる前に記述する html 断片
                $(csv1).each(function(key) { // 大カテゴリを html 断片化
                    html1 += '<option value="'+ this[0] +'">'+ this[1] +'</option>';
                });
                html1 += '</select></p><div class="boxRefine01"><h4>キーワードで絞り込む：</h4><ul class="'+ cnst.cls.S +'">'+ html2[1] +'</ul></div><ul class="navButton01 noneMargin"><li><input type="image" src="/images/common/btn_search_01_o.gif" alt="検索" /></li></ul></div></form>'; // csv のデータ入れた後に記述する html 断片
                $(refine.target).append(html1);
                $(refine.target).find('.'+ cnst.cls.L).bind('change', function() {
                    $('.'+ cnst.cls.S).find('li').remove();
                    $('.'+ cnst.cls.S).append(html2[$(this).val()]);
                });

                /* 検証用 */
                $('#inspect').bind('submit', function() {
                    var str = '[ '+ $('.'+ cnst.cls.L).val() +' ]';
                    $('[name="'+ cnst.name +'"]:checked').each(function() {
                        str += ' '+ $(this).val();
                    });
                    alert(str);
                    return false;
                });
                /* /検証用 */
            });
        });

});
