// 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,yesno,yes,no) {

  //  alert(message);

  if(!type) {
    type = 'error';
  }
  var autohide = 0;
  var dialog;
  var dialogheader;
  var dialogclose;
  var dialogtitle;
  var dialogcontainer;
  var dialogcontent;
  var bubbletop;
  var bubblebottom;
  var dialogmask;
  var messagebubblebottom;
  var messagebubbletop;
  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'
    dialogcontainer = document.createElement('div');
    dialogcontainer.id = 'dialog-container';
    bubbletop = document.createElement('div');
    bubbletop.id = 'bubble-top';
    messagebubbletop = document.createElement('img');
    messagebubbletop.src = "https://www.depositagift.com/img/messageBubbleTop.png";
    messagebubblebottom = document.createElement('img');
    messagebubblebottom.src = "https://www.depositagift.com/img/messageBubbleBottom.png";
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    bubblebottom = document.createElement('div');
    bubblebottom.id = 'bubble-bottom';
    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(dialogcontainer);
	dialogcontainer.appendChild(bubbletop);
	bubbletop.appendChild(messagebubbletop);
    dialogcontainer.appendChild(dialogcontent);
	dialogcontainer.appendChild(bubblebottom);
	bubblebottom.appendChild(messagebubblebottom);
    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');
    dialogcontainer = document.getElementById('dialog-container');
    bubbletop = document.getElementById('bubble-top');
    dialogcontent = document.getElementById('dialog-content');
    bubblebottom = document.getElementById('bubble-bottom');
    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 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(yesno==1)
    {
        dialogyesno = document.createElement('div');
        dialogyesno.id = "dialog-yesno";
        dialogyes = document.createElement('div');
        dialogyes.id = "dialog-yes";
        dialogyes.innerHTML = yes;
        dialogno = document.createElement('div');
        dialogno.id = "dialog-no";
        dialogno.innerHTML = no;
        dialogyesno.appendChild(dialogyes);
        dialogyesno.appendChild(dialogno);
        dialogcontent.appendChild(dialogyesno);
        $("#dialog-yes").click(function(){
            document.forms[0].submit();
        });
        $("#dialog-no").click(function(){
            $("#dialog-mask").remove();
            $("#dialog").remove();
        });
    }
  if(autohide) {
    dialogclose.style.visibility = "hidden";
    window.setTimeout("hideDialog()", (autohide * 1000));
  } else {
    dialogclose.style.visibility = "visible";
  }
}

// build/show the dialog box, populate the data and call the fadeDialog function //
function showConfirmDialog(title,message,type,yes,no,yesurl) {
  if(!type) {
    type = 'error';
  }
  var autohide = 0;
  var dialog;
  var dialogheader;
  var dialogclose;
  var dialogtitle;
  var dialogcontent;
  var bubbletop;
  var bubblebottom;
  var dialogmask;
  var messagebubblebottom;
  var messagebubbletop;
  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'
    dialogcontainer = document.createElement('div');
    dialogcontainer.id = 'dialog-container';
    bubbletop = document.createElement('div');
    bubbletop.id = 'bubble-top';
    messagebubbletop = document.createElement('img');
    messagebubbletop.src = "https://www.depositagift.com/img/messageBubbleTop.png";
    messagebubblebottom = document.createElement('img');
    messagebubblebottom.src = "https://www.depositagift.com/img/messageBubbleBottom.png";
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    bubblebottom = document.createElement('div');
    bubblebottom.id = 'bubble-bottom';
    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(dialogcontainer);
	dialogcontainer.appendChild(bubbletop);
	bubbletop.appendChild(messagebubbletop);
    dialogcontainer.appendChild(dialogcontent);
	dialogcontainer.appendChild(bubblebottom);
	bubblebottom.appendChild(messagebubblebottom);
    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');
    dialogcontainer = document.getElementById('dialog-container');
    bubbletop = document.getElementById('bubble-top');
    dialogcontent = document.getElementById('dialog-content');
    bubblebottom = document.getElementById('bubble-bottom');
    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 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 + "<hr/><p></p>";
  dialogcontent.innerHTML = message + "";
  var content = document.getElementById(WRAPPER);
  dialogmask.style.height = content.offsetHeight + 'px';
  dialog.timer = setInterval("fadeDialog(1)", TIMER);
 
  dialogyesno = document.createElement('div');
  dialogyesno.id = "dialog-yesno";
  dialogyes = document.createElement('div');
  dialogyes.id = "dialog-yes";
  dialogyes.innerHTML = yes;
  dialogno = document.createElement('div');
  dialogno.id = "dialog-no";
  dialogno.innerHTML = no;
  dialogyesno.appendChild(dialogyes);
  dialogyesno.appendChild(dialogno);
  dialogcontent.appendChild(dialogyesno);
  $("#dialog-yes").click(function(){
	if(typeof(yesurl) == 'string') {
		window.location = yesurl;
	}else {
		yesurl();
	}
    
  });
  $("#dialog-no").click(function(){
    $("#dialog-mask").remove();
    $("#dialog").remove();
  });
 
  if(autohide) {
    dialogclose.style.visibility = "hidden";
    window.setTimeout("hideDialog()", (autohide * 1000));
  } else {
    dialogclose.style.visibility = "visible";
  }
}
// hide the dialog box //
function hideDialog() {
//    alert(' test2 ');
    
  var dialog = document.getElementById('dialog');
  clearInterval(dialog.timer);
  dialog.timer = setInterval("fadeDialog(0)", TIMER);
}

// fade-in the dialog box //
function fadeDialog(flag) {
//    alert(' test3 ');

  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);
  }
}

$(document).ready(function(){
    $('#dialog-close').live('click',function(){
    //alert(' test4 ');

        $('#dialog-mask').remove();
        $('#dialog').remove();
    });
    $('#dialog-mask').live('click',function(){
        $('#dialog-mask').remove();
        $('#dialog').remove();
    });
});