// global variables //
var TIMER = 5;
var SPEED = 10;
var WRAPPER = 'content';

// calculate the current window width //
function pageWidth() {
  return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}

// calculate the current window height //
function pageHeight() {
  return window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}

// calculate the current window vertical offset //
function topPosition() {
  return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

// calculate the position starting at the left of the window //
function leftPosition() {
  return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}

// build/show the dialog box, populate the data and call the fadeDialog function //
function showDialog(title,message,type,autohide) {
  if(!type) {
    type = 'error';
  }
  var dialog;
  var dialogheader;
  var dialogclose;
  var dialogtitle;
  var dialogcontent;
  var dialogmask;
  if(!document.getElementById('dialog')) {
    dialog = document.createElement('div');
    dialog.id = 'dialog';
    dialogheader = document.createElement('div');
    dialogheader.id = 'dialog-header';
    dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialog-title';
    dialogclose = document.createElement('div');
    dialogclose.id = 'dialog-close'
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    dialogmask = document.createElement('div');
    dialogmask.id = 'dialog-mask';
    document.body.appendChild(dialogmask);
    document.body.appendChild(dialog);
    dialog.appendChild(dialogheader);
    dialogheader.appendChild(dialogtitle);
    dialogheader.appendChild(dialogclose);
    dialog.appendChild(dialogcontent);;
    dialogclose.setAttribute('onclick','hideDialog()');
    dialogclose.onclick = hideDialog;
  } else {
    dialog = document.getElementById('dialog');
    dialogheader = document.getElementById('dialog-header');
    dialogtitle = document.getElementById('dialog-title');
    dialogclose = document.getElementById('dialog-close');
    dialogcontent = document.getElementById('dialog-content');
    dialogmask = document.getElementById('dialog-mask');
    dialogmask.style.visibility = "visible";
    dialog.style.visibility = "visible";
  }
  dialog.style.opacity = .00;
  dialog.style.filter = 'alpha(opacity=0)';
  dialog.alpha = 0;
  var width = pageWidth();
  var height = pageHeight();
  var left = leftPosition();
  var top = topPosition();
  var dialogwidth = dialog.offsetWidth;
  var dialogheight = dialog.offsetHeight;
//  var topposition = top + (height / 3) - (dialogheight / 2);
  var topposition = top + (height / 2) - (dialogheight / 2);
  var leftposition = left + (width / 2) - (dialogwidth / 2);
  dialog.style.top = topposition + "px";
  dialog.style.left = leftposition + "px";
  dialogheader.className = type + "header";
  dialogtitle.innerHTML = title;
  dialogcontent.className = type;
  dialogcontent.innerHTML = message;
  var content = document.getElementById(WRAPPER);
  dialogmask.style.height = content.offsetHeight + 'px';
  dialog.timer = setInterval("fadeDialog(1)", TIMER);
  if(autohide) {
    dialogclose.style.visibility = "hidden";
    window.setTimeout("hideDialog()", (autohide * 1000));
  } else {
    dialogclose.style.visibility = "visible";
  }
}

// hide the dialog box //
function hideDialog() {
  var dialog = document.getElementById('dialog');
  clearInterval(dialog.timer);
  dialog.timer = setInterval("fadeDialog(0)", TIMER);
}

// fade-in the dialog box //
function fadeDialog(flag) {
  if(flag == null) {
    flag = 1;
  }
  var dialog = document.getElementById('dialog');
  var value;
  if(flag == 1) {
    value = dialog.alpha + SPEED;
  } else {
    value = dialog.alpha - SPEED;
  }
  dialog.alpha = value;
  dialog.style.opacity = (value / 100);
  dialog.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(dialog.timer);
    dialog.timer = null;
  } else if(value <= 1) {
    dialog.style.visibility = "hidden";
    document.getElementById('dialog-mask').style.visibility = "hidden";
    clearInterval(dialog.timer);
  }
}
function showalta(title,message,type,autohide) {
  if(!type) {
    type = 'error';
  }
  var alta;
  var altaheader;
  var altaclose;
  var altatitle;
  var altacontent;
  var altamask;
  if(!document.getElementById('alta')) {
    alta = document.createElement('div');
    alta.id = 'alta';
    altaheader = document.createElement('div');
    altaheader.id = 'alta-header';
    altatitle = document.createElement('div');
    altatitle.id = 'alta-title';
    altaclose = document.createElement('div');
    altaclose.id = 'alta-close'
    altacontent = document.createElement('div');
    altacontent.id = 'alta-content';
    altamask = document.createElement('div');
    altamask.id = 'alta-mask';
    document.body.appendChild(altamask);
    document.body.appendChild(alta);
    alta.appendChild(altaheader);
    altaheader.appendChild(altatitle);
    altaheader.appendChild(altaclose);
    alta.appendChild(altacontent);;
    altaclose.setAttribute('onclick','hidealta()');
    altaclose.onclick = hidealta;
  } else {
    alta = document.getElementById('alta');
    altaheader = document.getElementById('alta-header');
    altatitle = document.getElementById('alta-title');
    altaclose = document.getElementById('alta-close');
    altacontent = document.getElementById('alta-content');
    altamask = document.getElementById('alta-mask');
    altamask.style.visibility = "visible";
    alta.style.visibility = "visible";
  }
  alta.style.opacity = .00;
  alta.style.filter = 'alpha(opacity=0)';
  alta.alpha = 0;
  var width = pageWidth();
  var height = pageHeight();
  var left = leftPosition();
  var top = topPosition();
  var altawidth = alta.offsetWidth;
  var altaheight = alta.offsetHeight;
//  var topposition = top + (height / 3) - (altaheight / 2);
  var topposition = top + (height / 2) - (altaheight / 2);
  var leftposition = left + (width / 2) - (altawidth / 2);
  alta.style.top = topposition + "px";
  alta.style.left = leftposition + "px";
  altaheader.className = type + "header";
  altatitle.innerHTML = title;
  altacontent.className = type;
  altacontent.innerHTML = message;
  var content = document.getElementById(WRAPPER);
  altamask.style.height = content.offsetHeight + 'px';
  alta.timer = setInterval("fadealta(1)", TIMER);
  if(autohide) {
    altaclose.style.visibility = "hidden";
    window.setTimeout("hidealta()", (autohide * 1000));
  } else {
    altaclose.style.visibility = "visible";
  }
}

// hide the alta box //
function hidealta() {
  var alta = document.getElementById('alta');
  clearInterval(alta.timer);
  alta.timer = setInterval("fadealta(0)", TIMER);
}

// fade-in the alta box //
function fadealta(flag) {
  if(flag == null) {
    flag = 1;
  }
  var alta = document.getElementById('alta');
  var value;
  if(flag == 1) {
    value = alta.alpha + SPEED;
  } else {
    value = alta.alpha - SPEED;
  }
  alta.alpha = value;
  alta.style.opacity = (value / 100);
  alta.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(alta.timer);
    alta.timer = null;
  } else if(value <= 1) {
    alta.style.visibility = "hidden";
    document.getElementById('alta-mask').style.visibility = "hidden";
    clearInterval(alta.timer);
  }
}

function showTabella(title,message,type,autohide) {
  if(!type) {
    type = 'error';
  }
  var Tabella;
  var Tabellaheader;
  var Tabellaclose;
  var Tabellatitle;
  var Tabellacontent;
  var Tabellamask;
  if(!document.getElementById('Tabella')) {
    Tabella = document.createElement('div');
    Tabella.id = 'Tabella';
    Tabellaheader = document.createElement('div');
    Tabellaheader.id = 'Tabella-header';
    Tabellatitle = document.createElement('div');
    Tabellatitle.id = 'Tabella-title';
    Tabellaclose = document.createElement('div');
    Tabellaclose.id = 'Tabella-close'
    Tabellacontent = document.createElement('div');
    Tabellacontent.id = 'Tabella-content';
    Tabellamask = document.createElement('div');
    Tabellamask.id = 'Tabella-mask';
    document.body.appendChild(Tabellamask);
    document.body.appendChild(Tabella);
    Tabella.appendChild(Tabellaheader);
    Tabellaheader.appendChild(Tabellatitle);
    Tabellaheader.appendChild(Tabellaclose);
    Tabella.appendChild(Tabellacontent);;
    Tabellaclose.setAttribute('onclick','hideTabella()');
    Tabellaclose.onclick = hideTabella;
  } else {
    Tabella = document.getElementById('Tabella');
    Tabellaheader = document.getElementById('Tabella-header');
    Tabellatitle = document.getElementById('Tabella-title');
    Tabellaclose = document.getElementById('Tabella-close');
    Tabellacontent = document.getElementById('Tabella-content');
    Tabellamask = document.getElementById('Tabella-mask');
    Tabellamask.style.visibility = "visible";
    Tabella.style.visibility = "visible";
  }
  Tabella.style.opacity = .00;
  Tabella.style.filter = 'alpha(opacity=0)';
  Tabella.alpha = 0;
  var width = pageWidth();
  var height = pageHeight();
  var left = leftPosition();
  var top = topPosition();
  var Tabellawidth = Tabella.offsetWidth;
  var Tabellaheight = Tabella.offsetHeight;
//  var topposition = top + (height / 3) - (Tabellaheight / 2);
  var topposition = top + (height / 2) - (Tabellaheight / 2);
  var leftposition = left + (width / 2) - (Tabellawidth / 2);
  Tabella.style.top = topposition + "px";
  Tabella.style.left = leftposition + "px";
  Tabellaheader.className = type + "header";
  Tabellatitle.innerHTML = title;
  Tabellacontent.className = type;
  Tabellacontent.innerHTML = message;
  var content = document.getElementById(WRAPPER);
  Tabellamask.style.height = content.offsetHeight + 'px';
  Tabella.timer = setInterval("fadeTabella(1)", TIMER);
  if(autohide) {
    Tabellaclose.style.visibility = "hidden";
    window.setTimeout("hideTabella()", (autohide * 1000));
  } else {
    Tabellaclose.style.visibility = "visible";
  }
}

// hide the Tabella box //
function hideTabella() {
  var Tabella = document.getElementById('Tabella');
  clearInterval(Tabella.timer);
  Tabella.timer = setInterval("fadeTabella(0)", TIMER);
}

// fade-in the Tabella box //
function fadeTabella(flag) {
  if(flag == null) {
    flag = 1;
  }
  var Tabella = document.getElementById('Tabella');
  var value;
  if(flag == 1) {
    value = Tabella.alpha + SPEED;
  } else {
    value = Tabella.alpha - SPEED;
  }
  Tabella.alpha = value;
  Tabella.style.opacity = (value / 100);
  Tabella.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(Tabella.timer);
    Tabella.timer = null;
  } else if(value <= 1) {
    Tabella.style.visibility = "hidden";
    document.getElementById('Tabella-mask').style.visibility = "hidden";
    clearInterval(Tabella.timer);
  }
}
