/* (c) 2009 by Gerald Wodni */

/* make Array more usefull */
Array.prototype.contains = function( item )
{
	for( var i = 0; i < this.length; i++ )
		if( this[i] == item )
			return true;

	return false;
}

/* as IE is unable to give us the className as attribute, we need this function, sorry to every other browser :( */
function getElementsByClassname( tag, className )
{
        var elements = document.getElementsByTagName( tag );
        var retter = new Array();

        for( var i = 0; i < elements.length; i++ )
                if( elements[i].className == className )
                        retter.push( elements[i] );

        return retter;
}

function getNextSiblingByClassname( sibling, className )
{
	sibling = sibling.nextSibling;

	if( !sibling )
		return null;

	if( sibling.className == className )
		return sibling;

	return getNextSiblingByClassname( sibling, className );
}

function getElementsByAttributeValue( tag, attribute, value )
{
        var elements = document.getElementsByTagName( tag );
        var retter = new Array();

        for( var i = 0; i < elements.length; i++ )
                if( elements[i].getAttribute( attribute ) == value )
                        retter.push( elements[i] );

        return retter;
}

function getChildElementsByTagName( target, tag )
{
        var found = new Array();

        if( target.nodeName == tag )
                found.push( target );

        var node = target.firstChild;
        while( node )
        {
                found = found.concat( getChildElementsByTagName( node, tag ) );

                node = node.nextSibling;
        }

        return found;
}

function getChildElementByTagName( target, tag )
{
        var node = target;
        while( node )
        {
		if( node.nodeName == tag )
			return node;
		else
		{
			var childTarget = getChildElementByTagName( target.firstChild, tag );
			if( childTarget != null )
				return childTarget;
		}

                node = node.nextSibling;
        }

        return null;
}

function getTarget( evt )
{
        evt = ( (evt) ? evt : event);
        return ( (evt.target) ? evt.target : evt.srcElement );
}

/* as IE is unable to give us the className as attribute, we need this function, sorry to every other browser :( */
function getChildElementsByClassname( target, tag, className )
{
        var retter = new Array();
        var found = getChildElementsByTagName( target, tag );

        for( var i = 0; i < found.length; i++ )
                if( found[i].className == className )
                        retter.push( found[i] );

        return retter;
}

function getChildElementsByAttributeValue( target, tag, attribute, value )
{
        var retter = new Array();
        var found = getChildElementsByTagName( target, tag );

        for( var i = 0; i < found.length; i++ )
                if( found[i].getAttribute( attribute ) == value )
                        retter.push( found[i] );

        return retter;
}

function containsNode( parentNode, childNode )
{
	if( parentNode == childNode.parentNode )
		return true;

	if( childNode.parentNode )
		return containsNode( parentNode, childNode.parentNode );

	return false;
}

function trim( str )
{
	return str.replace(/^[\t\n ]+/, '').replace(/[\t\n ]$/, '');
}



