// Toolkit.DOM
// See http://api.13thparallel.org/api/dom/ for documentation.
// (c) 2003 13thparallel.org
if (!window.Toolkit) Toolkit = {};

Toolkit.DOM = {
	getAncestorElement : function( node, tagName ) {
		var output = null;
		if( tagName ) tagName = tagName.toLowerCase();
		while( !output && node ) {
			node = node.parentNode || node.parentElement;
			if( node && node.tagName ) {
				if( !tagName ) output = node;
				else if( node.tagName.toLowerCase() == tagName ) output = node;
			}
		}
		return output;
	},
	
	getChildElements : function( node, tagName ) {
		var output = [];
		var children = node.childNodes || node.children;
		if( tagName ) tagName = tagName.toLowerCase();
		if( children ) {
			for( var i = 0; i < children.length; i++ ) {
				if( children[ i ].tagName ) {
					if( !tagName ) output[ output.length ] = children[ i ];
					else if( children[ i ].tagName.toLowerCase() == tagName ) {
						output[ output.length ] = children[ i ];
					}
				}
			}
		}
		return output;
	},
	
	getFirstChildElement : function( node, tagName ) {
		var output = null;
		var childElements = this.getChildElements( node, tagName );
		if( childElements.length ) output = childElements[ 0 ];
		return output;
	},
	
	getLastChildElement : function( node, tagName ) {
		var output = null;
		var childElements = this.getChildElements( node, tagName );
		if( childElements.length ) output = childElements[ childElements.length - 1 ];
		return output;
	},
	
	getNextElement : function( node, tagName ) {
		var output = null;
		if( tagName ) tagName = tagName.toLowerCase();
		while( !output && node ) {
			node = node.nextSibling;
			if( node && node.tagName ) {
				if( !tagName ) output = node;
				else if( node.tagName.toLowerCase() == tagName ) output = node;
			}
		}
		return output;
	},
	
	getPreviousElement : function( node, tagName ) {
		var output = null;
		if( tagName ) tagName = tagName.toLowerCase();
		while( !output && node ) {
			node = node.previousSibling;
			if( node && node.tagName ) {
				if( !tagName ) output = node;
				else if( node.tagName.toLowerCase() == tagName ) output = node;
			}
		}
		return output;
	},
		
	getElementsByClassName : function( classes, tagName, containingNode, matchAll ) {
		var output = [], elms, matches;
		var classList = classes.replace( /\s{2,}/g, " " ).replace( /^\s+/, "" ).replace( /\s+$/, "" ).split( " " );
		var classExp = new RegExp( "[\\s^]?(" + classList.join( ")|(" ) + ")[\\s$]?", "g" );
		var node = containingNode ? containingNode : document;
		if( !tagName ) {
			if( node.all ) elms = node.all;
			else if( node.getElementsByTagName ) elms = node.getElementsByTagName( "*" );
		}
		else {
			tagName = tagName.toUpperCase();
			if( node.all && node.all.tags ) elms = node.all.tags( tagName );
			else if( node.getElementsByTagName ) elms = node.getElementsByTagName( tagName );
		}
		if( !elms ) elms = [];
		for( var i = 0; i < elms.length; i++ ) {
			matches = elms[ i ].className.match( classExp );
			if( matchAll ) {
				if( matches && matches.length == classList.length ) {
					output[ output.length ] = elms[ i ];
				}
			}
			else {
				if( matches && matches.length > 0 ) {
					output[ output.length ] = elms[ i ];
				}
			}
		}
		return output;
	}
}

// end