// xWindow r1, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function xWindow(name, w, h, x, y, loc, men, res, scr, sta, too)
{
  var e='',c=',',xf='left=',yf='top=';
  this.n = name;

  if (document.layers)
  {
    xf='screenX='; yf='screenY=';
  }

  this.f = (w?'width='+w+c:e)
         + (h?'height='+h+c:e)
         + (x>=0?xf+x+c:e)
         + (y>=0?yf+y+c:e)
         + 'location='+loc
         + ',menubar='+men
         + ',resizable='+res
         + ',scrollbars='+scr
         + ',status='+sta
         + ',toolbar='+too;

  this.opened = function()
  {
    return this.w && !this.w.closed;
  };

  this.close = function() {if(this.opened()) this.w.close();};
  this.focus = function() {if(this.opened()) this.w.focus();};

  this.load = function(sUrl)
  {
    if (this.opened()) this.w.location.href = sUrl;
    else this.w = window.open(sUrl,this.n,this.f);
    this.focus();
    return this.opened();
  };

  this.maximize = function()
  {
    this.w.moveTo(0,0);
    if (document.all)
    {
      this.w.resizeTo(screen.availWidth-10,screen.availHeight-58);
    }
    else if (document.layers || document.getElementById)
    {
      if (this.w.outerHeight < screen.availHeight ||
          this.w.outerWidth <  screen.availWidth)
      {
        this.w.outerHeight = screen.availHeight-58;
        this.w.outerWidth = screen.availWidth-10;
      }
    }
  };
}

xWindow.closeSelf=function()
{
  //Close the Parent Window
  //Works fine in all versions of IE.
  //May or May not close after a Confirmation in all other browsers.
  var win=window.open('','_self','');
  window.opener=win.opener;
  window.self.close();
}

xWindow.newApplicationWindow=function(name)
{
  var w=100, h=100;
  if (window.screen)
  {
    if (document.all)
    {
      w=(screen.availWidth-10)
      h=(screen.availHeight-58);
    }
    else
    {
      w=(screen.availWidth-10)
      h=(screen.availHeight-50);
    }
  }
  return new xWindow(name, w, h, 0, 0, 0, 0, 1, 1, 1, 0);
}

NavMgr = Class.create();
NavMgr.prototype =
{
  initialize: function(debug)
  {
    this.ctrl=false;
    this.alt=false;
    this.shift=false;
    if (typeof(debug) == 'undefined' || !debug)
    {
      // Observe when a key on the keyboard is pressed. In the observer, we check for
      // the tab key and alert a message if it is pressed.
      Event.observe(document, 'keydown', this.onKeyDown.bindAsEventListener(this));
      Event.observe(document, 'keypress', this.onKeyPress.bindAsEventListener(this));
      Event.observe(document, 'keyup', this.onKeyUp.bindAsEventListener(this));
      Event.observe(document, 'mousedown', this.onMouseDown.bindAsEventListener(this));
      //Event.observe(document, 'mouseup', this.onMouseUp.bindAsEventListener(this));
      //Event.observe(document, 'mousemove', this.onMouseMove.bindAsEventListener(this));
      Event.observe(document, 'contextmenu', this.onContextMenu.bindAsEventListener(this));
    }
  },

  // Arbitrary functions to respond to events
  getTagName: function(event)
  {
    return Event.element(event).tagName;
  },

  getKeyMessage: function(event)
  {
    var message='<'+this.getTagName(event)+'>';
    var code = event.keyCode;
    if(code == Event.KEY_TAB)
	  {
  	  message+='TAB';
	  }
	  else
	  {
  	  message+=code;
	  }
	  message+='</'+this.getTagName(event)+'>';
	  return message;
  },

  getMouseMessage: function(event)
  {
    var message='<'+this.getTagName(event)+'>';
    if (event.isRightClick())
	  {
      message+='RIGHT';
	  }
	  else if (event.isLeftClick())
	  {
      message+='LEFT';
	  }
	  else if (event.isMiddleClick())
	  {
      message+='MIDDLE';
	  }
	  else
	  {
      message+='UNKNOWN';
	  }
    message+="@(X: " + event.pointerX() + "px, Y: " + event.pointerY() + "px)";
	  message+='</'+this.getTagName(event)+'>';
    return message;
  },

  isInput: function(event)
  {
    var re=new RegExp("^(INPUT|SELECT|BUTTON|SUBMIT|RESET|TEXTAREA)(.*)$");
    return re.match(this.getTagName(event).toUpperCase())
  },

  handleBrowserNavigation: function(event, key, mode)
  {
    if (key)
    {
      var code = event.keyCode;
      var chr=String.fromCharCode(code)
      //document.forms[0].debugLog.value="[c:"+this.ctrl+";a:"+this.alt+";s:"+this.shift+";cl:"+this.capslck+";nl:"+this.numlck+";un:"+this.unknown+";chr:"+chr+";code:"+code+"]\n"+document.forms[0].debugLog.value;
      switch(code)
      {
        case 112: //F1
        case 113: //F2
        case 114: //F3
        case 115: //F4
        case 116: //F5
        case 117: //F6
        case 118: //F7
        case 119: //F8
        case 120: //F9
        case 121: //F10
        case 122: //F11
        case 123: //F12
          if (mode != 'press')
          {
            event.stop();
            event.returnValue = false;
            event.keyCode = 0;
            return false;
          }
          else
          {
            return true;
          }
        case Event.KEY_BACKSPACE:
          if (this.isInput(event))
          {
            return true;
          }
          else
          {
            event.stop();
            event.returnValue = false;
            event.keyCode = 0;
            return false;
          }
        default:
          if (this.ctrl && (chr == 'T' || chr == 'N' || chr == 'O'))
          {
            event.stop();
            event.returnValue = false;
            event.keyCode = 0;
            return false;
          }
          else
          {
            return true;
          }
      }
    }
    else
    {
      if (this.isInput(event))
      {
        return true;
      }
      else
      {
        if (event.isRightClick())
        {
          event.stop();
          event.returnValue = false;
        }
        return !event.isRightClick();
      }
    }
  },

  getModifierCode: function(event)
  {
    var code = event.keyCode;
    switch(code)
    {
      case 16:
        return 'shift';
      case 17:
        return 'ctrl';
      case 18:
        return 'alt';
      case 20:
        return 'capslck';
      case 144:
        return 'numlck';
      default:
        return 'unknown';
    }
  },

  setModifiers: function(event)
  {
    this[this.getModifierCode(event)]=true;
  },

  unsetModifiers: function(event)
  {
    this[this.getModifierCode(event)]=false;
  },

  onKeyPress: function(event)
  {
    return this.handleBrowserNavigation(event, true, 'press');
  },

  onKeyDown: function(event)
  {
    this.setModifiers(event);
    return this.handleBrowserNavigation(event, true, 'down');
  },

  onKeyUp: function(event)
  {
    this.unsetModifiers(event);
    return this.handleBrowserNavigation(event, true, 'up');
  },

  onMouseUp: function(event)
  {
    return this.handleBrowserNavigation(event, false, 'up');
  },

  onMouseDown: function(event)
  {
    return this.handleBrowserNavigation(event, false, 'down');
  },

  onMouseMove: function(event)
  {
    return this.handleBrowserNavigation(event, false, 'move');
  },

  onContextMenu: function(event)
  {
    var input=this.isInput(event);
    if (!input)
    {
      event.stop();
      event.returnValue = false;
    }
    return input;
  }
}


