/** Searches for all input elements with className 'yuih-checkbox' and runs them through yuiCheckbox */
function autoYuiCheckboxes()
{
	var els = getElementsByClassName('yuih-checkbox');
	for (var i = 0; i < els.length; ++i)
		yuiCheckbox(els[i]);
}
/** Searches for all divs with className 'yuih-radiogroup' and runs through yuiRadio */
/** The group DIV id must begin with 'grp' */
function autoYuiRadios()
{
	var els = getElementsByClassName('yuih-radiogroup');
	for (var i = 0; i < els.length; ++i)
	{
		if (els[i].nodeNode != 'DIV' || els[i].id.substring(0, 3) != 'grp')
			yuiRadio(els[i].id);
	}
}

/** Makes all elements with className 'yuih-resizer' resizable */
/** Optional constructorArgs attribute to change style of resizer */
function autoResizers()
{
	var els = getElementsByClassName('yuih-resizer');
	for (var i = 0; i < els.length; ++i)
		yuiResize(els[i]);
}
function yuiResize(el)
{
	var args = {};
	if (el.attributes['resizerArgs'])
		eval('args = { ' + el.attributes['resizerArgs'].value + ' };');
	return new YAHOO.util.Resize(el, args);
}

/** Replaces a normal checkbox with a YUI-based one, using the existing label as the caption (if exists) and the title of the checkbox or label */
function yuiCheckbox(chkEl)
{
	// TODO: Move across classes other than yuih-checkbox
	
	// Will need all labels
	var labels = document.getElementsByTagName('label');
	
	// Look for matching label
	var label = chkEl.value;
	var lblEl = null;
	for (var i = 0; i < labels.length; ++i)
	{
		if (labels[i].attributes['for'] && labels[i].attributes['for'].value == chkEl.id)
		{
			lblEl = labels[i];
			break;
		}
	}
	if (lblEl != null)
	{
		label = lblEl.innerHTML;
		lblEl.style.display = 'none';
	}
	
	// Remember checked	event
	var ev = null;
	if (chkEl.onchange)
		ev = chkEl.onchange;
	
	// Convert to button
	var btnEl = new YAHOO.widget.Button(chkEl.id, { label: label });
	if (chkEl.title)
		btnEl.title = chkEl.title;
	else if (lblEl && lblEl.title)
		btnEl.title = lblEl.title;
	btnEl.value = btnEl.get('value');
	
	// Move check event
	if (ev)
		btnEl.on('checkedChange', ev);
}

/** Replaces a complete radio group with a YUI-based one, using the existing label as the caption (if exists) and the title of the radio or label */
/** The individual radio options MUST have id 'opt{id}{val}' where {val} is the option value */
function yuiRadio(id)
{
	if (id.substring(0, 3) != 'grp')
		id = 'grp' + id;
	var grpEl = document.getElementById(id);
	if (grpEl == null)
		return;
	// TODO: Move across classes other than yuih-radiogroup
	
	// Build button group
	var btnGrpEl = new YAHOO.widget.ButtonGroup(id);
	
	// Will need all labels
	var labels = document.getElementsByTagName('label');
	
	// TODO: Propagate onchange from originals
	
	// Edit each button
	var btns = btnGrpEl.getButtons();
	for (var i = 0; i < btns.length; ++i)
	{
		// Look for matching label for each button
		var lblEl = null;
		for (var j = 0; j < labels.length; ++j)
		{
			if (labels[j].attributes['for'] && labels[j].attributes['for'].value == btns[i]._button.id)
			{
				lblEl = labels[j];
				break;
			}
		}
		if (lblEl != null)
		{
			btns[i].set("label", lblEl.innerHTML);
			lblEl.style.display = 'none';
		}
		
		// Set old title on new object
		if (lblEl.title)
			btns[i].title = lblEl.title;
		else if (lblEl && lblEl.title)
			btns[i].title = lblEl.title;
	}
}