// global variables //
var TIMERDIALOG = 5;
var SPEED = 10;
var WRAPPER = 'DialogContent';

// 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) {
  gorunenSelectleriGizle()
  if(!type) {
    type = 'warning';
  }
   switch (type){
      case ("hata") :{type = "error"; break;}
      case ("uyari") :{type = "warning"; break;}
      case ("basarili") :{type = "success"; break;}
   }
  var dialog;
  var dialogheader;
  var dialogclose;
  var dialogtitle;
  var dialogcontent;
  var dialogmask;
  if(!$('dialog')) {
    dialog = new Element('div', { 'id': 'dialog' });
    dialogheader = new Element('div', { 'id': 'dialog-header' });
    dialogtitle = new Element('div', { 'id': 'dialog-title' });
    dialogclose = new Element('div', { 'id': 'dialog-close' });
    dialogcontent = new Element('div', { 'id': 'dialog-content' });
    dialogmask = new Element('div', { '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 = $('dialog');
    dialogheader = $('dialog-header');
    dialogtitle = $('dialog-title');
    dialogclose = $('dialog-close');
    dialogcontent = $('dialog-content');
    dialogmask = $('dialog-mask');
    dialogmask.show();
    dialog.show();
  }
  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 leftposition = left + (width / 2) - (dialogwidth / 2);
  dialog.style.top = topposition + "px";
  dialog.style.left = leftposition + "px";
  dialogheader.className = type + "header";
  dialogtitle.innerHTML = title?title:"kitapbas.com";
  dialogcontent.className = type;
  dialogcontent.innerHTML = "<table class='style4' width='100%' height='100%' border='0' cellpadding='0' cellspacing='0'><tr><td height='100%' valign='center' style='font-size:12px' align='center'>" + message + "</td></tr><tr><td align='center'><img src='/images/btnTamam.gif' onclick='hideDialog()' style='cursor:hand;cursor:pointer' /></td></tr></table>";
  var content = $(WRAPPER);
  dialogmask.style.height = content? content.offsetHeight:height+ 'px';
  dialog.timer = setInterval("fadeDialog(1)", TIMERDIALOG);
  if(autohide) {
    dialogclose.style.visibility = "hidden";
    window.setTimeout("hideDialog()", (autohide * 1000));
  } else {
    dialogclose.style.visibility = "visible";
  }
}

// hide the dialog box //
function hideDialog() {
  var dialog = $('dialog');
  clearInterval(dialog.timer);
  dialog.timer = setInterval("fadeDialog(0)", TIMERDIALOG);
}

// fade-in the dialog box //
function fadeDialog(flag) {
  if(flag == null) {
    flag = 1;
  }
  var dialog = $('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.hide();
    $('dialog-mask').hide();
    clearInterval(dialog.timer);
    gorunenSelectleriGoster()
  }
}
