/**
 * mindia/jquery.popup.js
 * リンク先をajaxで取得して表示
 *
 */
(function(){
  var $ = jQuery;
  var defaultOptions = {
    'popupDelay': 2500
  }
  var strStyle = '<div class="AL_wrapper" style="'
  var StyleEnd ='"><div class="AL_top">&nbsp;</div><div class="AL_popup">'

  var strEnd = '<br clear="all"/></div><div class="AL_bottom">&nbsp;</div></div>'
  jQuery.fn.popup = function(options){
    options = $.extend({}, defaultOptions, options);
    this.each(function(){
      var self = $(this);
      // 読み込んだデータをキャッシュ
  	  var content = null;
      // 読み込み中かどうか
      var nowLoading = false;
      var bodyUrl = $(this).attr("href");

      // setTimeout
      var closeTimer = null;
      /**
       * すべてのポップアップを閉じる
       */
      var closeAll = function(){
        $('.AL_wrapper').remove();
      }
      /**
       * ポップアップを開く
       * @param e Event
       */
      var open = function(e){
        var horizontal = e.pageX+10;
        var vertical = e.pageY+10;

        if(navigator.appName.indexOf("Microsoft") >= 0){
          var position = self.position()
          horizontal = position.left + 30;
          vertical = position.top + 25;
        }

        //var strIE = '>left:' + (horizontal-200) + 'px;>top:' + (vertical-165) + 'px;';
        var positionStr = 'left:' + horizontal + 'px;top:' + vertical + 'px;'// + strIE;
        self.after(strStyle+positionStr+StyleEnd+content+strEnd);
        // 自動的に閉じるタイマーを設定
        closeTimer = setTimeout(function(){
          closeAll();
        }, options['popupDelay'])
      };
      var action =
        function(e) {
          // 今現在読み込み中ならキャンセル
          if (nowLoading){
            return;
          }
          // 自動的に閉じるタイマーをキャンセル
          if (closeTimer){
            clearTimeout(closeTimer);
          }
          closeAll();

          // すでにデータを読み込んでいればそれを表示
          if (content){
            open(e);
            return;
          }
          // 読み込みを開始
          nowLoading = true;
          $.ajax({
            url : bodyUrl,
            data : {
              format : 'json'
            },
            dataType : 'jsonp',
            success : function(data){
                if (data.response){
                content = data.response.keyword.text.substring(0,50);
				if (content==""){
					content = "<span style='color:#999;'>本文はまだありません</span>"
				}
                var img = data.response.keyword.image;
                if (img !="") {
                  content = '<img src="' + img + '">' + content;
                }
              }
			  nowLoading = false;
              open(e);
            }
          })
        }
      // 少し待ってから表示する
      var delayedTimer = null;
      var delayedAction = function(e){
        if (!delayedTimer){
          delayedTimer = setTimeout(function(){
            action(e);
            clearTimeout(delayedTimer);
            delayedTimer = null;
          }, 300);
        }
      }
      self
//        .hover(action, function(){
//            $('.AL_wrapper').remove();
//        })
        // .hoverだとload完了前にマウスが乗ってる場合にポップアップがでない
        .mousemove(delayedAction) // a要素内でマウス追従する
        .mouseover(delayedAction)
        .mouseout(function(){
          clearTimeout(delayedTimer);
          delayedTimer = null;
          closeAll();
        });
    });
    return this;
  }
})();


