/**
*
*  uno.dialog.js 
*  by Hideki Yamamoto eu-gsm[+393453332248]
*  Version Alpha 0.0.3.0
*  This file is part of the uno project.
*  The user must agree with the terms specified in
*
*  İHideki Yamamoto, all rights reserved.
*
*  Licence       : Http://www.unodot.net/framework/current/uno.licence.txt
*  Documentation : Http://www.unodot.net/framework/current/uno.js.usage.txt
*  History       : Http://www.unodot.net/framework/current/uno.js.history.txt
*
**/
Namespace.register("uno.dialog");
uno.dialog = {};
/////////////////////////////////GLOBALS//////////////////////////////////
uno.dialog.timer = 5;
uno.dialog.speed = 10;
uno.dialog.wrapper = 'aux';
/////////////////////////////////PRIVATE//////////////////////////////////
uno.dialog.lasthandler = {};
uno.dialog.yeshandler = function(){uno.dialog.hide();uno.dialog.lasthandler(true);};
uno.dialog.nohandler = function(){uno.dialog.hide();uno.dialog.lasthandler(false);};
uno.dialog.okhandler = function(){uno.dialog.hide();uno.dialog.lasthandler($('uno-dialog-prompt-input').value);};
uno.dialog.cancelhandler = function(){uno.dialog.hide();uno.dialog.lasthandler(false);};
/////////////////////////////////COMMON//////////////////////////////////
// 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 //
uno.dialog.show = function(title,message,type,autohide,handler) {
  if(!type) {type = 'error';}
  var dialog;var dialogheader;var dialogclose;var dialogtitle;var dialogcontent;var dialogmask;var buttonyes;var buttonno;var inputprompt;
  if(!document.getElementById('uno-dialog')) {
    dialog = document.createElement('div');dialog.id = 'uno-dialog';
    dialogheader = document.createElement('div');dialogheader.id = 'uno-dialog-header';
    dialogtitle = document.createElement('div');dialogtitle.id = 'uno-dialog-title';
    dialogclose = document.createElement('div');dialogclose.id = 'uno-dialog-close'
    dialogcontent = document.createElement('div');dialogcontent.id = 'uno-dialog-content';
    dialogmask = document.createElement('div');dialogmask.id = 'uno-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','uno.dialog.hide()');
    dialogclose.onclick = uno.dialog.hide;        
      buttonyes = document.createElement('input');
      buttonyes.id = 'uno-dialog-button-yes';
      buttonyes.type= 'button';
      buttonno = document.createElement('input');
      buttonno.type= 'button';
      buttonno.id = 'uno-dialog-button-no';   
      inputprompt = document.createElement('input');
      inputprompt.id = 'uno-dialog-prompt-input';
      inputprompt.type= 'text';
  } else {
    dialog = $('uno-dialog');
    dialogheader = $('uno-dialog-header');
    dialogtitle = $('uno-dialog-title');
    dialogclose = $('uno-dialog-close');
    dialogcontent = $('uno-dialog-content');
    dialogmask = $('uno-dialog-mask');
    dialogmask.style.visibility = "visible";
    dialog.style.visibility = "visible";
    inputprompt = $('uno-dialog-prompt-input');buttonyes = $('uno-dialog-button-yes');buttonno = $('uno-dialog-button-no');
  }
  uno.dialog.lasthandler = handler;
  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;
  buttonno.style.display='none';
  buttonyes.style.display='none';
  inputprompt.style.display='none';
  if (type=='confirm'){
      buttonno.setAttribute('onclick','uno.dialog.nohandler();');
      buttonno.value='no';buttonno.style.display='';
      buttonyes.setAttribute('onclick','uno.dialog.yeshandler();');
      buttonyes.value='yes';buttonyes.style.display='';
  }else if(type=='prompt'){
      inputprompt.value='';
      buttonno.setAttribute('onclick','uno.dialog.cancelhandler();');
      buttonno.value='cancel';buttonno.style.display='';
      buttonyes.setAttribute('onclick','uno.dialog.okhandler();');
      buttonyes.value='ok';buttonyes.style.display='';
      inputprompt.style.display='';
  }
  dialogcontent.appendChild(inputprompt);
  dialogcontent.appendChild(buttonyes);
  dialogcontent.appendChild(buttonno);
  var content = document.getElementById(uno.dialog.wrapper);
  dialogmask.style.height = content.offsetHeight + 'px';
  dialog.timer = setInterval("uno.dialog.fade(1)", uno.dialog.timer);
  if(autohide) {
    dialogclose.style.visibility = "hidden";
    window.setTimeout("uno.dialog.hide()", (autohide * 1000));
  } else {
    dialogclose.style.visibility = "visible";
  }
};

// hide the dialog box //
uno.dialog.hide = function() {
  var dialog = document.getElementById('uno-dialog');
  clearInterval(dialog.timer);
  dialog.timer = setInterval("uno.dialog.fade(0)", uno.dialog.timer);
};

// fade-in the dialog box //
uno.dialog.fade = function(flag) {
  if(flag == null){flag = 1;}
  var dialog = document.getElementById('uno-dialog');
  var value;
  if(flag == 1){value = dialog.alpha + uno.dialog.timer;}
  else {value = dialog.alpha - uno.dialog.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('uno-dialog-mask').style.visibility = "hidden";
    clearInterval(dialog.timer);
} };