﻿var divTag = document.createElement("div");
var TimeToFade = 1000.0;
function sleep(milliseconds)
{
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++)
  {
    if ((new Date().getTime() - start) > milliseconds)
    {
      break;
    }
  }
}
function fade(eid)
{
  var element = document.getElementById(eid);
  if(element == null)
    return;

  if(element.FadeState == null)
  {
    if(element.style.opacity == null
        || element.style.opacity == ''
        || element.style.opacity == '1')
    {
      element.FadeState = 2;
    }
    else
    {
      element.FadeState = -2;
    }
  }

  if(element.FadeState == 1 || element.FadeState == -1)
  {
    element.FadeState = element.FadeState == 1 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
  }
  else
  {
    element.FadeState = element.FadeState == 2 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade;
    setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
  }
}
function animateFade(lastTick, eid)
{
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;

  var element = document.getElementById(eid);

  if(element.FadeTimeLeft <= elapsedTicks)
  {
    element.style.opacity = element.FadeState == 1 ? '1' : '0';
    element.style.filter = 'alpha(opacity = '
        + (element.FadeState == 1 ? '100' : '0') + ')';
    element.FadeState = element.FadeState == 1 ? 2 : -2;
    return;
  }

  element.FadeTimeLeft -= elapsedTicks;
  var newOpVal = element.FadeTimeLeft/TimeToFade;
  if(element.FadeState == 1)
    newOpVal = 1 - newOpVal;

  element.style.opacity = newOpVal;
  element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';

  setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}
function count()
{
  var t=setTimeout("fade(\"notify\")",4000);
}
function center()
{
  divTag.style.marginLeft = "-" + parseInt(divTag.offsetWidth / 2) + "px";
}
function createDiv(icon,text,color,border)
{
  divTag.style.opacity=0;
  divTag.style.top="0px";
  divTag.id = "notify";
  divTag.setAttribute("align","center");
  divTag.style.margin = "0px auto";
  divTag.style.backgroundColor = color;
  divTag.className ="notifier";
  divTag.style.border="solid 1px "+border;
  divTag.style.backgroundImage= "url("+icon+")";
  divTag.innerHTML = text;
  document.body.appendChild(divTag);
  center();
  fade("notify");
  count();
}
function display_message (message,type)
{
  var color = '#99D6EB';
  var icon = 'default.jpg';
  switch (type)
  {
    case 'success':
        color = '#99E699';
        icon = 'http://lpt-media.hu/notificationbar/success.png';
        border = '#00FF00';
        break;
    case 'warning':
        color = '#FFBC7B';
        icon = 'http://lpt-media.hu/notificationbar/warn.png';
        border = '#FF9933';
        break;
    case 'error':
        color = '#FF7171';
        icon = 'http://lpt-media.hu/notificationbar/error.png';
        border = '#FF0000';
        break;
    default:
        color = '#99D6EB';
        icon = 'http://lpt-media.hu/notificationbar/default.png';
        border = '#0000FF';
        break;
  }
  createDiv(icon,message,color,border);
}
function instant_location (url)
{
  var u = setTimeout("window.location = url",5000);
}
