/***************************************
 * Project: 			Tempest
 * File: 					animation.js
 * Author:				Markus Kelz
 * Date created:	19 May 2005
 * Changes:
 * 01 Feb 2004 
 * Description:
 * JavaScript class for managing
 * an image slide show with fading effects 
 ***************************************/
 	
		
	var currDoc = window.document;	// that contains the navigation menu
	var doc = window.document;	// that contains the navigation menu
	
	singleImageDuration = 1000; 
	animationImgName = "targetImg";	
		
	// All current image objects
	var currentImages = new Array(); 
	
	var currImgIndex = 0;
	var docImages = new Array();
	docImages = getDocImagesByName(animationImgName);													
	
	function initAnimation(){
		for(var i=0; i<currentImages.length; i++) {				
			docImages[i].src = currentImages[i].src;
			var imgId = docImages[i].getAttribute("id");
			changeOpac(0, imgId);								
		}	
	}
	
	
		
	function loopAnimation() {			
		if (currImgIndex == currentImages.length) {
				resetIndex();
		}	
		var imgId = docImages[currImgIndex].getAttribute("id");		
		animateSingleImg(imgId);								
		setTimeout("incIndex()", 3000);					
		setTimeout("loopAnimation()", 3000);					
	}			
		
	function incIndex() {
		currImgIndex++;
	}
	
	function resetIndex() {
		currImgIndex=0;
	}
	
	function getImgIndex() {
		return currImgIndex;
	}
	

	function animateSingleImg(imgId) {	
		opacity(imgId, 0, 100, 1000);	
		setTimeout("opacity('" + imgId + "', 100, 0, 1000);", 1000);		
	}
	
	
	/**
	 * Method:	 		preloadImages
	 * Parameter:		none
	 * Description:		
	 * Constructs Image objects for given urls that are pointing to graphics
	 */
	function preloadImages() {		
		// loop through images in parameter array
		for (var i=0; i<preloadImages.arguments.length; i++) {
			currentImages[i]=new Image() // new image object
			currentImages[i].src=preloadImages.arguments[i] // set image source			
		}
	} // end preloadImages	
	
	
/**
	 * Method:	 		getDocumentImages
	 * Parameter:		none
	 * Description:		
	 * Returns all images in the current document
	 */	
	function getDocumentImages() {		
		return doc.images;
	} // end getDocumentImages
	
	/**
	 * Method:	 		getDocumentImages
	 * Parameter:		none
	 * Description:		
	 * Returns all images in the current document
	 */	
	function getDocImagesByName(imgName) {		
		var result = new Array();		
		for(var i=0; i<currDoc.images.length; i++) {					
			if (currDoc.images[i].name.indexOf(imgName) != -1) {				
				result[result.length] = currDoc.images[i];
			}
		}
		return result;
	} // end getDocImagesByName

	
	/**
	 * Method:	 		changeOpac
	 * Parameter:		Alpha value, Id of target object
	 * Description:		
	 * Change the opacity of an object for different browsers
	 */
	function changeOpac(opacity, id) {
	    var object = doc.getElementById(id).style;
	    object.opacity = (opacity / 100);
	    object.MozOpacity = (opacity / 100);
	    object.KhtmlOpacity = (opacity / 100);
	    object.filter = "alpha(opacity=" + opacity + ")";
	}

	
	/**
	 * Method:	 		opacity
	 * Parameter:		Object Id, alpha value for begining and end, animation duration 
	 * Description:		
	 * Changes the target object's opacity. Fade in/out Effect for various DOM objects.
	 */
	function opacity(id, opacStart, opacEnd, millisec) {
	    // Speed for each animation frame
	    var speed = Math.round(millisec / 100);
	    var timer = 0;
	
	    // Determine the direction for the blending, if start and end are the same nothing happens
	    if(opacStart > opacEnd) {
	        for(i = opacStart; i >= opacEnd; i--) {
	            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
	            timer++;
	        }
	    } else if(opacStart < opacEnd) {
	        for(i = opacStart; i <= opacEnd; i++) {
	        	setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
	          timer++;
	       	}
	    }
}
