// JavaScript Tools

var W3CDOM = (document.createElement && document.getElementsByTagName);


function id( el ){
	if( typeof (el) == 'string' ) return document.getElementById( el );
	else if( typeof (el) == 'object' ) return el; 
}



function getElementsByClassName(name, elm) {
	var elm = elm || document;
	var results = new Array();
	var classNames = new Array();
	var elems = elm.getElementsByTagName('*');
	for (var i=0; i<elems.length; i++) {
		classNames = elems[i].className.split(' ');
		for(var j=0; j<classNames.length; j++){
			if ( classNames[j].toUpperCase() == name.toUpperCase() ) {
				results[results.length] = elems[i];
			}		
		}

	}
	return results;
};





//change the opacity for different browsers
function changeOpac( opacity , el ) {
	if( !id(el) ) return;
	var objectStyle = id(el).style;
    objectStyle.opacity = (opacity / 100);
    objectStyle.MozOpacity = (opacity / 100);
    objectStyle.KhtmlOpacity = (opacity / 100);
    objectStyle.filter = 'alpha(opacity=' + opacity + ')';
}





/*
Fades an HTML element

el			:	string/object	the HTML element to fade
opacStart	:	integer			startvalue opacity
opacEnd		:	integer			endvalue opacity
delay		:	integer			number of milliseconds the script waits to start fade the next opacity %
onfinish	:	function		Method of the fading object, to be called when the fading-process has finished (optional)

NOTE: 	Since the setTimeout statement has to evaluate a string (the function to execute when it runs out)
		the HTML-element argument 'el' has to be a string as well.
		Which means we should write the objects ID down for this argument,
		which means the HTML element HAS TO CONTAIN and ID, also when this function is called with the THIS-keyword (e.g. fade(this, 100, 50, 2); )
*/
function fade( el , opacStart , opacEnd , delay , onfinish ){
	var ob = id(el);
	var opac;
			
	if( !ob ) return;
	
	// If the object is still fading, stop the 'old' fading
	try{clearTimeout(ob.fadingTimeOut);}
	catch(e){}
	
	// determine the direction for the blending, if start and end are the same we're done; return
	if(opacStart > opacEnd) {
		opac = opacStart - 5;
	} else if(opacStart < opacEnd) {
		opac = opacStart + 5;
	} else {
		if( onfinish ) ob.onfinish();
		return;
	}

	changeOpac( opac , ob.id );
	ob.fadingTimeOut = setTimeout('fade( "' + ob.id + '" , ' + opac + ' , ' + opacEnd + ' , ' + delay + ' , ' + onfinish + ' )',(delay));
}
