Ozein.ImgCheckbox = Class.create();
Ozein.ImgCheckbox.prototype = {
  initialize: function(node, options) {
    this.setOptions(options);
    this.node = node;
    var parts = document.getElementsByAttribute('ozein_imgchkbox', '*', node);
    var len = parts.length;
    for(var i=0; i<len; ++i) {
      var partsName = parts[i].getAttribute('ozein_imgchkbox');
      var funcName = 'set'+partsName;
      if (this[funcName]) this[funcName](parts[i]);
    }
    //IEのブラウザバックでチェックボックスが残ってたときの処理
    this.forceCheck(1, this.allcheck);
  },
  setOptions: function(options) {
    this.options = {
      onImg: null,
      offImg: null,
      onAlt: null,
      offAlt: null
    }
    Object.extend(this.options, options || {});
  },
  setCheckBox: function(node) {
    if(!this.imgchkbox) this.imgchkbox = new Array();
    this.imgchkbox.push(node);
    Event.observe(node, 'click', this.checkbox.bindAsEventListener(this, node));
  },
  setAllCheck: function(node) {
    this.allcheck = node;
    Event.observe(node, 'click', this.forceCheck.bindAsEventListener(this, node));
  },
  setSubmitBtn: function(node) {
    this.btn = node;
    Event.observe(node, 'click', this.checkSubmit.bindAsEventListener(this));
  },
  checkbox: function(e, node) {
    if(node.src.match(this.options.offImg)) {
      node.src = this.options.onImg;
      node.alt = this.options.onAlt;
      this.addHidden(node);
    } else {
      node.src = this.options.offImg;
      node.alt = this.options.offAlt;
      this.removeHidden(node);
    }
  },
  addHidden: function(node) {
    var p = node.parentNode;
    var input = document.createElement('input');
    input.type = "hidden";
    input.name = node.getAttribute('hdnName');
    input.value = node.getAttribute('hdnValue');
    input.ozein_favorite_mode = node.getAttribute('ozein_favorite_mode');
    input.style.margin = "0";
    input.style.padding = "0";
    input.style.width = "1";
    input.style.display = "none";
    p.appendChild(input);
  },
  removeHidden: function(node) {
    var p = node.parentNode;
    var inputs = p.getElementsByTagName("input");
    for(var i=0; i < inputs.length; i++) {
        p.removeChild(inputs[i]);
    }
  },
  forceCheck: function(e, node) {
    if(node.checked) {
      for(var i=0; i<this.imgchkbox.length; i++) {
        this.imgchkbox[i].src = this.options.onImg;
        this.imgchkbox[i].alt = this.options.onAlt;
        this.removeHidden(this.imgchkbox[i]);
        this.addHidden(this.imgchkbox[i]);
      }
    } else {
      for(var i=0; i<this.imgchkbox.length; i++) {
        this.imgchkbox[i].src = this.options.offImg;
        this.imgchkbox[i].alt = this.options.offAlt;
        this.removeHidden(this.imgchkbox[i]);
      }
    }
  },
  checkSubmit: function(e) {
    var check = false;
    inputs = this.node.getElementsByTagName('input');
    for(var i=0; i<inputs.length; i++) {
      if(inputs[i].type == "hidden") {
        check = true;
      }
    }
    if(!check) {
      alert('施設が選択されていません。必ず1件以上お選びください。');
      if(e) Event.stop(e);
    }
  }
}

