﻿/*---------------------------------------------------------------------------------
	* opacity transition *
	Partially based on (http://brainerror.net/scripts/javascript/blendtrans)
	[accessed 05.11.2008]
	
	Optionally uses: CTL COMMON JS INFRASTRUCTURE if ctlOpacer_switchOpacity is called.
	
	CTL version limitation: curr design admits only of 1 div to be controlled by this js,
	as there is only 1 set of glob vars representing this div's opacity state.

Maintenance Log:
Date		Responsible		Comments
---------------------------------------------------------------------------------
2008
06.Nov.2008		Kintat KO   Adaptation from brainerror.net/scripts/javascript/blendtrans
							with substantial rev.
---------------------------------------------------------------------------------*/

	// global var:
	var ctlOpacer_strOpacTmOutID = null;
	var ctlOpacer_currState = null;

	//change the opacity for different browsers
	function ctlOpacer_changeOpac(opacity, id) {
		var object = document.getElementById(id).style; 
		object.opacity = (opacity / 100);
		object.MozOpacity = (opacity / 100);
		object.KhtmlOpacity = (opacity / 100);
		object.filter = "alpha(opacity=" + opacity + ")";
	}
	
	/* 
	---------------------------------------------------------------------------------
	// main public API
	// CTL customised.  No need to revise if just for calling.
	---------------------------------------------------------------------------------
	*/	
	function ctlOpacer_opacity(id, opacStart, opacEnd, millisec) {
		//speed for each 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
		
		// CTL Customisation:
		if (ctlOpacer_strOpacTmOutID != null) {
			// clear previous timed evt:
			clearTimeout(ctlOpacer_strOpacTmOutID);
			// clear current state:
			ctlOpacer_currState = null;
		}
		
		if(opacStart > opacEnd) {
			// fading:
			ctlOpacer_currState = 'fadded';
			
			for(i = opacStart; i >= opacEnd; i--) {
				ctlOpacer_strOpacTmOutID = setTimeout("ctlOpacer_changeOpac(" + i + ",'" + id + "')",(timer * speed));
				timer++;
			}
			// CTL Customisation [Goodman 2nd p1010]:
			// Hide elmt *after* all timed fading actions are finished:
			// [Note: if there is no explicit div.visibility=visible call when branching
			//	to the fanning fork (below), this timed hiding act must be cleared off just when
			// calling the fanning fork.]
			setTimeout("document.getElementById(" + "'" + id + "'" + ")" + ".style.visibility='hidden'",(timer * speed));
			
		} else if(opacStart < opacEnd) {
			// fanning:
			
			ctlOpacer_currState = 'fanned';
			
			// CTL Customisation:
			/* Unlike the case of fading there is no harm in unhiding the div first before fanning in: */
			document.getElementById(id).style.visibility="visible"; 
			for(i = opacStart; i <= opacEnd; i++)
				{
				ctlOpacer_strOpacTmOutID = setTimeout("ctlOpacer_changeOpac(" + i + ",'" + id + "')",(timer * speed));
				timer++;
			}
		}
	}

	/*
	// Provided wrapper from src.  Seldom used; call it if needed
	/* 
	function ctlOpacer_switchOpacity(id, millisec) {
		// [CTL Customisation: uses CTL's infra js]
		//if an element is invisible, make it visible, else make it ivisible
		if(ctl_getStyleVal(document.getElementById(id), "opacity") == 0) {
			opacity(id, 0, 100, millisec);
		} else {
			opacity(id, 100, 0, millisec);
		}
	}
	
	/*
	---------------------------------------------------------------------------------
	// CTL customised wrappers (with fixed vals for params).  
	---------------------------------------------------------------------------------
	*/
	function ctlOpacer_fanning(id, intDelayedMS) {
		var evtTimed = 0;
		// chk if delayed state change is required:
		if (intDelayedMS > 0) {
			evtTimed = intDelayedMS/1000;
			setTimeout("ctlOpacer_lambdaDummy()", evtTimed);
			evtTimed = evtTimed + 1000;
		}
		ctlOpacer_strOpacTmOutID = setTimeout('ctlOpacer_opacity(' + '"' + id + '"' + ', 1, 92, 900);', evtTimed);
	}

	function ctlOpacer_fading(id, intDelayedMS) {
		var evtTimed = 0;
		// chk if delayed state change is required:
		if (intDelayedMS > 0) {
			evtTimed = intDelayedMS/1000;
			setTimeout("ctlOpacer_lambdaDummy()", evtTimed);
			evtTimed = evtTimed + 1000;
		}
		ctlOpacer_strOpacTmOutID = setTimeout('ctlOpacer_opacity(' + '"' + id + '"' + ', 60, 30, 300);', evtTimed);
	}

	function ctlOpacer_toggleFanFade(id) {
		if (ctlOpacer_currState == 'fanned') {
			ctlOpacer_currState = 'fadded';
			ctlOpacer_fading(id, 0);
		}
		else {
			// either previous state is 'fadded' or there is no previous state, 
			// which by assumption the div is still invisibly unfanned.
			ctlOpacer_currState = 'fanned';
			ctlOpacer_fanning(id, 0);
		}
	}
	
	function ctlOpacer_lambdaDummy() {
	}