$('link').each(function() {
	var lss = $(this);
	var lssurl = lss.attr('href');
	// Should this block?  Probably
	//  console.log('making ajax call for: ' + lssurl);
	$.ajax({url: lssurl, dataType:"text", async:false, success: function(css) { getcsscallback(lss, css); }});
});

function getcsscallback(lss, cssText) {	
	// console.log('getcsscallback: ' + cssText);	
	var vars = getVariables(cssText);		
	for (var i in vars) {
		i = trim(i);
		var val = trim(vars[i]);		
		cssText = cssText.replace(/@[A-z^:]+:.*/g, ''); // remove var line
		cssText = cssText.replace(new RegExp(i,  'g'), val); // replace replace variables
	}
	cssText = '<style>' + cssText + '</style>'; // TODO: Add media, etc, etc from the attributes of lss into this tag
	$('head').append(cssText);
}

function getVariables(cssText) {
	return getMatches(/(@[A-z^:]+):([^;]+);/g, cssText);
}

function getMatches(regex, text) {	
	var matches = {};
	var match = null;
	while (match = regex.exec(text)) { 		
		matches[match[1]] = match[2];		
	}
	return matches;
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}