function initTicker() {
	function scrollTicker(ticker) {
		var pps = Number($(ticker).data('speed'));
		var delay = Number($(ticker).data('delay'));
		var npd = Math.ceil(pps * delay);
		var transition = $(ticker).data('transition');
		var inDelay = npd > 0, delayCounter = 0;
		
		var scrollPositionProp = 'scrollLeft';
		var elementSize = 'width';
		
		if (transition == 'Scroll Up') {
			scrollPositionProp = 'scrollTop';
			elementSize = 'height';
		}
		
		function tick() {
			if (inDelay) {
				delayCounter++;
				if (delayCounter >= npd) {
					delayCounter = 0;
					inDelay = false;
				}
			} else {
				ticker[scrollPositionProp]++;
				if (ticker[scrollPositionProp] >= $(ticker.firstChild)[elementSize]()) {
					var fc = ticker.firstChild;
					var w = $(fc)[elementSize];
					ticker.removeChild(fc);
					ticker.appendChild(fc);
					ticker[scrollPositionProp] -= w;
					if (npd > 0)
					{
						inDelay = true;
					}
				}
			}
		}

		var iv = setInterval(tick, 1000 / pps);
		window.onfocus = function() {
			clearInterval(iv);
			iv = setInterval(tick, 1000 / pps);
		}
		window.onblur = function() {
			clearInterval(iv);
			iv = null;
		};
		$(ticker).unbind("mouseenter mouseleave").mouseleave(function() { 
			clearInterval(iv);
			iv = setInterval(tick, 1000 / pps);
		}).mouseenter(function() {
			clearInterval(iv);
			iv = null;
		});
	}
	
	if ($('.ticker-item-body:first', this).width() == 0) {
		setTimeout(initTicker, 10);
		return;
	}
	$(".ticker .ticker-item:first-child").addClass("first");
	$(".ticker[data-transition='Scroll Left'] .ticker-item").each(function() {
		var width = $(".ticker-item-body", this).width();
		var imageWidth = $(".ticker-item-body img", this).size() ? 36 : 0;
		var tw = width - imageWidth;
		// tw = Math.max(tw, 130);
		if (window.console)
			console.log(this, width, imageWidth, tw);
		var tp = $('.ticker-item-body .title', this).offset().top;
		do {
			$(".ticker-item-body", this).css("width", Math.ceil(imageWidth + (tw  / 2)) + 'px');
			if (tp != $('.ticker-item-body .title', this).offset().top)
			{
				if (tw > 300) break;
				tw += 10;
				continue;
			}
			break;
		} while (true);
		// $(this).css("width", Math.ceil(40 + ($(".title", this).width() / 2)) + 'px');
	});
	$(".ticker").each(function() {
		$(this).addClass('ticker-' + $(this).data('transition').toLowerCase().replace(/\s+/g, '-'));
		if ($(this).data('transition') == 'None') {
			return;
		}
		
		if ($(this).data('transition') == 'Scroll Up' || this.scrollWidth > this.offsetWidth) {
			if ($(this).data('transition') == 'Scroll Left') {
				// If the item widths are not great enough, add more space.
				var mw = 0;
				$(".ticker-item", this).each(function() {
					mw = Math.max(mw, $(this).width());
				});
				if ((this.offsetWidth + mw) > this.scrollWidth)
					$(".ticker-item-body", this).css({ width: mw + 'px' });
			}
			scrollTicker(this);
		}
	});
}

$(initTicker);
	

