/*
	ELSFader
	
	Copyright (c) 2006, Errol Sayre, The University of Mississippi
	All rights reserved.
	
	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:
	•	Redistributions of source code must retain the above copyright notice,
		this list of conditions and the following disclaimer.
	•	Redistributions in binary form must reproduce the above copyright
		notice, this list of conditions and the following disclaimer in the
		documentation and/or other materials provided with the distribution.
	•	Neither the name of The University of Mississippi nor the names of its
		contributors may be used to endorse or promote products derived from
		this software without specific prior written permission.
	
	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
	
	This class provides an object that manages the opacity of a DOM element. It
	supports any element that can be faded using standard CSS, Mozilla, and IE
	opacity implementations. Additionally, this class provides various "events"
	for attaching listeners for programming around the transitions.
	
	In order to reduce timing issues related to faster or slower processors, the
	animation is timed according to the milliseconds passed since the start of
	the animation interval.
	
	Events:
		start - thrown when animation begins
		finish - thrown when animation finishes (i.e. opacity reaches target)
		stop - thrown when animation is stopped by an external call
		pause - thrown when animation is temporarily stopped due to an external
			call
	
	Methods:
		setOpacity(opacity) - immediately sets the opacity of the element to the
			given opacity level (can be a decimal or a whole number percentage)
		setTarget(target) - sets the target opacity and direction
		setSpeed(speed) - sets the speed factor
		fade(target, speed, delay) - sets the target opacity, speed, and starts
			the animation after the specified delay in milleseconds (or
			immediately)
		reverse() - toggles direction of the fade (when called without resetting
			the target value, the logical endpoint is assumed)

*/
function ELSEventManager() { var instanceSelf = this; var instanceClass = instanceSelf.constructor.prototype; var instanceIdentifier; instanceSelf.addEventListener = addEventListener; instanceSelf.removeEventListener = removeEventListener; instanceSelf.callEventListeners = callEventListeners; var eventListeners; function addEventListener(eventName, eventListener) { if (eventListeners == null) { eventListeners = new Array(); } eventName = eventName.toLowerCase(); if (eventName != "") { if (eventListeners[eventName] == null) { eventListeners[eventName] = new Array(); } var eventListenerIndex = -1; for (var listenerIndex = 0; listenerIndex < eventListeners[eventName].length; listenerIndex++) { if (eventListeners[eventName][listenerIndex] == eventListener) { eventListenerIndex = listenerIndex; listenerIndex = eventListeners[eventName].length; } } if (eventListenerIndex == -1) { eventListeners[eventName][eventListeners[eventName].length] = eventListener; } } } function removeEventListener(eventName, eventListener) { if (eventListeners != null) { eventName = eventName.toLowerCase(); if (eventName != "") { if (eventListeners[eventName] != null) { for (listenerIndex = eventListeners[eventName].length - 1; listenerIndex >= 0; listenerIndex--) { if (eventListeners[eventName][listenerIndex] == eventListener) { eventListeners[eventName].splice(listenerIndex, 1); } } } } } } function callEventListeners(eventName) { if (eventListeners != null) { eventName = eventName.toLowerCase(); if (eventListeners[eventName] != null) { for (listenerIndex = 0; listenerIndex < eventListeners[eventName].length; listenerIndex++) { try { eventListener = eventListeners[eventName][listenerIndex]; eventListener(); } catch (exception) { eventListeners[eventName].splice(listenerIndex, 1); listenerIndex--; } } } } } };
function ELSFader(theElement) { var instanceClass = this.constructor.prototype; var instanceIndex; var element = theElement; var startTime = null; var opacity = 1000; var target = 0; var speed = 1000; var direction = -1; var status = ''; var animationInterval = null; var animationTimeout = null; var eventManager = null; this.setOpacity = setOpacity; this.setOpacityAsPercentage = setOpacityAsPercentage; this.setOpacityAsDecimal = setOpacityAsPercentage; this.setTarget = setTarget; this.setTargetAsPercentage = setTargetAsPercentage; this.setTargetAsDecimal = setTargetAsDecimal; this.setSpeed = setSpeed; this.fade = fade; this.animate = animate; this.start = start; this.stop = stop; this.play = play; this.pause = pause; this.playPause = playPause; this.show = show; this.hide = hide; this.pulse = pulse; eventManager = new ELSEventManager(); this.addEventListener = eventManager.addEventListener; this.removeEventListener = eventManager.removeEventListener; function setOpacity(theOpacity) { theOpacity = theOpacity * 1; if (theOpacity > 1000) { opacity = 1000; } else if (theOpacity < 0) { opacity = 0; } else { opacity = theOpacity; } updateElement(); } function setOpacityAsPercentage(theOpacity) { setOpacity(theOpacity * 10); } function setOpacityAsDecimal(theOpacity) { setOpacity(theOpacity * 1000); } function setTarget(theTarget) { theTarget = theTarget * 1; if (theTarget > 1000) { target = 1000; } else if (theTarget < 0) { target = 0; } else { target = theTarget; } if (target < opacity) { direction = -1; } else { direction = 1; } } function setTargetAsPercentage(theTarget) { setTarget(theTarget * 10); } function setTargetAsDecimal(theTarget) { setTarget(theTarget * 1000); } function setSpeed(theSpeed) { speed = theSpeed * 1; } function updateElement() { if (window.ActiveXObject) { element.style.filter = 'alpha(opacity=' + (opacity / 10) + ')'; } element.style.opacity = opacity / 1000; } function fade(theTarget, theSpeed, theDelay) { stopAnimation(); setTarget(theTarget); setSpeed(theSpeed); theDelay = theDelay * 1; animationTimeout = setTimeout(function() { start(); }, theDelay); } function start() { stopAnimation(); startTime = new Date(); startAnimation(); status = 'playing'; eventManager.callEventListeners('start'); } function stop() { stopAnimation(); status = 'paused'; eventManager.callEventListeners('stop'); } function animate() { x = new Date() - startTime; y = x / speed; if (direction < 0) { setOpacity(1000 - ((1000 - target) * y)); if ((opacity < target) || (opacity == 0)) { finish(); } } else { setOpacity(target * y); if ((opacity > target) || (opacity == 1000)) { finish(); } } } function startAnimation() { animationInterval = setInterval(function() { animate(); }, 10); } function stopAnimation() { clearInterval(animationInterval); clearTimeout(animationTimeout); } function reverse() { direction = direction * -1; } function finish() { stopAnimation(); status = 'finished'; eventManager.callEventListeners('finish'); } function play() { start(); status = 'playing'; eventManager.callEventListeners('play'); } function pause() { stopAnimation(); status = 'paused'; eventManager.callEventListeners('pause'); } function playPause() { if (status == 'paused') { play(); } else { pause(); } } function show(theSpeed) { if (theSpeed != null) { setSpeed(theSpeed); } stopAnimation(); setTarget(1000); start(); } function hide() { stopAnimation(); setTarget(0); start(); } function pulse(theSpeed) { if (theSpeed != null) { setSpeed(theSpeed); } if (opacity == 0) { show(); } else { hide(); } } element.fader = this; } function locateAndInstantiateELSFaders() { searchDomTreeForELSFaders(document.body); } function searchDomTreeForELSFaders(element) { if ( (element.className) && (element.className.indexOf('ELSFader') > -1) ) { new ELSFader(element); } var currentNode = false; if (element.hasChildNodes()) { currentNode = element.firstChild; } while (currentNode) { searchDomTreeForELSFaders(currentNode); currentNode = currentNode.nextSibling; } } try { if (window.attachEvent) { window.attachEvent('onload', locateAndInstantiateELSFaders); } else if (window.addEventListener) { window.addEventListener('load', locateAndInstantiateELSFaders); } } catch(e) { if (document.addEventListener) { document.addEventListener("DOMContentLoaded", locateAndInstantiateELSFaders, false); } };
