var mRotatorInstance;

function mRotator(cur,bid,options)
{
  this.current=cur;
  this.container=document.getElementById(bid);
  this.curfade=0;
  this.curappear=0;
  this.currotateelement=null;
  this.hovered=false;
  
  this.autorotate=0;
  this.autorotatesec=10000;
  this.pauseonmouseover=true;
  
  this.first=null;
  this.last=null;
  
  if(options)
  {
    if(options.autorotate)
      this.autorotate=options.autorotate;
    if(options.autorotatesec)
      this.autorotatesec=options.autorotatesec;
    if(options.pauseonmouseover)
      this.pauseonmouseover=options.pauseonmouseover;
  }
  
  this.init=function()
  {
    var as=this.container.getElementsByTagName('a');
    this.container.rotator=this;
    this.container.onmouseover=function(e)
    {
      this.rotator.hovered=true;
    }
    this.container.onmouseout=function(e)
    {
      this.rotator.hovered=false;
    }
    var prev=null;
    var next=null;
    for(var i=0;i<as.length;i++)
    {
      var m=as[i].id.match(/mnews-(\d+)/);
      if(m)
      {
        if(prev)
          as[i].prev=prev;
        prev=as[i];
        as[i].rotator=this;
        as[i].newsid=m[1];
        as[i].onmouseover=function(e)
        {
          if(this.rotator.current!=this.newsid)
          {
            this.rotator.rotate(this.newsid);
            this.rotator.currotateelement=this;
          }
//          this.rotator.hovered=true;
        }
        as[i].onmouseout=function(e)
        {
//          this.rotator.hovered=false;
        }
        var div=document.getElementById('gnews-'+m[1]);
        div.style.position='absolute';
        if(div.style.display!='none')
        {
          div.parentNode.style.height=div.offsetHeight+'px';
          this.first=as[i];
        }
        this.last=as[i];
      }
    }
    this.first.className='mouseover';
    this.first.prev=this.last;
    prev=this.last;
    while(prev!=this.first)
    {
      prev.prev.next=prev;
      prev=prev.prev;
    }
    this.last.next=this.first;
  };
  
  this.rotate=function(newid)
  {
    if(this.curfade>0)
      document.getElementById('gnews-'+this.curfade).style.display='none';
    if(this.curappear>0)
    {
      var appear=document.getElementById('gnews-'+this.curappear).style.display='block';
    }
    var queue = Effect.Queues.get('fadescope');
    queue.each(function(effect) { effect.cancel(); });
    var queue = Effect.Queues.get('appearscope');
    queue.each(function(effect) { effect.cancel(); });

    document.getElementById('mnews-'+this.current).className='mouseout';
    document.getElementById('mnews-'+newid).className='mouseover';
    var div=document.getElementById('gnews-'+this.current);
    div.parentNode.style.height=div.offsetHeight+'px';
    div.style.position='absolute';
    this.hide(this.current);
    this.show(newid,div.parentNode);
    this.current=newid;
  };
  
  this.hide=function(id)
  {
    this.curfade=id;
    var div=document.getElementById('gnews-'+id);
    new Effect.Fade(div, { duration: 0.3, queue: { position: 'end', scope: 'fadescope' } });
  };
  
  this.show=function(id,pnode)
  {
    this.curappear=id;
    var div=document.getElementById('gnews-'+id);
    new Effect.Appear(div, { duration: 0.3, queue: { position: 'end', scope: 'appearscope' }, afterFinish : function( effect ) {
        var div=document.getElementById(effect.element.id);
//        alert(div.parentNode.style.height);
        
//        div.parentNode.style.height=div.offsetHeight+'px';
        new Effect.Morph(div.parentNode, { style: 'height: '+div.offsetHeight+'px', duration: 0.3, queue: { position: 'end', scope: 'appearscope' } });
      } });
  };
  
  this.Rotuj=function()
  {
    if((!this.pauseonmouseover)||(!this.hovered))
    {
      if(this.currotateelement)
      {
        this.currotateelement=this.currotateelement.next;
        if(this.currotateelement)
        {
          this.currotateelement.onmouseover();
          this.hovered=false;
        }
      }
    }
  }
  
  if(this.current>0)
  {
    this.init();
    if(this.autorotate)
    {
      mRotatorInstance=this;
      this.currotateelement=this.first;
      setInterval("mRotatorInstance.Rotuj();", this.autorotatesec);
    }
  }
}
