if (typeof QQ === "undefined") {
  var QQ = {};
}

Array.prototype.contains = function (element) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == element) {
      return true;
    }
  }
  return false;
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

// pass a function that can be used at all nodes directly at node with id elementName, fn can use "node" as parameter
function doWithAllNodesIn(elementName,fn){
	
	var sourceNode = getFirstChild(obj(elementName));		
	while (sourceNode != null){
		var node = sourceNode;
		while (node.nodeType != 1){
			node = node.nextSibling;
			if (node == null) break;
		}
		if (node == null) break;
		sourceNode = sourceNode.nextSibling;
		eval(fn);
	}
}

// pass a function that can be used at all nodes with elementName as name, fn can use "node" as parameter
function doWithAllNodes(attribute,value,fn){

  var nodes = objs(attribute,value);
  var nodesLength = nodes.length;
	for(var i = 0;i < nodesLength;i++){
		var node = nodes[i];
		eval(fn);
	}

}

// check if the first node is an element node
function getFirstChild(element){
	var x;
	try {x = element.firstChild;}catch(e){}
	if (x != null){
		while (x.nodeType != 1){
			x = x.nextSibling;
			if (x == null) break;
		}
		return x;
	}else{
		return null;
	}
}

function obj(id){
	return document.getElementById(id);
}

// TODO select also elements if value is just part of attribute
function objs(attribute,value){
	if (attribute == undefined) attribute = "class";
  var objs = new Array();
	var providedTags = new Array("span","div","a","p","img","input","form","li","ul");
	for (var tagIndex in providedTags){
		var tags = document.getElementsByTagName(providedTags[tagIndex]);
		var tagsLength = tags.length;
		for (var i=0; i < tagsLength; i++){
      try {
			  if (attribute != "class" && tags[i].getAttribute(attribute).lastIndexOf(value) != -1) objs.push(tags[i]);
				else if (tags[i].className.lastIndexOf(value) != -1) objs.push(tags[i]);
			}catch(e){}			
		}
	}
	return objs;
}

/*
 * popUp
 */
function popUp(page,parameters,setFocus){	
	var win = window.open(page,"_blank",parameters);//, title, parameters);
	
	if (setFocus === undefined)
		win.focus();
}

/**
 * tooltip
 * @param {Object} text
 * @param {Object} className
 * @param {Object} event
 * @param {Object} show
 */
function tooltip(show,text,event,className,offsetX,offsetY){
	
	if (show){
		var tooltip = document.createElement("div");
		tooltip.id = "QQ_Tooltip";
		tooltip.innerHTML = text;
		
		if (className != undefined) tooltip.className = className;
		if (offsetX === undefined) offsetX = 20;		
		if (offsetY === undefined) offsetY = -5;
				
				
		tooltip.style.position = "absolute";
		tooltip.style.left = (event.clientX + offsetX) + QQ.Browser.getScrollPosition('x') + "px";
		tooltip.style.top = (event.clientY + offsetY) + QQ.Browser.getScrollPosition('y') + "px";

		document.body.appendChild(tooltip);
	}else{
		try{document.body.removeChild(obj("QQ_Tooltip"));}catch(e){}
	}
}