

(function () {

    var undefined;
	
	
	if(window.console == undefined){
		window.console = {
			log:function(){}
		};
	}
	
	if(window.assert == undefined){
		window.assert = function(condition, message){
			if(!condition){
				console.log(message);
				return false;
			}
			return true;
		}
	}

	window.popup = function (url, id, options) {
        var opts = $.extend({
            height: null,
            width: null,
            location: null,
            fullscreen: 0
        }, (typeof id === "object") ? id : options),
            win = window.open(url, id || window.location.host, (function () { var str = ""; for (var optName in opts) { str += optName + "=" + opts[optName] + ","; } return str; })());
        return win;
    };

    window.printHtml = function (htmlStr, opts) {
        var win = window.popup(null, "print", $.extend({ height: 500, width: 800 }, opts));
        win.document.writeln(htmlStr);
        win.print();
        return win;
    };

	
	//Add :external selector to jQuery
    $.expr[':'].external = function(obj){
        return obj.href && !obj.href.match(/^(mailto|tel)\:/)
                && (obj.hostname != location.hostname);
    };
	
	//Center an element in the window
	jQuery.fn.center = function () {
		this.css("position","absolute");
		this.css("top", ( $(window).height() - this.outerHeight(true) ) / 2+$(window).scrollTop() + "px");
		this.css("left", ( $(window).width() - this.outerWidth(true) ) / 2+$(window).scrollLeft() + "px");
		return this;
	}
	
	// Modified from http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript
	Number.format = function(num, precision, sep, dec){
        var n = parseFloat(num), 
            c = isNaN(precision = Math.abs(precision)) ? 2 : precision, 
            t = !sep ? "," : sep, 
            d = !dec ? "." : dec, 
            s = n < 0 ? "-" : "", 
            i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", 
            j = (j = i.length) > 3 ? j % 3 : 0;
        return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
    };
	
	
	//ContextString is useful to allow instance-relevant actions to be created in HTML without needing a global reference to the relevant objects.
    //    See SiteClassBase.bindContextAttr() for a useful implementation.
	var ContextString = window.ContextString = function(original, aliases, prefix){
	    if(!(this instanceof ContextString)){
            return new ContextString(original, aliases, prefix);
        }
        
	    if(!assert(original, "ContextString requires a valid string.")) return;
	    this.original = original;
	    if(aliases) this.aliases = aliases;
	    
	    if(prefix) this.prefix = prefix;
	};
	
	ContextString.prototype = {
	    original:null,	    
	    aliases:{window:window},
	    aliasesAlias:"aliases",
	    prefix:"$",
	    eval:function(){
	        var str = this.toString();
	        try{
	            eval("var "+this.aliasesAlias+" = this.aliases");
	            eval(str);
	        }catch(err){
	            console.log("There was an error evaluating \""+str+"\"");
	            console.log(err);
	        }
	    },
	    toString:function(){
	        //Create regex for the replace
		    var regex = "",
		        orig = this.original,
		        prefix = this.prefix,
		        aA = this.aliasesAlias;
		        
		    for(var alias in this.aliases){
                regex += ((regex.length ? "|" : "")) + "("+prefix+alias+"\\.)";
            }
            regex = new RegExp(regex,"g");
            
            var str = orig.replace(regex, function(m){	                    
                    return aA+"."+m.substring(prefix.length, m.length-1)+".";
                });
            
            return str;
	    }
	};
	
	




})();







