
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) {
     
     if(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);
     }
   };
   
   $('#desktop')[0].appendChild(win);
   $(document).bind('keydown', keyDown);
   
   assert(SWO.Event.stopPropagation, 'stopPropagation not found, load jQuery.swm');
   
   $(win).bind('click', SWO.Event.stopPropagation).bind('mousedown', SWO.Event.stopPropagation).bind('contextmenu', SWO.Event.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.toString().replace(/\n/g, '<br/>');
   s.appendChild(p);
   s.className = 'Msg Text';
   e.appendChild(s);
   
   f = document.createElement('form');
   f.setAttribute('action', 'javascript:void(null);'); 
   f.appendChild(e);
   
   win.appendChild(f);
   
   $('#disableDesktop').css('display', '');
   height += $(p).xHeight();
   $(win).xResizeTo(width).css('height','auto');
   $('#'+id).xMoveTo(($(document).width() - width) / 2, ($(document).height() - height) / 2);
   $('#'+id).css('visibility', 'visible').css('zIndex', 9999);
   
   if(type == 'error' || type == 'warning' || type == 'notice' || type == 'io') {
     
     div = d.createElement('div');
     b = d.createElement('button');
     abort = b.onclick = function() { close(id);
       if(url) { location.href = url; }
     };
     t = d.createTextNode('OK');
     b.className = 'MsgBtn';
     b.setAttribute('id', 'FocusBtn');
     b.appendChild(t);
     
     div.appendChild(b);
     div.appendChild(clearer);
     e.appendChild(div);
     b.focus();
     
   } else if(type == 'confirm' || type == 'confirmOut' || type == 'confirmClient') {
     
     if(type == 'confirmOut')
       $('#disableDesktop').css('backgroundColor', '#AAA');
     
     div = d.createElement('div');
     e.appendChild(div);
     b = d.createElement('button');
     div.appendChild(b);
     
     switch(type) {
       
       case 'confirm':
         b.onclick = function() { Loader.request(url); close(id); };
         break;
         
       case 'confirmOut':
         b.onclick = function () { location.href = url; };
         break;
         
       default:
         b.onclick = function() {
           
           if(typeof(url) == 'function') {
             url(); // callback on o.k.
           } else {
             eval(url.click);
           }
           
           close(id);
           
         };
       
     }
     
     for(var object in url) {
       if(object != 'click') { b[object] = url[object];}
     }
     
     t = d.createTextNode('OK');
     b.className = 'MsgBtn';
     b.appendChild(t);
     b.setAttribute('id', 'FocusBtn');
     b.focus();
     b = d.createElement('button');
     abort = b.onclick = function() {
       close(id);
       if(typeof(inputValue) == 'function') // optional callback on Abort;
         inputValue();
     };
     t = d.createTextNode('Abbrechen');
     b.className = 'MsgBtn';
     b.appendChild(t);
     div.appendChild(b);
     div.appendChild(clearer);
     
   } else if(type == 'prompt') {
     
     var di = d.createElement('div'), ok;
     input = d.createElement('input');
     s.appendChild(di);
     di.style.overflow = 'auto';
     di.appendChild(input);
          input.setAttribute('type', 'text');
          input.value = inputValue;
     input.select();
     input.onclick = function(e) { SWO.Event.stopPropagation(e); return true; };
     
     $.event.add(input, 'keydown', function(e) {
       
       if(e.keyCode == 13) 
         ok.onclick();
       
       return true;
       
     });
     
     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);
     
   }
   
   paint(id);
   
   // make methods public
   win.id = id;
   win.paint = function() { paint(this.id) };
   
   return win;
   
 };
 
 
})();


