var Chat = {
        W: undefined,
        open: function () {
                this.create();
                return false;
        },
        create: function () {
                var width = 1050;
                var height = 1000;
                var leftPx = ( screen.availWidth - width ) / 2; 
                var topPx = ( screen.availHeight - height ) / 2; 
                var params = "width=" +width+ ", height=" +height+ ", resizable=yes, scrollbars=yes, top=" +topPx+ ", left=" +leftPx; 
                this.W = window.open('/chat/#', 'chat', params);
                this.W.focus();
        },
        addContact: function (userId) {
                this.create();
                if(Chat.W.AddContact) {
                        this.W.AddContact(userId);
                } else {
                        this.onLoad = function () {
                                Chat.W.AddContact(userId);
                                Chat.onLoad = function () { };
                        }
                }
                return false;
        },
        onLoad: function () { },
     popup: function (win) {
//           ModalDialog.show('<b>' + name + '</b> invite you to <a href="#" onclick="Chat.open(); return ModalDialog.close();">chat</a>.', 300);
          ModalDialog.show(win, 230);
     }
}

var ModalDialog = {
     que: [],
//      bg: undefined, 
     md: undefined,
     ww: undefined,
     visible: false,
     show: function (content, width) {
          if(this.visible) {
//                this.que.push([content, width]);
               return false;
          }
          
          this.visible = true;
          var body = document.getElementsByTagName('body')[0];
          this.ww = width;
//           this.bg = document.createElement('div');
//           this.bg.setAttribute('class', 'md_bg');
//           this.bg.onclick = function () {
//                ModalDialog.close();
//           }
          this.md = document.createElement('div');
          this.md.setAttribute('class', 'md_w');
          var wh = document.createElement('div');
          wh.setAttribute('class', 'md_wh');
          wh.innerHTML = '<span>calling you in chat</span> <a href="#" onclick="return ModalDialog.close();" id="___CLOSE">hide</a>';
          this.md.appendChild(wh);
          
          var wc = document.createElement('div');
          wc.setAttribute('class', 'md_wc');
          wc.id = '__invite';
          wc.innerHTML = content;
          this.md.appendChild(wc);
          
          this.md.style.width = (width ? width : 300) + 'px';
          
          // positioning
          this.pos(true);
     
          // append
          body.appendChild(this.md);
//           body.appendChild(this.bg);
          
          return false;
     },
     pos: function (onscroll) {
          if(/*!this.bg || */!this.md) {
               return;
          }
          
          // horisontal positioning
          var vpw = (typeof window.innerWidth != 'undefined') ? window.innerHeight : document.documentElement.clientHeight;
          var wv = document.body.parentNode.scrollWidth;
          if ((vpw > document.body.parentNode.scrollWidth) && (vpw > document.body.parentNode.clientWidth)) {
               wv = vpw;
          } else if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
               wv = document.body.parentNode.clientWidth;
          }
          this.md.style.left = (wv / 2 - 450) + 'px';
          
          // vertical positioning
          var ScrollTop = document.body.scrollTop;
          if (ScrollTop == 0) {
               ScrollTop = (window.pageYOffset) ? window.pageYOffset : ((document.body.parentElement) ? document.body.parentElement.scrollTop : 0);
          }
          
          var vph = (typeof window.innerWidth != 'undefined') ? window.innerHeight : document.documentElement.clientHeight;
          var wh = document.body.parentNode.scrollHeight;
          
          if ((vph > document.body.parentNode.scrollHeight) && (vph > document.body.parentNode.clientHeight)) {
               wh = vph;
          } else if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
               wh = document.body.parentNode.clientHeight;
          }
//           this.bg.style.height = wh + 'px';
          this.md.style.top = (ScrollTop + 50) + 'px';
          
          if(onscroll) {
               window.onscroll = function () {
                    ModalDialog.pos();
               }
          }
     },
     close: function () {
          this.visible = false;
          
          var body = document.getElementsByTagName('body')[0];
//           body.removeChild(this.bg);
          body.removeChild(this.md);
          
          if(this.que.length > 0) {
               var NW = this.que.pop();
               this.show(NW[0], NW[1]);
          }
          return false;
     }
}
