var maskopen = 0;

var Dialog = new Class({
  initialize: function(){
    this.container = null;
    this.id = null;
  },
  seal: function(){
    this.id = generateLine();
  },
  dialogAlert: function( msg ){
    this.id = generateLine();
    showMask();
    var div = this.createdialogcontainer( 350,120, msg, null, 'okay' );
    document.getElementsByTagName("body")[0].appendChild( div );
    $("closebt_"+this.id).addEvent('click', function( parent ){
      parent.hidenewdialogcontainer(); hideMask(); return false;
    }.pass( this ) );
  },  
  dialogAlertTimeout : function (msg, timeout){
    this.id = generateLine();
    var div = this.createdialogcontainer( 430,120,msg );
    document.getElementsByTagName("body")[0].appendChild( div );
    this.hidenewdialogcontainer.delay(timeout, this);
  },
  createdialogcontainer : function( width, height, msg, title, type ){
    this.container = $(document.createElement('div'));
    var div = document.createElement('div');
    this.container.setAttribute('id', this.id );
    if( browser.isIE && browser.version <= 6 ){
      var center = browser.getAdjustedCenter();
      var x = center.x - (width/2);
      var y = center.y - (height/2);
      this.container.setStyle('position', 'absolute');
      this.container.setStyle('top', y-50 );
      this.container.setStyle('left', x );
    }else{
      var center = browser.getCenter();
      var x = center.x - (width/2);
      var y = center.y - (height/2);
      this.container.setStyle('position', 'fixed');
      this.container.setStyle('top', y-50 );
      this.container.setStyle('left', x );
    }
    this.container.setStyle('z-index', 2000);
    this.container.setStyle('opacity', 0.9);
    this.container.setStyle('display', 'block');
    this.container.setStyle('opacity', 1.0);

    var width1 = width - 4;
    var width2 = width - 2;
    var width3 = width - 0;
    if( !msg ) msg = "";

    var html = ''+
      '<div class="round_header" style="width:'+width+'px;">'+
      ' <span class="round_header1" style="width:'+width1+'px;background-color:#333"></span>'+
      ' <span class="round_header2" style="width:'+width2+'px;background-color:#333"></span>'+
      '  <div class="round_header_content" style="width:'+width3+'px;background-color:#333;color:#FFF;">';
    if( title ){
      html += ''+
      '   <div class="round_header_title" style="width:'+width3+'px">'+title+'</div>';
    }
    html += ''+
      '   <div style="padding-left:0px;padding-top:10px;padding-bottom:5px;text-align:center" id="_messagefield_' + this.id + '">'+ msg + '<p>';
    if( type && type == 'okay' ){
      html += '<div style="padding-top:3px;">'+
              '<a href="#" id="closebt_'+ this.id +'" class="hoverlink" style="font-size:8pt;color:#fff">Okay</a>' +
              '</div>';
    }
    html += ''+
      '   </div>'+
      '  </div>'+
      ' <span class="round_header2" style="width:'+width2+'px;background-color:#333;"></span>'+
      ' <span class="round_header1" style="width:'+width1+'px;background-color:#333"></span>'+
      '</div>';
    this.container.innerHTML = html;
    return this.container;
  },
  hidenewdialogcontainer : function(){
    var dialog = document.getElementById(this.id);
    if(dialog){
        var fx = new Fx.Morph( $(dialog), {duration: 500, transition: Fx.Transitions.linear });
        fx.start({ opacity:0 }).chain( function(){ document.getElementsByTagName("body")[0].removeChild(dialog); } );
    }
  },
  _createTextBox : function(msg, width, height){
    var table = document.createElement('table');
    var tbody = document.createElement('tbody');
    var tr = document.createElement('tr');
    var td = document.createElement('td');
    tr.appendChild(td);
    tbody.appendChild(tr);
    table.appendChild(tbody);
    $(table).setStyle("width", width );
    $(table).setStyle("height", height );
    $(td).setStyle("text-align", "center");
    td.innerHTML = msg;
    return table;
  },
  _createYesNo: function (handler){
    var table = document.createElement('table');
    var tbody = document.createElement('tbody');
    var tr = document.createElement('tr');
    var td1 = document.createElement('td');
    var td2 = document.createElement('td');
    tr.appendChild(td1);
    tr.appendChild(td2);
    tbody.appendChild(tr);
    table.appendChild(tbody);
    table.setAttribute("align", "center");
    $(table).setStyle("width", "100");
    $(td1).setStyle("text-align", "center");
    $(td2).setStyle("text-align", "center");
    td1.appendChild(this._createButton(getText("button_Yes"), handler ));
    td2.appendChild(this._createButton(getText("button_No"), handler ));
    return table;
  },
  createButton : function( name, handler, params ){
    this.args = params;
    return this._createButton( name, handler );
  },
  _createButton : function( name, handler ){
    var button = document.createElement('input');
    button.setAttribute("type", "button");
    button.setAttribute("value", name);
    $(button).setStyle("border", "1px solid #666");
    $(button).setStyle("padding", "2px");
    var obj = new Object();
    obj._dialog = this;
    obj.name = name;
    obj.handler = handler;
    button.onclick = function( _obj ){
        hideMask();
        _obj._dialog.hidenewdialogcontainer();
        if(_obj.handler) _obj.handler(_obj.name, _obj._dialog.args);
    }.pass( obj );
    return button;
  },
  _dialogConfirm : function ( msg, handler ){
    this.id = generateLine();
    showMask();
    var width = 320;
    var height = 80;
    var container = this.createdialogcontainer( width, height );
    document.getElementsByTagName("body")[0].appendChild( container );
    var div = $('_messagefield_' + this.id );
    div.appendChild( this._createTextBox(msg, width, height-40) );
    div.appendChild( this._createYesNo(handler) );
  },
  confirmSubmit : function( form, handler, args, msg ){
    this.args = args;
    this.handler = handler;
    this.custommsg = msg;
    this.form = form;
    if( this.custommsg != null ){
       this._dialogConfirm( this.custommsg, this._confirmSubmitHandler.bind(this) );
    }else{
       this._dialogConfirm( getText("form_confirm"), this._confirmSubmitHandler.bind(this) );
    }
    return false;
  },
  _confirmSubmitHandler : function( value ){ 
    var result = null;
    if(value == 'Yes'){
      result = true;
      if(this.form) this.form.confirm.value = "true";
      if(!this.handler && this.form ) {
        this.form.submit();
        return;
      }
    }else{
      result = false;
      if(this.form) this.form.confirm.value = "false";
    }
    if(this.handler) this.handler(result, this.args);
  }
});


function hideMask(){
    if( maskopen != 1 ) {
      maskopen--;
      return false;
    }
    var id = '_windowmask_';
    var mask = document.getElementById(id);
    if(mask){
        maskopen = false;
        var fx = new Fx.Morph( $(mask), {duration: 600, transition: Fx.Transitions.linear} );
        fx.start({ opacity:0 }).chain( function(){  document.getElementsByTagName("body")[0].removeChild(mask); } );
        window.onresize = null;
        window.onscroll = null;
    }
}

function showMask(_opacity){
    if( maskopen != 0 ){
      maskopen++;
      return false;
    }
    maskopen = 1;
    var dimensions = browser.getPageDimensions();
    var width  = dimensions.width;
    var height = dimensions.height;
    var id = '_windowmask_';
    var mask = document.createElement('div');
    mask.setAttribute('id', id);
    $(mask).setStyle('width',width);
    $(mask).setStyle('height', height);
    $(mask).setStyle('position', 'absolute');
    $(mask).setStyle('top', '0px');
    $(mask).setStyle('left', '0px');
    $(mask).setStyle('background', "url('"+statichost+"/images/global/maskbg.gif') #000"); 
    $(mask).setStyle('z-index', 1000);
    $(mask).setStyle('opacity', 0.0);
    var fx = new Fx.Morph( $(mask), {duration: 200, transition: Fx.Transitions.linear });
    if( _opacity ){
      fx.start({ opacity:_opacity+"" });
    }else{
      fx.start({ opacity:0.6 });
    }
    document.getElementsByTagName("body")[0].appendChild(mask);
    window.onresize = function (){
        var d = browser.getPageDimensions();
        var w = d.width;
        var h = d.height;
        $(id).setStyle('width',w);
        $(id).setStyle('height',h);
    }
    window.onscroll = function (){
        var d = browser.getPageDimensions();
        var w = d.width;
        var h = d.height;
        $(id).setStyle('width',w);
        $(id).setStyle('height',h);
    } 
}


/**************************************************************/

function create_iframe( id, width, height, src, title ){
  if( $(id) != null ) return;

  var container = document.createElement('div');
  $(container).setAttribute("id", id);
  $(container).setStyle("background-color", "#000");
  $(container).setStyle("width", width+"px");
  $(container).setStyle("height", height+"px");
  $(container).setStyle("border", "1px solid #1644c7");
  $(container).setStyle("position", "absolute");
  $(container).setStyle("z-index", "5000");
  var coor = browser.getAdjustedCenter();
  $(container).setStyle("top", coor.y - (height/2) );
  $(container).setStyle("left", coor.x - (width/2) );

  var safety = document.createElement('textarea');
  $(safety).setAttribute('id', id + '_safety');
  $(safety).setStyle("overflow", "hidden" );
  $(safety).setStyle("background-color", "#000");
  $(safety).setStyle("border", "1px solid #000");
  $(safety).setStyle("width", width+"px");
  $(safety).setStyle("height", height+"px");
  $(safety).setStyle("position", "absolute");
  $(safety).setStyle("z-index", "500");
  $(safety).setStyle("top", coor.y - (height/2) );
  $(safety).setStyle("left", coor.x - (width/2) );

  var closeclick = "close_iframe('" + id + "');return false";

  var html = '<table class="iframebar" cellpadding="4" width="100%"><tr>'+
             '<td><b>'+title+'</b></td>'+
             '<td align="right">'+
             '<a href="#" class="whitelink" onclick="' + closeclick + '">Close</a></td>'+
             '</tr></table>';
  html += '<iframe id="'+id+'frame" scrolling="no" frameborder="0" style="background-color:#000" '+
          '        width="'+(width-5)+'" height="'+(height-30)+'" src="' + src + '"></iframe>';
  $(container).innerHTML = html;
  document.getElementsByTagName("body")[0].appendChild( safety );
  document.getElementsByTagName("body")[0].appendChild( container );
}


function close_iframe( id ){
  if( $(id) != null ) document.getElementsByTagName("body")[0].removeChild( $(id) );
  if( $(id+'_safety') != null ) document.getElementsByTagName("body")[0].removeChild($(id + '_safety'));
}




/****************************************************************/

/*
function dialogValidate(handler, args){
    this.args = args;
    showMask();
    msg = "Are you sure you want to do this?";
    var width = 320;
    var height = 80;
    var container = createDialogContainer(width, height);
    var td = container.getElementsByTagName("tbody")[0].getElementsByTagName("tr")[0].getElementsByTagName("td")[0];
    var text = createTextBox(msg, width, height-40);
    td.appendChild(text);
    td.appendChild(createYesNo(handler));
    $(container).setStyle("opacity", "0");
    var fx = new Fx.Morph( $(container), {duration: 300, transition: Fx.Transitions.linear });
    fx.start({ opacity:1 });
    document.getElementsByTagName("body")[0].appendChild(container);
}
*/

function dialogAlertTimeout(msg, timeout){
   new Dialog().dialogAlertTimeout(msg, timeout);
}
function dialogAlert(msg){
   new Dialog().dialogAlert( msg );
}
function confirmSubmit( form, handler, args, msg ){
  new Dialog().confirmSubmit(form,handler,args,msg);
}