
(function($) {

//console.log();

    $.blueroom = $.blueroom || {};

    var g_ready = false;
    var g_self = this;

    $.fn.imgNumbers = function(options) {
        return this.each(function() {
            //alert("API method called on " + this);
            new $.blueroom.imgNumbers(this, options);
        });
    };
    
    $.blueroom.imgNumbers = function(element, options) {
        var c_self = this;
        var c_ready = false;
        c_self.options = $.extend({}, $.blueroom.imgNumbers.defaults, options);
        

        c_self.functions = {
            interval: function() {
                c_self.functions.debuglog("interval event");
                setTimeout(c_self.functions.interval, c_self.options.intervalFrequency);
            },
            initialise: function(context) {
                context.each(function(index) {
					//alert("function running on " + element);
					c_self.functions.replaceNumbers();
                });
            },
			replaceNumbers: function() {
				
				c_self.functions.trace("start " + $(element).html());
				var num = $(element).html();
				
				if(num.length < 3){
					var numLeft = 3 - num.length;
					c_self.functions.trace(numLeft);
					
					for(i=num.length; i<=2; i++){
						num = c_self.functions.addZero(num);
					}
					//c_self.functions.trace(num);
				}
				
				//c_self.functions.trace(" > " + num.charAt(0));
				
				var numArray = [num.charAt(0),num.charAt(1),num.charAt(2)];
				
				var img = $('<img />');
				var imgContainer = $('<div></div>');
				imgContainer.addClass('imgCount');
				
				for (var imgNum in numArray){
					//c_self.functions.trace("wow " + numArray[imgNum]);
					var img = img.clone();
					img.attr('src', 'Assets/Specific/ArsenalTrainingDay/Images/numbers/'+numArray[imgNum]+'.gif')
					imgContainer.append(img)
				}
				
				var parent = $(element);
				parent.replaceWith(imgContainer);
				
				
			},
			addZero: function(num){
				var updatedNum = "0" + num;
				return updatedNum;
			},
            preload: function() {
                c_self.functions.debuglog("preload");
                c_self.functions.preloadImages(/*insert array of images here*/);
            },
            preloadImages: function() {
                for (var i = 0; i < arguments.length; i++) {
                    $("<img>").attr("src", arguments[i]);
                }
            },
            trace: function(logtext) {
                try {
					if (console && console.log) {
						console.log(logtext);
					}
                } catch (err) {
					//alert(err);
                }
            }
        };

        c_self.functions.initialise($("body"));
        c_ready = true;

    };

    // defaults
    $.blueroom.imgNumbers.defaults = {
        //TODO:
        easing: "quad",
        animationSpeed: "1000",
        intervalFrequency: "10000"
    };

    // optional - initialise one or more instances of the class
    $(document).ready(function() {
        //new $.blueroom.imgNumbers();
        $('.count').imgNumbers();
    });
   })(jQuery);