/** ********************************************************************************
 *
 * Javascript Dialogs based on Prototype JS framework (http://www.prototypejs.org/) 
 *
 *    Author : SFX
 *       Web : http://sfx.elife.lv
 *       FAQ : http://sfx.elife.lv/js-prototypejs-dialogs/
 *   Version : v1.0
 *  
******************************************************************************** **/

var WindowsNumber   = 1;
var StyleError      = 'err';
var StyleInfo       = 'inf';
var StyleOverlay    = 'overlay';
var BodyDimensions  = 'body_dimensions';

var Dialog = Class.create({
  initialize: function(html, params) {
    this.html           = html;
    this.params         = new Object();
    this.params.height  = (params.height) ? params.height   : 'auto';
    this.params.width   = (params.width)  ? params.width    : (this.params.style == StyleError) ? 400 : 300;
    this.params.style   = (params.style)  ? params.style    : 'inf';
    this.params.title   = (params.title)  ? params.title    : '' ;
    this.params.transp  = (params.transp) ? true            : false ; 
    this.params.id      = (params.id)     ? params.id+'_dialog' : 'win_dialog_' + WindowsNumber;
    this.render();
  },

  render: function() {

    // Render dialog container
    vDialog  = this._dialog();    
    document.body.appendChild(vDialog);
    ScreenDimensions      = {height: screen.height, width: screen.width} 
    height                = ScreenDimensions.height;
    width                 = ScreenDimensions.width;
      
    if(this.params.style == StyleError){

      vOverlay = this._overlay();
      document.body.appendChild(vOverlay);
      vOverlay.style.height = height + 'px';
      vOverlay.style.width  = '100%';       

      vDialog.innerHTML   = this._error_body();
      vDialog.style.zIndex  = 1001;
      
    }else{

      vOverlay = this._overlay();
      document.body.appendChild(vOverlay);
      vOverlay.style.height = height + 'px';
      vOverlay.style.width  = '100%';
      vDialog.innerHTML   = this._body();
      vDialog.style.zIndex  = 1001;
     
    }

    vDialog.style.width   = this.params.width + 'px';
    
    DialogDimensions      = $('win_content_leyout_'+WindowsNumber).getDimensions();
     
    calcHeight            = ScreenDimensions.height / 2 - (DialogDimensions.height / 2) + 100;
    calcWidth             = ScreenDimensions.width / 2 - (DialogDimensions.width / 2);
    vDialog.style.left    = (calcWidth < 50) ? 50 : Math.round(calcWidth) + 'px';
    vDialog.style.top     = (this.params.style == StyleError) ? 300 + 'px' : 200 + 'px';
    
    // Make movable
    new Draggable(vDialog,{scroll : window,
                              handle : $('WinMovePoint_'+WindowsNumber)
                             });
    
    WindowsNumber++;
  },
  
  _dialog: function(){
    if($(this.params.id)) this._close();
    
    this.win            = document.createElement('div');
    this.win.id         = this.params.id;
    this.win.className  = 'win_dialog'; 
    return this.win;
  },
  
  _body: function(){
    height    = (this.params.height != 'auto') ? 'height:'+height + ' px' : ''; ;
    width     = 'width:' + this.params.width - 3 + ' px';
    title     = (this.params.style == 'err') ? '<b>Error!</b>' : this.params.title;
     
    bodyHtml  = "<div id=WinMovePoint_"+WindowsNumber+" class='win_dialog_header "+this.params.style+"'>";
    bodyHtml +=   "<div class='win_top_title move'>"+title+"</div>";
    bodyHtml +=   "<div class='win_top_button'>";
    bodyHtml +=     "<button class='close' onclick='closeDialog(\""+this.params.id+"\")'>&nbsp;</button>";
    bodyHtml +=     "</div><div style='font-size:0px;clear:both;height:0px'></div>";
    bodyHtml +="</div>";
    bodyHtml +="<div id='win_content_leyout_"+WindowsNumber+"' class='win_content bg_' style=' "+height+"; "+width+";'>";
    bodyHtml +=    this.html;
    bodyHtml +="</div>";    
    return bodyHtml;
  },
  
  _error_body: function(){
    bodyHtml = "<div id=WinMovePoint_"+WindowsNumber+" class='win_dialog_header "+StyleError+"'>";
    bodyHtml +=   "<div class='win_top_title move'><b>Error!</b></div>";
    bodyHtml +=   "<div class='win_top_button'>";
    bodyHtml +=     "<button class='close' onclick='closeDialog(\""+this.params.id+"\")'>&nbsp;</button>";
    bodyHtml +=     "</div><div style='font-size:0px;clear:both;height:0px'></div>";
    bodyHtml +="</div>";
    bodyHtml +="<div id='win_content_leyout_"+WindowsNumber+"' class='win_content bg_' style='width: "+(this.params.width - 20)+" px; padding:20px; text-align:center;'>";
    bodyHtml +=    this.html;
    bodyHtml +="</div>";
    return bodyHtml;
  },
  
  _overlay: function(){
    win_over              = new Object();
    win_over              = document.createElement('div');
    win_over.id           = StyleOverlay; 
    win_over.innerHTML    = '&nbsp;';  
    return win_over;
  },    
  
  _close: function(){
    $(this.params.id).remove();
  } 
});

function closeDialog(id){
    if($(id)){ $(id).remove(); }
    if($(StyleOverlay)) { $(StyleOverlay).remove(); }
}

