if(typeof(tmt) == "undefined"){
	tmt = {};
}

if(typeof(tmt.spry) == "undefined"){
	tmt.spry = {};
}

if(typeof(tmt.spry.widget) == "undefined"){
	tmt.spry.widget = {};
}

tmt.spry.widget.CollapsiblePanelOver = {};

// Constructor
tmt.spry.widget.CollapsiblePanelOver = function(element, opts){
	Spry.Widget.CollapsiblePanel.call(this, element, opts);
}

// Import all methods
for(var x in Spry.Widget.CollapsiblePanel.prototype){
	tmt.spry.widget.CollapsiblePanelOver.prototype[x] = Spry.Widget.CollapsiblePanel.prototype[x];
}
tmt.spry.widget.CollapsiblePanelOver.prototype.constructor = tmt.spry.widget.CollapsiblePanelOver;

/* Overwritten method */

tmt.spry.widget.CollapsiblePanelOver.prototype.attachPanelHandlers = function()
{
	var tab = this.getTab();
	if (!tab)
		return;

	var self = this;
	Spry.Widget.CollapsiblePanel.addEventListener(tab, "mouseover", function(e) { return self.onTabClick(e); }, false);
	Spry.Widget.CollapsiblePanel.addEventListener(tab, "mouseover", function(e) { return self.onTabMouseOver(e); }, false);
	Spry.Widget.CollapsiblePanel.addEventListener(tab, "mouseout", function(e) { return self.onTabMouseOut(e); }, false);

	if (this.enableKeyboardNavigation)
	{
		// XXX: IE doesn't allow the setting of tabindex dynamically. This means we can't
		// rely on adding the tabindex attribute if it is missing to enable keyboard navigation
		// by default.

		// Find the first element within the tab container that has a tabindex or the first
		// anchor tag.
		
		var tabIndexEle = null;
		var tabAnchorEle = null;

		this.preorderTraversal(tab, function(node) {
			if (node.nodeType == 1 /* NODE.ELEMENT_NODE */)
			{
				var tabIndexAttr = tab.attributes.getNamedItem("tabindex");
				if (tabIndexAttr)
				{
					tabIndexEle = node;
					return true;
				}
				if (!tabAnchorEle && node.nodeName.toLowerCase() == "a")
					tabAnchorEle = node;
			}
			return false;
		});

		if (tabIndexEle)
			this.focusElement = tabIndexEle;
		else if (tabAnchorEle)
			this.focusElement = tabAnchorEle;

		if (this.focusElement)
		{
			Spry.Widget.CollapsiblePanel.addEventListener(this.focusElement, "focus", function(e) { return self.onFocus(e); }, false);
			Spry.Widget.CollapsiblePanel.addEventListener(this.focusElement, "blur", function(e) { return self.onBlur(e); }, false);
			Spry.Widget.CollapsiblePanel.addEventListener(this.focusElement, "keydown", function(e) { return self.onKeyDown(e); }, false);
		}
	}
}

/* Panel Group */

// Constructor
tmt.spry.widget.CollapsiblePanelGroupOver = function(element, opts){
	Spry.Widget.CollapsiblePanelGroup.call(this, element, opts);
}

// Import all methods
for(var x in Spry.Widget.CollapsiblePanelGroup.prototype){
	tmt.spry.widget.CollapsiblePanelGroupOver.prototype[x] = Spry.Widget.CollapsiblePanelGroup.prototype[x];
}
tmt.spry.widget.CollapsiblePanelGroupOver.prototype.constructor = tmt.spry.widget.CollapsiblePanelGroupOver;

/* Overwritten method */

tmt.spry.widget.CollapsiblePanelGroupOver.prototype.attachBehaviors = function()
{
	if (!this.element)
		return;

	var cpanels = this.getPanels();
	var numCPanels = cpanels.length;
	for (var i = 0; i < numCPanels; i++)
	{
		var cpanel = cpanels[i];
		this.setElementWidget(cpanel, new tmt.spry.widget.CollapsiblePanelOver(cpanel, this.opts));
	}
};

