// global variables //
var TIMER = 10;
var SPEED = 10;
var WRAPPER = 'body';

// calculate the current window width //
function pageWidth2() {
  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 pageHeight2() {
  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 topPosition2() {
  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 leftPosition2() {
  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,type2) {
  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 = pageWidth2();
  var height = pageHeight2();
  var left = leftPosition2();
  var top = topPosition2();
  var dialogwidth = dialog.offsetWidth;
  var dialogheight = dialog.offsetHeight;
  var topposition =  (height / 3) - (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;
  if (type2 == 'login') {
  dialogcontent.innerHTML += "" +
  "<form action=\"#\" method=\"post\"><table>"+
  "<tr><th>Přihlašovací jméno:</th><td><input type=\"text\" name=\"nick\" class=\"text\" value=\"\"/></td></tr>"+
  "<tr><th>Heslo:</th><td><input type=\"password\" name=\"pass\" class=\"text\" value=\"\"/></td></tr>"+
  "<tr><td colspan=\"2\"><input type=\"submit\" class=\"submit\" name=\"sigin\" value=\"Přihlásit\"/><input type=\"reset\" onclick=\"hideDialog()\" class=\"submit\" value=\"Zrušit\"/></td></tr>"+
  "</table></form>"+
	"<p>Pokud ještě nemáte svůj účet, tak se můžete <a href=\"/informace/registrace.html\" title=\"Registrace\">zaregistrovat zde</a>!</p>"+
	"<p>Pokud jste zapoměli své heslo, tak si můžete <a href=\"/informace/zapomenute_heslo.html\" title=\"Zapomenuté heslo\">nechat zaslat nové heslo</a>!</p>";
  }
	else if (type2 == 'logout') {
	dialogcontent.innerHTML += "" +
  "<form action=\"#\" method=\"get\"><table>"+
  "<tr><td><input type=\"submit\" name=\"logoutUser\" class=\"submit\" value=\"Ano\"/></td><td><input type=\"reset\" class=\"submit\" onclick=\"hideDialog()\" value=\"Ne\"/></td></tr>"+
  "</table></form>";
	}
	else if (type2 == 'dotaz') {
dialogcontent.innerHTML = "" +
"<form action=\"#\" method=\"post\">"+
"<table style=\"margin: 0px; padding: 0px;\" cellpadding=\"0\" cellspacing=\"0\">"+
"<tr>"+
"<td><label for=\"email\">Váš email:</label></td>"+
"<td><input type=\"text\" id=\"email\" name=\"email\" value=\"@\" /></td>"+
"</tr>"+
"<tr>"+
"<td><label for=\"text2\">Zpráva:</label></td>"+
"<td><textarea name=\"text\" id=\"text2\" rows=\"5\" cols=\"30\"></textarea></td>"+
"</tr>"+
"<tr>"+
"<td><input type=\"submit\" class=\"submit\" name=\"question\" value=\"Odešli dotaz\" /></td>"+
"</tr></table></form>";
	}
	else if (type2 == 'doporucit') {
dialogcontent.innerHTML = "" +
"<form action=\"#\" method=\"post\">" +
"<table style=\"margin: 0px; padding: 0px;\" cellpadding=\"0\" cellspacing=\"0\">" +
"<tr>" +
"<td><label for=\"recommend-sender-name\">Vaše jméno:</label></td>" +
"<td><input type=\"text\" id=\"recommend-sender-name\" name=\"recommend_sender_name\" value=\"\" /></td>" +
"</tr>" +
"<tr>" +
"<td><label for=\"recommend-sender-email\">Váš email:</label></td>" +
"<td><input type=\"text\" id=\"recommend-sender-email\" name=\"recommend_sender_email\" value=\"\" /></td>" +
"</tr>" +
"<tr>" +
"<td><label for=\"recommend-recipient-email\">Email příjemce:</label></td>" +
"<td><input type=\"text\" id=\"recommend-recipient-email\" name=\"recommend_recipient_email\" value=\"\" /></td>" +
"</tr>" +
"<tr>" +
"<td><label for=\"recommend-text\">Zpráva:</label></td>" +
"<td><textarea id=\"recommend-text\" name=\"recommend_text\" rows=\"5\" cols=\"30\">"+dialogcontent.innerHTML+"</textarea></td>" +
"</tr>" +
"<tr>" +
"<td><input type=\"submit\" class=\"submit\" name=\"recommend\" value=\"Odeslat\" /></td>" +
"</tr></table></form>";
	}
  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);
  }
}