/**
 * DrPepper.Util
 *	
 * Utility functions for use across the site.
 *
 * Author:	Silvan Reinhold
 * Date:	2009-04-01
 **/

var DrPepper = DrPepper || {};

DrPepper.Util = {};


/**
 * URL-decode a string that was encoded using the PHP encoding function. (JS and PHP are incompatible in that respect.)
 *
 * @param	p_sString	String to be URL-decoded
 * @return	URL-decoded string
 **/
DrPepper.Util.urlDecode = function(p_sString)
{
	if (!p_sString) {
		return "";
	}
	
	var l_aReplacementMap = { "!": "%21", "%20": "+" };
	var l_sDecodedString = p_sString.toString();

	for (l_sReplace in l_aReplacementMap) {
		l_sDecodedString = l_sDecodedString.split(l_aReplacementMap[l_sReplace]).join(l_sReplace);
	}
	
	// End with decodeURIComponent, which most resembles PHP's encoding functions
	l_sDecodedString = decodeURIComponent(l_sDecodedString);
	return l_sDecodedString;
}


/**
 * Disable mouse selection of text inside a given DOM element.
 *
 * @param	p_sDomElementId		DOM ID of the element to be disabled for text selection
 **/
DrPepper.Util.disableSelection = function(p_sDomElementId)
{
	var l_xDomElement = document.getElementById(p_sDomElementId);

	if (l_xDomElement) {
		l_xDomElement.onselectstart = function () { return false; }
		l_xDomElement.onmousedown = function () { return false; }
	}
}

/**
 * Sets a new location.hash (must include # sign). 
 * Wrapped in a setTimeout to circumvent FF random screen artifact bug when updating the hash
 *
 * @param	p_sNewLocationHash	Hash to be set (with prepended # sign)
 * @param	p_nDelay			Optional delay in milliseconds (may be required if called from FF/Mac Flash, to avoid flicker)
 **/
DrPepper.Util.setLocationHash = function(p_sNewLocationHash, p_nDelay)
{
	if (p_sNewLocationHash) {
		if (p_nDelay == undefined) {
			window.location.hash = p_sNewLocationHash;
		}
		else {
			setTimeout(function() { window.location.hash = p_sNewLocationHash; }, p_nDelay);
		}
	}
}

/**
 * Returns the URL required to retrieve the specified text as a dynamically generated PNG
 * 
 * @param	p_sText			Text to be converted to a PNG
 * @param	p_nFontSize		Font size
 * @param	p_sLoColor		Font color (regular), as a 6-digit hex string (e.g. ffe297)
 * @param	p_sHiColor		Highlight color (for CSS sprites)
 * @param	p_nWidthLimit	Width to limit the string to (will break lines, if necessary)
 * @param	p_nOffsetHeight	Pixel offset for custom vertical placement of text (imagettfbbox() does not handle accents and descenders all that well)
 * 
 * @return	(String)		URL to invoke to retrieve the PNG from the server
 */
DrPepper.Util.getDynamicTextUrl = function(p_sText, p_nFontSize, p_sLoColor, p_sHiColor, p_nWidthLimit, p_nOffsetHeight)
{
	if (!p_nWidthLimit) p_nWidthLimit = 100;
	if (!p_nFontSize) p_nFontSize = 14;
	if (!p_sLoColor) p_sLoColor = "ffffff";
	if (!p_nOffsetHeight) p_nOffsetHeight = 0;
	
	return "/image/c" + p_sLoColor + (p_sHiColor ? p_sHiColor : "") + "s" + p_nFontSize + "l" + p_nWidthLimit + "h" + p_nOffsetHeight + "title_" + Base64.encode(encodeURI(p_sText)) + ".png";
}
