
/*************************************************
*
* (c)2009 domi, #colcode <domi@colcode.com>
*
 ************************************************/

IEX = (navigator.appVersion.indexOf('MSIE') > 0)? 1 : 0;
NS4 = (document.layers)? 1 : 0;
BV5 = (document.getElementById)? 1: 0; // browser version 5, gecko, opera, etc
IE7 = (navigator.appVersion.indexOf('MSIE 7') > 0)? 1 : 0;
IE5 = (IEX && BV5)? 1 : 0;  // IE5 and IE6
IE4 = (IEX && !BV5)? 1 : 0;

MOUSE_LEFT   = 1;

KEYB_BACKSPC = 8;
KEYB_TAB     = 9;
KEYB_RETURN  = 13;
KEYB_ENTER   = KEYB_RETURN;

KEYB_SHIFT   = 16;
KEYB_CTRL    = 17;
KEYB_ALT     = 18;
KEYB_BRK     = 19;
KEYB_CAPS    = 20;

KEYB_ESC     = 27;

KEYB_SPACE   = 32;
KEYB_SPC     = KEYB_SPACE;
KEYB_PGUP    = 33;
KEYB_PGDN    = 34;
KEYB_END     = 35;
KEYB_HOME    = 36;
KEYB_LEFT    = 37;
KEYB_UP      = 38;
KEYB_RIGHT   = 39;
KEYB_DOWN    = 40;

KEYB_INS     = 45;
KEYB_DEL     = 46;

KEYB_LOGO    = 91;
KEYB_WIN     = 91;

KEYB_F1      = 112;
KEYB_F2      = 113;
KEYB_F3      = 114;
KEYB_F4      = 115;
KEYB_F5      = 116;
KEYB_F6      = 117;
KEYB_F7      = 118;
KEYB_F8      = 119;
KEYB_F9      = 120;
KEYB_F10     = 121;
KEYB_F11     = 122;
KEYB_F12     = 123;

KEYB_SCROLL  = 145;
KEYB_SCRLK   = KEYB_SCROLL;
KEYB_SCRLOCK = KEYB_SCROLL;

function BYID(id)
{
   return document.getElementById(id);
}

function BYNAME(name)
{
   return document.getElementsByName(name);
}

function BYTAG(name)
{
   return document.getElementsByTagName(name);
}

var Gui = new function()
{
   var _pdim;  // dim layer
   var _pwin;  // popup window
   var _ptop;  // popup window list
   var _pttl;  // popup window title
   var _pfrm;  // popup content, iframe
   var _dwin;  // dialog window
   var _dtop;  // dialog window list
   var _dttl;  // dialog window title
   var _dtxt;  // dialog content
   var _drag;  // active drag object
   
   var _err_timer = 0;
   var _err_elem  = 0;
   var _err_text  = 0;
   var _err_class = 0;
   
   var _autoload  = new Array();
   var _me        = this;
   var _init      = 0;
   
   /* public read-only properties  */
   this.mx   = 0; // mouse X
   this.my   = 0; // mouse Y
   this.sx   = 0; // scroll X
   this.sy   = 0; // scroll Y
   this.winW = 0;
   this.winH = 0;
   
   /* some useful helpers         */
   this.varDump = function(obj, name, pad)
   {
      var buf = '';
      
      if(typeof pad  != 'string') pad = '';
      if(typeof name != 'string') name = 'Object';
      
      if(typeof obj == 'object' || typeof obj == 'array')
      {
         buf += pad + name +' ('+ typeof obj +')\n';
         pad += '   ';
         
         for(var key in obj)
         {
            buf += _me.varDump(obj[key], key, pad);
         }
      }
      else
      {
         return pad + name +' = ('+ typeof obj +') '+ obj +'\n';
      }
      
      return buf;
   }
   
   this.isControlKey = function(key)
   {
     if( (key < KEYB_SPACE) ||
         (key <= KEYB_DOWN && key >= KEYB_PGUP) ||
         (key == KEYB_LOGO || key == KEYB_INS || key == KEYB_DEL || key == KEYB_SCROLL) ||
         (key <= KEYB_F12 && key >= KEYB_F1)
       ) return key;
   
      return 0;
   }
   
   this.getEventKeycode = function(x)
   {
      var ev = (typeof x == 'undefined')? window.event : x;
      if(ev.button) return ev.button;
      if(ev.keyCode) return ev.keyCode;
      if(ev.which) return ev.which;
      return 0;
   }
   
   this.autoLoad = function(t)
   {
      _autoload[_autoload.length] = t;
   }

   this.pageInit = function()
   {
      if(!_init)
      {
         for(var i in _autoload)
         {
            var t = _autoload[i];
            if(typeof t == 'function') t();
            if(typeof t == 'string') eval(t);
         }

         _init = 1;
         _drag = 0;
         
         if(!(_pdim = BYID('popdim'))) return;
         
         if(!(_pwin = BYID('popwin'))) return;
         if(!(_ptop = BYID('poptop'))) return;
         if(!(_pttl = BYID('popttl'))) return;
         if(!(_pfrm = BYID('popfrm'))) return;
   
         if(!(_dwin = BYID('dlgwin'))) return;
         if(!(_dtop = BYID('dlgtop'))) return;
         if(!(_dttl = BYID('dlgttl'))) return;
         if(!(_dtxt = BYID('dlgtxt'))) return;
         
         _dtop.onmousedown = _me.dragInit;
         _ptop.onmousedown = _me.dragInit;
         
         document.onmousemove = _me.captureMouse;
         window.onresize      = _me.pageInit;
         
         if(_err_elem = BYID('error_cont'))
         {
            _err_text  = _err_elem.innerHTML;
            _err_class = _err_elem.className;
         }
      }
      
      _me.winW = (IEX)? document.body.offsetWidth : window.innerWidth;
      
      if(window.innerHeight)
         _me.winH = window.innerHeight;
      else if(document.documentElement && document.documentElement.clientHeight)
         _me.winH = document.documentElement.clientHeight;
      else if(document.body && document.body.clientHeight)
         _me.winH = document.body.clientHeight;
      else
         _me.winH = 680;
   }
   
   this.dragObject = function(obj, tgt)
   {
      this.obj    = obj;
      this.tgt    = tgt;
      this.origX  = parseInt(obj.style.left);
      this.origY  = parseInt(obj.style.top);
      this.evtX   = _me.mx;
      this.evtY   = _me.my;
      this.curX   = this.origX;
      this.curY   = this.origY;
      this.active = true;
      
      this.tgt.style.cursor = 'move';
      
      this.drag = function()
      {
         if(this.active)
         {
            this.curX = (_me.mx + this.origX - this.evtX);
            this.curY = (_me.my + this.origY - this.evtY);
            this.obj.style.top  = this.curY +'px';
            this.obj.style.left = this.curX +'px';
            
            window.status = 'cX:'+ this.curX +', cY:'+ this.curY +', eX:'+ this.evtX +', eY:'+ this.evtY +', oX:'+ this.origX +', oY:'+ this.origY;
         }
      };
      
      this.obj.onmouseup = function()
      {
         if(_drag)
         {
            _drag.active = 0;
            _drag.tgt.style.cursor = 'default';
            _drag = null;
         }
      };
   }
   
   this.dragInit = function(evt)
   {
      if(typeof evt == 'undefined' || !evt) var evt = window.event;
      
      if(evt.target)
         var tgt = evt.target;
      else if(evt.srcElement)
         var tgt = evt.srcElement;
      else
         return true;
      
      if(tgt.className.indexOf('drag') == -1) return true;
      
      _drag = new Gui.dragObject(tgt.parentNode, tgt);
      
      return false;
   }
   
   this.captureMouse = function(evt)
   {
      var mode;
      
      if(typeof evt == 'undefined' || !evt) var evt = window.event;
      
      if(document.body && evt.clientX && evt.clientY)
      {
         mode = 1;
         _me.mx = evt.clientX;
         _me.my = evt.clientY;
         _me.sx = (IEX)? document.documentElement.scrollLeft : (evt.pageX - _me.mx);
         _me.sy = (IEX)? document.documentElement.scrollTop  : (evt.pageY - _me.my);
      }
      else if(evt.pageX || evt.pageY)
      {
         mode = 2;
         _me.sx = (document.documentElement.scrollLeft)? document.documentElement.scrollLeft : 0;
         _me.sy = (document.documentElement.scrollTop)? document.documentElement.scrollTop : 0;
         _me.mx = evt.pageX - _me.sx;
         _me.my = evt.pageY - _me.sy;
      }
      else
      {
         mode = 0;
         _me.sx = 0;
         _me.sx = 0;
         _me.mx = 275;
         _me.my = 150;
      }
      
      if(_drag) _drag.drag();
      
      //window.status = 'mode1: '+ mode +', '+ _me.mx + ', mouseY: ' + _me.my + ' winW: ' + _me.winW + ', winH: ' + _me.winH +', scrollX:'+ _me.sx +', scrollY:'+ _me.sy;
   }
   
   this.overLay = function(act)
   {
      if(typeof act == 'undefined') act = false;   
      _pdim.style.display = (act)? 'block' : 'none';
   }
   
   this.dialogOpen = function(txt, tl, sx, sy, px, py, bg)
   {
      if(typeof bg == 'undefined' || !bg) bg = '#fff';
      if(typeof tl == 'undefined' || !tl) tl = 'Dialog';
      if(typeof sx == 'undefined' || !sx || sx == 'auto') sx = 300;
      if(typeof sy == 'undefined' || !sy || sy == 'auto') sy = 0;
      if(typeof px == 'undefined' || !px) px = parseInt(_me.mx - sx + _me.sx);
      if(typeof py == 'undefined' || !py) py = parseInt(_me.my - sy + _me.sy);
      
      if(px == 'c' || px == 'auto') px = parseInt((_me.winW - sx) / 2) + _me.sx;
      if(py == 'c' || py == 'auto') py = parseInt((_me.winH - sy) / 2) + _me.sy;
      
      if(py < 10) py = 10;
      if(px < 10) px = 10;
      
      //if(!confirm('winW'+_me.winW+', winH'+_me.winH+' sx:'+sx+', sy:'+sy+', px:'+px+', py:'+py+', mx:'+_me.mx+', .my:'+_me.my+', sx:'+_me.sx+', sy:'+_me.sy)) return;
      
      _dwin.style.left = px + 'px';
      _dwin.style.top = py + 'px';
      _dwin.style.width = sx + 'px';
      _dwin.style.height = (sy)? sy + 'px' : 'auto';
      _dwin.style.background = bg;
      _dtxt.innerHTML = txt;
      _dttl.innerHTML = tl;
      _pdim.style.display = 'block';
      _dwin.style.display = 'block';
   }
   
   this.popupOpen = function(uri, title, sx, sy, bg)
   {
      var px, py;
      
      if(typeof bg == 'undefined' || !bg) bg = '#fff';
      sx = (typeof sx == 'undefined' || sx == 'auto')? 450 : parseInt(sx);
      sy = (typeof sy == 'undefined' || sy == 'auto')? 300 : parseInt(sy);
      
      if(sx < 250) sx = 250;
      if(sy < 200) sy = 200;
      if(sx > (_me.winW - 20)) sx = _me.winW - 20;
      if(sy > (_me.winH - 50)) sy = _me.winH - 50;
      
      px = parseInt((_me.winW - sx) / 2) + _me.sx;
      py = parseInt((_me.winH - sy) / 2) + _me.sy;
      if(py < 10) py = 10;
      if(px < 10) px = 10;
      
      //if(!confirm('_me.winW'+_me.winW+', _me.winH'+_me.winH+' sx:'+sx+', sy:'+sy+', px:'+px+', py:'+py+', _me.mx:'+_me.mx+', _me.my:'+_me.my+', _me.sx:'+_me.sx+', _me.sy:'+_me.sy)) return;
      _pfrm.style.height = sy + 'px';
      _pfrm.style.width  = sx + 'px';
      
      sy += 25; // add the space needed by top and bot
      
      _pwin.style.background = bg;
      _pwin.style.height = sy + 'px';
      _pwin.style.width  = sx + 'px'; 
      _pwin.style.top    = py + 'px';
      _pwin.style.left   = px + 'px';
      _pttl.innerHTML = title;
            
      _pfrm.src = uri;
      _pdim.style.display = 'block';
      _pwin.style.display = 'block';
   }
   
   this.dialogClose = function()
   {
      _dtxt.innerHTML = '';
      _dttl.innerHTML = '';
      _dwin.style.display = 'none';
      _pdim.style.display = 'none';
   }
   
   this.popupClose = function(uri)
   {
      _pdim = BYID('popdim');
      _pwin = BYID('popwin');
      _pfrm = BYID('popfrm');
      
      _pwin.style.display = 'none';
      _pdim.style.display = 'none';
      _pfrm.src = '/empty.html';
      
      if(typeof uri != 'undefined') location = uri;
   }
   
   this.textKeyUp = function(obj, func, ev)
   {
      if(typeof func != 'function') return;
      
      var key = Gui.getEventKeycode(ev);
      
      func(obj, key);
   }
   
   this.btnClick = function(frm)
   {
      if(typeof frm == 'undefined') return;
      frm.submit();
   }
   
   this.pager = function(off, frm)
   {
      if(typeof frm != 'object') frm = document.navfrm;
      if(typeof frm == 'undefined') return;
      frm.off.value = off;
      frm.submit();
   }
   
   this.sortby = function(num, frm)
   {
      if(typeof frm != 'object') frm = document.navfrm;
      if(typeof frm == 'undefined') return;
      frm.sort.value = num;
      frm.submit();
   }
   
   this.clearError = function()
   {
      _err_elem.className = _err_class;
      _err_elem.innerHTML = _err_text;
      
      if(!_err_timer) return;
      
      clearInterval(_err_timer);
      _err_timer = 0;
   }
   
   this.setError = function(error, verbose)
   {
      var regex = /<br \/>/g;
      if(typeof verbose == 'undefined') verbose = 0;
      
      if(!_err_elem) return;
      
      if(_err_timer) clearInterval(_err_timer);
         
      _err_elem.className = 'err_cont';
      _err_elem.innerHTML = error;
      
      if(verb) alert(error.replace(regex, '\n'));
      
      _err_timer = setTimeout('clear_error()', 10000);
   }
   
   this.frmError = function(err)
   {
      var msg;
      
      msg  = 'Ett fel uppstod:\n';
      msg += err;
      
      setError(msg, 0);
   }
      
   this.deleteRow = function(tbl, id)
   {
      var tr, i = 0;      
      var _tbl = (typeof _tbl == 'object')?  tbl : BYID(tbl);
      
      while(tr = _tbl.rows[++i])
      {
         if(tr.id == id)
         {
            _tbl.deleteRow(i);
            break;
         }
      }
   }
   
   this.setRadio = function(name, value)
   {
      var rd = BYNAME(name);
      if(!rd || rd.length == 0) return;
      
      var len = rd.length;
      for(var i = 0; i < len; i++)
      {
         var r = rd[i];
         r.checked = (r.value == value.toString())? true : false;
      }
   }
   
   this.getRadio = function(name)
   {
      var rd = BYNAME(name);
      if(!rd || rd.length == 0) return null;
      
      var len = rd.length;
      for(var i = 0; i < len; i++)
      {
         var r = rd[i];
         if(r.checked) return r.value;
      }
      
      return null;
   }
}();

window.onload = Gui.pageInit;

/* eof */
