// Blue Discus Systems - BDS Dynamic Web Effects Lite(tm) - Clipping Module - Version 2.1.3
//
// Copyright 2005 Blue Discus Systems LLC - www.BlueDiscusSystems.com
//
// License is granted to freely use these Java Scripts for personal or commercial use.
// You may NOT, however, sell these Java Scripts except if written permission is provided
//		to you by Blue Discus Systems LLC.
//
// Contact: garyz@BlueDiscusSystems.com
//
// We would appreciate it if you would credit Blue Discus Systems for these scripts
//     and provide a link to our main website at www.BlueDiscusSystems.com
// 

var BDS_ClipTransAbort = false; // 'true' to abort all threads and disable execution; 'false' to enable
var BDS_ClipTransPause = false; // 'true' to pause all threads; 'false' to resume

function BDS_StartClipEffect( tokenName, objID, objTop, objLeft, objWidth, objHeight, objEffect, stepPixels, stepRate, holdRate, nextPgm) {
	var currentEffect = "";
	var startFlag = false;
	var currentWidth = 0;
	var currentHeight = 0;
	var clipEffects = new Array();
	var baseName = '';
	var testName = tokenName;
	if (testName == '') {
		baseName = 'GenericClipEffect';
	} else {
		if (testName == BDS_GetBaseEffect(testName)) {
			baseName = testName;
		}
	}
	if (baseName != '') {
		tokenName = BDS_RegisterEffect(baseName);
	}
	clipEffects = BDS_GetClipEffects(objEffect);
	currentEffect = clipEffects[0];
	BDS_DoClipEffect( tokenName, objID, objTop, objLeft, objWidth, objHeight, objEffect, stepPixels, stepRate, holdRate, nextPgm, currentEffect, startFlag, currentWidth, currentHeight);
}

function BDS_DoClipEffect( tokenName, objID, objTop, objLeft, objWidth, objHeight, objEffect, stepPixels, stepRate, holdRate, nextPgm, currentEffect, startFlag, currentWidth, currentHeight) {
	var cmd="";
	var done=false;
	var abortFlag = false;
	var pauseFlag = false;
	var temp = "";
	var clipTop = 0;
	var clipRight = objWidth;
	var clipBottom = objHeight;
	var clipLeft = 0;
	var currentTop = objTop;
	var currentLeft = objLeft;
	var newWidth = new Array();
	var newHeight = new Array();
	var newDimensions = new Array();
	var temp = 0;
	var temp2 = 0;
	
	if ((currentEffect.substring(0,5) == "Close") && (document.getElementById(objID).style.visibility != "visible")) {
		done = true;
	}
	if (BDS_ClipTransAbort) {
		abortFlag = true;
	} else if (BDS_EffectAborted(tokenName)==true){
		abortFlag = true;
	} else if (BDS_EffectPaused(tokenName)==true){
		pauseFlag=true;
	} else if (BDS_ClipTransPause){
		pauseFlag=true;
		BDS_PauseEffect(tokenName);
	}
	if ((done==false) && (abortFlag==false) && (pauseFlag==false)) {
		switch (currentEffect) {
			case ("OpenFlowDown"):
				newHeight = BDS_GetHeight ( objID, startFlag, 'expand', stepPixels, objHeight, currentHeight );
				done = newHeight[0];
				if (done == true) break;
				startFlag = true;
				currentHeight = newHeight[1];
				currentWidth = objWidth;
				clipTop = objHeight - currentHeight;
				currentTop = objTop - clipTop;
				break;
			case ("OpenFlowUp"):
				newHeight = BDS_GetHeight ( objID, startFlag, 'expand', stepPixels, objHeight, currentHeight );
				done = newHeight[0];
				if (done == true) break;
				startFlag = true;
				currentHeight = newHeight[1];
				currentWidth = objWidth;
				clipBottom = currentHeight;
				currentTop = objTop + (objHeight - currentHeight);
				break;
			case ("OpenFlowRight"):
				newWidth = BDS_GetWidth ( objID, startFlag, 'expand', stepPixels, objWidth, currentWidth );
				done = newWidth[0];
				if (done == true) break;
				startFlag = true;
				currentWidth = newWidth[1];
				currentHeight = objHeight;
				clipLeft = objWidth - currentWidth;
				currentLeft = objLeft - (objWidth - currentWidth);
				break;
			case ("OpenFlowLeft"):
				newWidth = BDS_GetWidth ( objID, startFlag, 'expand', stepPixels, objWidth, currentWidth );
				done = newWidth[0];
				if (done == true) break;
				startFlag = true;
				currentWidth = newWidth[1];
				currentHeight = objHeight;
				clipRight = currentWidth;
				currentLeft = objLeft + (objWidth - currentWidth);
				break;
			case ("OpenCenter"):
				newDimensions = BDS_GetClipDimensions(objID, startFlag, 'expand', stepPixels, currentWidth, currentHeight, objWidth, objHeight);
				done = newDimensions[0];
				if (done == true) break;
				currentWidth = newDimensions[1];
				currentHeight = newDimensions[2];
				startFlag = true;	
				temp = parseInt((objHeight-currentHeight) / 2);
				temp2 = (objHeight-currentHeight) - temp;
				clipTop = temp;
				clipBottom = objHeight - temp2;
				temp = parseInt((objWidth-currentWidth) / 2);
				temp2 = (objWidth-currentWidth) - temp;
				clipLeft = temp;
				clipRight = objWidth - temp2;
				break;
			case ("OpenTopLeft"):
				newDimensions = BDS_GetClipDimensions(objID, startFlag, 'expand', stepPixels, currentWidth, currentHeight, objWidth, objHeight);
				done = newDimensions[0];
				if (done == true) break;
				currentWidth = newDimensions[1];
				currentHeight = newDimensions[2];
				startFlag = true;	
				clipBottom = currentHeight;
				clipRight = currentWidth;
				break;
			case ("OpenTopRight"):
				newDimensions = BDS_GetClipDimensions(objID, startFlag, 'expand', stepPixels, currentWidth, currentHeight, objWidth, objHeight);
				done = newDimensions[0];
				if (done == true) break;
				currentWidth = newDimensions[1];
				currentHeight = newDimensions[2];
				startFlag = true;	
				clipBottom = currentHeight;
				clipLeft = objWidth-currentWidth;
				break;
			case ("OpenBottomLeft"):
				newDimensions = BDS_GetClipDimensions(objID, startFlag, 'expand', stepPixels, currentWidth, currentHeight, objWidth, objHeight);
				done = newDimensions[0];
				if (done == true) break;
				currentWidth = newDimensions[1];
				currentHeight = newDimensions[2];
				startFlag = true;	
				clipTop = objHeight - currentHeight;
				clipRight = currentWidth;
				break;
			case ("OpenBottomRight"):
				newDimensions = BDS_GetClipDimensions(objID, startFlag, 'expand', stepPixels, currentWidth, currentHeight, objWidth, objHeight);
				done = newDimensions[0];
				if (done == true) break;
				currentWidth = newDimensions[1];
				currentHeight = newDimensions[2];
				startFlag = true;	
				clipTop = objHeight - currentHeight;
				clipLeft = objWidth - currentWidth;
				break;
			case ("OpenWipeDown"):
				newHeight = BDS_GetHeight ( objID, startFlag, 'expand', stepPixels, objHeight, currentHeight );
				done = newHeight[0];
				if (done == true) break;
				startFlag = true;
				currentHeight = newHeight[1];
				currentWidth = objWidth;
				clipBottom = currentHeight;
				break;
			case ("OpenWipeUp"):
				newHeight = BDS_GetHeight ( objID, startFlag, 'expand', stepPixels, objHeight, currentHeight );
				done = newHeight[0];
				if (done == true) break;
				startFlag = true;
				currentHeight = newHeight[1];
				currentWidth = objWidth;
				clipTop = objHeight - currentHeight;
				break;
			case ("OpenWipeRight"):
				newWidth = BDS_GetWidth ( objID, startFlag, 'expand', stepPixels, objWidth, currentWidth );
				done = newWidth[0];
				if (done == true) break;
				startFlag = true;
				currentWidth = newWidth[1];
				currentHeight = objHeight;
				clipRight = currentWidth;
				break;
			case ("OpenWipeLeft"):
				newWidth = BDS_GetWidth ( objID, startFlag, 'expand', stepPixels, objWidth, currentWidth );
				done = newWidth[0];
				if (done == true) break;
				startFlag = true;
				currentWidth = newWidth[1];
				currentHeight = objHeight;
				clipLeft = objWidth - currentWidth;
				break;
			
			case ("CloseFlowDown"):
				newHeight = BDS_GetHeight ( objID, startFlag, 'contract', stepPixels, objHeight, currentHeight );
				done = newHeight[0];
				if (done == true) break;
				startFlag = true;
				currentHeight = newHeight[1];
				currentWidth = objWidth;
				clipBottom = currentHeight;
				currentTop = objTop + (objHeight - currentHeight);
				break;
			case ("CloseFlowUp"):
				newHeight = BDS_GetHeight ( objID, startFlag, 'contract', stepPixels, objHeight, currentHeight );
				done = newHeight[0];
				if (done == true) break;
				startFlag = true;
				currentHeight = newHeight[1];
				currentWidth = objWidth;
				clipTop = objHeight - currentHeight;
				currentTop = objTop - clipTop;
				break;
			case ("CloseFlowRight"):
				newWidth = BDS_GetWidth ( objID, startFlag, 'contract', stepPixels, objWidth, currentWidth );
				done = newWidth[0];
				if (done == true) break;
				startFlag = true;
				currentWidth = newWidth[1];
				currentHeight = objHeight;
				clipRight = currentWidth;
				currentLeft = objLeft + (objWidth - currentWidth);
				break;
			case ("CloseFlowLeft"):
				newWidth = BDS_GetWidth ( objID, startFlag, 'contract', stepPixels, objWidth, currentWidth );
				done = newWidth[0];
				if (done == true) break;
				startFlag = true;
				currentWidth = newWidth[1];
				currentHeight = objHeight;
				clipLeft = objWidth - currentWidth;
				currentLeft = objLeft - (objWidth - currentWidth);
				break;
			case ("CloseCenter"):
				newDimensions = BDS_GetClipDimensions(objID, startFlag, 'contract', stepPixels, currentWidth, currentHeight, objWidth, objHeight);
				done = newDimensions[0];
				if (done == true) break;	
				currentWidth = newDimensions[1];
				currentHeight = newDimensions[2];
				startFlag = true;	
				temp = parseInt((objHeight-currentHeight) / 2);
				temp2 = (objHeight-currentHeight) - temp;
				clipTop = temp;
				clipBottom = objHeight - temp2;
				temp = parseInt((objWidth-currentWidth) / 2);
				temp2 = (objWidth-currentWidth) - temp;
				clipLeft = temp;
				clipRight = objWidth - temp2;
				break;
			case ("CloseTopLeft"):
				newDimensions = BDS_GetClipDimensions(objID, startFlag, 'contract', stepPixels, currentWidth, currentHeight, objWidth, objHeight);
				done = newDimensions[0];
				if (done == true) break;	
				currentWidth = newDimensions[1];
				currentHeight = newDimensions[2];
				startFlag = true;	
				clipBottom = currentHeight;
				clipRight = currentWidth;
				break;
			case ("CloseTopRight"):
				newDimensions = BDS_GetClipDimensions(objID, startFlag, 'contract', stepPixels, currentWidth, currentHeight, objWidth, objHeight);
				done = newDimensions[0];
				if (done == true) break;	
				currentWidth = newDimensions[1];
				currentHeight = newDimensions[2];
				startFlag = true;	
				clipBottom = currentHeight;
				clipLeft = objWidth-currentWidth;
				break;
			case ("CloseBottomLeft"):
				newDimensions = BDS_GetClipDimensions(objID, startFlag, 'contract', stepPixels, currentWidth, currentHeight, objWidth, objHeight);
				done = newDimensions[0];
				if (done == true) break;	
				currentWidth = newDimensions[1];
				currentHeight = newDimensions[2];
				startFlag = true;	
				clipTop = objHeight - currentHeight;
				clipRight = currentWidth;
				break;
			case ("CloseBottomRight"):
				newDimensions = BDS_GetClipDimensions(objID, startFlag, 'contract', stepPixels, currentWidth, currentHeight, objWidth, objHeight);
				done = newDimensions[0];
				if (done == true) break;	
				currentWidth = newDimensions[1];
				currentHeight = newDimensions[2];
				startFlag = true;	
				clipTop = objHeight - currentHeight;
				clipLeft = objWidth - currentWidth;
				break;
			case ("CloseWipeDown"):
				newHeight = BDS_GetHeight ( objID, startFlag, 'contract', stepPixels, objHeight, currentHeight );
				done = newHeight[0];
				if (done == true) break;
				startFlag = true;
				currentHeight = newHeight[1];
				currentWidth = objWidth;
				clipTop = objHeight - currentHeight;
				break;
			case ("CloseWipeUp"):
				newHeight = BDS_GetHeight ( objID, startFlag, 'contract', stepPixels, objHeight, currentHeight );
				done = newHeight[0];
				if (done == true) break;
				startFlag = true;
				currentHeight = newHeight[1];
				currentWidth = objWidth;
				clipBottom = currentHeight;
				break;
			case ("CloseWipeRight"):
				newWidth = BDS_GetWidth ( objID, startFlag, 'contract', stepPixels, objWidth, currentWidth );
				done = newWidth[0];
				if (done == true) break;
				startFlag = true;
				currentWidth = newWidth[1];
				currentHeight = objHeight;
				clipLeft = objWidth - currentWidth;
				break;
			case ("CloseWipeLeft"):
				newWidth = BDS_GetWidth ( objID, startFlag, 'contract', stepPixels, objWidth, currentWidth );
				done = newWidth[0];
				if (done == true) break;
				startFlag = true;
				currentWidth = newWidth[1];
				currentHeight = objHeight;
				clipRight = currentWidth;
				break;
		
			default:
				done = true;
				break;
		} // end of switch cases
	} // end of initial abort, pause and visibility tests
	
	if (pauseFlag==true) {
		cmd = "BDS_DoClipEffect( '"+tokenName+"', '"+objID+"', "+objTop+", "+objLeft+", "+objWidth+", "+objHeight+", '"+objEffect+"', "+stepPixels+", "+stepRate+", "+holdRate+", '"+nextPgm+"', '"+currentEffect+"', "+startFlag+", "+currentWidth+", "+currentHeight+")";
		BDS_WaitForResumedEffect(tokenName,'"'+cmd+'"');
	} else if (done) {
		var clipEffects = new Array();
		var cntEffects = 0;
		clipEffects = BDS_GetClipEffects(objEffect);
		cntEffects = clipEffects.length;
		if ((cntEffects==2) && (clipEffects[0]==currentEffect)) {
			currentEffect = clipEffects[1];
			startFlag = false;
			cmd = "BDS_DoClipEffect( '"+tokenName+"', '"+objID+"', "+objTop+", "+objLeft+", "+objWidth+", "+objHeight+", '"+objEffect+"', "+stepPixels+", "+stepRate+", "+holdRate+", '"+nextPgm+"', '"+currentEffect+"', "+startFlag+", "+currentWidth+", "+currentHeight+")";
			setTimeout(cmd,holdRate);
		} else {
			if (nextPgm != "") {
				cmd = nextPgm+"('"+tokenName+"')";
				if (cntEffects ==1) {
					setTimeout(cmd,holdRate);
				} else {
					eval(cmd);
				}
			} else {
				abortFlag = true;
			}
		}
	} else if (abortFlag==false) {
		document.getElementById(objID).style.clip='rect('+clipTop+'px '+clipRight+'px '+clipBottom+'px '+clipLeft+'px)';
		document.getElementById(objID).style.left=currentLeft+"px";
		document.getElementById(objID).style.top=currentTop+"px";
		document.getElementById(objID).style.visibility="visible";
		cmd = "BDS_DoClipEffect( '"+tokenName+"', '"+objID+"', "+objTop+", "+objLeft+", "+objWidth+", "+objHeight+", '"+objEffect+"', "+stepPixels+", "+stepRate+", "+holdRate+", '"+nextPgm+"', '"+currentEffect+"', "+startFlag+", "+currentWidth+", "+currentHeight+")";
		setTimeout(cmd,stepRate);
	}
	if (abortFlag) {
		BDS_RemoveEffect(tokenName);
	}
}

function BDS_GetWidth ( objID, startFlag, stepDirection, stepPixels, objWidth, currentWidth ) {
	var newWidth = new Array();
	var done = false;
	if (stepDirection == 'expand') {
		if (startFlag==false) {
			currentWidth = stepPixels;
		} else {
			if (currentWidth >= objWidth) {
				done = true;
			} else {
				currentWidth += stepPixels;
				if (currentWidth > objWidth) currentWidth = objWidth;
			}
		}
	} else {
		if (startFlag==false) {
			currentWidth = objWidth;
		} else {
			currentWidth -= stepPixels;
			if (currentWidth <= 0) {
				document.getElementById(objID).style.visibility="hidden";
				done = true;
			}
		}
	}
	newWidth[0] = done;
	newWidth[1] = currentWidth;
	return newWidth;
}

function BDS_GetHeight ( objID, startFlag, stepDirection, stepPixels, objHeight, currentHeight ) {
	var newHeight = new Array();
	var done = false;
	if (stepDirection == 'expand') {
		if (startFlag==false) {
			currentHeight = stepPixels;
		} else {
			if (currentHeight >= objHeight) {
				done = true;
			} else {
				currentHeight += stepPixels;
				if (currentHeight > objHeight) currentHeight = objHeight;
			}
		}
	} else {
		if (startFlag==false) {
			currentHeight = objHeight;
		} else {
			currentHeight -= stepPixels
			if (currentHeight <= 0) {
				document.getElementById(objID).style.visibility="hidden";
				done = true;
			}
		}
	}
	newHeight[0] = done;
	newHeight[1] = currentHeight;
	return newHeight;
}

function BDS_GetClipDimensions(objID, startFlag, clipDirection, stepPixels, currentWidth, currentHeight, objWidth, objHeight) {
	var newDimensions = new Array();
	var done = false;
	if (clipDirection == 'expand') {
		if ( startFlag == false ) {
			currentWidth = stepPixels;
			currentHeight = stepPixels;
		} else {
			if ((currentWidth >= objWidth) || (currentHeight >= objHeight)) {
				done = true;
			} else {
				if (objWidth < objHeight) {
					currentWidth += stepPixels;
					currentHeight = parseInt( objHeight * (currentWidth/objWidth));
				} else {
					currentHeight += stepPixels;
					currentWidth = parseInt( objWidth * (currentHeight/objHeight));
				}
				if ((currentWidth >= objWidth) || (currentHeight >= objHeight)) {
					currentWidth = objWidth;
					currentHeight = objHeight;
				}
			}
		}
	} else {
		if ( startFlag == false ) {
			currentWidth = objWidth;
			currentHeight = objHeight;
		} else {
			if (objWidth < objHeight) {
				currentWidth -= stepPixels;
				currentHeight = parseInt( objHeight * (currentWidth/objWidth));
			} else {
				currentHeight -= stepPixels;
				currentWidth = parseInt( objWidth * (currentHeight/objHeight));
			}
			if ((currentWidth <= 0) || (currentHeight <= 0)) {
				document.getElementById(objID).style.visibility="hidden";
				done = true;
			}
		}
	}
	newDimensions[0] = done;
	newDimensions[1] = currentWidth;
	newDimensions[2] = currentHeight;
	return newDimensions;
}

function BDS_GetClipEffects(effectString) {
	var clipEffects = new Array();
	clipEffects = effectString.split('-',2);
	return clipEffects;
}

