function convertTextNodes(node, size)
{
	var image;
	
	if (node.nodeType == 3)
	{
		image = document.createElement('img');
		
		var prefix = node.data.replace(/^(\s*).*/,'$1');
		var suffix = node.data.replace(/.*?(\s*)$/,'$1');
		var text = node.data.replace(/(:?^\s*)|(\s*$)/, '');
		
		image.src = contextRoot+'/images/dynamic/text.jsp?c=0076ef&s=' + ((7 - size) * 3 + 3) + '&t=' + encodeURIComponent(text).replace('\'', '%27');
		image.style.visibility = 'hidden';
		image.alt = node.data;
		image.style.border = '0';
		
		if (prefix.length)
			node.parentNode.insertBefore(document.createTextNode(prefix), node);
		if (suffix.length)
			if (node.nextSibling)
				node.parentNode.insertBefore(document.createTextNode(suffix), node.nextSibling);
			else
				node.parentNode.appendChild(document.createTextNode(suffix));
		node.parentNode.replaceChild(image, node);
		
		if (ScriptingMagic && ScriptingMagic.fixPNG)
			ScriptingMagic.fixPNG(image);
			
		image.style.visibility = 'visible';
		
		return;
	}
	else
	{
		var i;
	
		for (i = node.childNodes.length - 1; i >= 0; i--)
			convertTextNodes(node.childNodes[i], size);
	}
}

$(function(){

// Change H1, H2, etc... into images
$('h1,h2,h3,h4,h5,h6').each(function(){
	if (typeof this.normalize == 'function') this.normalize();

	convertTextNodes(this, this.tagName.substring(1));
});

// Display label inside of text areas
$(':text[empty],:password[empty]').focus(emptyFocusHandler).blur(emptyBlurHandler).each(function(){
	if (this.id)
		$('label[for='+this.id+']').hide();
			
	emptyBlurHandler.call(this, null);
});



function emptyFocusHandler()
{
	if (this.value === this.getAttribute('empty'))
	{
		this.value = '';
			
		$(this).removeClass('empty');
		
		if (this.passwordField)
			this.setAttribute('type', 'password');
	}
}

function emptyBlurHandler()
{
	if ((this.value === '') || (this.value == this.getAttribute('empty')))
	{
		this.value = '';
		
		$(this).addClass('empty');
		
		if (this.getAttribute('type') == 'password')
		{
			try
			{
				this.setAttribute('type', 'text');
				this.passwordField = true;
			}
			catch (e) { }
		}
		
		this.value = this.getAttribute('empty');
	}
}

$('[shine]').mouseover(function(){
	var image = this;
	if (image.animating) return;

	image.animating = true;
	image.originalSrc = image.src;
	image.src = image.getAttribute('shine');
	setTimeout(function(){
		image.src = image.originalSrc;
		image.animating = false;
	}, 1500);
}).each(function(){
	var image = new Image();
	image.src = this.getAttribute('shine');
});

//////////////////////////////////////
// This is the auto-tab code
//////////////////////////////////////
$('input[maxlength]').keyup(function(e){
	var kc = e.keyCode;
		
	if ((kc < 32) ||
		(35 <= kc && kc <= 40) ||
		(kc == 46) ||
		(63232 <= kc) ||
		e.altKey || e.ctrlKey || e.metaKey)
	{
		return;
	}

	if (this.value.length != this.getAttribute('maxlength'))
	{
		return;
	}

	// TODO: check the tab-index first
	
	var i, c = this.form.elements.length;
	for (i = 0; i < c - 1; i++)
	{
		if (this.form.elements[i] == this)
		{
			var next = this.form.elements[i+1];
			next.focus();
			if (typeof next.select == 'function')
				next.select();
			return;
		}
	}
});

});