
function create_edit_button(root, open_expr, input_expr, close_expr){
  var open  = findFirstElement(root, open_expr);
  var input = findFirstElement(root, input_expr);
  var close = findFirstElement(root, close_expr);

  open.onclick = function(){ open.hide() ; input.show() ; close.show(); return false };
  close.onclick = function(){ open.show() ; input.hide() ; close.hide(); return false };
  return;
}

var AgeUpdator = Class.create();
AgeUpdator.prototype = {
  initialize : function(root, birthday) {
    this.root = $(root);
    this.select_boxes = (new Selector("select")).findElements(this.root);
    this.birthday = birthday;
    this.install();
  },

  install : function(){
    var updater = this;
    this.select_boxes.each(function(e){e.onchange = function(){ return updater.update();} });
  },
  format_02d : function(str){return ("0" + str).substr((str.length+1) - 2);},

  get_current_selection_as_num : function(){
    return Number(this.select_boxes[0].value + 
                  this.format_02d(this.select_boxes[1].value) + 
                  this.format_02d(this.select_boxes[2].value));
  },

  update: function(element){
    var age = Math.floor( (this.get_current_selection_as_num(root) - this.birthday) / 10000 );
    var txt = (age < 0) ? "(まだ生まれていません)" : "("+age+"才)";

    (new Selector("span.age")).findElements(this.root).each( function(span_age){span_age.innerHTML = txt;} );
    return null;
  }
}

function fucus_next(root){
  var detail_is_opened = findFirstElement(root, 'dd.dtlInput').visible();
  var next_element = detail_is_opened ? '[name="history[title]"]' : 'input.imgover'

  findFirstElement(root, next_element).focus();
  return true;
}

function init_question_form(root) {
  create_edit_button(root, 'a.edit_open', 'dd.dtlInput', 'a.edit_close');

  var description = findFirstElement(root, '[name="history[description]"]');
  description.onblur = function(){return fucus_next(root)}
}

function initialize_anniversary_question(question_id){
  root = $(question_id);
  init_question_form(root);
}

function initialize_time_periopd_qiestion(question_id){
  root = $(question_id);
  init_question_form(root);

  time_period = findFirstElement( findFirstElement(root, 'span.time_period'), "a");
  time_period.onclick = findFirstElement(root, 'a.edit_open').onclick;
}

function submit_once(form) {
  if(form.submitted){
    return false;
  }
  findFirstElement(form, 'span.indicator').show();
  form.submitted = true;
  return true;
}

var MyHistoryList = Class.create();
MyHistoryList.prototype = {
  initialize: function(target, url){ 
    this.target = $(target);
    this.url = url ;
    this.cache = [];
    this.all_status = 0;
  },

  show : function(all) {
    this.all_status = all;
    var content = this.cache[all];
    (null == content) ? this.fetch_and_cache(this.cache, this.all_status) : this.target.update(content) ;
  },

  refresh : function() {
    this.cache = [];
    this.show(this.all_status);
  },

  fetch_and_cache : function(_cache, all){
    var pars = (new Date()).getTime()+'&all='+all;
    var target = this.target;

    var callback = function(req) { target.update(_cache[all] = req.responseText); }
    new Ajax.Request(this.url, { method: 'get', parameters: pars, onComplete: callback});
  }
}

