function DialogTag(dialogId, launcherId, src)
{
   this.dialogId = dialogId;
   this.launcherId = launcherId;
   this.src = src;
   this.loaded = false;
   this.positionAlwaysVisible = false;
   this.positionCentered = false;
   this.sizeRefreshed = false;

   // store a method invocation which positions the dialog once it's visible...
   new MethodInvocation("DialogTag.positionDeferred", this, DialogTag.prototype.positionDeferred, null);
}

DialogTag.prototype.setPositionAlwaysVisible = function(aValue)
{
   this.positionAlwaysVisible = aValue;
}

DialogTag.prototype.setPositionCentered = function(aValue)
{
   this.positionCentered = aValue;
}

DialogTag.prototype.hide = function()
{
   try
   {
      var iframe = document.getElementById(this.dialogId);
      iframe.style.display = "none";
   }
   catch(err)
   {
      out.println('DialogTag.hide:' + err.message);
   }
}
function showDialogHelp()
{
   try
   {
      alert("Please attach the link for Help");

   }
   catch(err)
   {
      out.println('DialogTag.showHelp:' + err.message);
   }
}

DialogTag.prototype.doPositionAlwaysVisible = function()
{
   // canvas properties...
   var canvas = new Object();
   canvas.xoffset = document.body.scrollLeft;
   canvas.yoffset = document.body.scrollTop;
   canvas.width = document.body.clientWidth;
   canvas.height = document.body.clientHeight;

   // size properties of dialog
   var iframe = document.getElementById(this.dialogId);
   var dialogGeometry = new Object();
   dialogGeometry.x = iframe.offsetLeft;
   dialogGeometry.y = iframe.offsetTop;
   dialogGeometry.width = iframe.offsetWidth;
   dialogGeometry.height = iframe.offsetHeight;

   var newY = dialogGeometry.y;
   if ((dialogGeometry.y + dialogGeometry.height) > (canvas.height + canvas.yoffset))
   {
      newY = canvas.height + canvas.yoffset - dialogGeometry.height;
      if (newY < canvas.yoffset)
         newY = canvas.yoffset;
   }

   var newX = dialogGeometry.x;
   if ((dialogGeometry.x + dialogGeometry.width) > (canvas.width + canvas.xoffset))
   {
      newX = canvas.width + canvas.xoffset - dialogGeometry.width;
      if (newX < canvas.xoffset)
         newX = canvas.xoffset;
   }

   iframe.style.top = newY;
   iframe.style.left = newX;
}

DialogTag.prototype.doPositionCentered = function()
{
   // canvas properties...
   var canvas = new Object();
   canvas.xoffset = document.body.scrollLeft;
   canvas.yoffset = document.body.scrollTop;
   canvas.width = document.body.clientWidth;
   canvas.height = document.body.clientHeight;

   // size properties of dialog
   var iframe = document.getElementById(this.dialogId);
   var dialogSize = new Object();
   dialogSize.width = iframe.offsetWidth;
   dialogSize.height = iframe.offsetHeight;

   iframe.style.top = ( (canvas.yoffset + canvas.height) / 2 ) - ( dialogSize.height / 2 );
   iframe.style.left = ( (canvas.xoffset + canvas.width ) / 2 ) - ( dialogSize.width / 2 );
}

DialogTag.prototype.positionDefault = function()
{
   //noop...
}

DialogTag.prototype.show = function()
{
   new MethodInvocation("DialogTag.show", this, DialogTag.prototype.showImpl, arguments);
   sessionCheckGuard(function()
   {
      MethodInvocation.retrieve("DialogTag.show").execute();
   },
      null,
      "Session has expired.  Please re-login");
}

DialogTag.prototype.showImpl = function()
{
   try
   {
      if (!this.loaded)
         this.loadDialog();
      document.getElementById(this.dialogId).style.display = '';
      this.positionDeferred(250);
   }
   catch(err)
   {
      out.println('DialogTag.show:' + err.message);
   }
}

DialogTag.prototype.positionDeferred = function()
{
   var isVisible = document.getElementById(this.dialogId).style.display == '' && this.sizeRefreshed == true;
   if (! isVisible)
      setTimeout("MethodInvocation.retrieve('DialogTag.positionDeferred').execute(false)", 200);
   else
      this.position();
}

DialogTag.prototype.position = function()
{
   if (this.positionCentered)
      this.doPositionCentered();
   else if (this.positionAlwaysVisible)
      this.doPositionAlwaysVisible();
   else
      this.positionDefault();
}

DialogTag.prototype.refreshSize = function()
{
   try
   {
      var iframe = document.getElementById(this.dialogId);
      var dialogContainer = iframe.contentWindow.document.getElementById('dialogContainer');
      var width = dialogContainer.offsetWidth;
      var height = dialogContainer.offsetHeight;

      if (width > 0 && height > 0)
      {
         iframe.style.width = width;
         iframe.style.height = height;
         this.sizeRefreshed = true;
      }
   }
   catch(err)
   {
      out.println('DialogTag.refreshSize:' + err.message);
   }
}

DialogTag.prototype.setOpacity = function(n)
{
   setOpacity(this.dialogId, n);
}

DialogTag.prototype.showUrl = function(url)
{
   new MethodInvocation("dialogTagShowUrl", this, DialogTag.prototype.showUrlImpl, arguments);
   sessionCheckGuard(function()
   {
      MethodInvocation.retrieve("dialogTagShowUrl").execute();
   },
      null,
      "Session has expired.  Please re-login");
}

DialogTag.prototype.showUrlImpl = function(url)
{
   try
   {
      this.loaded = false;
      this.src = url;
      this.show();
   }
   catch(err)
   {
      out.println('DialogTag.showUrl:' + err.message);
   }
}

DialogTag.prototype.loadDialog = function()
{
   try
   {
      this.loaded = true;
      var iframe = document.getElementById(this.dialogId);
      iframe.src = this.src;
   }
   catch(err)
   {
      out.println('DialogTag.loadDialog:' + err.message)
   }
}

DialogTag.prototype.hideIfVisible = function()
{
   try
   {
      var eventGenerator = window.event.srcElement;
      var dialogLauncher = document.getElementById(this.launcherId);
      if (eventGenerator != dialogLauncher)
         this.hide();
   }
   catch(err)
   {
      out.println('DialogTag.hideIfVisible:' + err.message);
   }
}

/**
 * Old methods used by calendar tag.  when calender is refactored to use the
 * dialog tag, these can go away...
 */
function closeIframe(x)
{
   var iframe = parent.document.getElementById(x);
   iframe.style.display = "none";
}

function showIframe(x)
{
   document.getElementById(x).style.display = '';
}




