/*
version:		1.0
*/

function parseHtml( messageScript:String, returnHtml:Boolean )
{
	outputHtml = "<p>";
	outputText = "";
	for ( var index:Number = 0; index < messageScript.length; index++)
	{
		trace("index: "+index);
		var readCharacter = messageScript.charAt( index );
		trace("readCharacter: "+readCharacter);
		
		// detect escape character
		if (readCharacter == "\\")
		{
			trace("\t ESCAPE");
			// escape character, so read the next 2 digits
			var escapeCode = readCharacter;
			index++;		// now at  c  character
			escapeCode = messageScript.charAt( index );
			index++;		// now at  :  character
			escapeCode += messageScript.charAt( index );
			trace("escapeCode: "+escapeCode);
			// index is now at the  :  character
			
			if (escapeCode != "c\\")
			{
				// read value following the escape value
				var escapeValue = "";
				readCharacter = "";
				while( readCharacter != "\\"  &&  index < messageScript.length)
				{
					index++;
					readCharacter = messageScript.charAt( index );
					if (readCharacter != "\\")
					{
						escapeValue += readCharacter;
					}
				}
				trace("escapeValue: "+escapeValue);
			}else{
				// end color  =  no escapeValue
				var escapeValue = "";
			}
			
			readCharacter = "";
			// index is now at  \
			
			// react to escape code
			switch (escapeCode)
			{
				case "v:":
					// insert variable name  (optional)
					var insertString = _root.level.storyVariables[escapeValue];
					trace("insertString: "+insertString);
					if (insertString == undefined)
					{
						insertString = "";
					}
					outputHtml += insertString;
				break;
				
				case "c:":
					// add color code
					var insertString = "<font color='"+escapeValue+"'>";
					outputHtml += insertString;
				break;
				
				case "c\\":
					// add color code END
					var insertString = "</font>";
					outputHtml += insertString;
				break;
				
				case "a:":
					// alter alignment code
					var outputHtmlBegin = "<p";
					var sliceEnd = outputHtml.indexOf( ">" );
					var outputHtmlEnd = outputHtml.slice( sliceEnd );		// everything from  >  to the end of the string
					var insertString = " align='"+escapeValue+"'";
					outputHtml = outputHtmlBegin + insertString + outputHtmlEnd;
				break;
			}
			
		}
		
		// else:  write character to output
		outputHtml += readCharacter;
		outputText += readCharacter;
	}
	outputHtml += "</p>";
	
	
	
	// Return the result
	if (returnHtml)
	{
		// return html
		return outputHtml;
	}else{
		// return text
		return outputText;
	}
}