//
// hide/show the tool boxes on the right
//
function jsfRightToolsHideShow()
{
	toolBoxHideShowLinks = document.getElementsByTagName("A");
	for(i=0;i<toolBoxHideShowLinks.length;i++)
	{
		if(toolBoxHideShowLinks[i].className=="hsbcDivletToolboxHideShowLink")
		{
		if(nextSibling = jsfGetNextSibling(toolBoxHideShowLinks[i],1,"UL",""))
		{
			nextSibling.style.display = "none";
		}
		toolBoxHideShowLinks[i].onclick = function()
		{
			if(nextSibling = jsfGetNextSibling(this,1,"UL",""))
			{
				if(nextSibling.style.display=="")
				{
					nextSibling.style.display="none";
					this.className = "hsbcDivletToolboxHideShowLink";
				}
				else
				{
					nextSibling.style.display="";
					this.className = "hsbcDivletToolboxHideShowLink hsbcDivletToolboxHideShowLinkSelected";
				}
			}
			return false;
		}
		}
	}
}

//
// get the next sibling
//
function jsfGetNextSibling(objId,nodeType,nodeName,className)
{
	if(objId.nextSibling!=null)
	{
		if(objId.nextSibling.nodeType==nodeType && objId.nextSibling.nodeName==nodeName && objId.nextSibling.className==className)
		{
			return objId.nextSibling;
		}
		else
		{
			if(objId.nextSibling==null)
			{
				return null;
			}
			else
			{
				return jsfGetNextSibling(objId.nextSibling,nodeType,nodeName,className);
			}
		}
	}
	return null;
}

//
// get the next child
//
function jsfGetFirstChild(objId,nodeType,nodeName,className)
{
	if(objId.firstChild!=null)
	{
		if(objId.firstChild.nodeType==nodeType && objId.firstChild.nodeName==nodeName && objId.firstChild.className==className)
		{
			return objId.firstChild;
		}
		else
		{
			if(objId.firstChild==null)
			{
				return null;
			}
			else
			{
				return jsfGetFirstChild(objId.firstChild,nodeType,nodeName,className);
			}
		}
	}
	return null;
}

//
// simple browser detect code
//
function jsfBrowserDetect()
{
	var ua = navigator.userAgent.toLowerCase(); 
	this.isIE501 = (ua.indexOf('msie 5.01') != -1);
	this.isFF = (ua.indexOf('firefox')!= -1);
}

//
// apply css padding 
//
function jsfCssPadding()
{	
	jsfNextSiblingAddClass("P","H2","hsbcJsAddSpace01");
	jsfNextSiblingAddClass("IMG","H2","hsbcJsAddSpace01");
	jsfNextSiblingAddClass("IMG","P","hsbcJsAddSpace02");
	jsfNextSiblingAddClass("IMG","P.hsbcTextStyle01","hsbcJsAddSpace03");
	jsfFirstChildAddClass("DIV.hsbcStyleContent03","P","hsbcJsAddSpace04");
	jsfFirstChildAddClass("DIV.hsbcStyleContent04","P","hsbcJsAddSpace04");
}

function jsfNextSiblingAddClass(theTag,afterTag,theClass)
{
	theTagArray = theTag.split(".");
	afterTagArray = afterTag.split(".");
	
	theTags = document.getElementsByTagName(theTagArray[0]);

	for(i=0;i<theTags.length;i++)
	{
		classNameMatch = theTagArray[1] ? (theTags[i].className ? (theTagArray[1]==theTags[i].className?true:false):false):(theTags[i].className==""?true:false);
		if((nextSibling = getNextSibling(theTags[i])) && classNameMatch)
		{
			classNameMatch = afterTagArray[1] ? (nextSibling.className ? (afterTagArray[1]==nextSibling.className?true:false):false):(nextSibling.className==""?true:false);
			if((nextSibling.nodeName==afterTagArray[0]) && classNameMatch)
			{
				theTags[i].className+= " "+theClass;
			}
		}
	}
}

function jsfFirstChildAddClass(theTag,afterTag,theClass)
{
	theTagArray = theTag.split(".");
	afterTagArray = afterTag.split(".");
	
	theTags = document.getElementsByTagName(theTagArray[0]);

	for(i=0;i<theTags.length;i++)
	{
		classNameMatch = theTagArray[1] ? (theTags[i].className ? (theTagArray[1]==theTags[i].className?true:false):false):(theTags[i].className==""?true:false);
		if((firstChild = getFirstChild(theTags[i])) && classNameMatch)
		{
			classNameMatch = afterTagArray[1] ? (firstChild.className ? (afterTagArray[1]==firstChild.className?true:false):false):(firstChild.className==""?true:false);
			if((firstChild.nodeName==afterTagArray[0]) && classNameMatch)
			{
				theTags[i].className+= " "+theClass;
			}
		}
	}
}

function getNextSibling(theTag)
{
	return theTag.nextSibling ? (theTag.nextSibling.nodeType==3 ? getNextSibling(theTag.nextSibling) : theTag.nextSibling) : null;
}

function getFirstChild(theTag)
{
	return theTag.firstChild ? (theTag.firstChild.nodeType==3 ? getNextSibling(theTag.firstChild) : theTag.firstChild) : null;
}

function jsfOnLoad()
{
	var browser = new jsfBrowserDetect();
	if(browser.isIE501)
	{
		return;
	}
	jsfRightToolsHideShow();
	jsfCssPadding();
	if(document.getElementById("jsSecondaryFunctionality"))
	{
		document.getElementById("jsSecondaryFunctionality").style.display = "block";
	}
	if(searchButtonObj = document.getElementById("searchSubmitButton"))
	{
		attachSearchEvent(searchButtonObj);
	}
}



//
// trigger onLoad function (do_onload)
//
if (window.addEventListener)
{
	window.addEventListener("load", jsfOnLoad, false);
}
else
{
	if (window.attachEvent)
	{
		window.attachEvent("onload", jsfOnLoad);
	}
	else
	{
	if (document.getElementById)
		{
			window.onload = jsfOnLoad;
		}
	}
}

//
// customised prototype.js to allow document.getElementsByClassName
//

var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}

Object.extend = function(destination, source) {
  for (property in source) {
    destination[property] = source[property];
  }
  return destination;
}

function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}

var Enumerable = {
  each: function(iterator) {
    var index = 0;
    try {
      this._each(function(value) {
        try {
          iterator(value, index++);
        } catch (e) {
          if (e != $continue) throw e;
        }
      });
    } catch (e) {
      if (e != $break) throw e;
    }
  },

  inject: function(memo, iterator) {
    this.each(function(value, index) {
      memo = iterator(memo, value, index);
    });
    return memo;
  },

  inspect: function() {
    return '#<Enumerable:' + this.toArray().inspect() + '>';
  }
}

Object.extend(Enumerable, {
  map:     Enumerable.collect,
  find:    Enumerable.detect,
  select:  Enumerable.findAll,
  member:  Enumerable.include,
  entries: Enumerable.toArray
});

var $A = Array.from = function(iterable) {
  if (!iterable) return [];
  if (iterable.toArray) {
    return iterable.toArray();
  } else {
    var results = [];
    for (var i = 0; i < iterable.length; i++)
      results.push(iterable[i]);
    return results;
  }
}

Object.extend(Array.prototype, Enumerable);

Object.extend(Array.prototype, {
  _each: function(iterator) {
    for (var i = 0; i < this.length; i++)
      iterator(this[i]);
  },

  inspect: function() {
    return '[' + this.map(Object.inspect).join(', ') + ']';
  }
});

document.getElementsByClassName = function(className, parentElement) {
  var children = ($(parentElement) || document.body).getElementsByTagName('*');
  return $A(children).inject([], function(elements, child) {
    if (child.className.match(new RegExp("(^|\\s)" + className + "(\\s|$)")))
      elements.push(child);
    return elements;
  });
}

if (!window.Element) {
  var Element = new Object();
}

