//  マウスキャプチャ
//
var MouseCapture  = function () {
  var fInit_      = false;

  var mosPos_     = { x:0, y:0 };
  var fMosEvent_  = false;

  var mouseCBObj_   = null;
  var mouseDownCB_  = null;
  var mouseUpCB_    = null;
  var mouseMoveCB_  = null;

  //イベントオブジェクト正規化
  function getEvt( evt )  {
    return  (evt) ? evt : ((window.event) ? event : null );
  }
  //マウス位置の取り出し
  function getMousePos( evt ) {
    mosPos_.x   = Event.pointerX( evt );
    mosPos_.y   = Event.pointerY( evt );
    fMosEvent_  = true;
  }
  //マウスダウン
  function mouseDownEvent( evt ) {
    evt = getEvt( evt );
    if( evt ) {
      getMousePos( evt );
      if( mouseDownCB_!=null )  {
        mouseDownCB_( evt );
      }
    }
  }
  //マウスアップ
  function mouseUpEvent( evt ) {
    evt = getEvt( evt );
    if( evt ) {
      getMousePos( evt );
      if( mouseUpCB_!=null )  {
        mouseUpCB_( evt );
      }
    }
  }
  //マウス移動
  function mouseMoveEvent( evt ) {
    evt = getEvt( evt );
    if( evt ) {
      getMousePos( evt );
      if( mouseMoveCB_!=null )  {
        mouseMoveCB_( evt );
      }
    }
  }

  return  {
    /**
     *  ロード後に設定
     */
    Init: function() {
      if( !fInit_ ) {
        //windowにマウスダウンイベントの追加
        Event.observe( window.document, 'mousedown', mouseDownEvent, false );
        //windowにマウスダウンイベントの追加
        Event.observe( window.document, 'mouseup',   mouseUpEvent, false );
        //windowにマウス移動イベントの追加
        Event.observe( window.document, 'mousemove', mouseMoveEvent, false );
        //
        fInit_  = true;
      }
    },
    /**
     *  マウスイベントをとったか
     */
    IsMouseEvent: function() {
      return  fMosEvent_;
    },
    /**
     *  マウス座標の取り出し
     */
    GetMousePos: function() {
      return  mosPos_;
    },
    /**
     *  マウスコールバックの登録
     */
    SetMouseCB: function( obj, down, up, move )  {
      if( mouseCBObj_==null && obj!==null ) {
        mouseCBObj_ = obj;
        //
        down  = (down) ? down : null;
        up    = (up) ?   up   : null;
        move  = (move) ? move : null;
        //
        mouseDownCB_  = down;
        mouseUpCB_    = up;
        mouseMoveCB_  = move;
        return  true;
      }
      return false;
    },
    /**
     *  マウスコールバックの解除
     */
    ReleaseMouseCB: function( obj )  {
      if( mouseCBObj_==obj ) {
        mouseCBObj_   = null;
        //
        mouseDownCB_  = null;
        mouseUpCB_    = null;
        mouseMoveCB_  = null;
        return  true;
      }
      return false;
    }
  };
}();
