//マイコレクション
var MyCollectionPUP = function()  {
  var fInit_  = false;
  var id_;
  var err_;

  function checkmc_Success( obj ) {
    var res = parseInt( obj.responseText );
    if( res==1 )  {
      $( 'add_collection' ).style.display     = '';
      $( 'add_collection_ng' ).style.display  = 'none';
    }
  }
  function checkmc_Failure( obj ) {
  }

  //-----------------------------------------

  function baseInit( base, mosPos ) {
      base.style.display  = 'none'; //まず消す
      var html  = '<div align="right" style="margin-bottom: 2px;"><a href="javascript:PopupDiv.Close();"><img src="/shashin-bu/img/photo/close.gif" alt="閉じる" border="0"></a></div>'
        + '<table width="290">'
        + '  <tr>';
      if( err_==null )  {
        html  += '    <td width="50" align="center"><img src="/shashin-bu/contents/ts90/id/' + id_ + '" width="40" height="40" border="0"></td>'
               + '    <td width="240" align="left">この写真を「お気に入り写真」に追加しました。</td>';
      }
      else  {
        html  += '    <td align="center">' + err_ + '</td>';
      }
      html  += '  </tr></table>';
      base.innerHTML  = html;
      PopupDiv.SetBasePos( mosPos.x - 290, mosPos.y -  20 );
      return  true;
  }

  function addMyCollection_Success( obj ) {
    err_  = null;
    var res = parseInt( obj.responseText );
    if( res!=0 )  {
      if( res<0 || res>4 )  res = 1;
      var mes = [ '', 'ただいま込み合っております。', 'ログインして下さい。', 'コンテンツIDが不正です。', 'この画像は既に追加されています。' ];
      err_  = mes[ res ];
    }
    else  {
      $( 'add_collection' ).style.display     = 'none';
      $( 'add_collection_ng' ).style.display  = '';
    }
    PopupDiv.Popup( baseInit );
  }
  function addMyCollection_Failure( obj ) {
    err_  = 'ただいま込み合っております。';
    PopupDiv.Popup( baseInit );
  }

  return  {
    //画像IDを設定
    SetImageID: function( id )  {
      id_ = id;
      //追加できるか確認
      new Ajax.Request('/shashin-bu/C00201/checkmc/id/' + id_, {
      	method: 'get',
          requestHeaders: [ 'If-Modified-Since', 'Wed, 15 Nov 1995 00:00:00 GMT' ], //キャッシュ無効化
      	  onSuccess: checkmc_Success,
          onFailure: checkmc_Failure
      } );
    },

    //初期化
    Init: function( baseID ) {
      var parent  = $( baseID );
      var base    = document.createElement( 'div' );
      base.className      = 'mycollection_pop';
      base.style.display  = 'none';
      base.style.position = 'absolute';
      parent.appendChild( base );
      //
      PopupDiv.Init( base );
      fInit_  = true;
    },
    //コレクション追加
    Add: function()  {
      if( fInit_ )  {
        new Ajax.Request('/shashin-bu/C00201/addmc/id/' + id_, {
        	method: 'get',
            requestHeaders: [ 'If-Modified-Since', 'Wed, 15 Nov 1995 00:00:00 GMT' ], //キャッシュ無効化
        	  onSuccess: addMyCollection_Success,
            onFailure: addMyCollection_Failure
        } );
      }
    }
  }
}();

//-----------------------------------------------------

//ロード後に起動
Event.observe( window, 'load', function() {
  MyCollectionPUP.Init( 'favorite_pop' );
  MyFavoritesUsersPUP.Init( 'favoritesusers_pop' );
}, false );
