function urlencode(string) {
	string = string.replace(/\\r\\n/g,"\\n");
	var utftext = "";
	for (var n = 0; n < string.length; n++) {
		var c = string.charCodeAt(n);
        if (c == 10) {
			utftext += '\\n';
		} else if (c == 32) {
			utftext += '+';
		} else if (c < 128) {
			utftext += String.fromCharCode(c);
		} else if((c > 127) && (c < 2048)) {
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		} else {
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}
	}
	return escape(utftext);
}

function changeTextToBGI(field, size, color, align, font, width, imgsrc) {
	if (align == undefined) align = 'left';
	if (font == undefined) font = 'klavika';
	if (imgsrc == undefined) imgsrc = false;
	$(document).find(field).each(function(elem) {
		if (!$(this).hasClass('bgi')) {
			var src = 'http://images-sportigan.dk/' + font + '/' + size +
						'/' + color + '/' + (width == undefined || width == -1 ? $(this).width() : width) +
						'/' + (align != 'left' ? align + '/' : '') + 't/' +
						urlencode($(this).text()) + '.png';
			$(this).css({ 'width': (width == undefined || width == -1 ? $(this).width() : width) + 'px' });
			$(this).html('<span>' + $(this).text() + '</span>');
			$(this).addClass('bgi');

			if (imgsrc)
				$(this).prepend('<img src="' + src + '" />');
			else
				$(this).css({
					'background-image': 'url(\'' + src + '\')',
					'background-repeat': 'no-repeat',
					'background-position': 'top left'
				});
		}
	});
}
