/*!
 * Goodar
 * @author    Vyacheslav Voronchuk
 * @date      17.01.2010 
 * Copyright(c) 2009, by Vyacheslav Voronchuk
 */

//листалка объявлений
Goodar.News = Ext.extend(Ext.util.Observable, 
{
  selected: null,  
  
  //предварительная настройка класса
  constructor: function(config)
  {
    for(value in config)
      this[value] = config[value];
    
    var news = Ext.select('div[class$=news-button-off]');
    if(news.elements.length)
      this.selected = Ext.get(news.elements[0].id);
    
    Goodar.News.superclass.constructor.call(this);
    
    Goodar.Manager.addCmp(this.id, this);
    
    //переключение объявлений
    Ext.select('div[class^=news-button-]').on('click', function(event)
    {  
      Goodar.Manager.getCmp('news').select(Ext.get(event.target.id));
    });
  },
  
  //следующая новость
  next: function()
  {
    var oItem = this.selected.next();
    if(!oItem)
      oItem = Ext.get('announcement').next();
    
    this.select(oItem);    
  },
  
  //предыдущая новость
  prev: function()
  {
    var oItem = this.selected.prev();
    if(oItem.id === 'announcement')
      oItem = Ext.get('announcement').next('div:last-child');
    
    this.select(oItem); 
  },
  
  //показ новости
  select: function(oItem)
  {
    Ext.select('div[class$=news-button-off]').replaceClass('news-button-off', 'news-button-on');
    
    if(oItem.hasClass('news-button-on'))
      oItem.replaceClass('news-button-on', 'news-button-off');
    
    this.selected = oItem;
    
    Ext.fly('announcement').load({ url: '/module/index/getentry/entry/' + oItem.id.split('_')[2] });
  }
});  
new Goodar.News({ id: 'news' });