var Newsticker = new Class({
	initialize:function(elementName){
		this.element = $(elementName);
		this.stories = [];
		this.currentStory = null;
		this.storySlideIn = null;
		this.currentIndex = 0;
		this.height = this.element.getStyle('height').toInt();
		this.width = this.element.getStyle('width').toInt();
	},
	
	addStory:function(title, description, link, date){
		this.stories.push({title:title, description:description, link:link, date:date});
	},
	
	run:function(seconds){
	  //if(seconds == null)
	    seconds = 10;
	  this.showNextStory();
	  if(this.stories.length > 1)
	   this.showNextStory.periodical(seconds*1000, this);
  },
	
	showNextStory:function(){
	  var story = this.getCurrentStory();
    if(story)
    {
      var storyElement = new Element('div');
      storyElement.setClass
  	  var tplStr = this.template;
  	  tplStr = tplStr.split("[%DATE%]").join(story.date);
  	  tplStr = tplStr.split("[%TITLE%]").join(story.title);
  	  tplStr = tplStr.split("[%DESCRIPTION%]").join(story.description);
  	  tplStr = tplStr.split("[%LINK%]").join(story.link);
  	   
  		storyElement.setHTML(tplStr);
  		storyElement.addClass('story'); 
      storyElement.injectInside(this.element);
      
     
      
      storyElement.setStyles({
        position:'absolute',
        top:this.height,
        height:this.height,
        width:this.width
      });
      
      if(this.currentStory)
      {
        var storySlideOut = new Fx.Style(this.currentStory, 'top', {duration:500});
        var handleComplete = function(element){
          element.remove();
        }
        storySlideOut.addEvent('onComplete', handleComplete);
        storySlideOut.start(-this.height);  
      }
      
      this.storySlideIn = new Fx.Style(storyElement, 'top', {duration:500});
      this.storySlideIn.start(0);
      
      this.currentStory = storyElement;
  		
      this.currentIndex++;
    }
    
	},
	
	getCurrentStory:function(){
		return this.stories[this.currentIndex % this.stories.length];
	},
	
	setStoryTemplate:function(tplStr){
	  this.template = tplStr;
  }
	
});

