/*
 * hovering パッケージ
 *
 *
 * ■公開するインターフェース
 *
 * 1. onHover()
 *
 *   [機能概要]
 *   ・指定した設定情報ファイルからHTMLを表示させる。
 *
 *   [入力値]
 *   ・{String} 設定情報ファイルオブジェクト
 *
 *   [出力値]
 *   ・なし
 *
 *
 * 2. outHover()
 *
 *   [機能概要]
 *   ・指定した設定情報ファイルのHTMLを非表示にさせる。
 *
 *   [入力値]
 *   ・{String} 設定情報ファイルオブジェクト
 *
 *   [出力値]
 *   ・なし
 */
var onDisp = {};        //表示状態フラグ
var eventCache = {};    //イベントキャッシュ

/*
 * mouseover イベントハンドラ
 *
 * 入力値 [Object]設定情報オブジェクト
 * 出力値 なし
 *
 * 設定情報オブジェクトを読み出し、設定したエレメントを表示状態にする。
 * エレメントの表示後は表示状態フラグをオンに設定する。
 * 表示状態にされたエレメントに対しても自身をイベントハンドラとして登録する。
 */
function onHover(sg)
{
    var thisEnv = document.getElementById(sg);    //設定情報

    if (!(thisEnv)) {
        return;
    }
    if (onDisp[sg])
    {
        return;
    }

    thisEnv.style.position = "absolute";
    tuggleDisp(sg, true);
    onDisp[sg] = true;

    setEvent("mouseover", onHover.bind(this, sg), thisEnv);
    setEvent("mouseout", outHover.bind(this, sg), thisEnv);
};

/*
 * mouseout イベントハンドラ
 *
 * 入力値 [Object]設定情報オブジェクト
 * 出力値 なし
 *
 * 設定情報オブジェクトを読み出し、対象のエレメントを非表示状態にする。
 * エレメントの表示状態には左右されず実行される。非表示後は表示状態をオフにする。
 */
function outHover(sg)
{
    var thisEnv = document.getElementById(sg);

    if (!(thisEnv)) {
        return;
    }

    tuggleDisp(sg, false);
    onDisp[sg] = false;

//    delEvent(sg);
};

/*
 * 表示状態変更
 *
 * 入力値 [String]エレメントID
 * 入力値 [Boolean]要求表示状態(true:表示要求、false:非表示要求)
 * 出力値 なし
 *
 * 要求された表示状態を設定情報オブジェクトで設定したエレメントに設定する。
 */
function tuggleDisp(id, disp)
{
    if (disp) {
        document.getElementById(id).style.display = "block";
    } 
    else {
        document.getElementById(id).style.display = "none";
    }
};

/*
 * イベント設定関数
 *
 * 入力値 [String]イベント文字列
 * 入力値 [Function]コールバック関数
 * 入力値 [Object]エレメント
 * 出力値 なし
 *
 * 要求されたエレメントにイベントを追加する。
 * クロスブラウズに対応。
 * ※この関数だけはいじらないでください
 */
function setEvent(event, func, el)
{
    var e = el || document.body || window;

    if (e.addEventListener)
    {
        //eventCache[el.id] = [event, func, e];
        return (el || window).addEventListener(event, func, false);
    }
    else
    {
        //eventCache[el.id] = [event, func, e];
        return e.attachEvent("on" + event, func);
    }

    e.disabled = true;
    e.disabled = false;
};

//※使用禁止
function delEvent(id)
{
    var cache = eventCache[id];

    if (!(cache)) {
        return;
    }

    var e = cache[2] || document.body || window;

    if (e.removeEventListener)
    {
        (cache[2] || window).removeEventListener(cache[0], cache[1], false);
    }
    else
    {
        e.detachEvent("on" + cache[0], cache[1]);
    }
};

Function.prototype.bind = function() {
  var __method = this, args = $A(arguments), object = args.shift();
  return function() {
    return __method.apply(object, args.concat($A(arguments)));
  }
}
var $A = Array.from = function(iterable) {
  if (!iterable) return [];
  if (iterable.toArray) {
    return iterable.toArray();
  } else {
    var results = [];
    for (var i = 0, length = iterable.length; i < length; i++)
      results.push(iterable[i]);
    return results;
  }
}

