// CONFIG HERE
var __swfid       = 'myMovie';
var __overlayId   = 'popup_overlay';
var __containerId = 'popup_container';
var __overlaybg   = '#999';
var __msgbg       = '#678';

// ________________________________________________________________________

var openPopup = function (url, width, height, name)
{
  var pipe = openWindow(url, width, height, name);
  window.setTimeout(
    function (ev)
    {
      if ((typeof(pipe) == 'undefined') || (pipe == null))
      {
        openOverlay(
          function(){}, 
          genOpenContent(url, width, height)
        );
      }
    },
    500
  );
}


var openWindow = function (url, width, height, name)
{
  name    = (typeof(name)   != 'undefined') ? name    : 'child';
  width   = (typeof(width)  != 'undefined') ? width   : 750;
  height  = (typeof(height) != 'undefined') ? height  : 560;
  myleft  = Math.floor((screen.availWidth  - width)  / 2);
  mytop   = Math.floor((screen.availHeight - height) / 2);

  return window.open(
    url, 
    name, 
    'width=' + width + ',height=' + height + ',top=' + mytop + ',left=' + myleft 
      + ',location=1,resizable=1,scrollbars=1'
  );

}


var openOverlay = function (func, content)
{
  var dom_swf = document.getElementById(__swfid);
  dom_swf.style.visibility = 'hidden';

  var w_win = $(document).width();
  var h_win = $(document).height();
  var w_bod = $('body').innerWidth();
  var h_bod = $('body').innerHeight();
  var l_scr = $(document).scrollLeft();
  var t_scr = $(document).scrollTop();
  var w_cnt = 400;
  var h_cnt = 292;

  var overlay = document.createElement('div');
  overlay.id  = __overlayId;
  $(overlay).css({
    'left'            : '0 px',
    'top'             : '0 px',
    'width'           : (w_bod) +'px',
    'height'          : (h_bod) +'px',
    'background-color': __overlaybg,
    'z-index'         : '999',
    'opacity'         : 0.7,
    'position'        : 'absolute'
  });
  document.body.insertBefore(overlay, document.body.firstChild);
  var ol_scroll = function (ev)
  {
    $(overlay).css({
      'left'  : $(document).scrollLeft() + 'px',
      'top'   : $(document).scrollTop() + 'px'
    });
  }
  $(window).bind("scroll", ol_scroll);

  var ol_resize = function (ev)
  {
    $(overlay).css({
      'width'  : $('body').innerWidth() +'px',
      'height' : $('body').innerHeight() +'px'
    });
  }
  $(window).bind("resize", ol_resize);

  var container = document.createElement('table');
  container.id  = __containerId;
  $(container).css({
    'position'         : 'absolute',
    'width'            : w_cnt + 'px',
    'height'           : h_cnt + 'px',
    'background-color' : __msgbg,
    'z-index'          : '9999',
    'left'             : (Math.floor((w_bod - w_cnt) / 2) + l_scr) + 'px',
    'top'              : (Math.floor((h_bod - h_cnt) / 2) + t_scr) + 'px'
  });
  var ct_resize = function (ev)
  {
    $(container).css({
      'left'  : (Math.floor(($('body').innerWidth() - w_cnt) / 2) + $(document).scrollLeft()) + 'px',
      'top'   : (Math.floor(($('body').innerHeight() - h_cnt) / 2) + $(document).scrollTop()) + 'px'
    });
  }
  $(window).bind("resize", ct_resize);
  $(window).bind("scroll", ct_resize);

  var tbody = document.createElement('tbody');
  var tr    = document.createElement('tr');
  tbody.appendChild(tr);
  var td    = document.createElement('td');
  tr.appendChild(td);
  container.appendChild(tbody);
//  var img = document.createElement('img');
//  img.src = '/popup/corona_logo.gif';
//  var img_div = document.createElement('div');
//  img_div.className = 'imageDiv';
//  img_div.appendChild(img);
//  td.appendChild(img_div);

  if (typeof(content) == 'undefined')
  {
    if (!is_ie())
    {
      var p1 = document.createElement('p');
      p1.className = 'p1';
      p1.appendChild(document.createTextNode('Popup blocker is detected.'));

      var p2 = document.createElement('p');
      p2.className = 'p2';
      p2.appendChild(document.createTextNode('Please '));

      var link      = document.createElement('a');
      link.href     = '#';
      link.onclick  = function (ev) {
        func();
        window.setTimeout(closeOverlay, 100);
        return false;
      }
      link.appendChild(document.createTextNode('click here to open popup.'));
      p2.appendChild(link);
      td.appendChild(p1);
      td.appendChild(p2);
    }
    else
    {
      var p2 = document.createElement('p');
      p2.className = 'p2';
      p2.appendChild(document.createTextNode('Please click the Information Bar on the top of the window and allow us to open a popup window for you.'));
      td.appendChild(p2);
    }
  }
  else
  {
    td.appendChild(content);
  }
  document.body.insertBefore(container, document.body.firstChild);
}


var closeOverlay = function ()
{
  var dom_swf = document.getElementById(__swfid);
  dom_swf.style.visibility = 'visible';
  document.body.removeChild(document.getElementById(__overlayId));
  document.body.removeChild(document.getElementById(__containerId));
  $(window).unbind("resize");
  $(window).unbind("scroll");
}


var genOpenContent = function (url, width, height)
{
  var div = document.createElement('div');
  if (!is_ie())
  {
    var p1 = document.createElement('p');
    p1.className = 'p1';
    p1.appendChild(document.createTextNode('Popup blocker is detected.'));

    var p2 = document.createElement('p');
    p2.className = 'p2';
    p2.appendChild(document.createTextNode('Please '));

    var link      = document.createElement('a');
    link.href     = '#';
    link.onclick  = function (ev) {
      openWindow(url, width, height, 'again');
      window.setTimeout(closeOverlay, 100);
      return false;
    }
    link.appendChild(document.createTextNode('click here to open popup.'));
    p2.appendChild(link);
    div.appendChild(p1);
    div.appendChild(p2);
  }
  else
  {
    var p2 = document.createElement('p');
    p2.className = 'p2';
    p2.appendChild(document.createTextNode('Please click the Information Bar on the top of the window and allow us to open a popup window for you.'));
    div.appendChild(p2);
  }
  return div;
}


var is_ie = function ()
{
  return (document.all && !window.opera);
}
