/**
 * @author Sybren Dotinga, JDI internet professionals - http://www.jdi.nl/
 * @version 1.0
 */
function Harmonica( HarmonicaHolder, ChildType, Timeout, ActiveClass, InactiveClass ) {
	
	var Object = document.getElementById( HarmonicaHolder ); 
	
	/*
	 * Variable init
	 */
	if( Object ) {
		this.HarmonicaHolderElement = Object;
	} else {
		throw( 'HarmonicaHolder not found!' );
	}
	
	/*
	 * Variable init
	 */
	this.ChildType = ChildType;
	this.Timeout = Timeout;
	
	this.ActiveClass = ActiveClass;
	this.InactiveClass = InactiveClass;
	
	this.ActiveID = null;
	this.AllChildNodes = new Array();
	this.getChilds();
	
	this.HarmonicaID = ( HarmonicaArray.push( this ) - 1 );
	this.IntervalID = setInterval( 'HarmonicaArray[ '+ this.HarmonicaID +' ].loop();', this.Timeout );
	
}

Harmonica.prototype.loop = function() {
	var NextID = this.getNext();
	this.setActive( NextID );
}

Harmonica.prototype.getChilds = function() {
	
	var ChildNodes = this.HarmonicaHolderElement.childNodes;
	var Counter = 0;
	
	for ( var i = 1; i < ChildNodes.length; i++ ) {
		var Child = ChildNodes[ i ];
		
		if( Child.tagName == this.ChildType ) {
		
			/*
			 * Deze is gelijk aan wat we vragen, voeg hem toe, deze hebben we nodig!
			 */
			this.AllChildNodes[ Counter ] = Child.id;			
			Counter++;;
			
			if ( this.AllChildNodes.length == 1 ) {
				/*
				 * Zet de eerste actief
				 */
				this.setActive( Child.id );
			} else {
				/*
				 * Alle anderen inactief
				 */
				this.setInActive( Child.id );
			}
		} else {
			/*
			 * Node is not a DIV
			 */
		}
	}
}

Harmonica.prototype.setActive = function( ActiveID ) {
	if ( ActiveID ) {
		//Eerst de huidige inactief maken
		this.setInActive( this.ActiveID );
		
		/*
		 * Vervolgens de nieuwe actief maken
		 */
		var Object = document.getElementById( ActiveID );
		if( Object ) {
			
			//Zorg ervoor dat de nieuwe actieve is opgeslagen
			Object.className = this.ActiveClass;
			this.ActiveID = ActiveID;
			
		} else {
			throw( 'Dit object bestaat niet!' );
		}
		
	} else {
		throw( 'Er dient wel een ID meegegeven te worden!' );
	}
}

Harmonica.prototype.getNext = function(){
	/*
	 * Deze variable geeft aan of de volgende in de loop teruggegeven moet worden (of niet dus)
	 */
	var ReturnNext = false;
	
	for( var i = 0; i < this.AllChildNodes.length; i++ ) {
		if( ReturnNext === false ) {
			/*
			 * Als hij hem nog niet tegen is gekomen, controleer of deze het wel is
			 */
			if( this.AllChildNodes[ i ] == this.ActiveID ) {
				/*
				 * Dit is de huidige, geef dus de volgende terug! 
				 * Hij komt nu ook niet meer in deze lus (sowieso niet omdat hij in dit statement een return value doet).
				 */
				ReturnNext = true;
				/*
				 * Hij is hem tegen gekomen, dit is dus de volgende.
				 */
				if( typeof( this.AllChildNodes[ i + 1 ] ) == "string" ) {
					/*
					 * Hij is gevonden, geef deze terug!
					 */
					return this.AllChildNodes[ i + 1 ]
				} else{
					/*
					 * Indien de actieveID niet is teruggevonden dan zal het de laatste zijn. 
					 * Geef dan de eerste terug.
					 */				
					return this.AllChildNodes[ 0 ];
				}
			} else {
				/*
				 * Niet tegengekomen in deze lus
				 */
				ReturnNext = false;
			}
		}
	}	
}

/*
 * Pauzeer de harmonica
 */
Harmonica.prototype.pauze = function( ID ) {
	clearInterval( this.IntervalID );
	this.setActive( ID );
}

/*
 * Start de Harmonica weer
 */
Harmonica.prototype.start = function() {
	this.IntervalID = setInterval( 'HarmonicaArray[ '+ this.HarmonicaID +' ].loop();', this.Timeout );
}

/*
 * Zet een item op inactief door middel van een ID van een object!
 */
Harmonica.prototype.setInActive = function( ID ) {
	if ( ID ) {
		var Object = document.getElementById( ID );
		if( Object ) {
			Object.className = this.InactiveClass;	
		}
	} else {
		//throw( 'Er is nog geen actieveID ' );
	}
}

var HarmonicaArray = new Array();