
/**
* @pre the <head> tag allready exists
*/
function include(x)
{
	if(x)
	{
		var s = document.createElement('script');
		s.type="text/javascript";
		s.src=x;
		if( typeof document.head == 'undefined') document.head = document.getElementsByTagName('head')[0];
		document.head.appendChild(s);
	}
}


function empty($x)
{
	return typeof($x)=='undefined' || !$x;
}

/**
 * check if an element o is of class x
 */
function attr_match($o,$attr,$x,$isRegExp)
{
	if(empty($o) || empty($o[$attr])) return false;
	//alert($o[$attr]+"\n"+$x);//debug
	if(typeof $isRegExp!=='undefined' && $isRegExp) return $o[$attr].match(new RegExp($x));
	return $o[$attr].match(new RegExp('(^|\\s)'+$x+'(\\s|$)'));
}

/**
 * @return next sibling (optionally specific nodename) or null if no next sibling exists
 */
function nextSibling($o,$nodeName)
{
	if(typeof $nodeName == 'undefined') return $o.nextSibling;
	$nodeName = $nodeName.toLowerCase();
	$o = $o.nextSibling;
	while($o)
	{
		if($o.nodeName.toLowerCase()==$nodeName) return $o;
		$o = $o.nextSibling;
	}
	return null;
}



function byClass()
{
	var $x='',$t='*',$o=document,$i=0,$c,$n,$y=[];
	if(arguments[1]) $o=arguments[1];
	if(arguments[0]) $x=arguments[0].replace(/$(\.)|(\.)^/,'');
	if(($i=$x.indexOf('.'))>-1)
	{
		$t=$x.substr(0,$i);
		$x=$x.substr($i+1);
	}
	$c=$o.getElementsByTagName($t);
	$n=$c.length;
	$i=-1;
	while(++$i<$n)
	{
		if($c[$i].className && $c[$i].className.match(new RegExp('(\\s|^)('+$x+')(\\s|$)')))
		{
			$y.push($c[$i]);
		}
	}
	return $y;
}



/**
 * @return x,y,dx,dy
 */
function posdim($e)
{
	var $x=0;
	var $y=0;
	var $w=$e.offsetWidth;
	var $h=$e.offsetHeight;
	if($e.offsetParent)
	{
		do {		
		$x += $e.offsetLeft;
		$y += $e.offsetTop;
		} while ($e = $e.offsetParent);
	}
	return new Array($x,$y,$w,$h);
}




function addEvent($o, $type, $f, $useCapture)
{
	//todo add cleanup and caching
	if($o.addEventListener)
	{
		$o.addEventListener($type, $f, $useCapture);
		return true;
	}
	else if($o.attachEvent)
	{
		var r = $o.attachEvent('on' + $type, $f);
		return r;
	}
	$o['on' + $type] = $f;
	return true;
}




function firstElementOf($x)
{
	if($x.children.length)
	{
		var $c=$x.firstChild;
		while($c.nodeType!=1)
		{
			if($c.nextSibling)
			{
				$c=$c.nextSibling;
			}
			else
			{
				return null;//no elements	
			}
		}
		return $c;
	}
	return null;//no children
}





