var lastCreate;

function updateStatus(text)
{
  s = document.getElementById('czytnik_status');
  s.innerHTML = text;
}

function createAndAdd()
{
  clearTimeout(lastCreate);
  if( artList.length > 0 )
  {
    var art = artList.shift();
    var e = document.getElementById('contents');
    var box = document.createElement('div');
    box.id = 'art_'+cnt;
    if( e.childNodes.length > 0 )
    {
      e.insertBefore( box, e.childNodes[0] );
    }
    else
    {
      e.appendChild(box);
    }
    box.className = 'artbox';
    var ihtml = '<a href="/fr?f='+art.i+'" class="artboxtitle" target="art_'+art.i+'">'+art.t+'</a>';
    if(art.o)
    {
      ihtml += '<a href="/fr?f='+art.i+'" target="art_'+art.i+'">';
      ihtml += '<img src="'+art.o+'" class="pix"/>';
      ihtml += '</a>';
    }
    ihtml += art.d;
    ihtml += '<a href="/fr?f='+art.i+'" class="artboxlink" target=art_'+art.i+'">'+art.u+'</div>';
    box.innerHTML = ihtml;
    var box_height = box.clientHeight;
    box.style.height = 0;
    var attributes = { 
      height: { to: box_height }
     };
    var anim = new YAHOO.util.Anim(box.id, attributes);
    anim.duration = 1.5;
    anim.method = YAHOO.util.Easing.easeOut;
    anim.animate(); 
    cnt++;
    var height = 800;
    if(window.innerHeight)
    {
      height = window.innerHeight;
    }
    else if (document.documentElement.clientHeight)
    {
      height = document.documentElement.clientHeight;
    }
    else if (document.body.clientHeight)
    {
      height = document.body.clientHeight;
    }
    while( e.childNodes.length > 0 && document.body.clientHeight + box_height > height )
    {
      e.removeChild( e.lastChild );
    }
  }
  if( artList.length == 0 )  
  {
    fetchList();
  }
  else
  {
    updateStatus('Zostało: '+artList.length);
  }
  lastCreate = setTimeout('createAndAdd();', 8000);
}

function ListFetchSuccess(o)
{
    var feeds = new Array();
    if (o.responseText)
    {
      try
      {
        eval('feeds = '+o.responseText+';');
      }
      catch (e)
      {
      }
      if( feeds.length > 0 )
      {
        artList = feeds;
        lastFeedCheck = artList[artList.length - 1].pd;
      }
    }
    fetching_feeds = 0;
    if ( artList.length > 0 )
    {
      updateStatus('Pobranych: '+artList.length);
    }
    else
    {
      updateStatus('Oczekiwanie na nowe wiadomości');
    }
    if(undefined != _uacct && _uacct != '')
    {
      _uff=0;
      urchinTracker();
    }
}

function ListFetchFail(o)
{
    fetching_feeds = 0;
    updateStatus('Błąd komunikacji');
}

function fetchList()
{
  if (fetching_feeds == 1)
  {
    return;
  }
  fetching_feeds = 1;
  var callback =
  {
    success: ListFetchSuccess,
    failure: ListFetchFail,
      timeout: 10000,
      argument: {}
  };
  YAHOO.util.Connect.asyncRequest('POST', '/fds', callback, 't='+lastFeedCheck+'&k='+cat);
}


