var MAG = Object.extend({}, MAG || {});

var radioHeight = "25";

MAG.Reserve = {
	Init: function(options)
	{
		this.options = {};
		Object.extend(this.options, options || {});	
		
		
		$$('.combo').each(function(e) {
			setTimeout(function(){new Autocompleter.SelectBox(e.id)},0);
		});
	}
	
};

MAG.Custom = {
	Init: function(options) {
		this.options = {};
		Object.extend(this.options, options || {});	
		
		var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
		for(a = 0; a < inputs.length; a++) {
			if(inputs[a].type == "radio") {
				span[a] = document.createElement("span");
				span[a].className = inputs[a].type;

				if(inputs[a].checked == true) {
						position = "0 -" + (radioHeight*2) + "px";
						span[a].style.backgroundPosition = position;
				}
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				inputs[a].onchange = MAG.Custom.clear;
				span[a].onmousedown = MAG.Custom.pushed;
				span[a].onmouseup = MAG.Custom.check;
				document.onmouseup = MAG.Custom.clear;
			}
		}
	},
	pushed: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "radio") {
			this.style.backgroundPosition = "0 -" + radioHeight*3 + "px";
		}  else {
			this.style.backgroundPosition = "0 -" + radioHeight + "px";
		}
	},
	check: function() {
		element = this.nextSibling;

		if(element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
		} else {
			this.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
			group = this.nextSibling.name;
			inputs = document.getElementsByTagName("input");
			for(a = 0; a < inputs.length; a++) {
				if(inputs[a].name == group && inputs[a] != this.nextSibling) {
					inputs[a].previousSibling.style.backgroundPosition = "0 0";
				}
			}
		}
		element.checked = true;
	},
	clear: function() {
		inputs = document.getElementsByTagName("input");
		for(var b = 0; b < inputs.length; b++) {
			if(inputs[b].type == "radio" && inputs[b].checked == true) {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
			} else if(inputs[b].type == "radio") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			}
		}
	}
};

document.observe('dom:loaded', function(){
	MAG.Reserve.Init();
	MAG.Custom.Init();
	
	var buttons = $$('button[type=button]');
	if(buttons.length)
	{
		for(i = 0; i < buttons.length; i++)
        {
			if(buttons[i].form
			   && (btn_id=buttons[i].id.toString())
			   && btn_id.indexOf('btn_') == 0
			   && (inp=buttons[i].form[btn_id.replace(/^btn_/, '')]))
			{
				if($(inp).hasClassName('date_input')) {
					Calendar.setup({inputField:inp.id, button: btn_id});
				}
			}
        }
	}
});