/**
 * util.js 
 * by Garrett Smith 
 * Provides functionality for working nodeLists.
 */
Browser = {
	isSupported : function(){
			return (Boolean(document.getElementsByTagName)
						&& Boolean(document.getElementById));
		},
	id : new function() {

			var ua= navigator.userAgent;
			var OMNI = ua.indexOf("Omni") > 0;

			this.OP5 = ua.indexOf("Opera 5") >= 0 || ua.indexOf("Opera 6") >= 0;
			this.OP7 = ua.indexOf("Opera 7") >= 0;
			this.MAC = ua.indexOf("Mac") > 0;

			if(!this.OP5 && !OMNI){
				this.IE5 = ua.indexOf("MSIE 5") > 0;
				this.IE5_0 = ua.indexOf("MSIE 5.0") > 0;
				this.NS6 = ua.indexOf("Gecko") > 0;
				this.MOZ = this.NS6 && ua.indexOf("Netscape") == -1;
				this.MAC_IE5 = this.MAC && this.IE5;
				this.IE6 = ua.indexOf("MSIE 6") > 0;
				this.KONQUEROR = ua.indexOf("Konqueror/") > 0;
			}
		}		
};
var px = "px";

function hasToken(s, token){
	return new RegExp(" "+token+" |^"+token+" |^"+token+"$| "+token+"$", "\g").test(s);
};

function getChildElements(nodeList){
	var collection = new Array(0);
	for(var i = 0; i < nodeList.length; i++)
		if(nodeList[i].tagName)
			collection[i] = nodeList[i];
	return collection;		
}
	
function getChildNodesWithClass(parent, tagName, klass){
		
	var collection;
	var returnedCollection = [];
	var collection = parent.childNodes;
	tagName = tagName.toUpperCase();
	
	for(var i = 0, counter = 0; i < collection.length; i++){
		if(!collection[i].className
		   || collection[i].tagName.toUpperCase() != tagName)
			continue;
		
		if(hasToken(collection[i].className, klass))
			returnedCollection[counter++] = collection[i];
	}
	return returnedCollection;
}

function get_elements_with_class_from_classList(el, tagName, classList){

    var returnedCollection = new Array(0);
    
    var collection = (el.all && tagName == "*") ?
    	el.all : el.getElementsByTagName(tagName);
    
    for(var i = 0, coLen = collection.length; i < coLen; i++)
    	jloop: for(var j = 0, kLen = classList.length; j < kLen; j++)
			if(hasToken(collection[i].className, classList[j])) {
				returnedCollection[returnedCollection.length] = collection[i];
				break jloop;
			}

    return returnedCollection;
}

function findAncestorWithClass(el, klass) {
	
	for(var parent = el;parent != null;){
	
		if( parent.className != null && hasToken(parent.className, klass))
			return parent;
			
		parent = parent.parentNode;
	}
	return null;
}


function getDescendantById(parent, id){
	var childNodes = parent.all ? parent.all : parent.getElementsByTagName("*");
	for(var i = 0, len = childNodes.length; i < len; i++)
		if(childNodes[i].id == id)
			return childNodes[i];
	return null;
}

function getFirstDescendantWithClass(parent, tagName, klass){
		
	var collection = parent.all ? parent.all : parent.getElementsByTagName("*");
	tagName = tagName.toUpperCase();
	
	for(var i = 0, counter = 0; i < collection.length; i++){
		if(!collection[i].className
		   || collection[i].tagName.toUpperCase() != tagName)
			continue;
		
		if(hasToken(collection[i].className, klass))
			return collection[i];
	}
	return null;
};

function getScrollTop(){
	if(Browser.id.NS6 || Browser.id.OP5)
		return window.pageYOffset;
		
	if(document.documentElement.scrollTop) 
		return Math.max(document.documentElement.scrollTop, document.body.scrollTop);
		
	else if(document.body.scrollTop)
		return document.body.scrollTop;
}

function getViewportHeight() {
	if(window.innerHeight)
		return window.innerHeight;
		
	if(window.document.body.clientHeight)
		return document.body.clientHeight;
		
	return window.document.documentElement.clientHeight;
}

function repaintFix(el){
	el.style.visibility = 'hidden';
	el.style.visibility = 'visible';
}