// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var Murmanout = {};

Murmanout.TagsControl = Class.create();
Murmanout.TagsControl.prototype = {
  initialize: function(object_name, all_tags) {
    this.filter_id = object_name + "_tags_filter";
    this.all_id = object_name + "_all_tags";
    this.selected_id = object_name + "_selected_tags";
    this.all_tags = all_tags;
    this.object_name = object_name;

    this.all_tags.each(function(it) {
      node = document.createElement("option");
      node.setAttribute("value", it[0]);
      if (it[1].length == 0) {
        node.setAttribute("disabled", "disabled");
      }
      node.appendChild(document.createTextNode(it[0]));
      $(this.filter_id).appendChild(node);
    }.bind(this));

    Event.observe(this.all_id, "dblclick", this.onClickAll.bindAsEventListener(this));
    Event.observe(this.selected_id, "dblclick", this.onClickSelected.bindAsEventListener(this));
    Event.observe(this.filter_id, "change", this.onChangeFilter.bindAsEventListener(this));
    this.onChangeFilter(null);

    this.container = document.createElement("span");
    this.container.style.display = "none";
    $(this.filter_id).parentNode.appendChild(this.container);

    for (i = 0; i < $(this.selected_id).options.length; i++) {
      value = $(this.selected_id).options[i].value;
      this.createContainerNode(value);
    }
  },

  onClickAll: function(e) {
    sel_idx = $(this.all_id).selectedIndex;
    sel_node = $(this.all_id).options[sel_idx];

    exists = false;
    for(i = 0; i < $(this.selected_id).options.length; i++) {
      if (parseInt($(this.selected_id).options[i].value) == parseInt(sel_node.value)) {
        exists = true;
        break;
      }
    }

    if (!exists) {
      node = document.createElement("option");
      node.setAttribute("value", sel_node.getAttribute("value"));
      node_text = sel_node.firstChild.nodeValue;
      node_text = $(this.filter_id).value + " / " + node_text
      node.appendChild(document.createTextNode(node_text));
      $(this.selected_id).appendChild(node);

      this.createContainerNode(sel_node.getAttribute("value"))
    }
  },

  onClickSelected: function(e) {
    sel_idx = $(this.selected_id).selectedIndex;
    sel_node = $(this.selected_id).options[sel_idx];
    node = document.createElement("option");
    node.setAttribute("value", sel_node.getAttribute("value"));
    node.appendChild(document.createTextNode(sel_node.firstChild.nodeValue));
    $(this.selected_id).options[sel_idx] = null;
    this.deleteContainerNode(sel_node.getAttribute("value"))
  },

  onChangeFilter: function(e) {
    sel_val = $(this.filter_id).value;
    this.all_tags.each(function(it){
      if (it[0] == sel_val) {
        $(this.all_id).options.length = 0;
        for (i = 0; i < it[1].length; i++){
          node = document.createElement("option");
          node.setAttribute("value", it[1][i][0]);
          node.appendChild(document.createTextNode(it[1][i][1]));
          $(this.all_id).appendChild(node);
        }
      }
    }.bind(this));
  },

  createContainerNode: function(id) {
    node = document.createElement("input");
    node.setAttribute("id", "item_tag_id_" + id);
    node.setAttribute("name", this.object_name + "[tag_ids][]");
    node.setAttribute("value", id);
    this.container.appendChild(node);
  },

  deleteContainerNode: function(id) {
    this.container.removeChild($("item_tag_id_" + id));
  }
}

Murmanout.Form = {};
Murmanout.Form.Hightlight = Class.create();
Murmanout.Form.Hightlight.prototype = {
  initialize: function() {
    forms = document.getElementsByTagName("form");
    for(i = 0; i < forms.length; i++) {
      lists = forms[i].getElementsByTagName("li");
      for(l = 0; l < lists.length; l++) {
        ["input", "textarea", "select"].each(function(it){
            tags = lists[l].getElementsByTagName(it);
            for (j = 0; j < tags.length; j++) {
              new Murmanout.Form.HightlightItem(lists[l], tags[j]);
            }
        })
      }
    }
  }
}

Murmanout.Form.HightlightItem = Class.create();
Murmanout.Form.HightlightItem.prototype = {
  initialize: function(list, item) {
    this.list = list;
    this.item = item;
    if (!this.item.murmanoutHiglight) {
      this.item.murmanoutHiglight = true;
      Event.observe(this.item, "focus", this.onFocus.bindAsEventListener(this), false);
      Event.observe(this.item, "blur", this.onBlur.bindAsEventListener(this), false);
    }
  },

  onFocus: function(e) {
    Element.addClassName(this.list, "styled");
  },

  onBlur: function(e) {
    Element.removeClassName(this.list, "styled");
  }
}

Murmanout.Form.ExpandTextarea = Class.create();
Murmanout.Form.ExpandTextarea.prototype = {
  initialize: function() {
    forms = document.getElementsByTagName('form');
    for (f = 0; f < forms.length; f++) {
      lists = forms[f].getElementsByTagName('li');
      for (l = 0; l < lists.length; l++) {
        txts = lists[l].getElementsByTagName('textarea');
        if (txts.length == 2 &&
            !txts[0]._mirror &&
            txts[0].lang &&
            txts[1].lang &&
            txts[0].lang != txts[1].lang) {
          txts[0]._mirror = txts[1];
          txts[1]._mirror = txts[0];
          for (t = 0; t < txts.length; t++) {
            this.apply(txts[t]);
          }
        }
      }
    }
  },

  apply: function(textarea) {
    Event.observe(textarea, "focus", function(e) {
        Element.hide(this._mirror);
        Element.removeClassName(this, 'short');
      }, false);
    Event.observe(textarea, "blur", function(e) {
        Element.show(this._mirror);
        Element.addClassName(this, 'short');
      }, false);
  }
}

var MyAjaxHandlers = {
  onCreate: function(e) {
    Effect.Appear("ajax-status");
  },

  onComplete: function(e) {
    new Murmanout.Form.Hightlight();
    Effect.Fade("ajax-status");
  }
}
Ajax.Responders.register(MyAjaxHandlers);

Event.observe(window, "load", function(e){new Murmanout.Form.Hightlight()}, false);
Event.observe(window, "load", function(e){new Murmanout.Form.ExpandTextarea()}, false);