//$Id: Notification.js,v 1.18 2007-10-12 07:37:17 ctthang Exp $
if(typeof(Notifications) == 'undefined')
{
   Notifications = new Array;
   $(window).bind("unload", null, cgNotification_destroyNotifications);
}

//internal method (sorry for this :-( )
Notifications.registerNotification = function(notificationMethod)
{
   Notifications[notificationMethod.name] = getCenterWindow().Notifications.registerGlobalNotification(notificationMethod);
}

//global method
Notifications.registerGlobalNotification = function(notificationMethod)
{
   Notifications[notificationMethod.name] = notificationMethod.getBody();
   return Notifications[notificationMethod.name];
}

function cgNotification_destroyNotifications()
{
   if(Notifications != null)
   {
      for(var name in Notifications)
      {
         var method = Notifications[name];
         if(method && method.method)
         {
            var instance = Notifications.__checkInstance(method.method);
            if(instance != null)
               instance.style.display = 'none';
            method.method = null;
            method = null;
            Notifications[name] = null;
         }
      }
      Notifications = null;
   }
}

Notifications.__show = function(notificationMethod)
{
   var instance = Notifications.__checkInstance(notificationMethod);
   
   instance.firstChild.firstChild.innerHTML = notificationMethod.content;
   instance.popUpHeight = instance.firstChild.scrollHeight;
   
   instance.firstChild.style.width = "100%";
   instance.firstChild.style.height = 0;
   
   if (cg_shared.IsFireFox)
      instance.departurePoint = 10; 
   else 
      instance.departurePoint = (notificationMethod.window.document.body.offsetTop + notificationMethod.window.document.body.offsetHeight);
   instance.firstChild.style.top = instance.firstChild.style.bottom  = instance.departurePoint;
   instance.style.display = '';
   instance.firstChild.style.width  = instance.firstChild.firstChild.firstChild.offsetWidth + 32;
   
   notificationMethod.setTimer(notificationMethod.window.setInterval(notificationMethod.getUpTimeHandler(), 20));
   return null;
}

Notifications.remove = function(name)
{
   var method = Notifications[name];
   if(method)
   {
      method = null;
      Notifications[name] = null;
   }
}

Notifications.notify = function(imgurl/*obsoleted*/, content)
{
   getCenterWindow().Notifications.globalNotify(imgurl, content);
}

//private
Notifications.globalNotify = function(imgurl/*obsoleted*/, content)
{
   var date = new Date();
   var methodname = "notify" + date.getTime();
   var method = new NotificationMethod(methodname, virtualAppHost + "Images/Error.png", content);
   method.dynamic = true;
   Notifications.registerGlobalNotification(method);
   Notifications.__show(method);
}


Notifications.alert = function(content)
{
   getCenterWindow().Notifications.globalAlert(content);
}

//private
Notifications.globalAlert = function(content)
{
   var date = new Date();
   var methodname = "notify" + date.getTime();
   var method = new NotificationMethod(methodname, virtualAppHost + "Images/warning.png", content);
   method.dynamic = true;
   Notifications.registerGlobalNotification(method);
   Notifications.__show(method);
}

Notifications.__checkInstance = function (notificationMethod)
{
   var instance = notificationMethod.window.document.getElementById(notificationMethod.name + "_div");
   if(!instance)
   {
      instance = notificationMethod.window.document.createElement("DIV");
      instance.setAttribute("onselectstart", "return false;");
      instance.setAttribute("class", "PopupFrame");
      instance.innerHTML = '<div onmousedown="cg_shared.cancelEventBubble(event);" onselectstart="return false;" class="PopupContent"><span></span></div>';
      instance.id = notificationMethod.name + "_div";
      notificationMethod.window.document.body.appendChild(instance);
      instance.firstChild.id = notificationMethod.name + "_content";
      instance.firstChild.firstChild.id = notificationMethod.name + "_span";      
      instance.firstChild.onmouseover = notificationMethod.getClosingTimeHandler();
   }
   
   if(typeof(notificationMethod.window.getMaxzIndex) != 'undefined')
   {
      instance.style.zIndex = notificationMethod.window.getMaxzIndex() + 100;
      instance.firstChild.style.zIndex = notificationMethod.window.getMaxzIndex() + 100;
   }
   return instance;
}

//
// Represents a method
//
function NotificationMethod(name, imageurl, text, delay)
{
   this.dynamic = false;
   this.name = name;
   this.content = '<table cellpadding="5"><tr><td nowrap=nowrap><img src="' + imageurl + '"></td><td nowrap=nowrap>' + text + '</td></tr></table>';
   this.window = getCenterWindow();
   this.delaytime = (typeof(delay) != 'undefined')? delay : 3000;
}

NotificationMethod.prototype.getName = function()
{
   return this.name;
}

NotificationMethod.prototype.setDelay = function(delay)
{
   this.delay = delay;
}

NotificationMethod.prototype.setTimer = function(timerID)
{
   this.timerID = timerID;
}

NotificationMethod.prototype.getTimer = function()
{
   return this.timerID;
}

NotificationMethod.prototype.getUpTimeHandler = function()
{
   var me = this;
   var body =
      function()
      {
         var instance = Notifications.__checkInstance(body.method);
         instance.firstChild.style.height = parseInt(instance.firstChild.style.height) + 10;
         //instance.firstChild.style.top = body.method.window.document.body.offsetHeight - parseInt(instance.firstChild.style.height);
         instance.firstChild.style.top = instance.departurePoint - parseInt(instance.firstChild.style.height);
           
         if(parseInt(instance.firstChild.style.height) >= instance.popUpHeight)
         {
            body.method.window.clearInterval(body.method.getTimer());
            body.method.setTimer(body.method.window.setInterval(body.method.getClosingTimeHandler(), body.method.delaytime));
         }
      }

   body.method = me;
   
   return body;
}

NotificationMethod.prototype.getClosingTimeHandler = function()
{
   var me = this;
   var body =
      function()
      {
         body.method.window.clearInterval(body.method.getTimer());
         var instance = Notifications.__checkInstance(body.method);
         instance.style.display = 'none';
         if(body.method.dynamic)
            Notifications.remove(body.method.name);
      }

   body.method = me;
   
   return body;
}


NotificationMethod.prototype.getBody = function()
{
   var me = this;
   var body =
      function()
      {
         return me.window.Notifications.__show(me);
      }

   body.method = me;
   
   return body;
}

