var registredRadioGroup = new Array();

$(function(){
	var infoButton = $('a.infoButton');
	
	if ($(infoButton).length > 0)
	{
		$(infoButton).live('click', function(){ 
			$('div.moreInformation').hide();
			$(this).next('div.moreInformation').show();
		});
		
		$('div.moreInformation')
			.live('click', function(){ $(this).hide(); })
			.live('mouseout', function(){ setTimeout('closeInfoWindows()', 500); });
	}
	
	$.selectBoxes = $('select');
	
	$.selectBoxes.each(function(){ 
		var boxName = $(this).attr('name')
		,	boxId   = $(this).attr('id');
		
		$('option', this).each(function(index){
			var optionText = $(this).text();
			
			if (/#([^#]+)#/i.test(optionText))
			{
				var questionType = optionText.match(/#([^#]+)#/i);
				
				$(this)
					.attr('rel', index)
					.text(optionText.replace(/#([^#]+)#/i, ''))
					.attr('value', $(this).text());
				
				if ('input' == questionType[1])
				{
					$('<input class="extraField" type="text" rel="' + boxName + '_extra' + index + '" name="' + boxName + '_extra" id="' + boxName + index + '" value="" />')
						.insertAfter($('#' + boxId));
				}
				else if ('textarea' == questionType[1])
				{
					$('<textarea class="extraField" rel="' + boxName + '_extra' + index + '" name="'+ boxName + '_extra" id="'+ boxName + index + '"></textarea>')
						.insertAfter($('#' + boxId));
				}
			}
		});
		
		$(this)
			.change(toggleExtraField)
			.change();
	});

//	$('.extraField')
//		.click(function(e){ e.preventDefault(); })
//		.each(function(){ 
//			var fieldId   = $(this).attr('id').replace('_extra', '');
//			var fieldName = $('#' + fieldId).attr('name');
//			var fieldType = $('#' + fieldId).attr('type');
//			
//			if ('radio' == fieldType)
//			{
//				registredRadioGroup[registredRadioGroup.length] = fieldName;
//			}
//		});
//	
//	if ($(registredRadioGroup).size() > 0)
//	{
//		registredRadioGroup = $.unique(registredRadioGroup);
//		
//		var uniqueRadioGroupNames = $.grep(registredRadioGroup, function(v, k) {
//		    return $.inArray(v, registredRadioGroup) === k;
//		});
//		
//		$(uniqueRadioGroupNames).each(function(index, groupName){
//			$('input[name=' + groupName + ']:radio').bind('click', toggleExtraField);
//		});
//	}
});



function closeInfoWindows()
{
	$('div.moreInformation').hide();
}

function toggleExtraField()
{
	
	var fieldName		= $(this).attr('name')
	,	selectedIndex 	= $('option:selected', this).attr('rel');
	
	$('[name=' + fieldName + '_extra]').hide();
	
	if (selectedIndex !== undefined)
	{
		$('[rel=' + fieldName + '_extra' + selectedIndex + ']')
			.show()
			.css({ 'clear' : 'both', 'float' : 'none', 'display' : 'block' }) // IE 7 fix
			.focus();
	}
	
//	var fieldId   = $(this).attr('id'), 
//		fieldName = $(this).attr('name');
//	
//	if ($('#' + fieldId + '_extra').size() == 1)
//	{
//		var theExtraField = $('#' + fieldId + '_extra')[0];
//		
//		$('.extraField[name=' + fieldName + '_extra]')
//			.hide()
//			.not($(theExtraField))
//			.val('')
//			.attr('disabled', 'disabled');
//		
//		$(theExtraField).attr('disabled', '').removeAttr('disabled').show();
//	}
//	else
//	{
//		$('.extraField[name=' + fieldName + '_extra]')
//			.hide()
//			.val('')
//			.attr('disabled', 'disabled');
//	}
}
