/* ===============================================
 * Copyright(C) 2008 KK Mediaplan Murakami Jimusyo
 * ===============================================
 * build : QWM Framework 5.1.1 - 2008.01.11
 * -----------------------------------------------
 * <<JavaScript>>
 * ----------------------------------------------- */

/* ===============================================
 * 検索支援
 * =============================================== */

function SearchOption(pid, cid, lid, aid, dest, icon)
{
  this.elements_id = {
    parentContainer: pid,
    container      : cid,
    location_id    : lid,
    icon_src       : icon
  }
  this.elements   = new Array();
  this.result     = new Array();
  this.properties = {
    action : dest,
    area   : aid,
    parents: new Array(),
    checked: new Array()
  }
  this.messages   = new Array();
}

SearchOption.prototype.getLocations = function(location_id)
{
  var request = new Ajax.Request(
    this.properties.action,
    {
      method      : 'post',
      parameters  : 'action=get_locations&location_id=' + location_id,
      asynchronous: false,
      onFailure   : this.alertFailure,
      onException : this.alertException,
      onComplete  : function(request) {
                      if (request.responseText != '') {
                        var result = eval('(' + request.responseText + ')');
                        this.properties.parents = result.parents;
                        this.result = result.list;
                        this.showParentLocations();
                        this.showLocations();

                        this.getLocationId().value = location_id;
                      }
                    }.bindAsEventListener(this, false)
    }
  );
}

SearchOption.prototype.showParentLocations = function()
{
  if (this.properties.parents.length == 0) return false;

  var container = this.getParentContainer();

  while (container.hasChildNodes()) {
    container.removeChild(container.firstChild);
  }

  for (var idx = this.properties.parents.length -1; idx >= 0; idx--) {
    if (idx != this.properties.parents.length -1) {
      container.appendChild(document.createTextNode(' > '));
    }
    container.appendChild(this.getParentLocation(idx));
  }
}

SearchOption.prototype.showLocations = function()
{
  if (!this.result.length) return false;

  var container = this.getContainer();

  while (container.hasChildNodes()) {
    container.removeChild(container.firstChild);
  }

  for (var idx = 0; idx < this.result.length; idx++) {
    container.appendChild(this.getLocation(idx));
  }
}

SearchOption.prototype.getParentLocation = function(idx)
{
  var obj = this;
  var element = document.createElement('A');

  element.innerHTML = this.properties.parents[idx].location_name.split('／').pop();

  if (idx == 0 && this.properties.parents[idx].parent != '') {

    element.setAttribute('href',
      this.properties.action
      + '?area='
      + this.properties.area
      + '&location[]='
      + encodeURI(this.properties.parents[idx].location_name)
      + '&location_id='
      + this.properties.parents[idx].location_id
    );

  } else {

    element.setAttribute('href', '#');
    element.onclick = function()
    {
      obj.getLocations(obj.properties.parents[idx].location_id);
      return false;
    }
  }

  return element;
}

SearchOption.prototype.getLocation = function(idx)
{
  var obj = this;
  var element = document.createElement('SPAN');
  var checkbox = document.createElement('INPUT');

  checkbox.setAttribute('type'   , 'checkbox');
  checkbox.setAttribute('name'   , 'location[]');
  checkbox.setAttribute('value'  , this.result[idx].location_name);
  if (this.properties.checked[this.result[idx].location_id]) {
    checkbox.setAttribute('checked', true);
  }

  var title = document.createElement('A');

  title.setAttribute('href',
    this.properties.action
    + '?area='
    + this.properties.area
    + '&location[]='
    + encodeURI(this.result[idx].location_name)
    + '&location_id='
    + this.result[idx].parent
  );
  title.innerHTML = checkbox.value.split('／').pop();

  element.appendChild(checkbox);
  element.appendChild(title);

  if (this.result[idx].child_count > 0) {
    var icon = document.createElement('IMG');
    icon.setAttribute('src', this.elements_id.icon_src);
    icon.setAttribute('alt', 'さらに絞り込む');
    icon.onclick = function()
    {
      obj.getLocations(obj.result[idx].location_id);
      return false;
    }
    element.appendChild(icon);
  }

  return element;
}

SearchOption.prototype.getLocationId = function()
{
  if (!this.elements.locationId) {
    this.elements.locationId = document.getElementById(this.elements_id.location_id);
  }

  return this.elements.locationId;
}

SearchOption.prototype.getContainer = function()
{
  if (!this.elements.container) {
    this.elements.container = document.getElementById(this.elements_id.container);
  }

  return this.elements.container;
}

SearchOption.prototype.getParentContainer = function()
{
  if (!this.elements.parentContainer) {
    this.elements.parentContainer = document.getElementById(this.elements_id.parentContainer);
  }

  return this.elements.parentContainer;
}

SearchOption.prototype.alertFailure = function(result)
{
  alert('ステータスエラー：' + result.statusText);
}

SearchOption.prototype.alertException = function(result, error)
{
  alert('通信エラー：' + error.message);
}

