var gIniTweets = 4;
var gSponsor = "";

//{{{ Tstream
Tstream.gInst = new Array();//インスタンス
function Tstream( id, url, width, height )
{
  Tstream.gInst[id] = this;

  this.id     = id;
  this.url    = url;
  this._buffer = new Array();
  this.width  = width;
  this.height = height;
}
//}}} Tstream end
//{{{ Tstream::setBuffer
Tstream.prototype.setBuffer = function( buffer )
{
//  this._buffer = buffer;
  while ( buffer.length >= 1 )
  {
    this._buffer.push( buffer.pop() );
  }
}
//}}} Tstream::setBuffer end
//{{{ Tstream::getBuffer
Tstream.prototype.getBuffer = function()
{
  return this._buffer;
}
//}}} Tstream::getBuffer end
//{{{ Tstream::pushBuffer
Tstream.prototype.pushBuffer = function( buffer )
{
  this._buffer.push( buffer );
}
//}}} Tstream::pushBuffer end
//{{{ Tstream::popBuffer
Tstream.prototype.popBuffer = function()
{
  return this._buffer.pop();
}
//}}} Tstream::popBuffer end
//{{{ Tstream::getTweets
Tstream.prototype.getTweets = function()
{
  if ( this.getBuffer().length >= 1 )
  {
    return;
  }
//  $.ajaxSetup({ ifModified: true });
  $.getJSON(this.url,{ts:new Date().getTime(),id:this.id}, function( json, status )
  {
    if ( (json != null) && (status == "success") )
    {
      var buffer = new Array();
      if ( gSponsor != "" )
      {
        buffer.push( gSponsor );
      }
      for ( i=0; i<json.length; i++ )
      {
        buffer.push( json[i] );
      }
      var url = $.url( this.url ); //http://www.webopixel.net/javascript/416.html
      Tstream.gInst[url.param("id")].setBuffer( buffer );
    }
  });
}
//}}} Tstream::getTweets end
//{{{ Tstream::stream
Tstream.prototype.stream = function()
{
  var _id = "#"+this.id;

  if ( $(_id+" > div > .tweet:animated").length >= 1 )
  {
    if ( $(_id+" > div > .tweet:not(#bg)").length >= gIniTweets )
    {
      setTimeout("Tstream.gInst['"+this.id+"'].stream();", 500 );
    }
  }

  //ツイート取得
  if ( this.getBuffer().length == 0 )
  {
    this.getTweets(); 
    setTimeout("Tstream.gInst['"+this.id+"'].stream();", 500 );
    return;
  }
  var tweet = this.popBuffer();

  //ツイートを削除
  if ( $(_id+" > div > .tweet").length >= 10 )
  {
    $(_id+" > div > .tweet:not(#bg)").eq(-1).fadeOut(1000, function(){ $(this).remove(); });
  }

  _o  = null; //現在のトップ
  frame_t = $(_id).offset().top;
  $(_id+" > div > .tweet:not(#bg)").each( function()
  {
    _top = $(this).offset().top;
    if ( _top >= frame_t )
    {
      if ( _o == null )
      {
        _o = $(this);
      }
      else
      {
        if ( _top < _o.offset().top )
        {
          _o = $(this);
        }
      }
    }
  });

  //bgにデータを抽入して高さを取得する
  bg = $("<div id=\"bg\" class=\"tweet\" style=\"position:relative;top:-10000px;\">"+tweet+"</div>");
  bg.prependTo(_id+" > div");
  h = bg.height();
  bg.remove();

  //ツイートを追加
  div = $("<div class=\"tweet\" style=\"height:0px;display:block;\"></div>");
  if ( _o == null ) {
    div.prependTo(_id+" > div");
  } else {
    div.insertBefore( _o );
  }

  div.animate({height:h+15},{duration:300, complete:function(){ $(this).css("display","none"); $(this).html( tweet ); $(this).fadeIn(1000); }});

  if ( $(_id+" > div > .tweet:not(#bg)").length < gIniTweets ) { 
    setTimeout("Tstream.gInst['"+this.id+"'].stream();", 2000 );
  } else {
    setTimeout("Tstream.gInst['"+this.id+"'].stream();", 6000 );
  }
}
//}}} Tstream::stream end
//{{{ Tstream::start
Tstream.prototype.start = function()
{
  document.write("<div id=\""+this.id+"\"style=\"position:relative;width:"+this.width+"px;height:"+this.height+"px;overflow:hidden;text-align:left;\"><div style=\"position:relative;\"></div></div>");

//{{{ mousewheel
  $("#"+this.id).mousewheel( function( event, delta )
  {
    event.preventDefault();

    var _id = "#"+this.id;
    if ( $(_id+" > div:animated").length >= 1 )
    {
      return;
    }

    var _top   = 0; //ターゲットトップ
    var _y1    = $(_id).offset().top;               //フレーム上座標
    var _y2    = $(_id).height()          + _y1;    //フレーム下座標
    var _in_y1 = $(_id+" > div").offset().top;      //インナー上座標
    var _in_y2 = $(_id+" > div").height() + _in_y1; //インナー下座標
    if ( delta > 0 )
    {
      if ( _in_y1 < _y1 )
      {
        _top = -10000;
        $(_id+" > div > .tweet").each( function()
        {
          _tw_y = $(this).offset().top;
          if ( _tw_y < _y1 )
          {
            if ( _tw_y > _top )
            {
              _top = _tw_y;
            }
          }
        });
        _top = $(_id+" > div").offset().top - _top;

        $(_id+" > div").animate({top:_top},{duration:300});
      }
    }
    else
    {
      if ( _in_y2 > _y2 )
      {
        _top = 10000;
        $(_id+" > div > .tweet").each( function()
        {
          _tw_y = $(this).offset().top;
          if ( _tw_y > _y1 )
          {
            if ( _tw_y < _top )
            {
              _top = _tw_y;
            }
          }
        });
        _top = $(_id+" > div").offset().top - _top;

        $(_id+" > div").animate({top:_top},{duration:300});
      }
    }
  });
//}}} mousewheel end

  var cmd = "Tstream.gInst['"+this.id+"'].stream();";
  setTimeout( cmd, Tstream.gInst.length*500 ); 
}
//}}} Tstream::start end

