var counter = {};

counter.apiUrl = 'http://tools.fairsay.com/counter/API/rate/';
counter.jqueryUrl = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';

counter.counters = [];
counter.jqueryLoaded = (window['$'] != undefined) ? true : false;
counter.otherLib = false;
counter.loaderComplete = false;

(function(){

	if(	(counter.jqueryLoaded && window['$'] !== window['jQuery'])  || (!counter.jqueryLoaded && window['$'] != undefined)){
		counter.otherLib = true;
	}

	if(!counter.jQueryLoad){
		counter.jqueryLoaded = false;
		var scriptObj = document.createElement("script");

		scriptObj.type = "text/javascript";
		scriptObj.charset = "utf-8";
		scriptObj.id = 'counter-jquery';
		
		document.getElementsByTagName("head").item(0).appendChild(scriptObj);
		
		scriptObj.onreadystatechange = function(){
			if(this.readyState == 'loaded'){
				loaded();
			}
		}
		
		scriptObj.onload = function(){
			loaded();
		}
		
		scriptObj.src = counter.jqueryUrl;
		
	}else {
		loaded();
	}


	function loaded(){

		if(counter.jqueryLoaded) return;
		counter.jqueryLoaded = false;

		jQuery.fn.counter = function(filters, delay, sep){
			filters = filters || {};
			delay = delay || 15;
			sep = sep || ',';
			
			this.each(
				function(){
					jQuery.fn.counter.setup(this, filters, delay, sep);
				}
			);
		};

		jQuery.fn.counter.setup = function(element, filters, delay, sep){
		
			var options = {};
			options.interval = 0.5;
			options.filters = filters;
			options.delay = delay;
			options.$el = jQuery(element);
			options.sep = sep;

			jQuery.getJSON(
				jQuery.fn.counter.generateUrl(filters),
				function(d){
					var offset = Math.ceil((d.results[0].rate * delay));
					jQuery.fn.counter.startInterval(d.results[0].count - offset, d.results[0].count, options);
				}
			);
			
			setInterval(
				function(){
					jQuery.getJSON(
						jQuery.fn.counter.generateUrl(filters),
						function(d){
							jQuery.fn.counter.startInterval(options.current, d.results[0].count, options);
						}
					);
				},
				options.delay*1000
			);
		}

		jQuery.fn.counter.startInterval = function(from, to, options){

			options.from = options.current = from;

			if(from >= to) {
				var disp_text = counter_add_seperator(Math.ceil(from), options.sep);
				options.$el.text(disp_text);
			}
			if(options.tick){
				clearInterval(options.tick);
			}
			options.from = options.current = from;
			options.to = to;
			options.steps = options.delay / options.interval;
			options.rate = (to - from) / options.steps;
			options.tick = setInterval(
				function(){
					options.current += options.rate;
					var display_count = counter_add_seperator(Math.ceil(options.current), options.sep)
					if(options.current <= options.to){
						options.$el.text(display_count);
					}else {
						clearInterval(options.tick);
					}
				},
				options.interval*1000
			)
		}

		jQuery.fn.counter.generateUrl = function (filters){
			var URI = counter.apiUrl;
			$.each(filters, function(index,value){
				URI +=  encodeURIComponent(index) + '/' + encodeURIComponent(value) + '/';
			});
			return URI + '?callback=?';
		}

		if(counter.otherLib){
			jQuery.noConflict();
		}
		jQuery(function(){
			counter.loaded();
		});
	}
	
})();


counter.register = function(selector, filters, delay, sep){
	if(counter.jqueryLoaded){
		jQuery(selector).counter(filters, delay, sep);
	}else {
		counter.counters.push({selector: selector, filters: filters, delay:delay, sep:sep});
	}
}
counter.loaded = function(){
	var c;
	for(var i = 0, z = counter.counters.length; i<z; i++ ){
		c = counter.counters[i];
		jQuery(c.selector).counter(c.filters, c.delay, c.sep);
	}	
}

function counter_add_seperator(number, sep){
	var count_count = number.toString();
					var display_count = '';
					while (count_count.length > 3){
						var position = count_count.length - 3;
						var addon = '';
						if (display_count.length){
							addon = sep + display_count;
						}
						display_count = count_count.substring(position) + addon;
						count_count = count_count.substring(0, position);
					}
					display_count = count_count + sep + display_count;
					return display_count;
}




