

function startTicker(data) {
	var atDate = new Date(data.countDate);
	var currentDate = new Date();
	var rate = data.rate * 1000;
	var timeElapsed = currentDate.getTime() - atDate.getTime();
	var countElapsed = timeElapsed / rate;
	var count = Math.round(data.count + countElapsed);
	
	window.ticker = new Object();
	window.ticker = {"count":count,"imagesUrl":imagesUrl,"imagesSuffix":imagesSuffix,"imageHeight":imageHeight,"imageWidth":imageWidth,"rate":rate}
	
	setTicker();
	
	//alert (imagesUrl+imagesSuffix);

}

function setTicker() {
	var strCount = window.ticker.count.toString();
	for (i=0; i < strCount.length; i++) {
		var currentNumber = strCount.slice(i,i+1);
		$('#ticker').append(constructDigit(currentNumber,i));
		$('#ticker .digit'+i).append(constructNumber(currentNumber,i));
		$('#ticker .digit'+i+' .digitImg').css('top','0');
		$('#ticker .digit'+i+' .digitImg').removeClass('newDigit');
	}
	
	timeoutID = window.setInterval(changeCount, window.ticker.rate);
	window.ticker.count++;
}

function constructDigit(number,digit) {
	var htmlDigit = '<div class="tickerNumber digit'+digit+'"></div>';
	return htmlDigit;
}
function constructNumber(number,digit) {
	var imagesUrl = window.ticker.imagesUrl;
	var imagesSuffix = window.ticker.imagesSuffix;
	var imageHeight = window.ticker.imageHeight;
	var imageWidth = window.ticker.imageWidth;
	var htmlNumber = '<img src="'+imagesUrl+number+imagesSuffix+'" alt="'+number+'" class="digitImg newDigit" width="'+imageWidth+'" height="'+imageHeight+'" />';
	return htmlNumber;
}

function numberSlide(count,digit) {
	$("#ticker .digit"+digit+" .digitImg").addClass("oldDigit").removeClass("newDigit");
	var oldDigit = $("#ticker  .digit"+digit+" .oldDigit");
	var htmlDigit = constructNumber(count,digit);
	$("#ticker .digit"+digit).append(htmlDigit);
	var newDigit = $("#ticker .digit"+digit+" .newDigit");	
	oldDigit.animate({ top: 0 - window.ticker.imageHeight - 3 }, { duration: 2000, queue: false }, function() { oldDigit.remove() } );
	newDigit.animate({ top: 0 }, { duration: 2000, queue: false } );
}

function changeCount() {
	var strCount = window.ticker.count.toString();
	var position = strCount.length - 1;
	var newDigit = parseFloat(strCount.slice(position,strCount.length));
	numberSlide(newDigit,position);
	
	if (newDigit == 0) {
		numberSlide(parseFloat(strCount.slice(position-1,strCount.length-1)),position-1);
	}

	window.ticker.count++;
}
