function getMainRegEx()
{
	return new RegExp("%wstx\.(site|page|project)\.[a-z0-9]+?%", 'gi');
}

function getTokens(node)
{
	
	var mainRegex = getMainRegEx();
	var tokens = node.innerHTML.match(mainRegex);
	if (!tokens)
		return null;
	var uniqueTokens = new Hash();
	for (var j = 0; j < tokens.length; j++)
		uniqueTokens.setItem(tokens[j].toLowerCase(), null)
	return uniqueTokens;
}

function replaceTokens(node, tokens)
{
   	var setVisible = false;
	try
	{
		if (!node)
		{
			node = document.body;
			setVisible = true;
		}
		
		if (tokens == null)
			tokens = getTokens(node);
			
		if (tokens)
		{		
			if (node.hasChildNodes())
			{
			   //If td or table, replace the tokens in the background image if any.
			    if(node.tagName == 'TD' || node.tagName == 'td' || node.tagName == 'table' || node.tagName == 'TABLE')
			    {
			        replaceTokensMethod3(node);
			       replaceTokensMethod2(node,"background");
			    }
				for (var i=0; i < node.childNodes.length; i++)
				{
					replaceTokens(node.childNodes[i], tokens);
				}
			}
			else
			{
			   	// Only replace text node content
				if (node.nodeType == 3)
				{
					// replace known tokens & remove unknown tokens
					replaceTokensMethod1(node, tokens);
				}	
				else
				{
				   //If this is an image tag, then replace the tokens in the background image if any
				   if(node.tagName == 'IMG' || node.tagName == 'img')
				   {
				    	replaceTokensMethod2(node,"src");
				   }
				}			
			}
		}
	}
	finally
	{	
		if (setVisible)
			node.style.display = 'block';		
	}
}

function replaceTokensMethod1(node, tokens)
{
    var mainRegex = getMainRegEx();
	if (mainRegex.test(node.nodeValue))
	{		
		for (var token in tokens.items)
		{			
			var regexToken = new RegExp(token, 'gi');
			var replacement = (g_tokens.hasItem(token) ? g_tokens.getItem(token) : '');
			node.nodeValue = node.nodeValue.replace(regexToken, replacement);
		}
	}
}


//This will replace the value of a given attribute of a node with the correct colorset index.
function replaceTokensMethod2(node,attrName)
{
   
   //Get the Image src.
  // var nodeAttrib = node.attributes.getNamedItem(attrName);
	var nodeAttrib = node.getAttributeNode(attrName);
   if(nodeAttrib)
   {
	   var nodeValue = nodeAttrib.nodeValue;
	   var token = '%wstx.site.currentcolorsetindex%';
	   var mainRegex = new RegExp(token,'gi');
	   var flag = mainRegex.test(nodeValue);
	   if(flag) 
		{		
		    var replacement = (g_tokens.hasItem(token) ? g_tokens.getItem(token) : '');
			nodeValue = nodeValue.replace(token,replacement);
			nodeAttrib.nodeValue = nodeValue;
		}
    }
}


//This will replace the background image of the style attribute of the node.
function replaceTokensMethod3(node)
{
   var backgroundImage = node.style.backgroundImage;
   var token = '%wstx.site.currentcolorsetindex%';
   if(backgroundImage != '' && backgroundImage.indexOf(token) > -1)
   {
      var replacement = (g_tokens.hasItem(token) ? g_tokens.getItem(token) : '');
      node.style.backgroundImage = backgroundImage.replace(token,replacement);
	  
   }
}
	