/**
* AFW module Button
*
* needs image/button.php
*/
if(typeof(afw) === 'undefined')
{
	var afw = {};
}
if(typeof(afw.module) === 'undefined')
{
	afw.module = {};
}
afw.module.button =
{
	params :
	{
		buttons :
		{
			className : null
			,collection : []
		}
		,path : '/images/button.php'
		,style : 'padding:0px !important; border:0px none !important; cursor: pointer !important; width:auto !important; overflow:visible !important;'
	}
	,init : function()
	{
		if(afw.common.functions.isUndefined(afw.module.pageRestriction)
		|| afw.module.pageRestriction.checkPage())
		{
			afw.module.button.getButtons();
			afw.module.button.parseButtons();
		}
	}
	,getButtons : function()
	{
		var result, inputs, i, m;
		result = afw.common.functions.getElements(document.body, 'button', afw.module.button.params.buttons.className);
		inputs = afw.common.functions.getElements(document.body, 'input', afw.module.button.params.buttons.className);
		if(!result)
		{
			result = [];
		}
		if(inputs)
		{
			for(i = 0, m = inputs.length; i < m; ++i)
			{
				switch(inputs[i].type)
				{
					case 'submit':
					case 'reset':
					case 'button':
						result.push(inputs[i]);
					break;
				}
			}
		}
		afw.module.button.params.buttons.collection = result;
	}
	,parseButtons : function()
	{
		var i, m, text, button, image, collection = afw.module.button.params.buttons.collection;
		for(i = 0, m = collection.length; i < m; ++i)
		{
			if(collection[i].nodeName.toUpperCase() == 'BUTTON')
			{
				text = collection[i].innerHTML.replace(/<\/?.*?>/mig, '');
				image = afw.module.button.createImage(text);
				collection[i].innerHTML = '';
				collection[i].style.cssText = afw.module.button.params.style;
				collection[i].appendChild(image);
			}
			else if(collection[i].nodeName.toUpperCase() == 'INPUT')
			{
				text = collection[i].value;
				button = afw.module.button.createButton(text);
				switch(collection[i].type)
				{
					case 'reset':
						button.onclick = function(){ var form = afw.common.functions.getParent(this, 'form'); if(form){ form.reset(); } };
					break;

					case 'button':
						button = afw.module.button.createButton(text, 'button');
					break;
				}
				afw.module.button.copyEvents(button, collection[i]);
				collection[i].parentNode.replaceChild(button, collection[i]);
			}
		}
	}
	,createButton : function(text, type)
	{
		var element;
		if(afw.common.functions.isUndefined(type))
		{
			type = 'image';
		}
		switch(type)
		{
			default:
				element = document.createElement('INPUT');
				element.type = 'image';
				element.src = afw.module.button.params.path + '?text=' + text;
			break;

			case 'button':
				element = document.createElement('BUTTON');
				try
				{
					element.type = 'button';
				}catch(e){}
				element.style.cssText = afw.module.button.params.style;
				var image = afw.module.button.createImage(text);
				element.appendChild(image);
			break;
		}
		return element;
	}
	,createImage : function(text)
	{
		var element = document.createElement('IMG');
		element.src = afw.module.button.params.path + '?text=' + text;
		return element;
	}
	,copyEvents : function(newButton, oldButton)
	{
		var x, e, onclick = function(){};
		for(x in oldButton)
		{
			e = x.toUpperCase();
			if(e == 'ONCLICK')
			{
				onclick = oldButton[x];
			}
		}
		if(oldButton.onclick)
		{
			newButton.onclick = oldButton.onclick;
		}
		else
		{
			newButton.onclick = onclick;
		}
	}
};
afw.common.functions.addEvent('load', window, afw.module.button.init);
