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

/* ===============================================
 * お気に入りの検索条件
 * =============================================== */

function QuickAccess(wid, cid, num, dest, url)
{
  this.elements_id = {
    window   : wid,
    container: cid
  }
  this.elements   = new Array();
  this.result     = new Array();
  this.properties = {
    action         : dest,
    url            : url,
    limit          : num,
    delete_no_alert: 'on'
  }
  this.messages   = new Array();
}

QuickAccess.prototype.getList = function()
{
  this.messages.noResult = this.getContainer().innerHTML;

  var request = new Ajax.Request(
    this.properties.action,
    {
      method      : 'post',
      parameters  : 'action=get_search_bookmarks&delete_no_alert=' + this.properties.delete_no_alert,
      asynchronous: false,
      onFailure   : this.alertFailure,
      onException : this.alertException,
      onComplete  : function(request) {
                      if (request.responseText != '') {
                        var result = eval('(' + request.responseText + ')');
                        this.result = result.list;
                        if (result.option.delete_no_alert) {
                          this.properties.delete_no_alert = result.option.delete_no_alert;
                        }
                        this.showResult();
                      }
                    }.bindAsEventListener(this, false)
    }
  );
}

QuickAccess.prototype.showResult = 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.getListElement(idx));
  }

  this.getWindow().delete_no_alert.checked = this.properties.delete_no_alert == 'on' ? true : false;
}

QuickAccess.prototype.showNoResult = function()
{
  var container = this.getContainer();

  container.innerHTML = this.messages.noResult;
}

QuickAccess.prototype.showSummary = function(container_id, number)
{
  this.elements_id.summary_container = container_id;
  this.properties.summary_lines = number;

  if (this.result.length == 0) return false;

  var container = this.getSummaryContainer();

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

  for (var idx = 0; idx < this.properties.summary_lines; idx++) {
    if (!this.result[idx]) break;
    container.appendChild(this.getListElement(idx, 'summary'));
  }
}

QuickAccess.prototype.getListElement = function(idx, mode)
{
  var obj = this;
  var element = document.createElement('LI');

  if (mode != 'summary') {
    var checkbox = document.createElement('INPUT');
    checkbox.setAttribute('type' , 'checkbox');
    checkbox.setAttribute('name' , 'select');
    checkbox.setAttribute('value', this.result[idx].id);
    element.appendChild(checkbox);
  }
  var title = document.createElement('A');
  title.innerHTML = this.result[idx].title;
  title.setAttribute('href', this.properties.url + '?' + this.result[idx].query);

  element.appendChild(title);

  return element;
}

QuickAccess.prototype.submitAddAction = function(form_id, addButtonElement)
{
  var delete_no_alert = this.getWindow().elements[0];

  if (this.result.length >= this.properties.limit && !delete_no_alert.checked) {
    if (!confirm('検索条件は' + this.properties.limit + '件まで登録できます。\n登録日が古い順に削除しますか？')) {
      this.showAddActionCanceled(addButtonElement);
      return false;
    }
    delete_no_alert.checked = true;
  }

  var myForm = document.getElementById(form_id);
  var request = new Ajax.Request(
    this.properties.action,
    {
      method      : 'post',
      postBody    : 'action=add_search_bookmark&delete_no_alert=' + (delete_no_alert.checked ? 'on' : 'off') + '&' + Form.serialize(myForm),
      asynchronous: false,
      onFailure   : this.alertFailure,
      onException : this.alertException,
      onComplete  : function(request) {
                      if (request.responseText != '') {
                        var result = eval('(' + request.responseText + ')');
                        this.result = result.list;
                        if (result.option.delete_no_alert) {
                          this.properties.delete_no_alert = result.option.delete_no_alert;
                        }
                        this.showAddActionResult(addButtonElement);
                      }
                    }.bindAsEventListener(this, false)
    }
  );

  return false;
}

QuickAccess.prototype.showAddActionResult = function(addButtonElement)
{
  this.showResult();

  var container = this.getContainer();
  container.childNodes[0].style.color = "#ff6500";

  var window = this.getWindow();
  //window.style.left = Position.cumulativeOffset(addButtonElement)[0] + 'px';
  window.style.top  = Position.cumulativeOffset(addButtonElement)[1] -150 + 'px';

  alert('検索条件を保存しました。');
  //this.showWindow();
}

QuickAccess.prototype.showAddActionCanceled = function(addButtonElement)
{
  var window = this.getWindow();
  //window.style.left = Position.cumulativeOffset(addButtonElement)[0] + 'px';
  window.style.top  = Position.cumulativeOffset(addButtonElement)[1] -150 + 'px';

  this.showWindow();
}

QuickAccess.prototype.submitDeleteAction = function()
{
  var list = this.getContainer().getElementsByTagName('INPUT');
  var query = '';

  for (var i = 0; i < list.length; i++) {
    if (list[i].checked) {
      query += '&selected[]=' + list[i].value;
    }
  }
  if (query == '') return false;

  var request = new Ajax.Request(
    this.properties.action,
    {
      method      : 'post',
      parameters  : 'action=delete_search_bookmark' + query + '&delete_no_alert=' + (this.getWindow().delete_no_alert.checked ? 'on' : 'off'),
      asynchronous: false,
      onFailure   : this.alertFailure,
      onException : this.alertException,
      onComplete  : function(request) {
                      if (request.responseText != '') {
                        var result = eval('(' + request.responseText + ')');
                        this.result = result.list;
                        if (result.option.delete_no_alert) {
                          this.properties.delete_no_alert = result.option.delete_no_alert;
                        }
                        this.showResult();
                      } else {
                        this.showNoResult();
                      }
                    }.bindAsEventListener(this, false)
    }
  );
}

QuickAccess.prototype.showWindow = function()
{
  var window = this.getWindow();

  window.style.display = 'block';
  return false;
}

QuickAccess.prototype.hideWindow = function()
{
  var window = this.getWindow();

  window.style.display = 'none';
}

QuickAccess.prototype.getSummaryContainer = function()
{
  if (!this.elements.summary_container) {
    this.elements.summary_container = document.getElementById(this.elements_id.summary_container);
  }

  return this.elements.summary_container;
}

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

  return this.elements.container;
}

QuickAccess.prototype.getWindow = function()
{
  if (!this.elements.window) {
    this.elements.window = document.getElementById(this.elements_id.window);
  }

  return this.elements.window;
}

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

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

