// チェックボックス用イベントリスナー
function set_checkbox_event ( master, slave ) {
 $('.'+ master + ' input:checkbox').click (
  function () {
   name  = this.name;
   id    = name.substr (0,3);
   val   = $(this).val();
   check = $(this).attr('checked');

   // checkbox と label を表すセレクタ文字列
   input = '#'+ id +'_'+ slave + ' input[name="'+ name +'"][value="'+ val +'"]';
   label = '#'+ id +'_selected label:has(input[name="'+ name +'"][value="'+ val +'"])';

   if ( check ) {
    $(input).attr('checked','checked');
    $(label).css('display','inline');
   } else {
    $(input).attr('checked','');
    $(label).css('display','none');
   }
  }
 );
}

 // select 用イベントリスナー
function set_select_event () {
 $('select.category').change (
  function () {
   id_num   = this.id.substr ( 0, 3 );
   val_num  = this.value;
   show_id  = id_num +'_'+ val_num;

   // 変更された division の属性チェックボックスを非表示
   $('.list label:has(input[name="'+id_num+'[]"])').css('display','none');

   // 次の選択肢表示
   $('.list label:has(.attribute_'+ show_id +')').css('display','inline');

  }  // function end
 );  // .change end

}


// 全選択解除ボタン用イベントリスナー
function set_reset_check_event () {
 $('div.reset_check').click (
  function () {
   id_num   = this.id.substr ( 0, 3 );
   div_id   = id_num + '_list';
   $('div#'+ div_id + ' input:checkbox').attr('checked','');
  }  // function end
 );  // .click end

}


// submit button 用イベントリスナー
function set_submit_event () {
 $('input:submit').click (
  function () {
   $('div.selected label input:checkbox').attr('checked','')
   $('div.selected').css('display','none');
  }  // function end
 );  // .click end

}


 // クリアボタン 用イベントリスナー
function set_clear_event () {
 $('#clear_button').click (
  function () {
   $('input:checkbox').attr('checked','');
   $('input:text').attr('value','');
   $('select').attr('value','0');
   for (i=1; i<=6; i++ ) {
    $('div#p0'+ i +'_list label').css('display','none');
   }
   $('input:checkbox.from').eq(0).attr('checked','checked');
   alert('前回の条件をクリアしました');
  }  // function end
 );  // .change end

}


// 初期化処理 ==============================================
function initialize() {

 for (i=1; i<=6; i++ ) {
  $('div#p0'+ i +'_selected').html( $('div#p0'+ i +'_list').html() );
  $('div#p0'+ i +'_selected label').css('display','none');
  $('div#p0'+ i +'_list label').css('display','none');
 }

/*
 // selected 領域にチェックボックスをコピー
 $('div#p01_selected').html( $('div#p01_list').html() );
 $('div#p02_selected').html( $('div#p02_list').html() );
 $('div#p03_selected').html( $('div#p03_list').html() );
 $('div#p04_selected').html( $('div#p04_list').html() );
 $('div#p05_selected').html( $('div#p05_list').html() );
 $('div#p01_selected label').css('display','none');
 $('div#p02_selected label').css('display','none');
 $('div#p03_selected label').css('display','none');
 $('div#p04_selected label').css('display','none');
 $('div#p05_selected label').css('display','none');

 // チェックボックスのないカテゴリプルダウンは非表示
// $('select#p02').css('display','none');
// $('select#p04').css('display','none');

 // カテゴリのあるチェックボックスは非表示
 $('div#p01_list label').css('display','none');
 $('div#p02_list label').css('display','none');
 $('div#p03_list label').css('display','none');
 $('div#p04_list label').css('display','none');
 $('div#p05_list label').css('display','none');
*/

 // イベントリスナー設定
 set_select_event ();
// set_checkbox_event ('list','selected');
// set_checkbox_event ('selected', 'list');
 set_reset_check_event ();
 set_submit_event ();
 set_clear_event ();

}

// for event --------------------------------------------------------------------
$( initialize );




