// 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));
}





function domReady( f ) {
    // If the DOM is already loaded, execute the function right away
    if ( domReady.done ) return f();

    // If we’ve already added a function
    if ( domReady.timer ) {
        // Add it to the list of functions to execute
        domReady.ready.push( f  );
    } else {
        // Attach an event for when the page finishes  loading,
        // just in case it finishes first. Uses addEvent.
        addEvent( window, 'load', isDOMReady );

        // Initialize the array of functions to execute
        domReady.ready = [ f ];

        //  Check to see if the DOM is ready as quickly as possible
        domReady.timer = setInterval( isDOMReady, 13 );
    }
}

// Checks to see if the DOM is ready for navigation
function isDOMReady() {
    // If we already figured out that the page is ready, ignore
    if ( domReady.done ) return false;

    // Check to see if a number of functions and elements are
    // able to be accessed
    if ( document && document.getElementsByTagName && 
          document.getElementById && document.body ) {

        // If they’re ready, we can stop checking
        clearInterval( domReady.timer );
        domReady.timer = null;

        // Execute all the functions that were waiting
        for ( var i = 0; i < domReady.ready.length; i++ )
            domReady.ready[i]();

        // Remember that we’re now done
        domReady.ready = null;
        domReady.done = true;
    }
}
