function removeClass( element, classname )
{
	var newClassName = "";
	if( element != null 
			&& element.className != null
			&& classname != null )
	{
		var classElements = element.className.split(" ");
		for( var classElementIndex in classElements )
		{
			var classElement = classElements[ classElementIndex ];
			if( classElement != classname )
			{
				if( newClassName.length > 0 )
				{
					newClassName = newClassName + " ";
				}
				newClassName = newClassName + classElement;
			}
		}
		element.className = newClassName;
	}
	return newClassName;
}

function hasClass( element, classname )
{
	if( element != null 
			&& element.className != null
			&& classname != null )
	{
		
		var classElements = element.className.split(" ");
		for( var classElementIndex in classElements )
		{
			var classElement = classElements[ classElementIndex ];
			if( classElement == classname )
			{
				return true;
			}
		}
	}
	return false;
}

function addClass( element, classname )
{
	var newClassName = "";
	if( element != null 
			&& element.className != null
			&& classname != null )
	{
		newClassName = element.className;
		if( newClassName.length > 0 )
		{
			newClassName = newClassName + " ";
		}
		newClassName = newClassName + classname;
		element.className = newClassName;
	}
	return newClassName;
}

function actionbuttonGetButton( buttonChildOrId )
{
	var element = null;
	if( typeof(buttonChildOrId) == "string" )
	{
		element = document.getElementById( buttonChildOrId );
	}
	else if( typeof(buttonChildOrId) == "object" )
	{
		element = buttonChildOrId;
	}
	while( element != null
			&& !hasClass( element, "ActionButton" ) )
	{
		element = element.parentNode;
	}
	return element;
}

function _actionbuttonResetChildnode( childnode, triggerClass )
{
	if( childnode != null 
			&& hasClass( childnode, triggerClass ) )
	{
		removeClass( childnode, "Hover" + triggerClass );
		removeClass( childnode, "Disabled" + triggerClass );
		removeClass( childnode, "Clicked" + triggerClass );
		
		if( triggerClass == "ShapeL" )
		{
			_actionbuttonSwitchContainedImg( childnode, /left_hover/g, "left" );
			_actionbuttonSwitchContainedImg( childnode, /left_clicked/g, "left" );
			_actionbuttonSwitchContainedImg( childnode, /left_disabled/g, "left" );
		}
		if( triggerClass == "ShapeR" )
		{
			_actionbuttonSwitchContainedImg( childnode, /right_hover/g, "right" );
			_actionbuttonSwitchContainedImg( childnode, /right_clicked/g, "right" );
			_actionbuttonSwitchContainedImg( childnode, /right_disabled/g, "right" );
		}
	}
}

function _actionbuttonSetState( button, stateCode /*optional*/ )
{
	if( button != null )
	{
		for( nodeIndex in button.childNodes )
		{
			var child = button.childNodes[nodeIndex];
			if( hasClass( child, "ShapeL" ) )
			{
				_actionbuttonResetChildnode( child, "ShapeL" );
				if( stateCode != null )
				{
					addClass( child, stateCode + "ShapeL" );
					_actionbuttonSwitchContainedImg( child, /left/g, "left_" + stateCode.toLowerCase() );
				}
			}
			if( hasClass( child, "ShapeR" ) )
			{
				_actionbuttonResetChildnode( child, "ShapeR" );
				if( stateCode != null )
				{
					addClass( child, stateCode + "ShapeR" );
					_actionbuttonSwitchContainedImg( child, /right/g, "right_" + stateCode.toLowerCase() );
				}
			}
			if( hasClass( child, "Label" ) )
			{
				_actionbuttonResetChildnode( child, "Label" );
				if( stateCode != null )
				{
					addClass( child, stateCode + "Label" );
				}
			}
			
			if( hasClass( child, "Action" ) )
			{
				_actionbuttonResetChildnode( child, "Action" );
				if( stateCode != null )
				{
					addClass( child, stateCode + "Action" );
				}
			}
		}
	}
}

function _actionbuttonSwitchContainedImg( child, srcRegex, srcReplace )
{
	for( nodeIndex2 in child.childNodes )
	{
		var child2 = child.childNodes[nodeIndex2];	
		if( child2.nodeName != null
				&& child2.nodeName.indexOf( "IMG" ) != -1 )
		{
			child2.src = child2.src.replace( srcRegex, srcReplace );
		}
		// using src replacement for png images loaded with filter:progid
		else if(child2.nodeName != null
				&& child2.nodeName.indexOf( "DIV" ) != -1 
				&& child2.className.indexOf( "PngIMGRepDiv") != -1 )
		{
			child2.style.filter = child2.style.filter.replace( srcRegex, srcReplace );
		}
	}
}


function actionbuttonHover( buttonChildOrId, moveIn )
{
	var button = actionbuttonGetButton( buttonChildOrId );
	if( button != null )
	{
		if( moveIn == null 
				|| moveIn == true)
		{
			_actionbuttonSetState( button, "Hover" );
		}
		else
		{
			_actionbuttonSetState( button );
		}
	}
}

var actionbuttonDisabledLinks = new Array();

function ActionbuttonDisabledLinks( anchorElement, href, onclick, onmouseover, onmouseout )
{
	this.anchorElement = anchorElement;
	this.href = href;
	this.onclick = onclick;
	this.onmouseover = onmouseover;
	this.onmouseout = onmouseout;
}

function _actionbuttonGetDisabledLinks( anchorElement ) {

	for( var index in actionbuttonDisabledLinks )
	{
		var linksElement = actionbuttonDisabledLinks[index];
		if( linksElement.anchorElement == anchorElement )
		{
			return linksElement;
		}
	}
	return null;
}

function actionbuttonDisable( buttonChildOrId, stateCode /*optional*/ )
{
	var button = actionbuttonGetButton( buttonChildOrId );
	if( button != null )
	{
		_actionbuttonDisableButtonLink( button );
		if( stateCode == null )
		{
			_actionbuttonSetState( button, "Disabled" );
		}
		else
		{
			_actionbuttonSetState( button, stateCode );
		}
			
	}
}

function _actionbuttonDisableButtonLink( buttonChildOrId )
{
	var button = actionbuttonGetButton( buttonChildOrId );
	if( button != null )
	{
		for( nodeIndex in button.childNodes )
		{
			var child = button.childNodes[nodeIndex];
			if( hasClass( child, "Action" ) )
			{
				for( nodeIndex2 in child.childNodes )
				{
					var child2 = child.childNodes[nodeIndex2];	
					if( child2.nodeName != null
							&& child2.nodeName.indexOf( "A" ) != -1 )
					{
						if( _actionbuttonGetDisabledLinks(child2) == null )
						{
							actionbuttonDisabledLinks.push( new ActionbuttonDisabledLinks ( 
								child2,
								child2.href,
								child2.onclick,
								child2.onmouseover,
								child2.onmouseout
							) );
						}
						
						child2.href = "javascript: void();";
						child2.onmouseover = null;
						child2.onmouseout = null;
						child2.onclick = function() { return false; };
					}
				}
			}				
		}
	}
}

function actionbuttonEnable( buttonChildOrId )
{
	var button = actionbuttonGetButton( buttonChildOrId );
	if( button != null )
	{
		_actionbuttonEnableButtonLink( button );
		_actionbuttonSetState( button );
	}
}

function _actionbuttonEnableButtonLink( buttonChildOrId )
{
	var button = actionbuttonGetButton( buttonChildOrId );
	if( button != null )
	{
		for( nodeIndex in button.childNodes )
		{
			var child = button.childNodes[nodeIndex];
			if( hasClass( child, "Action" ) )
			{
				for( nodeIndex2 in child.childNodes )
				{
					var child2 = child.childNodes[nodeIndex2];	
					if( child2.nodeName != null
							&& child2.nodeName.indexOf( "A" ) != -1 )
					{
						var linksObject = _actionbuttonGetDisabledLinks(child2);
						if( linksObject != null )
						{
							child2.href = linksObject.href
							child2.onmouseover = linksObject.onmouseover;
							child2.onmouseout = linksObject.onmouseout;
							child2.onclick = linksObject.onclick;
						}
					}
				}
			}				
		}
	}
}

var actionbuttonDisableElement = null;
function _actionbuttonDisableAfterClick(  )
{
	var element = actionbuttonDisableElement;
	if( element != null )
	{
		_actionbuttonDisableButtonLink( element );
	}
}

function actionbuttonDisableAfterClick( buttonChildOrId )
{
	var element = actionbuttonGetButton( buttonChildOrId );
	_actionbuttonSetState( element, "Clicked" );
	actionbuttonDisableElement = element;
	window.setTimeout( "_actionbuttonDisableAfterClick()", 20 );
}


