﻿/*global jQuery window console alert*/
(function (window) {
    "use strict";
	var $ = window.jQuery;
		
	$.log = function (message) {
        if (window.console && window.console.debug) {
            console.debug(arguments);
        } else if (window.console && window.console.log) {
            console.log(arguments);
        } else {
            alert(message);
        }
    };
	
	$.objectWalk = function (obj, func, args) {
        var i,
			ar = args ? args : [];
 
        if (typeof func !== "function") {
            throw new Error("file: custom.js, Error: " + func + " is not a function");
        }
        func.apply(obj, ar);
 
        for (i in obj) {
            if (obj[i] && typeof obj[i] === "object" && (!obj[i].length && obj[i].length !== 0)) {
                $.objectWalk(obj[i], func, ar);
            }
        }
    };
    
    $.spaceNumber = function(theNumber, groupSize, spaceChar) {
		var numStr = String(theNumber);
		    newNumber = '';
		    
        for (i=numStr.length; i>=0; i--)
        {
            
            newNumber = numStr.charAt(i) + newNumber;
            if ((i < numStr.length) && ((numStr.length-i) % 3 === 0)) {
                newNumber = ' ' + newNumber;
            }
        }
        
		return $.trim(newNumber);
	};
	
	$.trim = function (str, chars) {
		return $.ltrim($.rtrim(str, chars), chars);
	};
	 
	$.ltrim = function (str, chars) {
		chars = chars || "\\s";
		return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
	};
	 
	$.rtrim = function (str, chars) {
		chars = chars || "\\s";
		return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
	};
	
	$.noWhitespace = function (str) {
		var arr = str.split(" ");
		return arr.join('');
	};

   
    $.runAll = function () {
        var that = this,
			prop,
			obj;
       
        $.objectWalk(that, function () {
            if (this[arguments[0]]) {
                if (typeof this[arguments[0]] === "function") {
                    this[arguments[0]]();
                } else if (typeof this[arguments[0]] === "object" && (!this[arguments[0]].length && this[arguments[0]].length !== 0)) {
                    // this generates an Error in JSLInt:
                    // "Problem at line 41 character 26: Bad for in variable 'k'."
                   
                    // I have no idea what it should be called to be a 'good one'.
                    // The 'k' variable is apparently ok in '$.objectWalk'...
                    obj = this[arguments[0]];
                    for (prop in obj) {
                        if (typeof obj[prop] === "function") {
                            obj[prop]();
                        }
                    }
                }
            }
        }, [arguments[0]]);
    };
    
    $.runInit = function (obj) {
        $.runAll.apply(obj, ["init"]);
    };
    $.runLoad = function (obj) {
        $.runAll.apply(obj, ["load"]);
    };
	
}(window));
