 var close_dialog=true;
  var dialog_hist=[];
// global variables //
var TIMER = 3;
var SPEED = 10;
//var WRAPPER = 'content';

// calculate the current window width //


function getDocumentHeight()
{
    return (document.body.scrollHeight > document.body.offsetHeight)?document.body.scrollHeight:document.body.offsetHeight;
}

//Размер документа по горизонтали
function getDocumentWidth()
{
    return (document.body.scrollWidth > document.body.offsetWidth)?document.body.scrollWidth:document.body.offsetWidth;
}

function pageWidth() {
return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
/*
  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 document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
/*
  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,buttons,close_but,autohide,unique) {
  if(!type) {
    type = 'error';
  }
  var dialog;
  var dialogheader;
  var dialogclose;
//  var dialogicon;

  var dialogtitle;
  var dialogcontent;
  var button_block;

    if (!close_dialog)
     {
           dialog_hist.push({title:title,message:message,type:type,buttons:buttons,close_but:close_but,autohide:autohide,unique:unique});
             return;
     }
  close_dialog=false;

  if(!document.getElementById('dialog'))
   {
      if (typeof(DragDrop)!="undefined")
       {
         if (DragDrop.initobj==false)
          {
           DragDrop.InitDragDrop();
              }
            DragDrop.dragpanels["dialog"]="";
         }

    dialog = document.createElement('div');
    dialog.id = 'dialog';

    dialogheader = document.createElement('div');
    dialogheader.id = 'dialog-header';
    dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialog-title';
/*    dialogicon= document.createElement('img');
    dialogicon.id ='dialog-icon';
*/
    dialogclose = document.createElement('div');
    dialogclose.id = 'dialog-close';
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';

    document.body.appendChild(dialog);
    dialog.appendChild(dialogheader);
//    dialogheader.appendChild(dialogicon);

    dialogheader.appendChild(dialogtitle);
    dialogheader.appendChild(dialogclose);

  
    dialog.appendChild(dialogcontent);
  
    if (!close_but)
    {
     dialogclose.style.display="";
    dialogclose.setAttribute('onclick','hideDialog()');
    dialogclose.onclick = hideDialog;   
    } else dialogclose.style.display="none";

    button_block=document.createElement('div');
    button_block.id="dialog-button";
    dialog.appendChild(button_block);


 
  } 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');
 
//   dialogicon =document.getElementById('dialog-icon');
 
    dialog.style.visibility = "visible";

    button_block=document.getElementById('dialog-button');
     }

    dialog.un=unique;

   var old_but=button_block.getElementsByTagName("button");

    for (var i=0; i< old_but.length; i++)
     {
        (old_but[i].parentNode||body).removeChild(old_but[i]);
          i--;
     }

    if ( buttons)
     for (var i=0; i<buttons.length; i++)
      {
       var button=document.createElement('button');
             if (buttons[i].style)
              {
                   set_obj(button.style,buttons[i].style);
              } else  
           button.style.width="90px";
           button.innerHTML=buttons[i].txt;

          button_block.appendChild(button);
   
          button.id="dialog-button"+i;
          button.onclick=(!buttons[i].act)? hideDialog: buttons[i].act;

/*
function (e) 
                                 {  if (e == null)
                                     e=window.event; 
                                      var target = (e.target != null) ? e.target : e.srcElement;
                                       alert(target.id);   
                                        return target.id;
                                 };
*/  
      }
  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;
 // dialogicon.className = type+"_icon";

 dialogcontent.innerHTML = message;
   
  showDialogmask();
    dialog.style.zIndex=10000000000001;

    if (typeof(DragDrop)!="undefined")
     {
  DragDrop._ZIndex++;
dialog.style.zIndex=DragDrop._ZIndex;
     }

  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);

        if (TIMER<=1) fadeDialog(0);
         else
          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";
    close_dialog=true;
    hideDialogmask();
    clearInterval(dialog.timer);

     if  (dialog_hist.length>0 )
             {
                   with (dialog_hist[0])
                   showDialog(title,message,type,buttons,close_but,autohide,unique);
                    dialog_hist.splice(0,1);
              }
  }
}

 function showDialogmask()
  {
    var dialogmask = document.getElementById('dialog-mask');
      
      if (!dialogmask)
      {
       dialogmask = document.createElement('div');
       dialogmask.id = 'dialog-mask';
       document.body.appendChild(dialogmask);
       }

         dialogmask.style.height = getDocumentHeight()+ 'px';
          dialogmask.style.width = getDocumentWidth()+ 'px';
         dialogmask.style.zIndex = 10000000000000;
     dialogmask.style.visibility = "visible";
         dialogmask.className="block";
    if (typeof(DragDrop)!="undefined")
     {
                DragDrop._ZIndex++;
         dialogmask.style.zIndex = DragDrop._ZIndex;
      }
 }

 function hideDialogmask()
  {      var obj;
         if ( (obj=document.getElementById('dialog-mask')) )
              obj.style.visibility = "hidden";
  }
