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

/* ===============================================
 * お気に入りの求人情報
 * =============================================== */

function JobBookmark(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();
}

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

  var request = new Ajax.Request(
    this.properties.action,
    {
      method      : 'post',
      parameters  : 'action=get_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)
    }
  );
}

JobBookmark.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;
}

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

  container.innerHTML = this.messages.noResult;
}

JobBookmark.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'));
  }
}

JobBookmark.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 formated_title = 'No.' + str_pad(this.result[idx].jid, 6, 0, 'STR_PAD_LEFT') + ' &gt; ' + this.result[idx].location + ' &gt; ' + this.result[idx].title;
  if (this.result[idx].active) {
    var title = document.createElement('A');
    title.innerHTML = formated_title;
    title.setAttribute('href', this.properties.url + '?jid=' + this.result[idx].jid);
  } else {
    var title = document.createElement('LABEL');
    title.innerHTML = formated_title;
    element.addClassName('notActive');
  }

  element.appendChild(title);

  return element;
}

JobBookmark.prototype.submitAddAction = function(id, addButtonElement)
{
  for (var idx = 0; idx < this.result.length; idx++) {
    if (this.result[idx].jid == id) {
      if (confirm('この求人情報は既にお気に入りに登録されています。\n編集画面を開きますか？')) {
        this.showAddActionCanceled(addButtonElement);
      }
      return false;
    }
  }

  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 request = new Ajax.Request(
    this.properties.action,
    {
      method      : 'post',
      parameters  : 'action=add_bookmark&jid=' + id + '&delete_no_alert=' + (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.showAddActionResult(addButtonElement);
                      }
                    }.bindAsEventListener(this, false)
    }
  );

  return false;
}

JobBookmark.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';

  this.showWindow();
}

JobBookmark.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();
}

JobBookmark.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_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)
    }
  );
}

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

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

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

  window.style.display = 'none';
}

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

  return this.elements.summary_container;
}

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

  return this.elements.container;
}

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

  return this.elements.window;
}

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

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

