var Newsticker = new Class({
					
	Implements: Options,

	options: {
		speed: 40,
		pauseOnContainerOver: true,
		pauseOnOver: true
	},

    initialize: function(options) {
		this.setOptions(options);
	    this.construct();
	},
	
	construct: function () {
	
		this.timer = null;
		this.active_ticker = $$('.newsticker .ticker.active')[0];
		this.marquee_element = $$('.newsticker .ticker_container')[0];
		this.marquee_navigation = $$('.newsticker ul')[0];
		
		this.active_ticker.setStyle('left', ( -1 * this.active_ticker.getCoordinates().width.toInt()));
		if(this.options.pauseOnOver){this.addMouseEvents();}
		
		this.timer = this.startMarquee();
		this.observeNavigation();
	},
	
	observeNavigation: function () {
		
		this.marquee_navigation.getChildren('li').each( function(ListElement,Key) {
		
			ListElement.addEvents({
				'mouseenter': function () {
					this.marquee_navigation.getChildren('li').each( function (elLi) {
						elLi.setStyle('display','block');
					});
				}.bind(this),
				'mouseleave': function () {
					this.marquee_navigation.getChildren('li').each( function (elLi) {
						if( !elLi.hasClass('active') ) {
							elLi.setStyle('display','none');
						}
					});
				}.bind(this),
				'click': function () {
					var name = ListElement.getChildren('span')[0].getAttribute('class');
					this.marquee_navigation.getChildren('li').each( function(ListElement) {
						ListElement.removeClass('active');
					});
					
					
					$$('.newsticker .ticker').each( function(div) {
						div.removeClass('active');
					});
					$$('.newsticker .ticker')[Key].addClass('active');									
					
					this.stopMarquee();
					
					this.timer = null;
					this.active_ticker = $$('.newsticker .ticker.active')[0];
					
					this.startMarquee();
					
					ListElement.addClass('active');
				}.bind(this)
			});
		
		}.bind(this));
		
	},
	
	addMouseEvents : function(){
	    if(!this.options.pauseOnContainerOver){
	        this.active_ticker.addEvents({
	            'mouseenter' : function(me){
	                this.clearTimer();
	            }.bind(this),
	            'mouseleave' : function(me){
	                this.timer = this.startMarquee.delay(this.options.speed, this);
	            }.bind(this)
	        });
	    }else{
	        this.marquee_element.addEvents({
	            'mouseenter' : function(me){
	                this.clearTimer();
	            }.bind(this),
	            'mouseleave' : function(me){
	                this.timer = this.startMarquee.delay(this.options.speed, this);
	            }.bind(this)
	        });
	    }
	},
	
	startMarquee: function () {
		var pos = this.active_ticker.getStyle('left').toInt();

		if( $type(pos) === false) pos = 0;

		if( pos >= 830 ) pos = 830;

		this.active_ticker.setStyle( 'left', ( pos + (-1) )  );
		this.checkEnd(pos);
		
		this.timer = this.startMarquee.delay(this.options.speed, this);
	},
	
	stopMarquee: function(){
        this.clearTimer();
        this.active_ticker.removeEvents();
    },
    
    clearTimer: function(){
        $clear(this.timer);
    },
    
    resumeMooquee: function(){
        this.stopMooquee();
        if(this.options.pauseOnOver){this.addMouseEvents();}
        this.timer = this.startMarquee.delay(this.options.speed, this);
    },
	
	checkEnd: function(pos){
		if(pos < -1 * (this.active_ticker.getCoordinates().width.toInt()))
           this.active_ticker.setStyle('left',this.active_ticker.getCoordinates().width.toInt() );
    }
					
	
});
