// DOM named node types, defined here owing to that IE doesn't support them
if (!window.Node)
{
	var Node = {
		ELEMENT_NODE: 1,
		ATTRIBUTE_NODE: 2,
		TEXT_NODE: 3,
		CDATA_SECTION_NODE: 4,
		ENTITY_REFERENCE_NODE: 5,
		ENTITY_NODE: 6,
		PROCESSING_INSTRUCTION_NODE: 7,
		COMMENT_NODE: 8,
		DOCUMENT_NODE: 9,
		DOCUMENT_TYPE_NODE: 10,
		DOCUMENT_FRAGMENT_NODE: 11,
		NOTATION_NODE: 12
	};
}

String.prototype.isString = true;
String.prototype.empty = '';

Array.prototype.isArray = true;
if (!Array.prototype.indexOf)
{
	Array.prototype.indexOf = function(elt /*, from*/)
	{
		var len = this.length;
		var from = Number(arguments[1]) || 0;
		from = (from < 0) ? Math.ceil(from) : Math.floor(from);
		if (from < 0)
		{
			from += len;
		}
		for (; from < len; from++)
		{
			if (from in this && elt === this[from])
			{
				return from;
			}
		}
		return -1;
	};
}

function getEvent(e)
{
	return e || window.event;
}

function getEventOrigin(e)
{
	return e.target || e.srcElement;
}

function preventDefaultAction(e)
{
	e.returnValue = false;
	if (e.cancelable && e.preventDefault) e.preventDefault();
}

function isElementWithTagName(o, name)
{
	return (o.nodeType == Node.ELEMENT_NODE && o.nodeName.toLowerCase() == name.toLowerCase());
}

function h_getArrayWithClassNames(s)
{
	// Utility function used by removeClassName() and appendClassName(). Prefix letter 'h' stands for 'hidden'.
	var a;
	return (s && (a = s.match(/\S+/g)) ? a : new Array());
}

function hasClassName(oElmnt, sClassName)
{
	return new RegExp(sClassName).test(oElmnt.className);
}

function removeClassName(oElmnt, sClassName, bReturn)
{
	// Removes a named class from referred element and leaving other classes by, good if you have multiple classes on that element.
	var a = h_getArrayWithClassNames(oElmnt.className), i = a.indexOf(sClassName);
	if (-1 != i) a.splice(i, 1); // Must be cause if you feed splice() with -1 it gets the elements in reverse order
	oElmnt.className = a.join(' ');
	if (Boolean(bReturn)) return a;
}

function appendClassName(oElmnt, sClassName)
{
	// Appends a named class to referred element, better then setting className as it overwrites the whole attribute value and makes it impossible to have multiple classes on an element.
	if (!(sClassName.isString && /([A-Za-z][-0-9A-Z_a-z]+)/.test(sClassName))) return; // Not valid class name
	var a = removeClassName(oElmnt, sClassName, true); // First remove the class name we want to append to avoid duplicates
	a.push(sClassName);
	oElmnt.className = a.join(' ');
}

window.onload = function()
{
	var a = function()
	{
		var o;
		if (o = document.getElementById('primary-navigation'))
		{
			o.onclick = function(e)
			{
				var t = getEventOrigin(getEvent(e));
				if (isElementWithTagName(t, 'li'))
				{
					var expand = !hasClassName(t, 'expand-children');
					var t_siblings = t.parentNode.childNodes;
					for (var i = 0, j = t_siblings.length; i < j; i++)
					{
						if (isElementWithTagName(t_siblings[i], 'li'))
						{
							removeClassName(t_siblings[i], 'expand-children');
						}
					}
					if (expand)
					{
						appendClassName(t, 'expand-children');
					}
				}
			}
		}
	}();
	
	var b = function()
	{
		var o;
		if (o = document.getElementById('business-offers'))
		{
			o.onclick = function(e)
			{
				var b = t = getEventOrigin(getEvent(e));
				preventDefaultAction(getEvent(e));
				
				while (!(isElementWithTagName(b, 'div') && hasClassName(b, 'puff-of-business-offer')))
				{
					b = b.parentNode;
					if ('business-offers' == b.id)
					{
						break;
					}
				}
				
				var h3 = b.firstChild;
				while (!isElementWithTagName(h3, 'h3'))
				{
					h3 = h3.nextSibling;
				}
				
				a = h3.firstChild;
				while (!isElementWithTagName(a, 'a'))
				{
					a = a.nextSibling;
				}
				
				location.href = a.href;
			}
			
			var o_children = o.childNodes;
			
			for (var i = 0, ilen = o_children.length; i < ilen; i++)
			{
				if (isElementWithTagName(o_children[i], 'div') && hasClassName(o_children[i], 'puff-of-business-offer'))
				{
					appendClassName(o_children[i], 'linked');
				}
			}
		}
	}();
	
	var c = function()
	{
		var o;
		if (o = document.getElementById('current'))
		{
			while ('primary-navigation' != o.id)
			{
				if (isElementWithTagName(o, 'li'))
				{
					appendClassName(o, 'expand-children');
				}
				o = o.parentNode;
			}
		}
	}();
}