/**
 * Convert all HTML entities to their applicable characters 
 * @param {String} str
 * @return - decoded string
 * @type String
 */
jQuery.htmlEntitiesDecode = function(str) {
	if ((typeof str == "string") && (str.search(/&.*?;/) != -1)) {
		str = $("<textarea/>").html(str).val();
	}	
	return str;
};

/**
 * Escape regular expression special characters in the given string
 * @param {String} str
 * @return - escaped string
 * @type String
 */
jQuery.escapeRegexSpecialChars = function(str) {
	var specials = ["/", ".", "*", "+", "?", "|", "(", ")", "[", "]", "{", "}", "\\"];
	var regexp = new RegExp("(\\" + specials.join("|\\") + ")", "gim");
	
	return str.replace(regexp, "\\$1"); 
};

/**
 * Stylize the number given and adds a suffix
 * For example if number param is 12345 and the give suffix is "foo" the function result
 * will be "12 345 BGN". If intervalBeforeSuffix is set to false then interval won't be added. 
 */
jQuery.formatInvPriceString = function(number, suffix, intervalBeforeSuffix, isInteger) {
	
	if(intervalBeforeSuffix === undefined || 
			(intervalBeforeSuffix !== true && intervalBeforeSuffix !== false)) {
		intervalBeforeSuffix = true;
	}
	
	if(isInteger === undefined || 
			(isInteger !== true && isInteger !== false)) {
		isInteger = false;
	}
	
	number = parseFloat(number);
	if(!isInteger) {
		number = number.toFixed(2);
	}
	number = number.toString();
	var decimalPart = "";
	var decimalPointPos = number.lastIndexOf("."); 
	if(decimalPointPos > 0) {
		decimalPart = number.substring(decimalPointPos);
		number = number.replace(decimalPart, "");
	}
	
	var numberParts = new Array();
	var j = 1;
	for(var i = number.length - 1; i >= 0; i--) {
		if(j % 3 == 0) {
			numberParts.unshift(number.substr(i, 3));
			number = number.substring(0, i);
		} else if(i == 0) {
			numberParts.unshift(number);
		}
		j++;
	}
	
	var interval = "";
	if(intervalBeforeSuffix) {
		interval = " ";
	}
	number = numberParts.join(" ") + decimalPart + interval + suffix;
	return number;
};

/**
 * Similar to String.format.
 * Usage: "{0}, {1}, {2}".format("foo", "bar", "baz");
 * Will produce: "foo, bar, baz".
 * 
 * @returns {String}
 */
String.prototype.format = function() {
	var formatted = this.valueOf();
 	for(var i = 0; i < arguments.length; i++) {	 
		formatted = formatted.replace("{" + i + "}", arguments[i]);
	}	
	return formatted;
};

/**
 * Tries to find a translation, using the key. If there is no translation, the key is returned instead.
 * The key may contain spaces, but they are going to be replaced by underscores.
 * If the translated text contains formatting, just pass the strings as additional parameters.
 * 
 * Example usage (from anywhere):
 * translate("Hello world"); 
 * translate("This string contains formatting", "foo", "bar", "baz");
 * 
 * @returns {String}
 */
window.translate = function(key) {
	var realKey = key.replace(/\ /g, "_");
	var translatedText = new String;
	if(translation[realKey] != undefined) {
		translatedText = translation[realKey];
		if(arguments.length > 1) {
			for(var i = 0; i < arguments.length - 1; i++) {
				translatedText = translatedText.replace("{" + i + "}", arguments[i+1]);
			}
		}
	} else {
		translatedText = key;
	}
	return translatedText;
}; 

