if(!console){
	var console = {
		log: function(){}
	};
}
	var Ticker = Class.create({
		initialize: function(news) {
			this.news = news;
			this.currentItem = null;
			this.playing = true;
//			this.transition = null;
			$('scrolltext').observe('click', this.setLocation.bind(this));
			this.next();
		},
		next: function() {
			if(!this.playing) return;
//			if(this.currentItem!=null){
/*				this.transition = new Effect.Parallel([
					new Effect.Opacity('tickerlabel', {sync: true, from:0.9, to:0.0}),
					new Effect.Opacity('tickertext', {sync: true, from:0.9, to:0.0})
				],{
					afterFinish: function(){
						this.resetCursor();
						this.setCurrentItem();						
						this.doTickerScroll();
					}.bind(this)
				});
*/
//			} else {
//				this.resetCursor();
				this.setCurrentItem()
				this.doTickerScroll();
//			}
		},
		resetCursor: function(){
			$('mask').setStyle({width: '0px'});
//			$('cursor').setStyle({left: '0px'});
//			$('cursor').show();		  
		},
		setCurrentItem: function(num){
			if(!isNaN(num))
				this.currentItem = num;
			else if (this.currentItem == null)
				this.currentItem = 0;
			else 
				this.currentItem++;
			if(this.currentItem > this.news.length-1)
				this.currentItem = 0;
			else if(this.currentItem < 0)
				this.currentItem = this.news.length-1;
			var item = this.news[this.currentItem];
			if(item.show){
				$('scrolltext').addClassName('show_hand');
			} else {
				$('scrolltext').removeClassName('show_hand');
			}
			$('scrolltext').update(item.headline);	
		},
		setLocation: function(event){
			event.stop();
			if(this.news[this.currentItem].show)
				document.location = this.news[this.currentItem].location;
		},
		doTickerScroll: function(){
//			new Effect.tagifyText('scrolltext');
//			var tags = $$('div#innertext span');
//			var tag = tags.last();

//			var dur = .08 * tags.length;
//			var rightmost = tag.cumulativeOffset().left + getMarginsAndPadding($('tickerlabel'));
//			var start = $('tickerlabel').cumulativeOffset().left + $('tickerlabel').getWidth();
//			var goto = rightmost - start;
//			console.log(goto)
//			var newWidth = 'width: ' + goto + 'px';
/*			this.transition = new Effect.Parallel([
				new Effect.Opacity('tickerlabel', {sync: true, from:0.0, to:0.9}),
				new Effect.Opacity('tickertext', {sync: true, from:0.0, to:0.9})
			],{
				queue: 'front'
			});
			this.transition = new Effect.Parallel([
				new Effect.Move('cursor', { sync: true, x: goto, y: 0, mode: 'absolute' }),
				new Effect.Morph('mask', {style: newWidth, sync: true})
			], { 
				duration: dur,
				queue: 'end',
				transition: Effect.Transitions.linear,
				afterFinish: function(){
					this.next.bind(this).delay(4);
				}.bind(this)
			});
*/			
			this.next.bind(this).delay(5);
		},
		jump: function(direction){
			this.pause();
			this.setCurrentItem(this.currentItem + direction);
		},
		pause: function(){
			this.playing = false;
//			if(this.transition!=null)
//				this.transition.cancel();
//			$('mask').setStyle({width: ($('tickertext').getWidth()+'px')});
//			$('cursor').hide();
//			$('tickertext').setOpacity(0.9);
//			$('tickerlabel').setOpacity(0.9);			
			$('pauseticker').writeAttribute({title: 'Play news ticker'});
			$('pauseticker').addClassName('play');
			$('pauseticker').removeClassName('pause');
		},
		play: function(){
			this.playing = true;
			this.next();
			$('pauseticker').writeAttribute({title: 'Pause news ticker'});
			$('pauseticker').addClassName('pause');
			$('pauseticker').removeClassName('play');			
		}

	});


function getMarginsAndPadding(El){
	var ml = 0//parseInt(El.getStyle('margin-left'));
	var pl = 0//parseInt(El.getStyle('padding-left'));
	var mr = parseInt(El.getStyle('margin-right'));
	var pr = parseInt(El.getStyle('padding-right'));
	return (ml?ml:0) + (pl?pl:0) + (mr?mr:0) + (pr?pr:0);
}
