/*
	Roku.Combo
*/

if(typeof(Roku) == "undefined")
    Roku = { classes : [] };

Roku.Combo =
{
	comboClassName				: "combo",
	displayClassName			: "display",
	dropDownClassName			: "drop",
	contentClassName			: "content",
	leftClassName				: "left",
	rightClassName				: "right",
	contentTextAttribName		: "content_text",
	
	selChangedEventName			: "on_selection_changed",
	dropDownEventName			: "drop_down",
	displayHilightHandler		: "display_hilight_handler",
	dropHilightHandler			: "drop_hilight_handler",
	
	currentDropDown				: null,
	currentHilight				: null
};

Roku.classes.push(Roku.Combo);

Roku.Combo.selected = function(combo)
{
	var display = Roku.Util.childClassElement(combo, this.displayClassName);
	var display_entry = Roku.Util.childClassElement(display, this.contentClassName);
	return display_entry ? display_entry.id : null;
}

Roku.Combo.selected_entry = function(combo)
{
	var selected_id = this.selected(combo);
	return selected_id ? this.contains(combo, selected_id) : null;
}

Roku.Combo.select = function(combo, id)
{
	var drop_down = Roku.Util.childClassElement(combo, this.dropDownClassName);
	var selItem = Roku.Util.childIdElement(drop_down, id);
	this.selectItem(combo, selItem);
}

Roku.Combo.contains = function(combo, id)
{
	var drop_down = Roku.Util.childClassElement(combo, this.dropDownClassName);
	var selItem = Roku.Util.childIdElement(drop_down, id);
	return selItem;
}

Roku.Combo.on_click_display = function(ev)
{
	var element = Roku.Util.srcEventElement(ev);
	var combo = Roku.Util.parentClassElement(element, this.comboClassName);
	this.showDropDown(combo);
}

Roku.Combo.on_hilight_display = function(ev)
{
	var element = Roku.Util.srcEventElement(ev);
	var display = Roku.Util.parentClassElement(element, this.displayClassName);
	this.hilightDisplay(display, ev && !Roku.Util.CompareNoCase(ev.type, "mouseout"));
}

Roku.Combo.hilightDisplay = function(display, hilight)
{
	var combo = Roku.Util.parentClassElement(display, this.comboClassName);
	var drop_down = Roku.Util.childClassElement(combo, this.dropDownClassName);
	
	if(this.currentDropDown && (drop_down == this.currentDropDown))
		hilight = false;
	
	if(combo && (typeof(combo[this.displayHilightHandler]) == "function"))
	{
		combo[this.displayHilightHandler](display, hilight);
	}
	else if(display)
	{
		var left = Roku.Util.childClassElement(display, this.leftClassName);
		var right = Roku.Util.childClassElement(display, this.rightClassName);
		var content = Roku.Util.childClassElement(display, this.contentClassName);

		if(left && right &&  content)
		{
			left.style.backgroundImage = hilight ? "url(images/combo-left-hilight.gif)" : "";
			right.style.backgroundImage = hilight ? "url(images/combo-right-hilight.gif)" : "";
			content.style.backgroundImage = hilight ? "url(images/combo-center-hilight.png)" : "";
			
			display.style.color = hilight ? "#0000E0" : ""; // #0000FF #9279D0 #9882E2
		}
	} 
}

Roku.Combo.showDropDown = function(combo)
{
	var display = Roku.Util.childClassElement(combo, this.displayClassName);
	var display_entry = Roku.Util.childClassElement(display, this.contentClassName);
	var drop_down = Roku.Util.childClassElement(combo, this.dropDownClassName);
	if(!display || !drop_down || !display_entry)
		return;
		
	if(this.currentDropDown && (Roku.Util.parentClassElement(this.currentDropDown, this.comboClassName) != combo))
	{
		this.currentDropDown.style.display = "none";
		this.currentDropDown = null;
		this.currentHilight = null;
	}
		
	if(this.currentDropDown == drop_down)
	{
		drop_down.style.display = "none";
		this.currentDropDown = null;
		this.currentHilight = null;
	}
	else
	{
		drop_down.style.visibility = "hidden";
		drop_down.style.display = "block";
		
		var displayRect = Roku.Util.elementRect(display);
		drop_down.style.left = (displayRect.left +  (displayRect.width - drop_down.offsetWidth) / 2) + "px";
		drop_down.style.top = (displayRect.top + displayRect.height) + "px";
		
		if(combo[this.dropDownEventName])
			combo[this.dropDownEventName](combo);
			
		this.currentDropDown = drop_down;
		drop_down.style.visibility = "visible";
		
		this.hilightDropList(this.currentDropDown, display_entry.id);
		this.hilightDisplay(display, false);
	}
}

Roku.Combo.hilightDropList = function(drop_down, hilight_id)
{
	var drop_entries = Roku.Util.childrenClassElements(drop_down, this.contentClassName);
	var drop_entries_count = drop_entries.length;
	for(var index = 0; index < drop_entries_count; index++)
	{
		var drop_entry = drop_entries[index];
		this.hilightDropEntry(drop_entry, drop_entry.id == hilight_id);
	} 
}

Roku.Combo.hilightDropEntry = function(drop_entry, hilight)
{
	var combo = Roku.Util.parentClassElement(drop_entry, this.comboClassName);
	var dropHilightHandler = (typeof(combo[this.dropHilightHandler]) == "function") ? combo[this.dropHilightHandler] : null; 
	
	if(hilight && (this.currentHilight != drop_entry))
	{
		if(dropHilightHandler)
			dropHilightHandler(this.currentHilight, false);
		else if(this.currentHilight)
		{
			this.currentHilight.style.backgroundImage =  "";
			this.currentHilight.style.color = "";
		}
		
		this.currentHilight = drop_entry; 
	}
	
	if(dropHilightHandler)
		dropHilightHandler(drop_entry, hilight);
	else if(drop_entry)
	{
		drop_entry.style.backgroundImage =  hilight ? "url(images/combo-drop-center-hilight.png)" : "";
		drop_entry.style.color =  hilight ? "#6F51C2" : "";
		
	}
}

Roku.Combo.on_hilight_drop_entry = function(ev)
{
	if(!ev)
		return;

	var element = Roku.Util.srcEventElement(ev);
	var entry = Roku.Util.parentClassElement(element, this.contentClassName);
	var hilight = !Roku.Util.CompareNoCase(ev.type, "mouseout");
	this.hilightDropEntry(entry, hilight);
}

Roku.Combo.on_click_drop_entry = function(ev)
{
	if(!ev)
		return;

	var element = Roku.Util.srcEventElement(ev);
	var drop_entry = Roku.Util.parentClassElement(element, this.contentClassName);
	var combo = Roku.Util.parentClassElement(drop_entry, this.comboClassName);
	this.selectItem(combo, drop_entry);
	this.showDropDown(combo);

	if(combo)
	{
		if(typeof(combo[this.selChangedEventName]) == "function")
			combo[this.selChangedEventName](combo, drop_entry);
	}

}

Roku.Combo.selectItem = function(combo, drop_item)
{
	var display = Roku.Util.childClassElement(combo, this.displayClassName);
	var display_entry = Roku.Util.childClassElement(display, this.contentClassName);
	if(display_entry && drop_item)
	{
		display_entry.id = drop_item.id;
		
		var text_content = drop_item.getAttribute(this.contentTextAttribName);
		display_entry.innerHTML = (text_content) ? text_content : drop_item.innerHTML;
	}
}

Roku.Combo.need_mouse_down = function()
{
	return Roku.Combo.currentDropDown;
}

Roku.Combo.on_mouse_down = function(ev)
{
	var element = Roku.Util.srcEventElement(ev);
	if(!element)
		return;
		
	if(Roku.Util.parentClassElement(element, Roku.Combo.comboClassName))
		return;
	
	var combo = Roku.Util.parentClassElement(Roku.Combo.currentDropDown, this.comboClassName);
	this.showDropDown(combo);
}


Roku.Combo.initialize = function()
{
	var displayClickHandler = function() { return Roku.Combo.on_click_display((0 < arguments.length) ? arguments[0] : null); };
	var displayHilightHandler = function() { return Roku.Combo.on_hilight_display((0 < arguments.length) ? arguments[0] : null); };

	var dropEntryHilightHandler = function() { return Roku.Combo.on_hilight_drop_entry((0 < arguments.length) ? arguments[0] : null); };
	var dropEntryClickHandler = function() { return Roku.Combo.on_click_drop_entry((0 < arguments.length) ? arguments[0] : null); };
	
	var combos = (Roku.Body && Roku.Body.all) ? Roku.Body.all[this.comboClassName] : Roku.Util.childrenClassElements(document.body, this.comboClassName);
	var combos_count = combos ? combos.length : 0;   
	for(var iCombo = 0; iCombo < combos_count; iCombo++)
	{
		var combo = combos[iCombo];
		
		var display = Roku.Util.childClassElement(combo, this.displayClassName);
		Roku.Util.eventHandler(display, "click", displayClickHandler);
		Roku.Util.eventHandler(display, "mouseover", displayHilightHandler);
		Roku.Util.eventHandler(display, "mouseout", displayHilightHandler);
		
		var drop_down = Roku.Util.childClassElement(combo, this.dropDownClassName);
		var drop_entries = Roku.Util.childrenClassElements(drop_down, this.contentClassName);
		var drop_entries_count = drop_entries.length;
		for(var index = 0; index < drop_entries_count; index++)
		{
			var drop_entry = drop_entries[index];
			Roku.Util.eventHandler(drop_entry, "click", dropEntryClickHandler);
			Roku.Util.eventHandler(drop_entry, "mouseover", dropEntryHilightHandler);
			Roku.Util.eventHandler(drop_entry, "mouseout", dropEntryHilightHandler);
		} 
	}
}

