var StNews = new Class({
	initialize: function(element, options) {
		if(Browser.loaded){
			this.setup(element, options);
		} else{
			window.addEvent('domready', this.setup.pass([element, options]));
		}
	},
	setup: function(element, options) {
    $$('a.'+options['newsTabs']).addEvents({
      click: function(event){
       showtab(this);
       event.stop();
      }
    });
      
    var showtab = function(tab){ 
      tab_array = $$('a.'+options['newsTabs']);
      tab_array.each(function(item, index){
          item.className = 'user-stnews-pi1-head user-stnews-pi1-headlink';
          item.removeEvents('click');
          item.addEvents({
            click: function(event){
            showtab(this);
            event.stop();
           }
          });
        }
      );
      tab.className = 'user-stnews-pi1-head-active user-stnews-pi1-headlink'; 
      tab.removeEvents('click');
      tab.addEvents({
            click: function(event){
            event.stop();
           }
          });
      $$('div.user-stnews-pi1-div').setStyle('display', 'none'); 	
      var divid = tab.id;
      divid = ''+divid+'-block';
      $(divid).setStyle('display', 'block');
      
      if(this.ticker) {
        this.ticker.delTicker();
        this.ticker.initialize(divid,{speed:1000,delay:5000,direction:'vertical'});
      } else {
        this.ticker = new Ticker(divid,{speed:1000,delay:5000,direction:'vertical'});
      }
      
    }; 
    
    showtab($(options['firstID']));
  }
});


var Ticker = new Class({
		setOptions: function(options) {
			this.options = Object.extend({
				speed: 1000,
				delay: 5000,
				direction: 'vertical',
				onComplete: Class.empty,
				onStart: Class.empty
			}, options || {});
		},
		delTicker: function(){
      this.el = null;
      this.items = null;
      this.fx.cancel();
      delete this.fx;
      this.current = 0;
      this.nextTimer = $clear(this.nextTimer);
    },
		initialize: function(el,options){
			this.setOptions(options);			
			this.el = $(el);			
			this.items = this.el.getElements('div.user-stnews-pi1-item');
			var w = 0;
			var h = 0;
			if(this.options.direction.toLowerCase()=='horizontal') {
				h = this.el.getSize().y;
				this.items.each(function(div,index) {
					w += div.getSize().x;
				});
			} else {
				w = this.el.getSize().x;
				this.items.each(function(div,index) {
					h += div.getSize().y;
				});
			}
			this.el.setStyles({
				//position: 'absolute',
				top: 0,
				left: 0,
				width: w,
				height: h
			});
			this.fx = new Fx.Morph(this.el,{duration:this.options.speed,onComplete:function() {
				var i = (this.current==0)?this.items.length:this.current;
				this.items[i-1].inject(this.el);
				this.el.setStyles({
					left:0,
					top:0
				});
			}.bind(this)});
			this.current = 0;
			// this.next();
			this.nextTimer = this.next.bind(this).delay(this.options.delay+this.options.speed);
		},
		next: function() {
			this.current++;
			if (this.current >= this.items.length) this.current = 0;
			var pos = this.items[this.current];
			this.fx.start({
				top: -pos.offsetTop,
				left: -pos.offsetLeft
			});
			this.nextTimer = this.next.bind(this).delay(this.options.delay+this.options.speed);
		}
});

