/*==================================================================================================
= 名前: MugendaiCommon
= 概要: mugendai.infoの共通JS。
==================================================================================================*/

/*----- MugendaiCommon初期定義 -------------------------------------------------------------------*/

if (!$defined(this.mugendai)) {

    /*----- パッケージ管理 -----------------------------------------------------------------------*/

    /*
     * パッケージを生成する。
     */
    mugendai = { };

    /*----- ネイティブ拡張 -----------------------------------------------------------------------*/

    /*
     * 文字列クラスを拡張する。
     */
    String.implement( {

        /**
         * 真偽値に変換する。
         * @return 真偽値
         */
        toBoolean: function() {
            return !this && !this.test(/^false$/i);
        },

        /**
         * HTMLエスケープを行う。
         * @return エスケープ結果
         */
        escapeHTML: function() {
            return this.replace(/[&"'<>]/g, function(target) {
                return {
                    "&": "&amp;",
                    "\"": "&quot;",
                    "'": "&#39;", //"&apos;"はサポートしていない場合もある為、数値文字参照としている
                    "<": "&lt;",
                    ">": "&gt;"
                }[target];
            } );
        },

        /**
         * HTMLアンエスケープを行う。
         * @return アンエスケープ結果
         */
        unescapeHTML: function() {
            return this.replace(/&(?:amp|quot|#39;|lt|gt);/g, function(target) {
                return {
                    "&amp;": "&",
                    "&quot;": "\"",
                    "&#39;": "'",
                    "&lt;": "<",
                    "&gt;": ">"
                }[target];
            } );
        },

        /**
         * URIエンコードされたクエリ文字列を解析し、ハッシュを作成する。
         * @return 解析結果
         *      KEY: "<key>=<value>"の<key>部分, VALUE: "<key>=<value>"の<value>部分
         */
        parseQueryString: function() {
            var targets = this.replace(/.*?\?/, "").replace(/#.*/, "").split(/[&;]/);
            var result = { };
            targets.each(function(target, index, targets) {
                if (!target.test(/(.*?)=(.*)/)) {
                    return;
                }
                var key = decodeURIComponent(RegExp.$1);
                var value = decodeURIComponent(RegExp.$2);
                if (!$defined(this[key])) {
                    this[key] = value;
                } else {
                    this[key] = $splat(this[key]);
                    this[key].push(value);
                }
            }, result);
            return result;
        }

    } );

    /*----- JSローダ定義 -------------------------------------------------------------------------*/

    /**
     * JSローダ。
     * JSのロード管理を行う。
     */
    mugendai.JSLoader = new Class( {

        /**
         * コンストラクタ。
         */
        initialize: function() {
        },

        /**
         * 最深部のscript要素を取得する。
         * @return 最深部のscript要素 (存在しない場合はnull)
         */
        getDeepestScriptElem: function() {
            return this.getDeepestScriptElemIn($(document.documentElement));
        },

        /**
         * 指定要素内で最深部のscript要素を取得する。
         * @param elem 検索開始要素
         * @return 最深部のscript要素 (存在しない場合はnull)
         */
        getDeepestScriptElemIn: function(elem) {

            //指定要素がscript要素なら、そのまま返却する
            if (elem.get("tag") == "script") {
                return elem;
            }

            //次の階層の最終要素に対して再帰的に処理する
            //次の階層がない場合はnullを返却する
            elem = elem.getLast();
            return elem ? this.getDeepestScriptElemIn(elem) : null;

        }

    } );

    /*----- 初期処理 ---------------------------------------------------------------------------------*/

    /*
     * JSローダをSingletonとして生成する。
     */
    mugendai.JSLoader = new mugendai.JSLoader();

}
