/*@cc_on
if (@_jscript_version < 9) {
    var _d = document;
    eval("var document = _d");
}
@*/
/**
 * js/Mdk.js
 *
 * @author T.Mori <t.mori@moshimo.co.jp>
 * @package Mdk
 * @version $Id$
 */

/**
 * Mdk の既定クラス
 *
 *   $ で始まる変数は Element を継承したオブジェクトを表現します
 *   $event は例外的に Event を継承したオブジェクトを表現します
 *
 * @author T.Mori <t.mori@moshimo.co.jp>
 * @package Mdk
 * @access public
 */
var Mdk = Class.create(
  {

    /** @var Object <form ...> 関連のオブジェクト */
    form: {

      /**
       * 確認ダイアログを出してからフォームを送信する
       *
       * @param Form $form フォーム要素 (フォームの ID も可)
       * @param String message 確認メッセージ
       */
      confirm: function ($form, message) {
        message = message || "実行しますか？";
        if (confirm(message)) {
          this.submit($form);
        }
      },

      /**
       * フォームを送信する
       *
       * @param Form $form フォーム要素 (フォームの ID も可)
       */
      submit: function ($form) {
        var $form = $($form);
        $form.submit();
      },

      /**
       * Ajax なリクエストを送信する
       *
       * @param Form $form フォーム要素
       * @param Function callback コールバックメソッド
       */
      request: function ($form, callback, options) {
        options = Object.extend(
          {
            onSuccess: function (response) {
              callback(response, $form);
            }
          },
          options || {}
        );
        $form = $($form);
        // post じゃないと IE で重複送信が出来ない
        $form.request(
          options
        );
      },

      /** @var Object <input ...> 関連のオブジェクト */
      input: {

        /** @var Object <input type="checkbox" ...> 関連のオブジェクト */
        checkbox: {

          /**
           * チェックボックスを全てチェックする
           *
           * @param Array $elements checkbox の要素を含む配列
           */
          checkAll: function ($elements) {
            $elements.each(
              function ($element) {
                var call = false;
                if (!$element.checked) {
                  call = true;
                }
                if ($element.tagName.toLowerCase() == 'input' && $element.readAttribute('type') == 'checkbox' && !$element.disabled) {
                  $element.checked = 1;
                }
                if (call) {
                  $element.fire('mdk:change');
                }
              }
            );
          },

          /**
           * チェックボックスを全てチェック解除する
           *
           * @param Array $elements checkbox の要素を含む配列
           */
          releaseAll: function ($elements) {
            if (!Object.isArray($elements)) {
              return;
            }
            $elements.each(
              function ($element) {
                var call = false;
                if ($element.checked) {
                  call = true;
                }
                if ($element.tagName.toLowerCase() == 'input' && $element.readAttribute('type') == 'checkbox') {
                  $element.checked = 0;
                }
                if (call) {
                  $element.fire('mdk:change');
                }
              }
            );
          },

          /**
           * チェックが付いているチェックボックスの個数をカウントする
           *
           * @param Array $elements checkbox の要素を含む配列
           * @return Integer チェックが付いているチェックボックスの個数
           */
          countChecked: function ($elements) {
            if (!Object.isArray($elements)) {
              return;
            }
            var counter = 0;
            $elements.each(
              function ($element) {
                if ($element.tagName.toLowerCase() == 'input' && $element.readAttribute('type') == 'checkbox' && $element.checked) {
                  counter++;
                }
              }
            );
            return counter;
          }

        }

      },

      /**
       * ダミーフォームを生成する
       *
       * @return String 生成したフォームの ID
       */
      createDummy: function(action, parameters) {
        var $form = new Element('form');
        var form_id = $form.identify();
        $form.writeAttribute('action', action);
        $form.writeAttribute('mthod', 'post');
        if (parameters) {
          $H(parameters).each(
            function(parameter) {
              var $input = new Element('input');
              $input.writeAttribute('type', 'hidden');
              $input.writeAttribute('name', parameter.key);
              $input.writeAttribute('value', parameter.value);
              $form.insert({bottom: $input});
            }
          );
        }
        $$('body')[0].insert({bottom: $form});
        return form_id;
      },

      /**
       * サジェストするためのテキストをフォームに表示する
       *
       * @param Element $element フォームの INPUT TEXT
       * @param Object  options   オプション
       */
      suggestText: function($element) {

          var options = Object.extend({
            attribute:     'suggest',
            suggest_color: '#AAAAAA',
            normal_color:  '#000000',
            remove: true,
            remove_onsubmit: true
          }, arguments[1] || {});

          var suggest_forcus = function(onsubmit) {
            if($element.value == $element.readAttribute(options.attribute)){
              if((onsubmit && options.remove_onsubmit) || (!onsubmit && options.remove)) {
                $element.value="";
              }
              $element.setStyle({'color': options.normal_color});
            }
          }

          var suggest_blur = function() {
            if($element.value == "" || (!options.remove && $element.value == $element.readAttribute(options.attribute))) {
              $element.setStyle({'color': options.suggest_color});
              $element.value=$element.readAttribute(options.attribute);
            }
          }

          Event.observe($element, 'focus', suggest_forcus.bind(this, false));
          Event.observe($element, 'blur', suggest_blur.bind(this));
          Event.observe($element.up('form'), 'submit', suggest_forcus.bind(this, true));
          suggest_blur.bind(this)();

          return $element;
      }
    },


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

  }
);

Mdk.PROTOCOL = location.protocol;
Mdk.SECURE = (Mdk.PROTOCOL.match(/^https/) ? true : false)
Mdk.HOSTNAME = location.hostname;
var a_hostname = location.hostname.split(/\./);
var tld = a_hostname.pop();
var sld = a_hostname.pop();
Mdk.DOMAIN = sld + '.' + tld;

var Mdk_Util = {

    ClearElement: function() {
      var e_div = new Element('div');
      e_div.addClassName('clear-both');
      e_div.update('&nbsp;');
      return e_div;
    },

    CreateWindow: function(event, e_target, e_dummy_parent, x, y) {
      var e_div, e_iframe, e_dummy, left, top, width, height;
      e_div = new Element('div');
      if (Prototype.Browser.IE) {
        e_iframe = new Element('iframe', {frameborder: 0, src: 'javascript: false;'});
      }
      e_dummy = e_target.cloneNode(true);

      var e_parent = e_dummy_parent ? $(e_dummy_parent) : $$('body')[0];
      var parent_position = e_parent.getStyle('position');
      e_parent.setStyle({position: 'relative'});
      e_dummy.setStyle({position: 'absolute', top: 0, left: 0});
      e_dummy.setOpacity(0);
      e_parent.insert({top: e_dummy});
      width = e_dummy.getWidth();
      height = e_dummy.getHeight();
      left = event.pointerX();
      top = event.pointerY();
      if (x == 'right') {
        left = left - width - 15;
      }
      if (y == 'bottom') {
        top = top - height - 15;
      }
      e_dummy.remove();
      e_parent.setStyle({position: parent_position});

      if (Prototype.Browser.IE) {
        e_iframe.setStyle(
          {
            width     : width + "px",
            height    : height + "px",
            position  : 'absolute',
            color     : '#ffffff',
            left      : left + "px",
            top       : top + "px",
            zIndex    : 10,
            padding   : 0,
            margin    : 0,
            border    : "none"
          }
        );
      }
      e_div.setStyle(
        {
          position  : 'absolute',
          zIndex    : 20,
          left      : left + "px",
          top       : top + "px",
          padding   : 0,
          margin    : 0,
          border    : "none"
        }
      );
      e_div.insert({bottom: e_target});
      e_target.removeAttribute('id');
      return {element: e_div, iframe: e_iframe};
    },

    /**
     * 郵便番号検索を行う
     *   郵便番号などの要素名は決め打ち
     *
     * @param String action アクション名
     * @param Element e_form フォーム要素
     * @param Function callback フィルインするためのコールバックメソッド
     * @return void
     */
    ZipcodeSearch: function(action, e_form, callback) {
      var zipcode = '';
      if (e_form.down('input[name="zipcode"]')) {
        e_form.down('input[name="zipcode"]').fire('mdk:focus');
        zipcode = '' + e_form.down('input[name="zipcode"]').getValue();
      } else if (e_form.down('input[name="zipcode_list[0]"]') && e_form.down('input[name="zipcode_list[1]"]')) {
        e_form.down('input[name="zipcode_list[0]"]').fire('mdk:focus');
        e_form.down('input[name="zipcode_list[1]"]').fire('mdk:focus');
        zipcode = '' + e_form.down('input[name="zipcode_list[0]"]').getValue() + e_form.down('input[name="zipcode_list[1]"]').getValue();
      } else {
        return;
      }

      // {{{ フィルするためのメソッド
      this.FillAddress = function(response) {
        r = response.responseJSON;
        if (r.result) {
          document.zipcode_list = r.zipcode_list;
          if (r.zipcode_list.length == 1) {
            this.callback.bind({ zipcode: r.zipcode_list[0], e_form: this.e_form })();
          } else {
            var e_div, e_p, e_a;
            e_div = new Element('div');
            e_div.addClassName('inline-left');
            e_div.setStyle(
              {
                'width': '400px',
                'height': '600px',
                'overflowY': 'scroll',
                'backgroundColor': '#ffffff',
                'border': 'solid 1px #cccccc'
              }
            );
            e_p = new Element('p');
            e_p.addClassName('text-x-large');
            e_p.addClassName('inline-center');
            e_p.addClassName('bold');
            e_p.setStyle(
              {
                'margin': '10px'
              }
            );
            e_p.update('住所を選択してください');
            e_div.insert({bottom: e_p});
            for (var i = 0; i < r.zipcode_list.length; i++) {
              e_p = new Element('p');
              e_p.addClassName('text-large');
              e_p.setStyle(
                {
                  'margin': '10px'
                }
              );
              e_a = new Element('a', { 'href': 'javascript: void(0);' });
              e_a.observe('click', this.callback.bind({ zipcode: r.zipcode_list[i], e_form: this.e_form }));
              e_a.update(r.zipcode_list[i].prefecture_label + r.zipcode_list[i].municipality + r.zipcode_list[i].town);
              e_p.insert({bottom: e_a});
              e_div.insert({bottom: e_p});
            }
            e_p = new Element('p');
            e_p.addClassName('inline-center');
            e_p.addClassName('text-large');
            e_p.setStyle(
              {
                'margin': '10px'
              }
            );
            e_a = new Element('a', { 'href': 'javascript: void(0);' });
            e_a.addClassName('mdk-black-out-close');
            e_a.update('閉じる');
            e_p.insert({bottom: e_a});
            e_div.insert({bottom: e_p});
            Mdk_Globals.black_out = new Mdk_BlackOut(e_div, { keep_id: true });
          }
        } else {
          alert('該当する住所が見付かりませんでした');
        }
      }.bind({callback: callback, e_form: e_form});
      // }}}

      new Ajax.Request(
        action
        , {
          method: 'get'
          , onSuccess: this.FillAddress
          , parameters: {
            zipcode: zipcode
          }
        }
      );
    },

    ZipcodeDecideHelper: {

      joined: function() {
        if (this.e_form.down('input[name="zipcode_list[0]"]')) {
          this.e_form.down('input[name="zipcode_list[0]"]').setValue(this.zipcode.zipcode_list[0]);
        }
        if (this.e_form.down('input[name="zipcode_list[1]"]')) {
          this.e_form.down('input[name="zipcode_list[1]"]').setValue(this.zipcode.zipcode_list[1]);
        }
        if (this.e_form.down('input[name="zipcode"]')) {
          this.e_form.down('input[name="zipcode"]').setValue(this.zipcode.zipcode);
        }
        if (this.e_form.down('select[name="prefecture"]')) {
          this.e_form.down('select[name="prefecture"]').setValue(this.zipcode.prefecture);
        }
        if (this.e_form.down('input[name="address"]')) {
          this.e_form.down('input[name="address"]').setValue(this.zipcode.municipality + this.zipcode.town);
          this.e_form.down('input[name="address"]').fire('mdk:focus');
          this.e_form.down('input[name="address"]').fire('mdk:blur');
          this.e_form.down('input[name="address"]').focus();
          this.e_form.down('input[name="address"]').setValue(this.e_form.down('input[name="address"]').getValue());
        }
        if (Mdk_Globals.black_out) {
          Mdk_Globals.black_out.hide();
        }
      },

      splited: function() {
        if (this.e_form.down('input[name="zipcode_list[0]"]')) {
          this.e_form.down('input[name="zipcode_list[0]"]').setValue(this.zipcode.zipcode_list[0]);
        }
        if (this.e_form.down('input[name="zipcode_list[1]"]')) {
          this.e_form.down('input[name="zipcode_list[1]"]').setValue(this.zipcode.zipcode_list[1]);
        }
        if (this.e_form.down('input[name="zipcode"]')) {
          this.e_form.down('input[name="zipcode"]').setValue(this.zipcode.zipcode);
        }
        if (this.e_form.down('select[name="prefecture"]')) {
          this.e_form.down('select[name="prefecture"]').setValue(this.zipcode.prefecture);
        }
        if (this.e_form.down('input[name="municipality"]')) {
          this.e_form.down('input[name="municipality"]').setValue(this.zipcode.municipality);
          this.e_form.down('input[name="municipality"]').fire('mdk:focus');
          this.e_form.down('input[name="municipality"]').fire('mdk:blur');
          this.e_form.down('input[name="municipality"]').setValue(this.e_form.down('input[name="municipality"]').getValue());
        }
        if (this.e_form.down('input[name="town"]')) {
          this.e_form.down('input[name="town"]').setValue(this.zipcode.town);
          this.e_form.down('input[name="town"]').fire('mdk:focus');
          this.e_form.down('input[name="town"]').fire('mdk:blur');
          this.e_form.down('input[name="town"]').focus();
          this.e_form.down('input[name="town"]').setValue(this.e_form.down('input[name="town"]').getValue());
        }
        if (Mdk_Globals.black_out) {
          Mdk_Globals.black_out.hide();
        }
      }

    },

    SwitchImage: function(element, _switch) {
      element = $(element);
      if (element.readAttribute('src')) {
        element.writeAttribute('src', element.readAttribute('src').sub(/(on|off)\.(gif|jpe?g|png)(\?|$)/i, (_switch ? 'on' : 'off') + '.#{2}#{3}'));
      }
    }

};

var Mdk_Alert = Class.create(
  {

    /**
     * @var Object static 変数を格納するオブジェクト
     * @static
     */
    Statics: {

      /** @var Number 表示オブジェクトのインデックス */
      index: 0,

      /** @var Number 表示しているオブジェクトの数 */
      displayed_number: 0,

      /** @var Number 表示オブジェクトの高さ合計 */
      composited_height: 0

    },

    /**
     * 実際に表示する
     */
    show: function() {
      // 要素の構築
      this.Statics.index++;
      this.base = new Element(
        'div',
        {
          id: 'mdk-alert-base-' + this.Statics.index
        }
      );
      if (Prototype.Browser.IE) {
        this.iframe = new Element(
          'iframe',
          {
            id: 'mdk-alert-iframe' + this.Statics.index,
            src: '/images/spacer.gif',
            frameborder: "0"
          }
        );
      }
      this.div = new Element(
        'div',
        {
          id: 'mdk-alert-div-' + this.Statics.index
        }
      );

      // 中身をセット
      this.div.insert({bottom: this.$element});

      // style を設定
      var position = 'fixed';
      if (Prototype.Browser.IE) {
        position = 'absolute';
      }
      this.base.setStyle(
        {
          display: 'none',
          position: position,
          zIndex: 105,
          padding: 0,
          margin: 0,
          border: "none"
        }
      );
      if (Prototype.Browser.IE) {
        this.iframe.setStyle(
          {
            position: position,
            zIndex: 110,
            padding: 0,
            margin: 0,
            border: "none"
          }
        );
      }
      this.div.setStyle(
        {
          position: position,
          zIndex: 120,
          border: 'solid 1px #aaaaaa',
          backgroundColor: '#ffffff'
        }
      );

      // 要素を追加
      if (Prototype.Browser.IE) {
        this.base.insert({bottom: this.iframe});
      }
      this.base.insert({bottom: this.div});
      $$('body')[0].insert({bottom: this.base});

      // エフェクトを設定
      new Effect.Parallel(
        [
          Effect.Appear(
            this.base,
            {
              duration: 0.5,
              beforeStartInternal: function() {
                this.Statics.displayed_number++;
                this.base.setOpacity(0.1);
                this.base.show();
                this.updateSize();
                this.updatePosition();
              }.bind(this)
            }
          ),
          Effect.Fade(
            this.base,
            {
              duration: 1,
              delay: 3,
              afterFinishInternal: function() {
                this.div.hide();
                if (Prototype.Browser.IE) {
                  this.iframe.hide();
                }
                this.base.hide();
                this.Statics.displayed_number--;
                if (this.Statics.displayed_number <= 0) {
                  this.Statics.composited_height = 0;
                }
              }.bind(this)
            }
          )
        ],
        {delay: 3}
      );
    },

    /**
     * 位置を更新する
     *
     * @param Number left 横位置
     * @param Number top 縦位置
     */
    updatePosition: function(left, top) {
      if (!this.base) {
        return;
      }
      if (!left) {
        left = document.viewport.getScrollOffsets().left + document.viewport.getDimensions().width - this.$element.getWidth() - 2;
      }
      if (!top) {
        top = this.Statics.composited_height;
      }
      this.Statics.composited_height += this.$element.getHeight();
      if (Prototype.Browser.IE) {
        this.base.setStyle({left: document.viewport.getScrollOffsets().left + left + 'px', top: document.viewport.getScrollOffsets().top + top + 'px'});
      } else {
        this.base.setStyle({left: left + 'px', top: top + 'px'});
      }
    },

    /**
     * サイズを更新する
     *
     * @param Number width 横幅
     * @param Number height 縦幅
     */
    updateSize: function(width, height) {
      if (!this.base) {
        return;
      }
      width = width || this.$element.getWidth();
      height = height || this.$element.getHeight();

      this.base.setStyle({width: width + 'px', height: height + 'px'});
      if (Prototype.Browser.IE) {
        this.iframe.setStyle({width: width + 'px', height: height + 'px'});
      }
      this.div.setStyle({width: width + 'px', height: height + 'px'});
    },

    /**
     * コンストラクタ
     */
    initialize: function($element) {
      var div = new Element('div');
      if (Object.isString($element)) {
        div.update($element);
      } else {
        div.insert({bottom: $element});
      }
      div.setStyle({padding: '5px 10px', whiteSpace: 'nowrap'});
      this.$element = div;
      this.show();
    }

  }
);

var Mdk_Induce = Class.create(
  {

    focus: function() {
      if (this.element.getValue().gsub(/\r|\n/, '') == this.induce_message.gsub(/\r|\n/, '')) {
        this.element.setValue('');
      }
      this.element.removeClassName('silver');
    },

    blur: function() {
      if (this.element.getValue().gsub(/\r|\n/, '') === '' || this.element.getValue().gsub(/\r|\n/, '') == this.induce_message.gsub(/\r|\n/, '')) {
        this.element.addClassName('silver');
        this.element.setValue(this.induce_message);
      }
    },

    submit: function() {
      if (this.element.getValue().gsub(/\r|\n/, '') == this.induce_message.gsub(/\r|\n/, '')) {
        this.element.setValue('');
      }
    },

    initialize: function(element, induce_message) {
      this.element = $(element);
      this.induce_message = induce_message;
      this.element.observe('focus', this.focus.bind(this));
      this.element.observe('mdk:focus', this.focus.bind(this));
      this.element.observe('blur', this.blur.bind(this));
      this.element.observe('mdk:blur', this.blur.bind(this));
      this.element.up('form').observe('submit', this.submit.bind(this));
      this.element.up('form').observe('mdk:submit', this.submit.bind(this));
      this.element.mdk_induce = this;
      this.element.fire('mdk:focus');
      this.element.fire('mdk:blur');
    }

  }
);

var Mdk_Tooltip = Class.create(
  {

    show: function(event) {
      this.o_tooltip = Mdk_Util.CreateWindow(event, this.e_tooltip, false, 'left', 'bottom');
      if (Object.isElement(this.o_tooltip.element)) {
        this.o_tooltip.element.addClassName('mdk-tooltip');
        this.o_tooltip.element.addClassName('inline-left');
        $$('body')[0].insert({bottom: this.o_tooltip.element});
      }
      if (Object.isElement(this.o_tooltip.iframe)) {
        $$('body')[0].insert({bottom: this.o_tooltip.iframe});
      }
      this.hide_timer = setTimeout(this.hide.bindAsEventListener(this), this.display_second * 1000);
    },

    hide: function(event) {
      if (Object.isUndefined(this.o_tooltip)) {
        return;
      }
      if (Object.isElement(this.o_tooltip.element)) {
        this.o_tooltip.element.remove();
        this.o_tooltip.element = undefined;
      }
      if (Object.isElement(this.o_tooltip.iframe)) {
        this.o_tooltip.iframe.remove();
        this.o_tooltip.iframe = undefined;
      }
      this.o_tooltip = undefined;
      if (!Object.isUndefined(this.hide_timer)) {
        clearTimeout(this.hide_timer);
      }
      this.hide_timer = undefined;
    },

    update: function(event) {
      if (Object.isUndefined(this.o_tooltip)) {
        return;
      }
      var left, top;
      left = event.pointerX();
      top = event.pointerY();
      if (Object.isElement(this.o_tooltip.element)) {
        this.o_tooltip.element.setStyle({left: left + 'px', top: (top - this.o_tooltip.element.getHeight() - 15) + 'px'});
      }
      if (Object.isElement(this.o_tooltip.iframe)) {
        this.o_tooltip.iframe.setStyle({left: left + 'px', top: (top - this.o_tooltip.element.getHeight() - 15) + 'px'});
      }
      if (!Object.isUndefined(this.hide_timer)) {
        clearTimeout(this.hide_timer);
      }
      this.hide_timer = setTimeout(this.hide.bindAsEventListener(this), this.display_second * 1000);
    },

    initialize: function(element, content, display_second) {
      element = $(element);
      if (!element) {
        return;
      }
      if (Object.isString(content)) {
        this.e_tooltip = new Element('div');
        this.e_tooltip.update(content);
      } else if (Object.isElement(content)) {
        this.e_tooltip = content.cloneNode(true);
      } else {
        return;
      }
      this.e_tooltip.setStyle(
        {
          border: 'solid 1px #000000',
          backgroundColor: '#ffffe7',
          padding: '5px'
        }
      );
      if (display_second) {
        this.display_second = display_second;
      } else {
        this.display_second = 3;
      }
      $$('body')[0].setStyle({position: 'relative'});
      element.observe('mouseover', this.show.bindAsEventListener(this));
      element.observe('mousemove', this.update.bindAsEventListener(this));
      element.observe('mouseout', this.hide.bindAsEventListener(this));
      element.observe(
        'mdk:dummy',
        function() {
          this.show.bindAsEventListener(this)();
          this.hide.bindAsEventListener(this)();
        }.bind(this)
      );
      element.observe('mdk:tooltip:show', this.show.bindAsEventListener(this));
      element.observe('mdk:tooltip:hide', this.hide.bindAsEventListener(this));
    }

  }
);

var Mdk_NowLoading = {

    show: function() {
      var e_loading_mask = new Element('div', {id: 'mdk-now-loading-mask'});
      var e_loading = new Element('div', {id: 'mdk-now-loading'});
      var e_loading_indicator = new Element('div', {id: 'mdk-now-loading-indicator'});
      var e_loading_message = new Element('p');
      var e_loading_image = new Element('img', {src: '/images/common/now-loading.gif', width: 48, height: 48});
      e_loading_mask.setOpacity(0.5);
      e_loading_message.update('ただいま読み込み中です');
      e_loading_indicator.insert({bottom: e_loading_image});
      e_loading_indicator.insert({bottom: e_loading_message});
      e_loading.insert({bottom: e_loading_indicator});
      $$('body')[0].insert({top: e_loading});
      $$('body')[0].insert({top: e_loading_mask});
    },

    hide: function() {
      $('mdk-now-loading').remove();
      $('mdk-now-loading-mask').remove();
      if ($('mdk-now-loading-iframe')) {
        $('mdk-now-loading-iframe').remove();
      }
    }

};

var Mdk_BlackOut = Class.create(
  {

    show: function() {
      if ($('mdk-black-out-mask') || $('mdk-black-out')) {
        return;
      }
      var dummy = this.element.cloneNode(true);
      dummy.setOpacity(0);
      dummy.setStyle(
        {
          position: 'absolute'
        }
      );
      $$('body')[0].insert({bottom: dummy});
      var width, height, left, top, body_position;
      width = dummy.getWidth();
      height = dummy.getHeight();
      left = (document.viewport.getWidth() / 2) - (width / 2);
      top = (document.viewport.getHeight() / 2) - (height / 2);
      if (top < 0) {
        top = 0;
      }
      dummy.remove();
      if (Prototype.Browser.IE) {
        top += document.viewport.getScrollOffsets().top;
      }
      body_position = $$('body')[0].getStyle('position');
      $$('body')[0].setStyle('position', 'relative');
      this.e_black_out_mask = new Element('div', {id: 'mdk-black-out-mask'});
      if (Prototype.Browser.IE) {
        this.e_black_out_iframe = new Element('iframe', {id: 'mdk-black-out-iframe', src: "javascript: false;", frameborder: 0});
      }
      this.e_black_out = new Element('div', {id: 'mdk-black-out'});
      this.e_black_out_indicator = new Element('div', {id: 'mdk-black-out-indicator'});
      if (!Object.isUndefined(this.options) && this.options.hide_at_mask) {
        this.e_black_out_mask.observe('click', this.hide.bind(this));
      }
      if (Object.isElement(this.element) && this.element.down('.mdk-black-out-close')) {
        this.element.select('.mdk-black-out-close').each(
          function(e_close) {
            e_close.observe('click', this.hide.bind(this));
          }.bind(this)
        );
      }
      this.e_black_out_mask.setStyle(
        {
          height: ($('container') ? $('container').getHeight() : $$('body')[0].getHeight()) + 'px'
        }
      );
      if (Prototype.Browser.IE) {
        this.e_black_out_iframe.setStyle(
          {
            height: ($('container') ? $('container').getHeight() : $$('body')[0].getHeight()) + 'px'
          }
        );
      }
      this.e_black_out.setStyle(
        {
          position: 'absolute',
          width: width + 'px',
          height: height + 'px',
          left: left + 'px',
          top: top + 'px'
        }
      );
      this.element.setStyle('position', 'relative');
      this.e_black_out_indicator.setStyle(
        {
          width: width + 'px'
        }
      );
      this.e_black_out_indicator.insert({bottom: this.element});
      this.e_black_out.insert({bottom: this.e_black_out_indicator});
      this.e_black_out.hide();
      $$('body')[0].insert({top: this.e_black_out});
      $$('body')[0].insert({top: this.e_black_out_mask});
      if (Prototype.Browser.IE) {
        $$('body')[0].insert({top: this.e_black_out_iframe});
      }
      Effect.BlindDown(this.e_black_out, {duration: 1.0});
      $$('body')[0].setStyle('position', body_position);
    },

    hide: function() {
      if (this.e_black_out_mask) {
        this.e_black_out_mask.remove();
      }
      if (this.e_black_out_iframe) {
        this.e_black_out_iframe.remove();
      }
      if (this.e_black_out) {
        this.e_black_out.remove();
      }
    },

    initialize: function(element, options) {
      element = $(element);
      if (!Object.isElement(element)) {
        return;
      }

      this.options = options || {};
      if (this.options && !this.options.keep_id) {
        this.element = element.cloneNode(true);
        this.element.removeAttribute('id');
        this.element.identify();
      } else {
        this.element = element;
      }
      this.show.bind(this)();
    }

  }
);

var Mdk_Decrypt = Class.create(
  {

    /** @var Integer 固定シード */
    FIXED_SEED: [7, 13],

    /**
     * 複合化
     *
     * @return void
     */
    decrypt: function() {
      if (!this.text_only) {
        document.write('<a href="ma' + 'il' + 'to:');
        this.value[0].each(
          function(character) {
            document.write(this._decrypt(character, 0));
          }.bind(this)
        );
        document.write('">');
      }
      this.value[1].each(
        function(character) {
          document.write(this._decrypt(character, 1));
        }.bind(this)
      );
      if (!this.text_only) {
        document.write('<' + '/a' + '>');
      }
    },

    /**
     * 文字単位の複合化
     *
     * @param String character 一文字
     * @param Integer index インデックス
     * @return String 複合した文字
     */
    _decrypt: function(character, index) {
      return String.fromCharCode(character - this.seed + this.FIXED_SEED[index]);
    },

    /**
     * コンストラクタ
     *
     * @param Array value 文字の配列
     * @param Integer seed シード
     * @param Boolean text_only テキスト表示のみにするかどうか
     * @return void
     */
    initialize: function(value, seed, text_only) {
      this.value = value;
      this.seed = seed;
      this.text_only = text_only;
      this.decrypt();
    }

  }
);

var Mdk_Globals = {};

var mdk = new Mdk;

/**
 * 数字 3 桁ごとにカンマを入れる
 *
 * @return String カンマを入れた数値
 */
Number.prototype.commify = function() {
  var s = this.toString().split("\.");
  return s[0].replace(/((^[+-])?(\d)+?)(?=(\d{3})+$)/g, "$1,") + (s[1]? "." + s[1].replace(/(\d{3})(?=\d+)/g, "$1,") : "");
}

Number.prototype.roundByDigit = function(digit) {
  var value = this;
  value = value * Math.pow(10, digit);
  value = parseInt((Math.round(value)).toString());
  value = value / Math.pow(10, digit);
  return value;
};

/**
 * ゼロ埋めする
 *
 * @param Number digit 埋める桁数
 * @return String 埋めた文字列
 */
Number.prototype.zerofill = function(digit) {
  var string = ('0'.repeat(digit) + this.toString());
  return string.substr(string.length - digit, digit);
};

/**
 * 文字列を繰り返す
 *
 * @param Number length 繰り返す回数
 * @return String 構築した文字列
 */
String.prototype.repeat = function(length) {
  var s = [];
  while (s.length < length) {
    s.push(this);
  }
  return s.join('');
}

/**
 * 改行コードを BR タグに置換する
 *
 * @param Boolen xhtml 対象とする文字列
 * @return String 置換後の文字列
 */
String.prototype.nl2br = function(xhtml) {
  xhtml = Object.isUndefined(xhtml) || xhtml ? true : false;
  br = xhtml ? '<br />' : '<br>';
  return this.gsub(/\r\n|\r|\n/i, br);
}

/**
 * URL的な文字列をアンカータグに置換する
 *
 * @param String target ターゲットフレーム
 * @return String 置換後の文字列
 */
String.prototype.anchorize = function(target) {
  target = Object.isUndefined(target) ? false : ' target="' + target + '"';
  var string = this.toString().gsub(/(https?:\/\/[a-zA-Z0-9#$%&,.\/;=?@^_\-\[\]]+)/m, '<a href="#{0}"' + target + '>#{0}</a>');
  return string;
}

/**
 * prototypeUtils.js from http://jehiah.com/
 * Licensed under Creative Commons.
 * version 1.0 December 20 2005
 *
 * Contains:
 * + Form.Element.setValue()
 * + unpackToForm()
 */
/* Form.Element.setValue("fieldname/id","valueToSet") */
Form.Element.Methods.setValue = function(element,newValue) {
  element_id = element;
  element = $(element);
  if (!element){element = document.getElementsByName(element_id)[0];}
  if (!element){return false;}
  var method = element.tagName.toLowerCase();
  var parameter = Form.Element.SetSerializers[method](element,newValue);
}

Form.Element.SetSerializers = {
  input: function(element,newValue) {
    switch (element.type.toLowerCase()) {
      case 'submit':
      case 'hidden':
      case 'password':
      case 'text':
        return Form.Element.SetSerializers.textarea(element,newValue);
      case 'checkbox':
      case 'radio':
        return Form.Element.SetSerializers.inputSelector(element,newValue);
    }
    return false;
  },

  inputSelector: function(element,newValue) {
    fields = document.getElementsByName(element.name);
    for (var i=0;i<fields.length;i++){
      if (Object.isArray(newValue)) {
        if (newValue.include(fields[i].value)) {
          fields[i].checked = true;
        }
      } else {
        if (fields[i].value == newValue){
          fields[i].checked = true;
        }
      }
    }
  },

  textarea: function(element,newValue) {
    element.value = newValue;
  },

  select: function(element,newValue) {
    var value = '', opt, index = element.selectedIndex;
    for (var i=0;i< element.options.length;i++){
      if (Object.isArray(newValue)) {
        if (newValue.include(element.options[i].value)) {
          element.options[i].selected = true;
        }
      } else {
        if (element.options[i].value == newValue){
          element.options[i].selected = true;
          return true;
        }
      }
    }
  }
}

/* Form.Element.clear("fieldname/id") */
Form.Element.Methods.clear = function(element) {
  element_id = element;
  element = $(element);
  if (!element){element = document.getElementsByName(element_id)[0];}
  if (!element){return false;}
  var method = element.tagName.toLowerCase();
  var parameter = Form.Element.ClearSerializers[method](element);
  return parameter;
}

Form.Element.Methods.selectAll = function(element) {
  element = $(element);
  setTimeout(
    function() {
      this.select();
    }.bind(element)
    , 10
  );
  return element;
};

Form.Element.ClearSerializers = {
  input: function(element) {
    switch (element.type.toLowerCase()) {
      case 'submit':
      case 'hidden':
      case 'password':
      case 'text':
        return Form.Element.ClearSerializers.textarea(element);
      case 'checkbox':
      case 'radio':
        return Form.Element.ClearSerializers.inputSelector(element);
    }
    return false;
  },

  inputSelector: function(element) {
    if (!$(element).up('form').select('[name="' + element.name + '"]')) {
      return false;
    }
    fields = $(element).up('form').select('[name="' + element.name + '"]');
    for (var i=0;i<fields.length;i++){
      fields[i].checked = false;
      fields[i].removeAttribute('checked');
    }
    return element;
  },

  textarea: function(element) {
    element.value = '';
    return element;
  },

  select: function(element,newValue) {
    var value = '', opt, index = element.selectedIndex;
    for (var i=0;i< element.options.length;i++){
      element.options[i].selected = false;
      element.options[i].removeAttribute('selected');
    }
  }
}

Element.addMethods();

function unpackToForm(data){
   for (i in data){
     Form.Element.setValue(i,data[i].toString());
   }
}

document.viewport.getDimensions = function() {
  var dimensions = { }, B = Prototype.Browser;
  $w('width height').each(function(d) {
    var D = d.capitalize();
    if (B.WebKit && !document.evaluate) {
      // Safari <3.0 needs self.innerWidth/Height
      dimensions[d] = self['inner' + D];
    } else if (B.Opera && parseFloat(window.opera.version()) < 9.5) {
      // Opera <9.5 needs document.body.clientWidth/Height
      dimensions[d] = document.body['client' + D];
    //} else if (B.IE) {
    //  dimensions[d] = document.body['client' + D];
    } else {
      dimensions[d] = document.documentElement['client' + D];
    }
  });
  return dimensions;
}


