var D = false;

function Dialog(txt,buttons){
  this.buttons =  buttons ? buttons : {};
  this.txt     =  txt ? txt : "Opps no text was set";
  var ins = this;
  ins.Overlay();                
  ins.View();
}

Dialog.prototype.Overlay = function(){
    var dh = $(document).height() < $(window).height() ?  $(window).height() : $(document).height();
    if(!$(".DialogOverlay").length){
      var r  = '<div class="DialogOverlay" style="width:100%;position:absolute;z-index:100;height:'+dh+'px;top:0;left:0;"></div>';
      $(r).appendTo("body");
    }else{
      $(".DialogOverlay").css('height',dh+'px');
    }
    
    if($.browser.msie && ($.browser.version == "8.0" || $.browser.version == "7.0") ){
      $(".DialogOverlay").css('opacity','0.6');
    }
    
}

Dialog.prototype.View = function(){
  var timer = new Date().valueOf();
  var ins = this;
  var t   = '';
  var i   = 0;
  $(this.buttons).each(function(e){        
    if(this.v != "" && this.a){
      i++;
      t += '<a id="DialogButton'+i+timer+'" href="javascript:void(0);">'+this.v+'</a>';
    }
  });
  
  var r = '<div class="DialogDataCont">'+
            '<div class="DialogData">'+
              '<div class="DialogClose"></div>' +
              '<div class="DialogText">'+ins.txt+'</div>' +
              '<div class="DialogButtons">'+t+'</div>' +
            '</div>'+
          '</div>';
          
  $(r).appendTo("body");  
  
  $('.DialogClose').unbind('click').click(function(){
    D.Dismiss();
  });
  
  i  = 0;
  $(this.buttons).each(function(e){        
    if(this.v != "" && this.a){
      i++;
      $('#DialogButton'+i+timer).unbind('click').click(this.a);
    } 
  });  
  
      if($.browser.msie && ($.browser.version == "8.0" || $.browser.version == "7.0") ){
        $(".DialogButtons a").corner().css('paddingLeft','10px').css('paddingRight','10px');
        $(".DialogData").corner("cc:#E0E0E0");
        $(".DialogClose").corner("cc:#E0E0E0 tl tr br").corner("cc:#fff bl");
      }  
  
  ins.CenterView();
}

Dialog.prototype.CenterView = function(){
  var dh = $('.DialogData').height();
  var dw = $('.DialogData').width();
  var wh = $(window).height();
  
  $('.DialogData').css({
    top: parseInt((wh - dh) / 2,10) + 'px',
    visibility : 'visible'
  });
}

Dialog.prototype.Dismiss = function(){
  $('.DialogDataCont,.DialogOverlay').animate({opacity:0},'normal',false,function(){
    $('.DialogDataCont,.DialogOverlay').remove();
  });
  D = false;
}

Dialog.prototype.DismissAndRemove = function(elem){
  
  $('.DialogDataCont,.DialogOverlay').animate({opacity:0},'normal',false,function(){
    $('.DialogDataCont,.DialogOverlay').remove();
    $(elem).effect('shake',{},'fast',function(){
      $(this).remove();
    });  
  });
  D = false;
}

