/**********  POPUP  **********/

/* setting */
var popup_flg                = 'on';
//var nPopupX                  = 400;                            // ポップアップ画像サイズ
var nPopupX                  = 800;                            // ポップアップ画像サイズ
var nPopupY                  = 600;
var dnPopupX                 = 800;                            // ポップアップ画像サイズ
var dnPopupY                 = 600;
var nPopupBorderWidth        = 1;
var sPopupBackgroundImgSrc   = '/images/popup_back.gif';       // 背景画像のパス
var sPopupBackgroundPadding  = '12px 11px 11px 12px';          // 背景画像表示時の余白CSS
var sPopupAppearEffect       = "fadeIn";                       // 表示エフェクト   (fadeIn | slideDown | show)
var nPopupAppearTime         = "slow"; //0.1;                  // 表示にかかる秒数  - 秒数指定の場合は、実行する部分の"を取る必要あり
var sPopupFadeEffect         = "fadeOut";                      // 非表示エフェクト (fadeOut | slideUp | hide)
var nPopupFadeTime           = "slow"; //0.2;                  // 非表示にかかる秒数
var nPopupEscapeY            = 30;                             // オンマウスで下にずらすピクセル数

var method_flg               = '1';                            // データ取得方法(1:ajax, 2:hidden)
var eventTrigger             = '1';                            // 動作イベント(1:click, 2:hover)
var popOn                    = false;                          // 現在ポップアップしているか

var nMouseX                  = 0;
var nMouseY                  = 0;

var ImgArr                   = new Array;

/* start */
$(function(){

  var PopupDiv = '<div class="modal-overlay" style="display:none"><div id="pop_glossary" style="display:none"></div></div>';
  $("body").prepend(PopupDiv);
  if (dnPopupX >= $(window).width() / 1.5) {
    nPopupX = $(window).width() / 1.5;
  }
  if (dnPopupY >= $(window).height() / 1.5) {
    nPopupY = $(window).height() / 1.5;
  }
  $('#pop_glossary').css({
                        'z-index':  '9999',
                        'position': 'absolute',
                        'width':    nPopupX + nPopupBorderWidth * 2,
                        'height':   nPopupY + nPopupBorderWidth * 2
                        });

  if(sPopupBackgroundImgSrc){
      $('#pop_glossary').css({
                            'background-repeat':   'no-repeat',
                            'background-position': 'top left',
                            /*'background-image':    'url("'+ sPopupBackgroundImgSrc +'")',*/
                            'padding':             sPopupBackgroundPadding
                            });
  }

  if(sPopupBackgroundImgSrc){
      $('#pop_glossary').css({
                            'background-repeat':   'no-repeat',
                            'background-position': 'top left',
                            'padding':             sPopupBackgroundPadding
                            });
  }

  var btnStyle = 'width: 40px;' +
                 'height: 40px;' +
                 'z-index: 9999;' +
                 'background: #999;' +
                 'border-radius: 50%;' +
                 'cursor: pointer;' +
                 'line-height: 40px;' +
                 'text-align: center;' +
                 'color: #fff;' +
                 'position: absolute;' +
                 'font-size: 30px';

  $('.modal-overlay').css({
                         'z-index' : '5',
                         'display' : 'none',
                         'position' : 'fixed',
                         'top' : '0',
                         'left' : '0',
                         'width' : '100%',
                         'height' : '120%',
                         'background-color' : 'rgba(0,0,0,0.75)'
                         });

  var PopContents = '';
  PopContents += '<div id="pop_content">';
  PopContents += '<div id="pop_title">';
  if(eventTrigger == '1'){
    PopContents += '<div id="g_title"></div>';
  }
//  PopContents += '<hr>';
  PopContents += '</div>';
  PopContents += '<div id="pop_detail"></div>';
//  PopContents += '<div id="closeArea"><span onclick="AskPopDown()"><img src="/images/icon_close.gif" alt="閉じる">閉じる</span></div>';
//  PopContents += '<div id="closeArea"><span onclick="AskPopDown()"><span class="closeBtn">×</span>&nbsp;閉じる</span></div>';
//  PopContents += '<div id="closeArea"><span onclick="AskPopDown()"><img src="/images/ajbox_close.gif" alt="閉じる"></span></div>';
  PopContents += '<div id="closeArea"><span onclick="AskPopDown()"><span class="closeBtn" style="'+ btnStyle +'">×</span></span></div>';
  PopContents += '</div>';

  $('#pop_glossary').append(PopContents);
  $('#pop_content').css({
/*                       'background':  '#FFFFFF',*/
/*                       'background':  '#F5F5F5',*/
                       'background':  '#EDEDED',
                       'width':       nPopupX,
                       'height':      nPopupY,
/*                       'margin':    nPopupBorderWidth + 'px',*/
                       'margin':      '0 ' + nPopupBorderWidth + 'px ' + nPopupBorderWidth + 'px 0',
/*                       'border':    nPopupBorderWidth + 'px solid #DF0128',*/
/*                       'border':      nPopupBorderWidth + 'px solid #000000',*/
                       'border-radius': '2%',
                       'font-family': '"ヒラギノ角ゴ Pro W3","Hiragino Kaku Gothic Pro","メイリオ",Meiryo,"ＭＳ Ｐゴシック",sans-serif',
                       'text-align':  'left'
                       });

  $('#pop_content #pop_title').css({
                                  'display': 'block',
                                  'min-height': '30px',
                                  'padding': '10px 10px 5px 10px'
                                  });

  if (eventTrigger == '2'){
    $('#pop_content #pop_title').css({
                                    'border-bottom': '3px solid #FFE586',
                                    'font-size':     '18px',
                                    'font-weight':   'bold',
                                    'padding':       '3px'
                                    });
  }

  if(eventTrigger == '1' || eventTrigger == '2'){
    $('#pop_content #pop_title #g_title').css({
                                             /*'border-bottom': '3px solid #FFE586',*/
                                             /*'border-bottom': '3px solid #00A3B6',*/
                                             'border-bottom': '3px solid #45b4a6',
                                             'font-size':     '18px',
                                             'font-weight':   'bold',
                                             'padding':       '3px'
                                             });
//
//    $('#pop_content #closeArea').css({
//                                    'color':          '#1A57CD',
//                                    'cursor':         'pointer',
//                                    'font-size':      '12px',
//                                    'text-align':     'center',
//                                    'position':       'absolute',
//                                    'center':          '10px',
//                                    'bottom':         '20px',
//                                    /*'padding-right':  '10px',*/
//                                    'width':           '100%'
//                                    });
//
    $('#pop_content #closeArea img').css({
                                    'cursor':         'pointer',
                                    'margin':         '0pt 5px',
                                    'vertical-align': 'bottom'
                                    });
    $('#pop_content #closeArea span').css({
                                    'cursor':         'pointer',
                                    'vertical-align': 'bottom'
                                    });

/*
    $('#pop_content #closeArea span.closeBtn').css({
                                    'background-color': '#1A57CD',
                                    'color':            '#FFFFFF'
                                    });
*/
  }

  $('#pop_content #pop_detail').css({
                                   'font-size':  '13px',
                                   'height':     '210px',
                                   'margin':     '0px 5px 0px 5px',
                                   'overflow':   'auto',
                                   'padding':    '3px 10px 10px 10px'
                                   });


  if(popup_flg == 'on'){

    $("body").mousemove(function(e){
      nMouseX = e.pageX;
      nMouseY = e.pageY;
    });

    // clickされた時のxy座標を取得
    $(".ask_glossary").click(function(e){

      nMouseX = e.pageX;
      nMouseY = e.pageY;
    });

    if(eventTrigger == '2'){
      $(".ask_glossary").hover(
        function(){
          glossaryId = this.id.replace(/[^0-9]+/g,'');
          AskPopUp(glossaryId);
        },
        function(){
          AskPopDown();
      });
    }else{
      $(".ask_glossary").click(function(){
        glossaryId = this.id.replace(/[^0-9]+/g,'');
        var popOn = true;
        AskPopUp(glossaryId);
      });
    }
  }
});

function AskPopUp(gid) {
  if(method_flg == '2'){
    var titleId   = 'tGlossary'+ gid;
    var contentId = 'cGlossary'+ gid;
    gTitle   = $("#"+ titleId).val();
    gContent = $("#"+ contentId).val();
  }else{
//    if(eventTrigger == '2'){

      // get Window size
      var nWindowSizeX = $(window).width();
      var nWindowSizeY = $(window).height();

      // get scroll
      var nScrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
      var nScrollY = document.documentElement.scrollTop || document.body.scrollTop;


      // popup position
      nPopupPositionX = 0;
      nPopupPositionY = 0;

      nPopupPositionX = (nWindowSizeX / 2) - ((nPopupX + nPopupBorderWidth * 2) / 2) + nScrollX;
//    nPopupPositionX = 160;
//      nPopupPositionY = (nWindowSizeY / 2) - ((nPopupY + nPopupBorderWidth * 2) / 2) + nScrollY;
      nPopupPositionY = (nWindowSizeY / 2) - ((nPopupY + nPopupBorderWidth * 2) / 2);
//    nPopupPositionX = ((nWindowSizeX / 2) - 200) - ((nPopupBorderWidth * 2) / 2) + nScrollX;
//    nPopupPositionY = (nWindowSizeY / 2) - ((nPopupBorderWidth * 2) / 2) + nScrollY;


      // ------------------
      // 2012.02.15 E.Sakai
      // 常に真ん中にポップアップさせる為、以下コメントアウト
      /***
      // mouse on pop_content
      if(nMouseX >= nPopupPositionX &&
         nMouseX <= nPopupPositionX + (nPopupX + nPopupBorderWidth * 2) &&
         nMouseY >= nPopupPositionY &&
         nMouseY <= nPopupPositionY + (nPopupY + nPopupBorderWidth * 2)){
           nPopupPositionY = nMouseY + nPopupEscapeY;
      }
      ***/
      // ------------------

      // clickされた場所にpopup
//      nPopupPositionY = nMouseY;
//      nPopupPositionX = nMouseX;

      $('.modal-overlay').fadeIn('slow');
      $('#pop_glossary').css({
                            'top':    nPopupPositionY,
                            'left':   nPopupPositionX
                            });
//

    jQuery.ajax({
            url : "../js/searchGlossary.php",
            type : "get",
            data : "id=" + gid,
            success : function(data){
              if(data == 'ERROR'){
                var popOn = false;
                return false;
              }
              g_arr = data.split('@gASK@');
              if(eventTrigger == '1'){
                $("#pop_content #g_title").html(g_arr[0]);
              }else{
                $("#pop_content #pop_title").html(g_arr[0]);
              }
              if(g_arr[2] != ''){
                $("#pop_content #pop_detail").html(g_arr[1]);
              }else{
                $("#pop_content #pop_detail").html(g_arr[1]);
              }

/*
              $('.r_num').css({
                             'text-align':   'right'
                             });
*/

              if(nPopupAppearTime.match(/[^0-9\.]+/g))
//                eval('$("#pop_glossary").'+ sPopupAppearEffect +'("'+ nPopupAppearTime +'")');
                eval('$("#pop_glossary").'+ sPopupAppearEffect +'("'+ nPopupAppearTime +'",function(){$("body").bind("click", bdCloseBtn);})');
              else
                eval('$("#pop_glossary").'+ sPopupAppearEffect +'("'+ nPopupAppearTime +'",function(){$("body").bind("click", bdCloseBtn);})');
//                eval('$("#pop_glossary").'+ sPopupAppearEffect +'('+ nPopupAppearTime +')');


              tHeight = $("#pop_content #pop_title").height() + 30;
//              dHeight = (230 - tHeight) + 'px';
              dHeight = (nPopupY - tHeight) + 'px';
              $("#pop_content #pop_detail").css({
                                               'height': dHeight
                                               });
              ImgArr = new Array();
              $('#pop_detail img').each(function(i){
                $(this).bind('load', function(){
                  ImgArr[i] = $('#pop_detail img:eq('+i+')').width();
                });
              });
              $('#pop_detail img').bind('load', function(){
                AskPopImg(ImgArr);
              });
              $('.closeBtn').css({
                                'top':   0,
                                'left':  nPopupX - 20
                                });

//              $("body").bind("click", bdCloseBtn);

            },
            error : function(){
              var popOn = false;
              return false;
            }
    });
  }
}

var timer = false;
$(window).resize(function() {
    if (timer !== false) {
        clearTimeout(timer);
    }
    timer = setTimeout(function() {
        nWindowSizeX = $(window).width();
        nWindowSizeY = $(window).height();
        if (dnPopupX >= $(window).width() / 1.5) {
          nPopupX = $(window).width() / 1.5;
        } else {
          nPopupX = dnPopupX;
        }
        if (dnPopupY >= $(window).height() / 1.5) {
          nPopupY = $(window).height() / 1.5;
        } else {
          nPopupY = dnPopupY;
        }
        nScrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
        nScrollY = document.documentElement.scrollTop || document.body.scrollTop;
        nPopupPositionX = (nWindowSizeX / 2) - ((nPopupX + nPopupBorderWidth * 2) / 2) + nScrollX;
//        nPopupPositionY = (nWindowSizeY / 2) - ((nPopupY + nPopupBorderWidth * 2) / 2) + nScrollY;
        nPopupPositionY = (nWindowSizeY / 2) - ((nPopupY + nPopupBorderWidth * 2) / 2);
        tHeight = $("#pop_content #pop_title").height() + 50;
        dHeight = (nPopupY - tHeight) + 'px';
        $('#pop_glossary').css({
                              'top':    nPopupPositionY,
                              'left':   nPopupPositionX
                              });
        $('.closeBtn').css({
                          'top':   0,
                          'left':  nPopupX - 20
                          });
        $('#pop_content').css({
                             'width':       nPopupX,
                             'height':      nPopupY
                             });
        $("#pop_content #pop_detail").css({
                                         'height': dHeight
                                         });
        AskPopImg(ImgArr);
    }, 500);
});

function AskPopImg(ImgArr) {
  $.each(ImgArr, function(i){
    if(ImgArr[i] >= $('#pop_detail').width()-10) {
      $('#pop_detail img:eq('+i+')').width($('#pop_detail').width()-20);
    } else {
      $('#pop_detail img:eq('+i+')').width(ImgArr[i]);
    }
  });
}

function AskPopDown() {
    var popOn = false;
    $("body").unbind("click");
    if(nPopupFadeTime.match(/[^0-9\.]/g))
      eval('$("#pop_glossary").'+ sPopupFadeEffect +'("'+ nPopupFadeTime +'")');
    else
      eval('$("#pop_glossary").'+ sPopupFadeEffect +'('+ nPopupFadeTime +')');
    $('.modal-overlay').fadeOut('slow');
}


function gb_pop(id){
  return false;
  var pop_url  = '../faq_glossary.html?id='+ id;
  GB_showCenter('用語解説', pop_url, 300, 450);
}


function bdCloseBtn(){
  AskPopDown();
}


