var Dip;
if (typeof(Dip) == 'undefined') { Dip = {}; }

Dip.Utils = {
  clearSelectBox:function(name){
    for (var option in document.searchform.elements[name].options) {
      if (option.value == '') {
        document.removeChild(option);
      }
    }
  },
  updateSelectBox:function(parentName, childName, dataKey, pattern) {
    Dip.Utils.clearSelectBox(childName);
    var parent_element = document.searchform.elements[parentName];
    var parent_code = parent_element.options[parent_element.selectedIndex].value;
    var child_element = document.searchform.elements[childName];
    var options = child_element.options;
    var children = [];
    if(typeof(Dip.Data[dataKey]) != 'undefined') {
      for (var key in Dip.Data[dataKey][parent_code]) {
        children.push(key);
      }
      children.sort(function(a,b){ return (parseInt(a, 10) >  parseInt(b, 10)) ? 1 : -1; });
    }
    options.length = children.length + 1;
    var n = 0;
    if(typeof(pattern) != 'undefined') {
      for (var i = 0; i < children.length; i++){
        if(pattern.test(Dip.Data[dataKey][parent_code][children[i]])) {
          options[n+1].text = Dip.Data[dataKey][parent_code][children[i]];
          options[n+1].value = children[i];
          options[n+1].selected = false;
          n++;
        }
      }
      options.length = n + 1;
    } else {
      for (var i = 0; i < children.length; i++){
        options[i+1].text = Dip.Data[dataKey][parent_code][children[i]];
        options[i+1].value = children[i];
        options[i+1].selected = false;
      }
    }
  }
};

Dip.Action = {
  changeCities:function(){
    Dip.Utils.updateSelectBox('prefecture_code[]', 'city_code[]', 'city', /(市|区)$/);
  },
  changeAreaDetails:function(){
    Dip.Utils.updateSelectBox('city_code[]', 'area_detail_code[]', 'area_detail');
  },
  changeJobDetails:function(){
    var job_category = document.searchform.elements['job_category_code[]'];
    var job_category_code = job_category.options[job_category.selectedIndex].value;
    var target_id = 'job_detail_code_box_' + job_category_code;
    var visible_targets = $('div.job_detail_code_box:visible');
    if (visible_targets.length > 0) {
      visible_targets.each(function(){
        $(this).find('input').each(function(){ this.checked = false; });
        $(this).slideUp('', function(){$('#' + target_id).slideDown();});
      });
    } else {
      $('#' + target_id).slideDown();
    }
  },
  changeSalaryRanges:function(){
    Dip.Utils.updateSelectBox('salary_type_code[]', 'salary_range[]', 'salary_range');
  },
  checkFeatureListCount:function(){
    if (this.checked) {
      var features = $(":input[name='feature_list[]']");
      var count = 0;
      for (var i in features) {
        if(features[i].checked) {
          count++;
        }
      }
      if (count > 10) {
        this.checked = false;
        alert('特徴は10個までしか選べません。');
      }
    }
  },
  changeLimit:function(){
    var path =  $(this).attr('value');
    window.location.href = path;
  }
};

Dip.Initializer = {
  run:function(data){
    $(function() {
      Dip.Data = data;
      $('#prefecture_code_').change(Dip.Action.changeCities);
      if($('#area_detail_code_')){
        $('#city_code_').change(Dip.Action.changeAreaDetails);
      }
      $('#job_category_code_').change(Dip.Action.changeJobDetails);
      $('#salary_type_code_').change(Dip.Action.changeSalaryRanges);
      Dip.Initializer.featureListAction();
      $('#limit_').change(Dip.Action.changeLimit);
    });
  },

  featureListAction:function(){
    //チェックは10個まで可能
    $(":input[name='feature_list[]']").each(function(){
      $(this).change(Dip.Action.checkFeatureListCount);
    });
  }
};