var counterSourceURL = 'http://kerzenachlibyen.s3.amazonaws.com/counter.json';
var counterUpdateScriptURL = 'http://kerzenachlibyen.s3.amazonaws.com/media/js/counter_update.js'
var updateInterval = 2500;
var targetSelector = '.jquery_live_counter';
var counterDigitArray;

$.ajaxSetup ({
	cache : false
});

$(function () {
	
	localCnt = $(targetSelector).text();
	
	counterDigitArray = sliceToDigits($(targetSelector).text());
	
	$(targetSelector).html(sourroundHTML(counterDigitArray, '<span>', '</span>'));
	
	updateCounter();
});

function updateCounter() {
	var now = new Date();
	var counterUpdateScriptURLhash = counterUpdateScriptURL + '?' + now.getTime();
	jQuery.getScript(counterUpdateScriptURLhash);

	window.setTimeout('updateCounter()', updateInterval)
}

first_call = true

function sliceToDigits(string) {
	var digitArray = new Array();
    if (first_call) {
        document.title =  string + ' ' + document.title;
        first_call = false
    } else {
        document.title = string + ' ' + document.title.slice(string.length+1);
    }
	for (var cursor = 0 ; string.length > cursor ; cursor++) {
		digitArray.push(string.slice(cursor, cursor + 1));
	}
	
	return digitArray;
}

function sourroundHTML(array, preHTML, postHTML) {
	var html = "";
	
	for (var i = 0; i < array.length; i++) {
		html += preHTML + array[i] + postHTML;
	}
	
	return html;
}

function updateAnimation(target, compareArray) {	
	if ($(target).children().length != compareArray.length) {
		$(target).hide();
		$(target).html(sourroundHTML(compareArray, '<span>', '</span>'));
		$(target).fadeIn();
	};
	
	jQuery.each($(target).children(), function(index, value) {
		if ($(this).text() != compareArray[index]) {
			var span = $(this).clone();
			$(span).text(compareArray[index]).hide();
			$(this).after(span);
			$(this).remove();
			//$(this).slideUp("normal", function() {$(this).remove()} );
			$(span).fadeIn('slow');
		}
	});
}

