var controlConfig = {
};

document.observe("dom:loaded", function()
{
	controlConfig = Object.extend({
		nCoreRegister: true
	}, controlConfig || {});
	
	if(controlConfig.nCoreRegister)
		nCore.Controls.register({
		controlName: "Skinned Radio Buttons",
		name: "radioButton",
		className: "nCore_radioButton",
		version: 0.2,		
		control: nCore_radioButton,
		behavior:{className: "nCore_radioButtonInput", behavior: nCore_radioButtonInput}		
		});
});







nCore_radioButtonInput = Behavior.create();
nCore_radioButtonInput.prototype.initialize = function(options) { if(Object.isUndefined(this.element.radioButtonClass)) new nCore_radioButton(this.element,options);};



var nCore_radioButton = Class.create();

nCore_radioButton.prototype.initialize = function(element)
{
	if(element.nodeName.toLowerCase() !== "input") return false;
	elType = element.readAttribute("type");
	if(elType == undefined) return false;
	if(elType.toLowerCase() != "radio") return false;
	
	this.element = element;
	this.element.radioButtonClass = this;
	this.baseClass = this.element.className;		
	this.div = $div({"class": this.baseClass});
	if(this.element.checked) this.div.addClassName(this.baseClass + "_on");
		
	this.div.owner = this;		
	this.element.hide();
		
	Element.insert(this.element,{'before':this.div});
	
	this._form = nCore.Element.getForm(this.element);

	this.captureRadioGroup();
	this.startObservers();
}

nCore_radioButton.prototype.captureRadioGroup = function()
{
	if(!this._form) var allRadios = $$("input");
	else var allRadios = this._form.select("input");
	
	this.radioGroup = new Array();
	if(allRadios.length) allRadios.each(function(radio)
	{
		$(radio);
		elType = radio.readAttribute("type");
		if(elType != undefined)
			if(elType.toLowerCase() == "radio")
				if(radio.readAttribute("name") == this.element.readAttribute("name"))
					if(radio != this.element)
						this.radioGroup.push(radio);
	}.bind(this));
}

nCore_radioButton.prototype.startObservers = function()
{
	this.div.observe("mouseover",this.onmouseover.bind(this));
	this.div.observe("mouseout",this.onmouseout.bind(this));
	this.div.observe("click",this.onclick.bind(this));
}

nCore_radioButton.prototype.onmouseover = function(e){this.div.addClassName(this.baseClass + "_hover");}
nCore_radioButton.prototype.onmouseout = function(e){this.div.removeClassName(this.baseClass + "_hover");}

nCore_radioButton.prototype.onchange = function(e)
{
	if(this.element.checked){ if(!this.div.hasClassName(this.baseClass + "_on")) this.div.addClassName(this.baseClass + "_on");}
	else{if(this.div.hasClassName(this.baseClass + "_on")) this.div.removeClassName(this.baseClass + "_on");}
}

nCore_radioButton.prototype.onclick = function(e)
{
	this.element.checked = true;
	this.onchange();
	this.sindicateGroupState();
}

nCore_radioButton.prototype.sindicateGroupState = function(){if(this.radioGroup.length) this.radioGroup.each(function(radio){ if(!Object.isUndefined(radio.radioButtonClass)) radio.radioButtonClass.onchange();});}