$(document).ready(function(){
	identifyExternalLinks();
});

var absoluteUrlPattern = /^([^:\/?#]+):\/\/(([^@\/?#]*)@)?([^:\/?#]*)(:([0123456789]*))?([^?#]*)(\?([^#]*))?(#(.*))?/g;

function parseAbsoluteURL(urlString) {

    var components = {
        "scheme"    :null,
        "userinfo"  :null,
        "hostname"  :null,
        "host"      :null,
        "port"      :null,
        "path"      :null,
        "query"     :null,
        "fragment"  :null
    };
    
    var a = absoluteUrlPattern.exec(urlString);
    absoluteUrlPattern.lastIndex = 0;
    if(!a) return null;
    
    components.scheme   = a[1];
    components.userinfo = a[3];
    components.hostname = a[4];
    components.port     = a[6];
    components.path     = a[7];
    components.query    = a[9];
    components.fragment = a[11];
    
    components.host = components.hostname;
    if(components.port) components.host += ":" + components.port;
    
    // potential TODO -- remove redundant slashes found in the path component
    
    return components;
}

function identifyExternalLinks(){
	$("a").each(function(){
		var url = parseAbsoluteURL(this.href);
		if(url && url.host != location.host) $(this).addClass("external-link");
	});
}
