/*******************************************************************************
*
* © 2009 Copyright A-Vision
*
* File description :       General user interface functions.
* 
* Created by       :       Arnold Velzel
* Created on       :       06/02/2009
*
* Last changed by  :       -
* Last changed on  :       -
* 
*******************************************************************************/

function msgBox( msg, color, bgcolor, bdcolor, timeout)
{
 if ( !timeout) timeout = 5000;
 if ( !color) color = "#444";
 if ( !bgcolor) bgcolor = "#eee";
 if ( !bdcolor) bdcolor = "#eee";
 if ( !msg) msg = "PLEASE WAIT";

 var re = /\<title\>([^\<]+)\<\/title\>/;
 var t = msg.match(re);
 var msgtitle = "Alert";
 if ( t) {
  msgtitle = t[1];
  msg = msg.replace( t[0], '');
 }

 var re = /\<id\>([^\<]+)\<\/id\>/;
 var i = msg.match(re);
 var msgid = "_messageBox";
 if ( i) {
  msgid = i[1];
  msg = msg.replace( i[0], '');
 }

 var div = document.getElementById( msgid);
 if ( div) closeMsgBox( msgid);
 div = document.createElement( 'DIV');
 div.id = msgid;
 div.className = "messagebox messagebox-"+msgid;
 div.style.position = "absolute";
 div.style.backgroundColor = bgcolor;
 div.style.color = color;
 div.style.zIndex = 9999;
 if ( typeof(bdcolor) == "string") div.style.border = "3px ridge "+bdcolor;

 var btop = document.createElement( 'DIV');
 btop.className = "messagebox-border-top";
 btop.innerHTML = "<DIV class='messagebox-border-top1'></DIV><DIV class='messagebox-border-top2'></DIV><DIV class='messagebox-border-top3'></DIV><DIV class='messagebox-border-top4'></DIV><DIV class='messagebox-border-top5'></DIV>";
 div.appendChild( btop);

 var btopmenu = document.createElement( 'DIV');
 btopmenu.className = "messagebox-border-title topcenter";
 btopmenu.style.height = "0px";
 btopmenu.style.paddingLeft = "5px";
 btopmenu.style.overflow = "hidden";
 btopmenu.style.fontSize = "10px";
 btopmenu.innerHTML = "<a href='#' class='messagebox-close' onclick='closeMsgBox(\""+msgid.toString()+"\");return(false);'>&times;</a>";
 if ( msgtitle) btopmenu.innerHTML += "<div class='messagebox-title'>"+msgtitle +"</div>";
 div.appendChild( btopmenu);

 var bcenter = document.createElement( 'DIV');
 bcenter.className = "messagebox-border-center";
 bcenter.innerHTML = "<DIV class='messagebox-border-center1' style='padding:5px;'><DIV id='messagebox-message'>"+msg+"</DIV></DIV>";
 div.appendChild( bcenter);

 var bbottom = document.createElement( 'DIV');
 bbottom.className = "messagebox-border-bottom";
 bbottom.innerHTML = "<DIV class='messagebox-border-bottom5'></DIV><DIV class='messagebox-border-bottom4'></DIV><DIV class='messagebox-border-bottom3'></DIV><DIV class='messagebox-border-bottom2'></DIV><DIV class='messagebox-border-bottom1'></DIV>";
 div.appendChild( bbottom);

 if( timeout == "STATIC") {
  div.onclick = function(e) {
   closeMsgBox( this.id.toString());
   return(false);
  }
 }

 document.body.appendChild( div);

 // Always position in center;
 var size = applicationSize();
 var left = eval((size[0]-div.offsetWidth)/2);
 var top = eval((size[1]-div.offsetHeight)/2);
 div.style.left = left+"px";
 div.style.top = top+"px";

// initDragging(div);
 if( timeout != "STATIC") div.timer = window.setTimeout("closeMsgBox('"+msgid.toString()+"');",timeout);
}
function closeMsgBox( msgid)
{
 if ( !msgid) msgid = "_messageBox";
 var div = document.getElementById( msgid);
 if ( div) {
  if ( div.timer) window.clearTimeout( div.timer);
  document.body.removeChild( div);
 }
}

function hidealerts()
{
 var interval = 20;

 var countdown = 0;
 var opacity = 0;
 var activealerts = 0;
 var reminders = top.document.getElementById("reminderbucket");
 if ( reminders) {
  var divs = reminders.getElementsByTagName('DIV');
  for (var i=0;i<divs.length;i++) {
   if ( !divs[i].getAttribute('originalheight')) {
    divs[i].setAttribute('originalheight', divs[i].offsetHeight-8);
   }
   if ( divs[i].getAttribute('countdown')) {
    countdown = divs[i].getAttribute('countdown');
    if ( countdown!='STATIC') {
     if ( countdown > interval) {
      divs[i].setAttribute('countdown', eval(countdown-interval));
      activealerts++;
      if ( countdown < 1000) {
       opacity = eval(100*countdown/1000);
      } else {
       opacity = 100;
      }
      divs[i].style.overflow = "hidden";
      divs[i].style.height = eval(divs[i].getAttribute('originalheight')*opacity/100)+"px";
      divs[i].style.opacity = (opacity / 100); 
      divs[i].style.MozOpacity = (opacity / 100); 
      divs[i].style.KhtmlOpacity = (opacity / 100); 
      divs[i].style.filter = "progid:DXImageTransform.Microsoft.alpha(opacity=" + opacity + ")";
 //     divs[i].style.backgroundColor = "rgb("+(155+opacity)+","+(100+opacity)+","+(100+opacity)+")";
     } else {
      reminders.removeChild( divs[i]);
     }
    }
   }
  }
  if ( activealerts > 0) {
   reminders.checking = window.setTimeout("hidealerts()",interval);
  } else {
   reminders.checking = false;
  }
 }
}
function alertmessage( msg, color, bgcolor, bdcolor, timeout)
{
 if ( !timeout) timeout = 5000; // ms before message disappears automatically
 var reminders = top.document.getElementById("reminderbucket");
 if ( !reminders) {
  var opacity = 90;
  reminders = top.document.createElement('DIV');
  reminders.id = "reminderbucket";
  reminders.style.position = "fixed";
  reminders.style.bottom = "0px";
  reminders.style.right = "10px";
  reminders.style.zIndex = 9999;
  top.document.body.appendChild( reminders);
 }
/* if ( reminders.offsetWidth > 300) {
  reminders.style.width = "300px";
 }*/

 var div = top.document.createElement('DIV');
 div.style.padding = "3px";
 div.style.fontSize = "12px";
 if ( bdcolor) {
  div.style.border = "1px solid "+bdcolor;
 } else {
  div.style.border = "1px solid #a00";
 }
 if ( bgcolor) {
  div.style.backgroundColor = bgcolor;
 } else {
  div.style.backgroundColor = "#fcc";
 }
 if ( color) {
  div.style.color = color;
 } else {
  div.style.color = "#400";
 }
 div.innerHTML = "";
 if ( msg) {
  div.innerHTML += msg;
 } else {
  div.innerHTML += "<div style='padding:10px;'><center><b>WAKE UP !!!</b></center></div>";
 }
 div.setAttribute("countdown",timeout);
 div.setAttribute("timeout",timeout);
 div.onmousemove = function() {
  this.setAttribute("countdown", div.getAttribute("timeout"));
 };
 if ( timeout=='STATIC') {
  div.onclick = function() {
   // Dismiss
   this.onmousemove = null;
   this.setAttribute("timeout", 5000);
   this.setAttribute("countdown", 1000);
   if ( !top.document.getElementById("reminderbucket").checking) hidealerts();
//   this.parentNode.removeChild( this);
  }
 }
 reminders.appendChild( div);
 if ( typeof(setnotifications)=='function') setnotifications(top.document.getElementById("reminderbucket"));

 hidealerts();
}

function alertmessages()
{
 var url = "/tasks/reminders.php?random="+Math.random();
 getServerHtml(url,null,function(){
  window.setTimeout("alertmessages()",1000*60*1); // Check every 5 minutes
 });
}

