//Exifポップアップウィンドウ
//--MouseCapture.jsが必要
var ExifPopup = function() {
  var fInit_    = false;
  var exifData_ = new Array();

  var base_;
  var basePos_;

  var dragMode_ = false;
  var dragStartDlsPos_;
  var dragStartMosPos_;

  //ドラッグ解除
  function releaseDrag()  {
    if( dragMode_ ) {
      //window.status = 'releaseDrag';
      dragMode_ = false;
      MouseCapture.ReleaseMouseCB( ExifPopup );
    }
  }

  //マウスドラッグ処理
  function dragUp( evt )  {
    if( Event.isLeftClick( evt ) )  {
      releaseDrag();
    }
  }
  function dragMove( evt )  {
    mpos                = MouseCapture.GetMousePos();
    basePos_.x          = dragStartDlgPos_.x + ( mpos.x - dragStartMosPos_.x );
    basePos_.y          = dragStartDlgPos_.y + ( mpos.y - dragStartMosPos_.y );
    base_.style.left    = basePos_.x + 'px';
    base_.style.top     = basePos_.y + 'px';
    //
    Event.stop( evt );
  }

  //マウスDOWN処理
  function mouseDownEvent( evt )  {
    if( !dragMode_ && MouseCapture.IsMouseEvent() )  {
      evt = (evt) ? evt : ((window.event) ? event : null );
      if( Event.isLeftClick( evt ) )  {
        mpos              = MouseCapture.GetMousePos();
        //
        dragStartDlgPos_  = { x: basePos_.x, y: basePos_.y };
        dragStartMosPos_  = { x: mpos.x, y: mpos.y }; //コピーしないといけない
        if( MouseCapture.SetMouseCB( ExifPopup, null, dragUp, dragMove ) ) {
          dragMode_ = true;
        }
      }
      //
      Event.stop( evt );
    }
  }


  //Exifデータをテーブルに追加
  function insertExifData( tbl, name, value ) {
    var tr  = document.createElement( 'tr' );
    var tdn = document.createElement( 'td' );
    var tdv = document.createElement( 'td' );
    tdn.setAttribute( 'width', 120 );
    tdn.setAttribute( 'bgcolor', '#ffffff' );
    tdn.innerHTML = name;
    //
    tdv.setAttribute( 'width', 165 );
    tdv.setAttribute( 'bgcolor', '#ffffff' );
    tdv.innerHTML = value;
    //
    tr.appendChild( tdn );
    tr.appendChild( tdv );
    tbl.appendChild( tr );
  }
  //Exifデータを表示
  function popupExif( exif, pos )  {
    var mosPos  = MouseCapture.GetMousePos();
    var dragOk  = true;
    base_.style.display   = 'none'; //まず消す
    base_.innerHTML       = '<div align="right" style="margin-bottom: 2px;"><a href="javascript:ExifPopup.close();"><img src="/shashin-bu/img/upload/close.gif" alt="閉じる" border="0"></a></div>';
    basePos_              = { x: mosPos.x - 150, y: mosPos.y -  20 };
    if( pos!=undefined )  {
      basePos_.x          = pos.x;
      basePos_.y          = pos.y;
      dragOk              = false;
    }
    base_.style.left      = basePos_.x + 'px';
    base_.style.top       = basePos_.y + 'px';
    //
    var tbl   = document.createElement( 'table' );
    var tbdy  = document.createElement( 'tbody' );  //IEではこれがないとダメ
    tbl.appendChild( tbdy );
    tbdy.setAttribute( 'width', 300 );
    tbdy.setAttribute( 'border', 0 );
    tbdy.setAttribute( 'align', 'center' );
    tbdy.setAttribute( 'cellpadding', 3 );
    tbdy.setAttribute( 'cellspacing', 1 );
    tbdy.setAttribute( 'bgcolor', '#bbbbbb' );
    base_.appendChild( tbl );
    //
    if( exif.Maker!=undefined )  {
      insertExifData( tbdy, 'メーカー', exif.Maker );
    }
    if( exif.Model!=undefined )  {
      insertExifData( tbdy, '機種', exif.Model );
    }
    if( exif.Lens!=undefined ) {
      insertExifData( tbdy, 'レンズ', exif.Lens );
    }
    if( exif.CreateTime!=undefined ) {
      insertExifData( tbdy, '作成日時', exif.CreateTime );
    }
    if( exif.UpdateTime!=undefined ) {
      insertExifData( tbdy, '更新日時', exif.UpdateTime );
    }
    if( exif.Size!=undefined ) {
      insertExifData( tbdy, 'オリジナルサイズ', exif.Size );
    }
    if( exif.ExposureTime!=undefined ) {
      insertExifData( tbdy, 'シャッタースピード', exif.ExposureTime );
    }
    if( exif.FNumber!=undefined ) {
      insertExifData( tbdy, '絞り値', exif.FNumber );
    }
    if( exif.ISO!=undefined ) {
      insertExifData( tbdy, 'ISO感度', exif.ISO );
    }
    if( exif.ExposureBiasValue!=undefined ) {
      insertExifData( tbdy, '露出補正値', exif.ExposureBiasValue );
    }
    if( exif.MaxApertureValue!=undefined ) {
      insertExifData( tbdy, '最小絞り値', exif.MaxApertureValue );
    }
    if( exif.Flash!=undefined ) {
      insertExifData( tbdy, 'フラッシュ', exif.Flash );
    }
    if( exif.FocalLength!=undefined ) {
      if( exif.FocalLengthIn35mmFilm!=undefined )  {
        insertExifData( tbdy, '焦点距離(35mm換算)', exif.FocalLength + '(' + exif.FocalLengthIn35mmFilm + ')' );
      }
      else  {
        insertExifData( tbdy, '焦点距離', exif.FocalLength );
      }
    }
    if( exif.WhiteBalance!=undefined ) {
      insertExifData( tbdy, 'ホワイトバランス', exif.WhiteBalance );
    }
    if( exif.GPSLatitude!=undefined ) {
      insertExifData( tbdy, '緯度', exif.GPSLatitude );
    }
    if( exif.GPSLongitude!=undefined ) {
      insertExifData( tbdy, '経度', exif.GPSLongitude );
    }
    //イベントの追加
    if( dragOk ) {
      Event.observe( base_, 'mousedown', mouseDownEvent, true );  //子より先行で受け取る
    }
    //表示
    base_.style.display   = '';
  }

  return  {
    /** 幅 */
    Width: 300,
    /** 初期化（ロード完了後に） */
    Init: function( baseID ) {
      if( !fInit_ ) {
        var base  = $( baseID );
        //
        base_                     = document.createElement( 'div' );
        base_.className           = 'exif';
        base_.style.display       = 'none';
        base_.style.position      = 'absolute';
        base.appendChild( base_ );
        //
        MouseCapture.Init();
        //
        fInit_  = true;
      }
    },
    /** Exifデータの追加 */
    AddData: function( cnt, data ) {
      exifData_[ cnt ]  = data;
    },
    /** Exifポップアップ */
    PopupWindow: function( cnt, pos ) {
      if( MouseCapture.IsMouseEvent() )  {
        //D&Dから解除
        releaseDrag();
        //
        popupExif( exifData_[ cnt ], pos );
      }
    },
    /** ポップアップ消去 */
    close: function() {
      base_.style.display = 'none';
      Event.stopObserving( base_, 'mousedown', mouseDownEvent, true );
      //D&Dから解除
      releaseDrag();
    }
  };
}();
