/* dialog.js - 包含创建与操作网页对话框的方法 */

var _index = 1000;

var topWin = window.top || window;

// 记录对话框数据
var dialogList = null;

if (window == topWin)
{
  dialogList = topWin.dialogList || new Array();
}

var dialogData = function (isPage, dlgId, callback)
{
  this.isPage = isPage,
  this.dlgId = dlgId;
  this.beforeClose = null;
  this.callback = callback;
}

/*
 * 弹出消息提示，并返回操作值(外部js调用)
 */
function message(content, style, callback)
{
  var width = 320;
  var height = 120;
  var title = '';

  switch (style)
  {
    case 'OK':
      title = '提示';
      content = '<div class="msg-alert"><div class="msg-content">' + content +
      '</div><div class="msg-btn"><input type="button" class="btn-blue" value="确定" onclick="closeDialog(true)" /></div></div>';

      break;

    case 'ERROR':
      title = '错误';
      content = '<div class="msg-error"><div class="msg-content">' + content +
      '</div><div class="msg-btn"><input type="button" class="btn-blue" value="确定" onclick="closeDialog(false)" /></div></div>';

      break;

    case 'YESNO':
      title = '确认';
      content = '<div class="msg-confirm"><div class="msg-content">' + content +
      '</div><div class="msg-btn"><input type="button" class="btn-blue" value="确定" onclick="closeDialog(true)" />' +
      '<input type="button" class="btn-gray" value="取消" onclick="closeDialog(false)" /></div></div>';

      break;

    default:
      return;
  }

  topWin._openDialog(false, content, width, height, title, callback);
}

// 显示表单错误信息
function showFieldError(text, element)
{
  message(text, 'OK', function () {
    var tagName = element.get(0).tagName.toUpperCase();

    if (tagName == 'SELECT')
      element.focus();
    else
      element.select();
  });
}

/*
 * 打开网页对话框(外部js调用)
 */
function openDialog(url, width, height, title, callback)
{
  return topWin._openDialog(true, url, width, height, title, callback);
}

/*
 * 关闭网页对话框(外部js调用)
 */
function closeDialog(value)
{
  topWin._closeDialog(value);
}

// 设置当前对话框宽度(外部js调用)
function setDialogWidth(width)
{
  topWin._setDialogWidth(width);
}

// 设置当前对话框高度(外部js调用)
function setDialogHeight(height)
{
  topWin._setDialogHeight(height);
}

function setDialogSize(width, height)
{
  topWin._setDialogWidth(width);
  topWin._setDialogHeight(height);
}

// 返回当前的对话框(外部js调用)
function getCurrentDialog()
{
  return topWin._getTopDialog();
}

// 返回当前的对话框
function _getTopDialog()
{
  if (dialogList.length <= 0)
    return false;

  var topDialog = dialogList[dialogList.length -1];

  return topDialog;
}

// 关闭网页对话框(内部js调用)
function _closeDialog(value)
{
  var topDialog = _getTopDialog();

  if (topDialog != null && typeof (topDialog) != 'undefined')
  {
    if (topDialog.beforeClose != null)
    {
      try
      {
        topDialog.beforeClose();
      }
      catch (e) {}

      topDialog.beforeClose = null;
    }

    var dlg = $('#' + topDialog.dlgId);

    if (dlg.get(0) != null)
    {
      $('.dlg-close-btn', dlg).unbind('click');

      $('iframe', dlg).removeAttr('src').remove();

      $('.dialog-content iframe', dlg).removeAttr('src').empty().remove();

      dlg.remove();
    }

    var overlay = $('.overlay[for=' + topDialog.dlgId + '], .overlay[htmlfor=' + topDialog.dlgId + ']');
    overlay.remove();

    // 对话框关闭后，从列表中移除
    dialogList.pop();

    if ($.isFunction(topDialog.callback))
    {
      try
      {
        topDialog.callback(value);
        topDialog.callback = null;
      }
      catch (e)
      {
      }
    }

    delete(topDialog);
    topDialog = null;
  }
}

// 打开网页对话框(内部js调用)
function _openDialog(isPage, src, width, height, title, callback)
{
  var overlay = null;

  if (dialogList.length > 0)
  {
    overlay = $('<div class="overlay transparent"></div>');
  }
  else
  {
    overlay = $('<div class="overlay"></div>');
  }

  $('body').append(overlay);
  overlay.css('zIndex', _index);

  var titleId = 'dlg_title_' + _index;

  var WRAPPER = '<table cellspacing="0" cellpadding="0" border="0" class="dialog">' +
  '<tbody><tr><td class="dtl"></td><td class="dtc">' +
  '<div class="title-bar"><h2 id="' + titleId + '">' + title + '</h2><a href="javascript:void(0)" class="dlg-close-btn"></a></div>' +
  '</td><td class="dtr"></td></tr>' +
  '<tr><td class="dlg-left"></td><td class="dialog-inner">' +
  '<div class="dialog-content"><div class="dialog-mask"></div></div>' +
  '</td><td class="dlg-right"></td></tr>' +
  '<tr><td class="dbl"></td><td class="dbc"></td><td class="dbr"></td></tr>' +
  '</tbody></table>';

  var dialog = $(WRAPPER).css({
    zIndex: (_index + 1)
  });

  var container = $('.dialog-content', dialog);

  container.css({
    width: width,
    height: height
  });

  $('.dialog-mask', dialog).width(width).height(height);

  $('body').append(dialog);

  $('.dlg-close-btn', dialog).click(function() {
    closeDialog(false);
  });

  var content = null;

  if (isPage)
  {
    content = $('<iframe name="frame_' + _index + '" scrolling="no" frameborder="0"></iframe>').attr({
      width: width,
      height: height
    });

    content.appendTo(container).attr('src', src);
  }
  else
  {
    content = $(src);
    content.appendTo(container);
  }

  _fitDialogPosition(dialog);
  _fitOverlayPosition(overlay);

  dialog.show();
  dialog.bgiframe();

  dialog.draggable({
    handle: $('#' + titleId),
    dragstart: function () {_dragStart(dialog);},
    dragend: function () {_dropEnd(dialog);}
  });

  // 为对话框和遮罩添加Id属性
  var dlgId = 'dlg_' + _index;
  dialog.attr('id', dlgId);
  overlay.attr('for', dlgId);

  // 如果是弹出提示，则获取焦点，防止父窗口还可以点击
  if (! isPage)
  {
    $('.button-blue', content).focus();
  }

  // 将新建的对话框元素信息加到列表中，等待关闭时调用。
  var thisDialog = new dialogData(isPage, dlgId, callback);

  dialogList[dialogList.length] = thisDialog;

  _index ++;

  return thisDialog;
}

function _dragStart(dialog)
{
  $('.title-bar h2', dialog).addClass('dialog-title-focus');
  $('.dialog-mask', dialog).show();
}

function _dropEnd(dialog)
{
  $('.title-bar h2', dialog).removeClass('dialog-title-focus');
  $('.dialog-mask', dialog).hide();
}

// 设置当前对话框宽度
function _setDialogWidth(width)
{
  var topDialog = _getTopDialog();

  if (topDialog != null && typeof (topDialog) != 'undefined')
  {
    topDialog.dialog.css('width', width);
    topDialog.content.css('width', width);

    _fitDialog();
  }
}

// 设置当前对话框高度
function _setDialogHeight(height)
{
  var topDialog = _getTopDialog();

  if (topDialog != null && typeof (topDialog) != 'undefined')
  {
    topDialog.dialog.css('height', height);
    topDialog.content.css('height', height - 25);

    _fitDialog();
  }
}

// 自适应对话框位置(屏幕居中显示)
function _fitDialogPosition(dialog)
{
  var wnd = $(window), doc = $(document), pTop = doc.scrollTop(), pLeft = doc.scrollLeft(), minTop = pTop;;

  pLeft += (wnd.width() - dialog.width()) / 2;
  pTop += (wnd.height() - dialog.height()) / 2;

  // 防止对话框显示太高
  pTop = Math.max(pTop, minTop);

  dialog.css({left:pLeft, top: pTop});
}

// 自适应对话框遮罩显示
function _fitOverlayPosition(overlay)
{
  var doc = $(document.body);

  overlay.css({width: doc.width(), height: doc.height()});
}

// 调整对话框和遮罩的显示
function _fitDialog()
{
  $('.dialog').each(function()
  {
    _fitDialogPosition($(this));
  });

  $('.overlay').each(function () {
    _fitOverlayPosition($(this));
  });
}

$(document).ready(function ()
{
  // 窗口大小改变时，重新调整对话框和遮罩的显示位置
  $(window).resize(function () {
    _fitDialog();
  });
});

$(window).unload(function () {
  $('.dlg-close-btn').unbind('click');

  $('.overlay').empty().remove();
  $('.dialog').empty().remove();
});
