

var Msg;  // Msg (messageBoxes) is a global function

(function(){
   var boxes = new Array();

 
 Msg = function (type, msg, title, url, inputValue) {
   
   var width = 400;
   var height = 85; // 95
   
   var d = document, abort = function(){};
   var e, id, win, t, b, div, p ,s;
   
   
   function makeAttribute(name, val) {
     var attr           = document.createAttribute(name);
         attr.nodeValue = val;
     
     return attr;
   }
   
   
   function makeArea(id, className) {
     
      var e = document.createElement('div');
      e.setAttributeNode(makeAttribute('id', id));
      e.setAttributeNode(makeAttribute('class', className));
      e.appendChild(document.createTextNode(' '));
      
      return e;
   }
   
   
   function makeButton(id, className, src) {
     
     var e = d.createElement('img');
     e.setAttributeNode(makeAttribute('id', id));
     e.setAttributeNode(makeAttribute('class', className));
     e.setAttributeNode(makeAttribute('src', src));
     
     return e;
   }
   
   
   function keyDown(e) {
     e = e || event;
     if((e.charCode || e.keyCode) !== 27) return;
     abort();
   }
   
   
   function close(w_id) {
     var remove = $('#'+w_id)[0],
         parent = remove.parentNode;
     parent.removeChild(remove);
     $(document).unbind('keydown', keyDown);
     $('#disableDesktop').attr('style', '').css('display', 'none');
     if(Msg && typeof(Msg.onClose) === 'function') // global onclose Callback (used in AUT)
       Msg.onClose();
   }
   
   
   function find() {
     var id = 'msg' + (boxes.length + 1).toString();
     boxes.push(id);
     
     return id;
   }
   
   
   var _Width = 0;
   var _Height = 0;
   var clientWidth = 0;
   var clientHeight = 0;
   
   
   function startDrag() {
     
     clientWidth =  $(document).width(id);
     clientHeight = $(document).height(id);
     _Width  = $('#'+id).xWidth();
     _Height = $('#'+id).xHeight();
     
   }
   
   function drag(mdx, mdy) {
     var w_id = this.parentNode.id;
     var xpos = $('#'+id).xLeft() + mdx;
     var ypos = $('#'+id).xTop() + mdy;
     
     if(xpos > 0 && ypos > 0 && xpos + _Width < clientWidth && ypos + _Height < clientHeight) {
       $('#'+w_id).xMoveTo(xpos, ypos);
     }
   }
   
   
   function paint(w_id) {
     var wObj = $('#'+w_id);
     var cBtn  = $('#'+w_id + 'CloseBtn');
     cBtn.xMoveTo( wObj.xWidth() - cBtn.xWidth() - 6, 1);
   }
   
   var id = find(), fb, input = false;
   
   var clearer = d.createElement('div');
   clearer.style.clear = 'both';
   clearer.style.height = '5px';
   
   
   var noSelect = ' unselectable="on" class="unSelectable" ';
   win = d.createElement('div');
   win.setAttribute('id', id);
   win.className = 'MBox ' + 'Msg_' + type;
   win.onclick = function() {
     if($('#'+id)[0]) {
       fb = document.getElementById('FocusBtn').focus();
       $('#'+id).css('zIndex', 9999);
     }
   };
   
   
   $(document).bind('keydown', keyDown);
   
   assert($.stopPropagation, 'stopPropagation not found, load jQuery.swm');
   
   $(win).bind('click', $.stopPropagation).bind('mousedown', $.stopPropagation); // muss
   
 
   e = makeArea(id + 'Bar', 'Title Bar');
   var img = '<img style="margin-left: 3px;" src="/templates/images/object_CMS_about_16x16.png" alt="" />';
   e.innerHTML = img+'<span' + noSelect + '>' + title + '</span>';
   $(e).enableDrag(startDrag, drag, null);
   win.appendChild(e);
   
   e = makeButton(id + 'CloseBtn', 'MBtn', '/templates/images/swm_close.jpg');
   e.onclick = function() { close(id); }
   win.appendChild(e);
   
   e = makeArea(id + 'Content', 'Msg');
   
   s = d.createElement('div');
   p = document.createElement('div');
   p.setAttribute('unselectable', 'on');
   p.className = 'unSelectable';
   
   p.innerHTML = msg.replace(/\n/g, '<br/>');
   s.appendChild(p);
   s.className = 'Msg Text';
   e.appendChild(s);
   
   if(type == 'error' || type == 'warning' || type == 'notice' || type == 'io') {
         div = d.createElement('div');
         b = d.createElement('button');
         abort = b.onclick = function() { close(id); }
          t = d.createTextNode('OK');
         b.className = 'MsgBtn';
         b.setAttribute('id', 'FocusBtn');
         b.appendChild(t);
       div.appendChild(b);
       div.appendChild(clearer);
       e.appendChild(div);
   } else if(type == 'confirm' || type == 'confirmOut' || type == 'confirmClient') {
       if(type == 'confirmOut')
         $('#disableDesktop').css('backgroundColor', '#AAA');
       div = d.createElement('div');
         b = d.createElement('button');
         var ajax = function() {
           Loader.request(url);
           close(id);
         };
         var linkout = function () {
             location.href = url;
         };
         
         for(var object in url) {
           if(object != 'click') { b[object] = url[object];}
         }
         
         var clientDo = function() {
           if(typeof(url) == 'function') {
             url(); // callback on o.k.
           } else {
             eval(url.click);
           }
           close(id);
         }
         abort = b.onclick = (type === 'confirm') ? ajax : 
                                                   (type === 'confirmOut') ? linkout :
                                                                             clientDo;
         t = d.createTextNode('OK');
         b.className = 'MsgBtn';
         b.appendChild(t);
       div.appendChild(b);
         
         b = d.createElement('button');
         b.onclick = function() {
           close(id);
           if(typeof(inputValue) == 'function') // optional callback on Abort;
             inputValue();
         };
         t = d.createTextNode('Abbrechen');
         b.className = 'MsgBtn';
         b.setAttribute('id', 'FocusBtn');
         b.appendChild(t);
       div.appendChild(b);
       div.appendChild(clearer);
      e.appendChild(div);
     
   } else if(type == 'prompt') {
     var di = d.createElement('div'), ok;
       input = d.createElement('input');
       di.style.overflow = 'auto';
       di.appendChild(input);
       input.setAttribute('type', 'text');
       input.value = inputValue;
       input.select();
       input.onclick = function(e) { $.stopPropagation(e); return true; }
       input.onkeydown = function(e) { e = e || event; if((e.charCode || e.keyCode) == 13) ok.onclick(); return true; } // send on enter key
       
       s.appendChild(di);
       div = d.createElement('div');
       div.className = 'MsgBtnContext';
         ok = d.createElement('button');
         ok.onclick = function() {
           if(typeof(url) == 'function') {
             url(input.value);
           } else {
             newUrl = url + '&promptValue=' + encodeURI(input.value);
             Loader.request(newUrl);
           }
           close(id);
         }
         
         t = d.createTextNode('OK');
         ok.setAttribute('class', 'MsgBtn');
         ok.className = 'MsgBtn';
         ok.setAttribute('id', 'FocusBtn');
         ok.appendChild(t);
       div.appendChild(ok);
         
         b = d.createElement('button');
         abort = b.onclick = function() { close(id); }
         t = d.createTextNode('Abbrechen');
         b.className = 'MsgBtn';
         b.appendChild(t);
       div.appendChild(b);
       div.appendChild(clearer);
     e.appendChild(div);
   }
   
   
   s = document.createElement('form')
   s.setAttribute('action', 'javascript:Void();'); 
   s.appendChild(e);
   
   win.appendChild(s);
   
   $('#disableDesktop').css('display', '');
   $('#desktop')[0].appendChild(win);
   
   // console.log('p height: '+$(p).xHeight());
   height += $(p).xHeight();
   // alert('height:'+height);
   $(win).xResizeTo(width).css('height','auto');
   //console.log(width);
   $('#'+id).xMoveTo(($(document).width() - width) / 2, ($(document).height() - height) / 2);
   
   paint(id);
   $('#'+id).css('visibility', 'visible').css('zIndex', 9999);
   
   if(input)
     input.select();
   
   try {
     document.getElementById('FocusBtn').focus();
   } catch(e) {
   }
  
 };
 
 
})();

