/**
 * @author Sybren Dotinga, JDI internet professionals - http://www.jdi.nl/
 * @version 1.0
 */

/**
 * Javascript class to loop trough objects in a 'CycleHolder'
 */
function FotoCycle( CycleHolder, ObjectType, Timer ) {

	this.Setup( CycleHolder, ObjectType );
			
	/*
	 * Ready, set... GO!
	 */
	this.checkObjects();
	
	setInterval( 'FotoCycles[ '+ this.CycleID +' ].loop();', Timer );
}

/**
 * Setting up the foto cycle process
 */
FotoCycle.prototype.Setup = function( CycleHolder, ObjectType ) {
	
	/*
	 * Variable initalisation
	 */
	
	this.ObjectType = ObjectType;
	this.CycleHolder = CycleHolder;
	
	this.Started = false;
	this.Objects = new Array();
	this.CurrentActive = 0;
	this.NextActive = 1;
	
	this.getObjects();
	
	this.CycleID = ( FotoCycles.push( this ) - 1 );
}

/**
 * Getting all objects.
 */
FotoCycle.prototype.getObjects = function() {
	
	/*
	 * Getting all elements
	 */
	var CycleObject = document.getElementById( this.CycleHolder );
	if ( CycleObject ) {
		var Elements = CycleObject.childNodes;
		var Counter = 1;
		
		/*
		 * Loop trought the element to be sure there are only objects inside
		 */
		for( var i = 0; i < Elements.length; i++ ) {
			
			if( Elements[ i ].tagName == this.ObjectType ) {
				
				/*
				 * Element is equal to default object type, push it in the array
				 */
				Elements[ i ].className = 'Invisible';
				Elements[ i ].id = this.CycleHolder + 'Object' + Counter;
				this.Objects.push( Elements[ i ] );
				
				Counter++;
			} else {
				
				/*
				 * Element is not an equal to default, we don't need this element
				 */
			}
		}
		
		this.setDefaultStyle();
		
	} else {
		/*
		 * Given object not found
		 */
	}
}

FotoCycle.prototype.setDefaultStyle = function() {
	
	/*
	 * Loop trought the element to set right z-index
	 */
	for( var i = 0; i < this.Objects.length; i++ ) {
		
		/*
		 * Set default opacity
		 */
		opacity( this.Objects[ i ].id, 100, 0, 10 );
		
		/*
		 * Set default z-index and classname
		 */
		this.Objects[ i ].style.zIndex = 0;
		this.Objects[ i ].className = 'Visible';
	}
	
	for( var i = 0; i < this.Objects.length; i++ ) {
		if ( i == 0 ) {
			/*
			 * The first one needs to be the highest
			 */
			opacity( this.Objects[ i ].id, 0, 100, 1000 );
		}
	}
}

FotoCycle.prototype.loop = function() {
	
	if ( this.Started ) {
		
		/*
		 * Fade the picture current element out
		 */
		this.clearCurrent();
		
		/*
		 * Make the next toplevel
		 */
		this.setNext();
		
		/*
		 * Set a new current element
		 */
		this.setNewCurrent();
	} else {
		/*
		 * Don't cycle
		 */
	}
}

FotoCycle.prototype.setNewCurrent = function() {
	
	/*
	 * The new current is the next active var
	 */
	this.CurrentActive = this.NextActive;
	
	/*
	 * Setting a new current active
	 */
	if ( ( this.NextActive + 1 ) >= this.Objects.length ) {
		this.NextActive = 0;
	} else {
		this.NextActive++;
	}
	
	return true;
}

FotoCycle.prototype.clearCurrent = function() {

	/*
	 * Fade out the current
	 */
	opacity( this.Objects[ this.CurrentActive ].id, 100, 0, 1000 );
	this.Objects[ this.CurrentActive ].style.zIndex = 0;
	
	return true;
}

FotoCycle.prototype.setNext = function() {
	/*
	 * Fade in next active
	 */
	opacity( this.Objects[ this.NextActive ].id, 0, 100, 1000 );
	var NextObject = this.Objects[ this.NextActive ];	
	NextObject.style.zIndex = 300;
	
	return true;
}

FotoCycle.prototype.setClassnameToHolder = function( ClassName ) {
	var Object = document.getElementById( this.CycleHolder );
	
	if( Object ) {
		Object.className = ClassName;
	}
}

/**
 * Start the cycle process!
 */
FotoCycle.prototype.checkObjects = function() {

	if( this.Objects.length == 0 ) {
		
		/*
		 * There are no objects, alert to the administrator 
		 */
		this.Started = false;
		this.setClassnameToHolder( 'Error' );
		
		if( console )
			console.error( 'This gallery contains no images! You should add more images to this gallery.' );
		
	} else if( this.Objects.length == 1 ) {
		
		/*
		 * Loading is done, but not more than one object, don't cycle!
		 */
		this.Started = false;
		this.setClassnameToHolder( 'Done' );
		
	} else if( this.Objects.length >= 1 ) {
		
		/*
		 * Cycle trough all objects
		 */
		this.Started = true;
		this.setClassnameToHolder( 'Done' );
		
	} else {
		
		/*
		 * There is only 1 (or less) objects in the FotoCycleHolder, we can't cycle one picture do we?
		 */
		this.Started = false;
		this.setClassnameToHolder( 'Done' );
		
	}
}

/*
 * This array include all fotocycle instances. We loop trough it so we can call the loop function
 */
var FotoCycles = new Array();
