/*
	Roku.Button
*/

if(typeof(Roku) == "undefined")
    Roku = { classes : [] };

Roku.Button =
{
	buttonClassName				: "button",
	buttonLeftClassName			: "button_left",
	buttonContentClassName		: "button_content",
	buttonRightClassName		: "button_right",
	buttonClickPropName			: "on_click",
	buttonDisabledAttribName	: "button_disabled",
	
	hilightElement				: null,
	hilightElementColor			: null,
	hilightElementDecor			: null
};

Roku.classes.push(Roku.Button);

Roku.Button.on_mouse_over = function(ev)
{
	var element = Roku.Util.srcEventElement(ev);
	var button = Roku.Util.parentClassElement(element, this.buttonClassName);
	if(button && this.enabled(button))
	{
		if(this.hilightElement && (this.hilightElement != button))
		{
			this.hilightElement.style.color = this.hilightElementColor;
			this.hilightElement.style.textDecoration = this.hilightElementDecor;
		}
		
		this.hilightElement = button;
		this.hilightElementColor = button.style.color;
		this.hilightElementDecor = button.style.textDecoration;
		
		button.style.color = "#765CF0";
		button.style.textDecoration = "underline";
	}
}

Roku.Button.on_mouse_out = function()
{
	if(this.hilightElement)
	{
		this.hilightElement.style.color = this.hilightElementColor;
		this.hilightElement.style.textDecoration = this.hilightElementDecor;
		this.hilightElement = null;
	}
}

Roku.Button.on_mouse_click = function(ev)
{
	var element = Roku.Util.srcEventElement(ev);
	var button = Roku.Util.parentClassElement(element, this.buttonClassName);
	if(button && this.enabled(button))
	{
		var on_click_value = button.getAttribute(this.buttonClickPropName);
		if(!on_click_value)
			on_click_value = button[this.buttonClickPropName];
		
		if(typeof(on_click_value) == "string")
			window.eval(on_click_value);
		else if(typeof(on_click_value) == "function")
			on_click_value(button, ev);
	}
}

Roku.Button.set_disabled = function(button, disabled)
{
	if(!button)
		return;

	var left = Roku.Util.childClassElement(button, this.buttonLeftClassName);
	if(left)
		left.style.backgroundImage = disabled ? "url('images/button-left-disabled.gif')" : "";
	
	var center = Roku.Util.childClassElement(button, this.buttonContentClassName);
	if(center)
		center.style.backgroundImage = disabled ? "url('images/button-center-disabled.png')" : "";

	var right = Roku.Util.childClassElement(button, this.buttonRightClassName);
	if(right)
		right.style.backgroundImage = disabled ? "url('images/button-right-disabled.gif')" : "";

	button.disabled = disabled;
	button.style.cursor = disabled ? "default" : "";
	if(disabled)
		button.setAttribute(this.buttonDisabledAttribName, "true");
	else
		button.removeAttribute(this.buttonDisabledAttribName);
	
}

Roku.Button.disabled = function(button)	{ return button ? (button.getAttribute(this.buttonDisabledAttribName) == "true") : true; }
Roku.Button.disable = function(button, disabled)
{
	if(typeof(disabled) == "undefined")
		disabled = true;
	if(this.disabled(button) != disabled)
		this.set_disabled(button, disabled ? true : false);
}

Roku.Button.enabled = function(button)	{ return button ? (button.getAttribute(this.buttonDisabledAttribName) != "true") : false; }
Roku.Button.enable = function(button, enabled)
{
	if(typeof(enabled) == "undefined")
		enabled = true;
	if(this.enabled(button) != enabled)
		this.set_disabled(button, enabled ? false : true);
}

Roku.Button.get_text = function(button)
{
	var center = Roku.Util.childClassElement(button, this.buttonContentClassName);
	return center ? center.innerHTML : "";
}

Roku.Button.set_text = function(button, text)
{
	var center = Roku.Util.childClassElement(button, this.buttonContentClassName);
	if(center)
		center.innerHTML = text;
}


Roku.Button.initialize = function()
{
	var mouseOverHandler = function() { return Roku.Button.on_mouse_over((0 < arguments.length) ? arguments[0] : null); };
	var mouseOutHandler = function() { return Roku.Button.on_mouse_out((0 < arguments.length) ? arguments[0] : null); };
	var mouseClickHandler = function() { return Roku.Button.on_mouse_click((0 < arguments.length) ? arguments[0] : null); };

	var buttons = (Roku.Body && Roku.Body.all) ? Roku.Body.all[this.buttonClassName] : Roku.Util.childrenClassElements(document.body, this.buttonClassName);
	var buttons_count = buttons ? buttons.length : 0; 
	for(var index = 0; index < buttons_count; index++)
	{
		var button = buttons[index]; 

		Roku.Util.eventHandler(button, "mouseover", mouseOverHandler);
		Roku.Util.eventHandler(button, "mouseout", mouseOutHandler);
		Roku.Util.eventHandler(button, "click", mouseClickHandler);

		if(this.disabled(button))
			this.set_disabled(button, true);
	}
}

