/* 
ABC News Ticker script
Created by: Andrew Kesper, 3 July 2009
Modified by: Andrew Kesper, 5 January 2010
*/

var index = 0;
var items = new Array();
var paused = false;
var timer;
var defaultFontSize = 100;
var defaultFontUnit = '%';

if (window.parent) {
	$(window.parent.document).ready(go);
}
else {
	$(document).ready(go);
}

function go () {
	String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };
	defaultFontSize = $('#tickerText').css('fontSize').replace(/[^.0-9]+$/, '');
	defaultFontUnit = $('#tickerText').css('fontSize').replace(/^[.0-9]+/, '');
	t = $('#ticker').get(0);
	$('#tickerContent>div').each(function () {
		if ($('h1, h2', this).length > 0 && $('ul>li, p, h3', this).length > 0) {
			var classes = $(this).attr('class').trim();
			var heading = $('h1, h2', this).eq(0).html().trim();
			$('ul>li, p, h3', this).each(function () {
				var text = $(this).html().trim();
				var title = heading.replace(/<[^>]+>/g, '')+': '+text.replace(/<[^>]+>/g, '')+' - ABC News';
				items[items.length] = { 'classes':classes, 'heading':heading, 'text':text, 'title':title };
			});
		}
	});
	if (items.length <= 1) {
		$('#tickerNext, #tickerPrevious').css({visibility:'hidden'});
	}
	$('#ticker').mouseover(function () {
		$('#ticker').attr('class', 'hover');
		t.pause();
	});
	$('#ticker').mouseout(function () {
		$('#ticker').removeAttr('class');
		t.resume();
	});
	$('#tickerPrevious>a').click(function () {
		t.previous(true);
		$(this).blur();
	});
	$('#tickerNext>a').click(function () {
		t.next(true);
		$(this).blur();
	});
	if (navigator.userAgent.toUpperCase().indexOf('MSIE') != -1) { // disable double-clicks in Internet Explorer
		$('#tickerPrevious>a').dblclick(function () {
			$(this).click();
		});
		$('#tickerNext>a').dblclick(function () {
			$(this).click();
		});
	}
	t.pause = function () {
		paused = true;
		if (typeof timer != 'undefined') clearTimeout(timer);
	}
	t.resume = function () {
		paused = false;
		if (typeof timer != 'undefined') clearTimeout(timer);
		var delay = 6*1000;
		if (items[index].classes == 'breaking') delay = 12*1000;
		else if (items[index].classes == 'stream') delay = 9*1000;
		else if (items[index].classes == 'special') delay = 9*1000;
		delay += items[index].text.replace(/<[^>]+>/g, '').length*20; // extra time to read long text
		if (items.length > 1) timer = setTimeout('t.next(false)', delay);
	}
	t.next = function (manual) {
		if (typeof manual == 'undefined') var manual = false;
		index++;
		if (index >= items.length) index = 0;
		t.swap(manual);
	}
	t.previous = function (manual) {
		if (typeof manual == 'undefined') var manual = false;
		index--;
		if (index < 0) index = items.length-1;
		t.swap(manual);
	}
	t.copyfit = function () {
		var attempts = 100;
		var navOffset = $('#tickerPrevious>a').offset();
		var textOffset = $('#tickerText>span').offset();
		$('#tickerText').css({fontSize:defaultFontSize+defaultFontUnit});
		var currentFontSize = defaultFontSize;
		while (attempts > 0 && textOffset.left+$('#tickerText>span').outerWidth() > navOffset.left) {
			currentFontSize = currentFontSize - (defaultFontUnit == 'em' ? 0.1 : 1);
			$('#tickerText').css({fontSize:currentFontSize+defaultFontUnit});
			attempts--;
		}
	}
	t.swap = function (instant) {
		if (typeof instant == 'undefined') var instant = false;
		if (instant) {
			$('#tickerText>span').stop(true, true); // Stop animation
			$('#tickerHeading').attr('class', items[index].classes);
			$('body').addClass('ready');
			$('#tickerHeading>span').html(items[index].heading);
			$('#tickerText>span').html(items[index].text);
			t.copyfit();
			document.title = items[index].title;
			t.resume();
		}
		else {
			$('body').addClass('transition');
			$('#tickerText>span').fadeTo(300, 0, function () {
				$('#tickerHeading').attr('class', items[index].classes);
				$('body').addClass('ready');
				$('#tickerHeading>span').html(items[index].heading);
				$('#tickerText>span').html(items[index].text);
				t.copyfit();
				document.title = items[index].title;
				if (items[index].classes == 'breaking') { // Ticker style for breaking news
					var w = $('#tickerText>span').width();
					$(this).css({width: '0px', display: 'block'}).fadeTo(0, 1);
					if (this.style.removeAttribute) this.style.removeAttribute('filter'); // Fix ClearType in IE6/7
					$(this).animate({width: w+'px'}, w*6, 'linear', function () {
						$('#tickerText>span').css({width: 'auto', display: 'inline'});
						$('body').removeClass('transition');
					});
				}
				else {
					$(this).fadeTo(700, 1, function () { // Gentle fade-in for other items
						if (this.style.removeAttribute) this.style.removeAttribute('filter'); // Fix ClearType in IE6/7
						$(this).css({width: 'auto'});
						$('body').removeClass('transition');
					});
				}
				t.resume();
			});
		}
	}
	t.swap();
}
