
// FTB_Button
// ----------------------------------------------
function FTB_Button(id, commandIdentifier, customAction, customStateQuery, htmlModeEnabled, customEnabled) 
{
	this.state = FTB_BUTTON_OFF;
	this.id = id;
	this.ftb = null;
	this.commandIdentifier = commandIdentifier;
	this.customAction = customAction;
	this.customStateQuery = customStateQuery;	
	
	this.disabled = false;
	this.htmlModeEnabled = htmlModeEnabled;
	this.customEnabled = customEnabled;
	
	this.td = document.getElementById(id);
	this.td.button = this;

	for (var i = 0; i < this.td.childNodes.length; i++)	
	{
		if (this.td.childNodes[i].nodeName == "IMG")
		{
			this.buttonImage = this.td.childNodes[i];
			continue;
		}
		
		if (this.td.childNodes[i].nodeName != "DIV")
			continue;
		for (var j = 0; j < this.td.childNodes[i].childNodes.length; j++)	
		{
			if (this.td.childNodes[i].childNodes[j].nodeName == "IMG")
			{
				this.buttonImage = this.td.childNodes[i].childNodes[j];
				break;
			}
		}
	}
};
FTB_Button.prototype.Initialize = function() 
{
	var id = this.td.button.id;
	FTB_AddEvent(this.td,"click",function() { if(FTB_Browser.isIE) document.getElementById(id).button.Click(); else this.button.Click(); } );
	FTB_AddEvent(this.td,"mouseover",function() { if(FTB_Browser.isIE) document.getElementById(id).button.MouseOver(); else this.button.MouseOver(); } );
	FTB_AddEvent(this.td,"mouseout",function() { if(FTB_Browser.isIE) document.getElementById(id).button.MouseOut(); else this.button.MouseOut(); } );
};

FTB_Button.prototype.Click = function() 
{
	if (!this.disabled) 
	{
		if (this.customAction) 			
			this.customAction();	
		else if (this.commandIdentifier != null && this.commandIdentifier != '') 
			this.ftb.ExecuteCommand(this.commandIdentifier);

		this.ftb.Event();
	}
};

FTB_Button.prototype.MouseOver = function() { if (!this.disabled) this.SetButtonBackground("Over"); };
FTB_Button.prototype.MouseOut = function() { if (!this.disabled) this.SetButtonBackground("Out"); };
FTB_Button.prototype.SetButtonBackground = function(mouseState) { this.SetButtonStyle(mouseState); }
FTB_Button.prototype.SetButtonStyle = function(mouseState) 
{
	this.td.className = "Button_" + ((this.state == FTB_BUTTON_ON) ? "On" : "Off") + "_" + mouseState;
}


// FTB_ButtonDropDown
// ---------------------------------------------- 
function FTB_ButtonDropDown(id, ddType, customAction, htmlModeEnabled, customEnabled)
{
	this.state = FTB_BUTTON_OFF;
	this.id = id;
	this.DDType = ddType;
	this.ftb = null;
	this.customAction = customAction;
	this.commandIdentifier = null;
	
	this.disabled = false;
	this.htmlModeEnabled = htmlModeEnabled;
	this.customEnabled = customEnabled;
	
	this.isOpened = false;
	this.selectedItem = null;

	// these variables changing by the type of the dropdown:
	this.oGroupContainer = null;
	this.oGroupArray = null;
	this.oGroupSelectorArray = null;
	this.Close = null;
	this.selectorLongTxts = null;
	this.selectorShortTxts = null;
	
	if (ddType == "smiley")
	{
		this.oGroupContainer = document.getElementById(this.id + "_SmileyContainer");
		this.oGroupArray = new Array(
			document.getElementById(this.id + "_SmileyGroupN"), 
			document.getElementById(this.id + "_SmileyGroupR"), 
			document.getElementById(this.id + "_SmileyGroupE"), 
			document.getElementById(this.id + "_SmileyGroupS"));
		this.oGroupSelectorArray = new Array(
			document.getElementById(this.id + "_SmileyGrpSelN"), 
			document.getElementById(this.id + "_SmileyGrpSelR"), 
			document.getElementById(this.id + "_SmileyGrpSelE"), 
			document.getElementById(this.id + "_SmileyGrpSelS"));
		this.Close = document.getElementById(this.id + "_SmileyClose");
		this.selectorLongTxts = new Array("Normal", "Rare", "Extra", "Sign");
		this.selectorShortTxts = new Array("N", "R", "E", "S");
	}
	else if (ddType == "symbol")
	{
		this.oGroupContainer = document.getElementById(this.id + "_Container");
		this.oGroupArray = new Array(
			document.getElementById(this.id + "_Group1"), 
			document.getElementById(this.id + "_GroupG"),
			document.getElementById(this.id + "_GroupM"), 
			document.getElementById(this.id + "_GroupH"));
		this.oGroupSelectorArray = new Array(
			document.getElementById(this.id + "_GrpSel1"), 
			document.getElementById(this.id + "_GrpSelG"),
			document.getElementById(this.id + "_GrpSelM"), 
			document.getElementById(this.id + "_GrpSelH"));
		this.Close = document.getElementById(this.id + "_Close");
		this.selectorLongTxts = new Array("Symbols", "Signs", "Math", "Greek");
		this.selectorShortTxts = new Array("&#8364;", "&#9827;", "+/-", "&#946;");
	}

	this.td = document.getElementById(id);
	this.td.button = this;

	for (var i = 0; i < this.td.childNodes.length; i++)	
	{
		if (this.td.childNodes[i].nodeName == "IMG")
		{
			this.buttonImage = this.td.childNodes[i];
			continue;
		}
		
		if (this.td.childNodes[i].nodeName != "DIV")
			continue;
		for (var j = 0; j < this.td.childNodes[i].childNodes.length; j++)	
		{
			if (this.td.childNodes[i].childNodes[j].nodeName == "IMG")
			{
				this.buttonImage = this.td.childNodes[i].childNodes[j];
				break;
			}
		}
	}
};
FTB_ButtonDropDown.prototype.Initialize = function() 
{
	var id = this.td.button.id;
	FTB_AddEvent(this.td,"click",function(o) { if(FTB_Browser.isIE) document.getElementById(id).button.Click(event); else this.button.Click(o); } );
	FTB_AddEvent(this.td,"mouseover",function() { if(FTB_Browser.isIE) document.getElementById(id).button.MouseOver(); else this.button.MouseOver(); } );
	FTB_AddEvent(this.td,"mouseout",function() { if(FTB_Browser.isIE) document.getElementById(id).button.MouseOut(); else this.button.MouseOut(); } );
};

FTB_ButtonDropDown.prototype.Click = function(o) 
{
	if (this.disabled) 
		return;
	
	var targetNode = FTB_Browser.isIE ? o.srcElement : o.originalTarget;
	try	{ var targetParent = targetNode.parentNode; } catch(e) { return; }
	if (targetParent.id == this.id)				// Dropdown icon clicked: open/close the dropdown
	{
		if (!this.isOpened)
		{
			if (this.customAction)
				this.customAction();
	
			this.ShowDropDown();
		}
		else
		{
			this.HideDropDown(o);
		}
	}
	else if (targetParent.id == this.Close.id) 	// close img on dropdown clicked
	{
		this.HideDropDown(o);
	}
	else if (targetNode.id != "")							// one of the selectable item clicked
	{
		for (var i = 0; i < this.oGroupArray.length; i++)
		{
			if (targetNode.parentNode.id == this.oGroupArray[i].id)
			{
				this.selectedItem = targetNode.id;
				this.HideDropDown(o);
				
				this.InsertItem(this.selectedItem);
				
				break;
			}
		}
	}
	
//	this.ftb.Event();
};

FTB_ButtonDropDown.prototype.MouseOver = function() { if (!this.disabled) this.SetButtonBackground("Over"); };
FTB_ButtonDropDown.prototype.MouseOut = function() { if (!this.disabled) this.SetButtonBackground("Out"); };
FTB_ButtonDropDown.prototype.SetButtonBackground = function(mouseState) { this.SetButtonStyle(mouseState); }
FTB_ButtonDropDown.prototype.SetButtonStyle = function(mouseState) 
{
	this.td.className = "ButtonDD_" + (this.isOpened ? "On" : "Off") + "_" + mouseState;
}

FTB_ButtonDropDown.prototype.ShowDropDown = function()
{
	if (this.isOpened)
		return;
	
	// Closes the other dropdowns	
	this.ftb.CloseEveryDropDownButtons();
	
//	this.selectedItem = null;
	
	this.oGroupContainer.style.visibility = "visible";
	this.ShowGroup(this.oGroupSelectorArray[0].id);
	this.isOpened = true;
}

FTB_ButtonDropDown.prototype.HideDropDown = function(o)
{
	if (!this.isOpened)
		return;

	if (o)
	{
		var targetNode = FTB_Browser.isIE ? o.srcElement : o.originalTarget;
		
		for (var i = 0; i < this.oGroupSelectorArray.length; i++)
			if (targetNode.id == this.oGroupSelectorArray[i].id)
				return;
	}
	
	this.oGroupContainer.style.visibility = "hidden";
	this.ShowGroup("-");
	this.isOpened = false;
	this.MouseOut();
}

FTB_ButtonDropDown.prototype.ShowGroup = function(groupId)
{
	for (var i = 0; i < this.oGroupArray.length; i++)
	{
		var oDDGroup = this.oGroupArray[i];
		var oDDGrpSel = this.oGroupSelectorArray[i];
		
		var isSelect = (groupId == oDDGrpSel.id);
		oDDGroup.style.visibility = isSelect ? "visible" : "hidden";
		oDDGroup.style.position = isSelect ? "static" : "absolute";
		oDDGrpSel.className = isSelect ? "DDGrpSelSel" : "DDGrpSel";
		oDDGrpSel.innerHTML = isSelect ? this.selectorLongTxts[i] : this.selectorShortTxts[i];
	}
}

FTB_ButtonDropDown.prototype.InsertItem = function(itemID)
{
	switch (this.DDType)
	{
		case "smiley": this.ftb.InsertSmiley(itemID); break;
		case "symbol": this.ftb.InsertHtml(itemID); break;
	}

	return;
}
