(function() {
    biooncology = {};
})();

document.observe("dom:loaded", function(ev) {	
            var nf = new biooncology.NewsFeed(); 
    });

biooncology.NewsFeed = Class.create({

      /** @constructor */
        initialize: function() {
            this.tabs = new Array();
            this.changeCounter = 0;
            this.create();
        },
        Version: "1.0",
        toString: function() {
            return "[object NewsFeed]";
        }
});

/**
 * Creation function which renders the Ext.Panel
 */
biooncology.NewsFeed.prototype.create = function(){
 
     this.NewsPanel = new Ext.TabPanel({
        renderTo: "news-feed",
        activeTab: 0,
        width: 217,
        height: 'auto',
        plain: true,
        autoShow: true,
        listeners:{
          beforetabchange : this.onClick.bind(this)
        },
        defaults: {
          autoScroll: false,
          autoHeight: true
        },
        items: [{
                 contentEl:'reuters-feed',
                 cls: 'first-tab',
                 listeners: { 
                              activate: this.onActivate.bind(this),
                              deactivate: this.onDeactivate.bind(this),
                              beforehide: this.onHide.bind(this)
                             }
               },{
                 contentEl: 'bioonc-feed', 
                 cls: 'second-tab',
                 style:{opacity:0},
                 listeners: { 
                              activate: this.onActivate.bind(this), 
                              deactivate: this.onDeactivate.bind(this),
                              beforehide: this.onHide.bind(this)
                            }
                 }]
      });
     
      this.tabs[0] = this.NewsPanel.getTabEl(0).parentNode;
      this.tabs[0].childElements()[0].addClassName('reuters-tab');
      this.tabs[0].childElements()[1].addClassName('bioonc-tab');
      this.tabs[1] = this.createDummyTabs();
      this.NewsPanel.doLayout();
     
     
      //adding class to differnt pages
      if(location.href.match(/news-feed/)) $('news-feed').addClassName('detail-page');
      
  //auto change //
  //since IE dosen't let setTimeout to pass extra arguments so made them array
            this.autoChangeStage = 0;
            this.autoChanges = new Array();
            this.autoChanges[0] = [setTimeout(this.autoClick.bind(this),6000),1];
            this.autoChanges[1] = [setTimeout(this.autoClick.bind(this),10000),0];
            
            var lis = $$('#reuters-feed li');
            for(var i=5;i<lis.length;i++) lis[i].setStyle({display:'none'});

         
};
//creating a copy of the entire tab(ul)
biooncology.NewsFeed.prototype.createDummyTabs = function(){
    var TabsCopy = this.tabs[0].cloneNode(true);
    TabsCopy.setStyle({position:'absolute',top:0, left:0, display: 'none'});
    TabsCopy.addClassName('flip');
    this.tabs[0].parentNode.appendChild(TabsCopy);
    return TabsCopy;
}



biooncology.NewsFeed.prototype.autoClick = function(){
  if (this.autoChanges[this.changeCounter]) {
    if (this.autoChangeStage == this.changeCounter) { //checking if how many times the tab was changed and if it matches with how many times it should be clicked automatically 
      this.NewsPanel.activate(this.NewsPanel.getComponent(this.autoChanges[this.changeCounter][1]));
      this.autoChangeStage++;
    } else {
      for (var i = 0; i < this.autoChanges.length; i++) {
        if (this.autoChanges[i][0]) window.clearTimeout(this.autoChanges[i][0]);
      }
    }
  }
};

biooncology.NewsFeed.prototype.onActivate = function(tab){
    if(tab.body){ 
      this.fadeIn(tab.body.dom.parentNode.parentNode);
      if(this.tabs[0]) this.fadeIn(this.tabs[0]);
    };
};

biooncology.NewsFeed.prototype.onDeactivate = function(tab){
    if(tab.body){
      this.fadeOut(tab.body.dom.parentNode.parentNode);
      this.fadeOut(this.tabs[1]);
    };
};

biooncology.NewsFeed.prototype.onHide = function(tab){
    if(tab)return false;
};

biooncology.NewsFeed.prototype.onClick = function(panel, nTab, cTab){
    if (cTab) {
      flip(this.tabs[1]);
      this.tabs[1].setOpacity(1);
      this.tabs[1].show();
      this.tabs[0].setOpacity(0);
      flip(this.tabs[0]);
     
      if(this.changeCounter<2) this.changeCounter++;
    };
    
    function flip(ulEl){
      if(ulEl.hasClassName('flip')){
        ulEl.removeClassName('flip');
      }else{
        ulEl.addClassName('flip');
      }; 
    };

};

biooncology.NewsFeed.prototype.fadeOut = function(el){
    new Effect.Opacity(el, {from: 1, to: 0, duration: 1 });
    var tb = this.tabs[1];

    new PeriodicalExecuter(function(pe){
         el.style.zIndex = 1;
         tb.hide();
         pe.stop();
        },1); 
}

biooncology.NewsFeed.prototype.fadeIn = function(el){ 
    el.style.zIndex = 10;
    new Effect.Opacity(el, {from: 0,to: 1,duration: 1});  
};

biooncology.NewsFeed.prototype.hideEl = function(){
  
}
