$(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));
});

$('.button-light').each(function(){
	if (typeof this.normalize == 'function') this.normalize();
	
	createButton(this.firstChild, 'light');
});

$('.button-dark').each(function(){
	if (typeof this.normalize == 'function') this.normalize();
	
	createButton(this.firstChild, 'dark');
});

// 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 createButton(node, style)
{
	if (!node)
		return;
		
	var image = document.createElement('img');
	
	var minWidth = 0;
	
	var i,c;

	var classes = node.parentNode.className;
	
	if (classes)
	{
		var classes = classes.split(' ');
		for (i in classes)
		{
			var c = classes[i];

			if (c.match(/^mw\d+$/))
				minWidth = Number(c.replace(/\D/g,''));
		}
	}
	
	var color = 'ffffff';
	
	switch (style)
	{
		case 'light':
			color = '0f1a64';
	}
	
	image.src = contextRoot+'/images/dynamic/button.jsp?c=' + color + '&t=' + encodeURIComponent(node.data).replace('\'', '%27') + '&style=' + style + '&mw=' + minWidth;
	image.style.visibility = 'hidden';
	image.alt = node.data;
	image.style.border = '0';
	
	node.parentNode.replaceChild(image, node);
	
	// TODO: add png fix for IE6
	if (ScriptingMagic && ScriptingMagic.fixPNG)
		ScriptingMagic.fixPNG(image);
		
	image.style.visibility = 'visible';
	
	return;
}

function convertTextNodes(node, size)
{
	var image;
	
	if (node.nodeType == 3)
	{
		image = document.createElement('img');
		
		image.src = contextRoot+'/images/dynamic/text.jsp?c=262338&s=' + ((7 - size) * 5 + 0) + '&t=' + encodeURIComponent(node.data).replace('\'', '%27');
		image.style.visibility = 'hidden';
		image.alt = node.data;
		image.style.border = '0';
		
		node.parentNode.replaceChild(image, node);
		
		// TODO: add png fix for IE6
		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 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');
});

});