/*----- require common.js -----*/

$(document).ready(function() {
  buttonClickedEventListener();
});

var todofuken_c   = null;
var shikuchoson_c = null;
var chomei_c      = null;
var chomoku_c     = null;
var prefOpened    = false;

// ボタンクリック
var buttonClickedEventListener = function() {
  // 都道府県リスト開閉
  $('#togglePref').toggle(
    function() {
      $('#block_pref').show('blind');
      $(this).text('都道府県リストを隠す');
      prefOpened = true;
    },
    function() {
      $('#block_pref').hide('blind');
      $(this).text('都道府県をリストで選ぶ');
      prefOpened = false;
    }
  );

  // 都道府県クリック
  $('#block_pref .selector a[todofuken_c]').each(function() {
    $(this).click(function() {
      todofuken_c = $(this).attr('todofuken_c');
      $.getJSON(urlJsonSearchShop, { mode:2, j1:todofuken_c }, renderCity);   // urlJsonSearchShop @common.js
      $('#selected_pref').text($(this).text());
      $('#block_pref .selector').hide();
      $('#block_pref .selected').show();
      $('#list_pref').hide('blind');
      $('#block_city').show('blind');
    });
  });

  // 郵便番号検索ボタンクリック
  $('#btnZip').click(function() {
    var z1 = $('#z1').val();
    var z2 = $('#z2').val();
    if (z1.match(/[0-9]{3}/) && z2.match(/[0-9]{4}/))
    {
      $.getJSON(urlJsonSearchShop, { mode:1, z1:z1, z2:z2 }, renderZip);   // urlJsonSearchShop @common.js
      return true;
    }
    alert('正しい郵便番号を入力してください。');
    $('#z1').focus();
  });

  // あかさたナビクリック
  $('.akstnavi').each(function() {
    $(this).click(function() {
      var id = '#' + $(this).attr('index')
      if ($(id).length > 0) $.scrollTo($(id), 200);
    });
  });

  // 都道府県を選びなおす
  $('#open_pref a').click(resetPref);

  // 市区町村を選びなおす
  $('#open_city a').click(resetCity);

  // 町名を選びなおす
  $('#open_town a').click(resetTown);

  // 丁目を選びなおす
  $('#open_street a').click(resetStreet);
}

// 市区町村を描画します。
function renderCity(data)
{
  if (data.error) errorRedirect(data);

  resetCity();
  for (var akstn in data.shikuchoson)
  {
    var buf = '';
    $('#city_' + akstn).empty();
    for (var index in data.shikuchoson[akstn])
    {
      var code = data.shikuchoson[akstn][index].shikuchoson_c;
      var name = data.shikuchoson[akstn][index].shikuchoson_nm;
      if (data.shikuchoson[akstn][index].exist)
      {
        // <a>タグのclickイベントに、JSON通信、市区町村選択肢の非表示、選択済み市区町村の表示、町名選択肢の表示を結び付けます。
        buf = $('<a href="javascript:" shikuchoson_c="' + code + '">' + name + '</a>');
        buf.bind('click', function() {
          var j2 = shikuchoson_c = $(this).attr('shikuchoson_c');
          $.getJSON(urlJsonSearchShop, { mode:3, j1:todofuken_c, j2:j2 }, renderTown);   // urlJsonSearchShop @common.js
          $('#selected_city').text($(this).text());
          $('#block_city .selector').hide();
          $('#block_city .selected').show();
          $('#block_town').show('blind');
        });
      }
      else
      {
        buf = $('<span class="miss">' + name + '</span><wbr>');
      }
      $('#city_' + akstn).append(buf);
    }
  }
}

// 町名を描画します。
function renderTown(data)
{
  if (data.error) errorRedirect(data);

  for (var akstn in data.chomei)
  {
    var buf = '';
    $('#town_' + akstn).empty();
    for (var index in data.chomei[akstn])
    {
      var code = data.chomei[akstn][index].chomei_c;
      var name = data.chomei[akstn][index].chomei_nm;
      if (data.chomei[akstn][index].exist)
      {
        // <a>タグのclickイベントに、JSON通信、町名選択肢の非表示、選択済み町名の表示、丁目選択肢の表示を結び付けます。
        buf = $('<a href="javascript:" chomei_c="' + code + '">' + name + '</a>');
        buf.bind('click', function() {
          var j3 = chomei_c = $(this).attr('chomei_c');
          $.getJSON(urlJsonSearchShop, { mode:4, j1:todofuken_c, j2:shikuchoson_c, j3:chomei_c }, renderStreet);   // urlJsonSearchShop @common.js
          $('#selected_town').text($(this).text());
          $('#block_town .selector').hide();
          $('#block_town .selected').show();
          $('#block_street').show('blind');
        });
      }
      else
      {
        buf = $('<span class="miss">' + name + '</span><wbr>');
      }
      $('#town_' + akstn).append(buf);
    }
  }
}

// 丁目を描画します。
function renderStreet(data)
{
  if (data.error) errorRedirect(data);

  var buf = '';
  $('#block_street dd.selector').empty();
  for (var index in data.chomoku)
  {
    var code    = data.chomoku[index].chomoku_c;
    var name    = data.chomoku[index].chomoku_nm;
    var tenpo_c = data.chomoku[index].tenpo_c;
    if (tenpo_c)
    {
      // <a>タグのclickイベントに、hiddenへのセット、action先の設定、submitイベントを結び付けます。
      buf = $('<a href="javascript:" chomoku_c="' + code + '" tenpo_c="' + tenpo_c + '">' + name + '</a>');
      buf.bind('click', function() {
        chomoku_c = $(this).attr('chomoku_c');
        tenpo_c   = $(this).attr('tenpo_c');
        $('#jusho_c').val(todofuken_c + shikuchoson_c + chomei_c + chomoku_c);
        $('#tenpo_c').val(tenpo_c);
        $('#form_tenpo').attr('action', urlShopDetail + tenpo_c);    // urlShopDetail @common.js
        $('#form_tenpo').submit();
        return false;
      });
    }
    else if (data.chomoku[index].exist)
    {
      // <a>タグのclickイベントに、JSON通信、丁目選択肢の非表示、選択済み丁目の表示、番地選択肢の表示を結び付けます。
      buf = $('<a href="javascript:" chomoku_c="' + code + '">' + name + '</a>');
      buf.bind('click', function() {
        var j4 = chomoku_c = $(this).attr('chomoku_c');
        $.getJSON(urlJsonSearchShop, { mode:5, j1:todofuken_c, j2:shikuchoson_c, j3:chomei_c, j4:chomoku_c }, renderBlock);  // urlJsonSearchShop @common.js
        $('#selected_street').text($(this).text());
        $('#block_street .selector').hide();
        $('#block_street .selected').show();
        $('#block_block').show('blind');
      });
    }
    else
    {
      buf = $('<span class="miss">' + name + '</span><wbr>');
    }
    $('#block_street dd.selector').append(buf);
  }
}

// 番地を描画します。
function renderBlock(data)
{
  if (data.error) errorRedirect(data);

  var buf = '';
  $('#block_block dd').empty();
  for (var index in data.banchi)
  {
    var banchi_c  = data.banchi[index].banchi_c;
    var banchi_nm = data.banchi[index].banchi_nm;
    var tenpo_c   = data.banchi[index].tenpo_c;
    if (tenpo_c)
    {
      // <a>タグのclickイベントに、hiddenへのセット、action先の設定、submitイベントを結び付けます。
      buf = $('<a href="javascript:" tenpo_c="' + tenpo_c + '">' + banchi_nm + '</a>');
      buf.bind('click', function() {
        tenpo_c = $(this).attr('tenpo_c');
        $('#jusho_c').val(todofuken_c + shikuchoson_c + chomei_c + chomoku_c);
        $('#tenpo_c').val(tenpo_c);
        $('#form_tenpo').attr('action', urlShopDetail + tenpo_c);    // urlShopDetail @common.js
        $('#form_tenpo').submit();
        return false;
      });
    }
    else
    {
      buf = $('<span class="miss">' + banchi_nm + '</span><wbr>');
    }
    $('#block_block dd').append(buf);
  }
}

// 郵便番号検索結果で描画します。
function renderZip(data)
{
  if (data.error) errorRedirect(data);

  $('#block_pref').show('blind');
  if (data.selected.todofuken_c)
  {
    todofuken_c = data.selected.todofuken_c;
    $('#selected_pref').text(data.selected.todofuken_nm);
    renderCity(data);
    $('#block_pref .selector').hide();
    $('#block_pref .selected').show();
    $('#list_pref').hide('blind');
    $('#block_city').show('blind');
  }
  if (data.selected.shikuchoson_c)
  {
    shikuchoson_c = data.selected.shikuchoson_c;
    $('#selected_city').text(data.selected.shikuchoson_nm);
    renderTown(data);
    $('#block_city .selector').hide();
    $('#block_city .selected').show();
    $('#block_town').show('blind');
  }
  if (data.selected.chomei_c)
  {
    chomei_c = data.selected.chomei_c;
    $('#selected_town').text(data.selected.chomei_nm);
    renderStreet(data);
    $('#block_town .selector').hide();
    $('#block_town .selected').show();
    $('#block_street').show('blind');
  }
  if (data.selected.chomoku_c)
  {
    chomoku_c = data.selected.chomoku_c;
    $('#selected_street').text(data.selected.chomoku_nm);
    renderBlock(data);
    $('#block_street .selector').hide();
    $('#block_street .selected').show();
    $('#block_block').show('blind');
  }
  if (data.selected.banchi_c)
  {
    $('#block_block').show();
    $('#block_block dd').show();
  }
}

// 都道府県選択をリセットします。
function resetPref()
{
  clearList();
  todofuken_c = null;
  $('#block_pref .selector').show();
  $('#block_pref .selected').hide();
  $('#list_pref').show('blind');
  $('#block_city:visible').hide('blind');
  resetCity();
}

// 市区町村選択をリセットします。
function resetCity()
{
  shikuchoson_c = null;
  $('#block_city .selector').show();
  $('#block_city .selected').hide();
  $('#block_town:visible').hide('blind');
  resetTown();
}

// 町名選択をリセットします。
function resetTown()
{
  chomei_c = null;
  $('#block_town .selector').show();
  $('#block_town .selected').hide();
  $('#block_street:visible').hide('blind');
  resetStreet();
}

// 丁目選択をリセットします。
function resetStreet()
{
  chomoku_c = null;
  $('#block_street .selector').show();
  $('#block_street .selected').hide();
  $('#block_block:visible').hide('blind');
  resetBlock();
}

// 番地選択をリセットします。
function resetBlock()
{
}

// エラー処理
function errorRedirect(data)
{
  if (data.error.url) location.href = data.error.url;
}

// FLASHから呼ばれる市区町村一覧表示
function searchCity(todofukenC) {
  $('#togglePref').trigger('click');
  todofuken_c = todofukenC;
  $.getJSON(urlJsonSearchShop, { mode:2, j1:todofuken_c }, renderCity);   // urlJsonSearchShop @common.js
  var todofukenNm = $('#block_pref .selector a[todofuken_c=' + todofuken_c + ']').text();
  $('#selected_pref').text(todofukenNm);
  $('#block_pref .selector').hide();
  $('#block_pref .selected').show();
  $('#list_pref').hide();
  $('#block_city').queue(function() {
    $(this).show('blind');
    $.scrollTo($('#selected_Todofuken'), 400);
    $(this).dequeue();
  });
}
// FLASHの「選びなおす」を一覧に反映
function resetList()
{
  clearList();
  $('#block_pref').hide();
  $('#block_city').hide();
  $('#block_town').hide();
  $('#block_street').hide();
  if (prefOpened)  // toggleイベントの位置が正しい場合
    $('#togglePref').trigger('click');
  else
    $('#togglePref').text('都道府県をリストで選ぶ');
  resetPref();
}

// 描画クリア
function clearList()
{
  $('#block_city .selector dd span,#block_city .selector dd a').empty();
  $('#block_town .selector dd span,#block_town .selector dd a').empty();
  $('#block_street .selector dd span,#block_street .selector dd a').empty();
}
