/*	============================================================================
	Copyright (c) 2004 Macromedia Inc.
	dhtml animation library
	$Revision: 1.5 $ 
	
	*** Requires global.js ***
	
============================================================================= */


DhtmlObj = function (objRef,elementID) {
	
	/* private instance variables */
	var self = this;
	var coords = getElementBoxCoordsById(elementID);
	
	/* public instance variables */
	this.objRef = objRef;
	eval(this.objRef+"=this;");
	
	this.element = document.getElementById(elementID);
	this.width 	= coords.x2 - coords.x1;
	this.height = coords.y2 - coords.y1;
	
	/* priviledged methods */
	
	
	return this;
}


DhtmlObj.prototype.clipto = function (clippingValues) {
	
	var clippingObj = new Object();
	
	switch (typeof clippingValues) {
		
		case "object":
			clippingObj.top 	= clippingValues.top 	|| 0;
			clippingObj.right 	= clippingValues.right 	|| this.width;
			clippingObj.bottom 	= clippingValues.bottom || this.height;
			clippingObj.left 	= clippingValues.left 	|| 0;
			break;
		
		case "array":
			if (clippingValues.length == 4) {
				clippingObj.top 	= parseInt(clippingValues[0]);
				clippingObj.right 	= parseInt(clippingValues[1]);
				clippingObj.bottom 	= parseInt(clippingValues[2]);
				clippingObj.left 	= parseInt(clippingValues[3]);
			} else {
				// unable to parse string for clipping values
				return false;
			}
			break;
			
		case "string":
			if (clippingValues.indexOf('(') != -1 && clippingValues.indexOf(')') != -1) {
				clippingValues = clippingValues.slice(clippingValues.indexOf('('),clippingValues.indexOf(')'));
			}
			if (clippingValues.indexOf(',') != -1) {
				clippingValues = clippingValues.split(',');
			} else if (clippingValues.indexOf(' ') != -1) {
				clippingValues = clippingValues.split(' ');
			} else {
				// unable to parse string for clipping values
				return false;
			}
			if (clippingValues.length == 4) {
				clippingObj.top 	= parseInt(clippingValues[0]);
				clippingObj.right 	= parseInt(clippingValues[1]);
				clippingObj.bottom 	= parseInt(clippingValues[2]);
				clippingObj.left 	= parseInt(clippingValues[3]);
			} else {
				// unable to parse string for clipping values
				return false;
			}
			break;
		
		default:
			return false;
	}
	
	if (typeof this.element.style.clip != 'undefined') {
		if (typeof element.style.clip.top != 'undefined') {	
			//this.element.style.clip = clippingObj;
			this.element.style.clip.top 	= clippingObj.top;
			this.element.style.clip.right 	= clippingObj.right;
			this.element.style.clip.bottom 	= clippingObj.bottom;
			this.element.style.clip.left 	= clippingObj.left;
		} else {
			this.element.style.clip = 'rect('+clippingObj.top+'px,'+clippingObj.right+'px,'+clippingObj.bottom+'px,'+clippingObj.left+'px)';
		}
	}
}


DhtmlObj.prototype.getClippingValues = function () {
	var clippingValues = new Object();
	if (typeof this.element.style.clip != "undefined") {
		if (typeof this.element.style.clip.top != "undefined") {
			clippingValues = this.element.style.clip;
		} else {
			valuesArray = this.element.style.clip.slice(5,-1).split(' ');
			clippingValues.top 		= parseInt(valuesArray[0]);
			clippingValues.right 	= parseInt(valuesArray[1]);
			clippingValues.bottom 	= parseInt(valuesArray[2]);
			clippingValues.left 	= parseInt(valuesArray[3]);
		}
	}
	return clippingValues;
}



DhtmlObj.prototype.wipeInFromTop = function () {
	this.elapsed = new Date().getTime() - this.starttime;
	var distance = this.height*(this.elapsed/this.duration);
	var clippingObj;
	if (this.elapsed < this.duration) {
		clippingObj	= { top:0, right:this.width, bottom:distance, left:0 };
	} else {
		clippingObj	= { top:0, right:this.width, bottom:this.height, left:0 };
		this.oncomplete();
	}
	this.clipto(clippingObj);
}


DhtmlObj.prototype.wipeOutFromTop = function () {
	this.elapsed = new Date().getTime() - this.starttime;
	var distance = this.wipeHeight*(this.elapsed/this.duration);
	var clippingObj;
	if (this.elapsed < this.duration) {
		clippingObj	= { top:0, right:this.width, bottom:this.height-distance, left:0 };
	} else {
		clippingObj	= (this.endclip) ? this.endclip : { top:0, right:this.width, bottom:this.height, left:this.width };
		this.oncomplete();
	}
	this.clipto(clippingObj);
}


DhtmlObj.prototype.wipeInFromRight = function () {
	this.elapsed = new Date().getTime() - this.starttime;
	var distance = this.width*(this.elapsed/this.duration);
	var clippingObj;
	if (this.elapsed < this.duration) {
		clippingObj	= { top:0, right:this.width, bottom:this.height, left:this.width-distance };
	} else {
		clippingObj	= { top:0, right:this.width, bottom:this.height, left:0 };
		this.oncomplete();
	}
	this.clipto(clippingObj);
}


DhtmlObj.prototype.wipeOutFromRight = function () {
	this.elapsed = new Date().getTime() - this.starttime;
	var distance = this.width*(this.elapsed/this.duration);
	var clippingObj;
	if (this.elapsed < this.duration) {
		clippingObj	= { top:0, right:this.width, bottom:this.height, left:distance };
	} else {
		clippingObj	= { top:0, right:this.width, bottom:this.height, left:this.width };
		this.oncomplete();
	}
	this.clipto(clippingObj);
}


DhtmlObj.prototype.wipeInFromLeft = function () {
	this.elapsed = new Date().getTime() - this.starttime;
	var distance = this.width*(this.elapsed/this.duration);
	var clippingObj;
	if (this.elapsed < this.duration) {
		clippingObj	= { top:0, right:distance, bottom:this.height, left:0 };
	} else {
		clippingObj	= { top:0, right:this.width, bottom:this.height, left:0 };
		this.oncomplete();
	}
	this.clipto(clippingObj);
}


DhtmlObj.prototype.wipeOutFromLeft = function () {
	this.elapsed = new Date().getTime() - this.starttime;
	var distance = this.width*(this.elapsed/this.duration);
	var clippingObj;
	if (this.elapsed < this.duration) {
		clippingObj	= { top:0, right:this.width-distance, bottom:this.height, left:0 };
	} else {
		clippingObj	= { top:0, right:0, bottom:this.height, left:0 };
		this.oncomplete();
	}
	this.clipto(clippingObj);
}

DhtmlObj.prototype.wipeInFromBottom = function () {
	this.elapsed = new Date().getTime() - this.starttime;
	var distance = this.height*(this.elapsed/this.duration);
	var clippingObj;
	if (this.elapsed < this.duration) {
		clippingObj	= { top:this.height-distance, right:this.width, bottom:this.height, left:0 };
	} else {
		clippingObj	= { top:0, right:this.width, bottom:this.height, left:0 };
		this.oncomplete();
	}
	this.clipto(clippingObj);
}


DhtmlObj.prototype.wipeOutFromBottom = function () {
	this.elapsed = new Date().getTime() - this.starttime;
	var distance = this.height*(this.elapsed/this.duration);
	var clippingObj;
	if (this.elapsed < this.duration) {
		clippingObj	= { top:distance, right:this.width, bottom:this.height, left:0 };
	} else {
		clippingObj	= { top:this.height, right:this.width, bottom:this.height, left:0 };
		this.oncomplete();
	}
	this.clipto(clippingObj);
}


DhtmlObj.prototype.wipeInHorizontalFromMiddle = function () {
	this.elapsed = new Date().getTime() - this.starttime;
	var distance = (this.width/2)*(this.elapsed/this.duration);
	var clippingObj;
	if (this.elapsed < this.duration) {
		clippingObj	= { top:0, right:(this.width/2)+distance, bottom:this.height, left:(this.width/2)-distance };
	} else {
		clippingObj	= { top:0, right:this.width, bottom:this.height, left:0 };
		this.oncomplete();
	}
	this.clipto(clippingObj);
}


DhtmlObj.prototype.wipeOutHorizontalFromMiddle = function () {
	this.elapsed = new Date().getTime() - this.starttime;
	var distance = (this.width/2)*(this.elapsed/this.duration);
	var clippingObj;
	if (this.elapsed < this.duration) {
		clippingObj	= { top:0, right:this.width-distance, bottom:this.height, left:distance };
	} else {
		clippingObj	= { top:0, right:0, bottom:this.height, left:this.width };
		this.oncomplete();
	}
	this.clipto(clippingObj);
}


DhtmlObj.prototype.wipeInVerticalFromMiddle = function () {
	this.elapsed = new Date().getTime() - this.starttime;
	var distance = (this.height/2)*(this.elapsed/this.duration);
	var clippingObj;
	if (this.elapsed < this.duration) {
		clippingObj	= { top:(this.height/2)-distance, right:this.width, bottom:(this.height/2)+distance, left:0 };
	} else {
		clippingObj	= { top:0, right:this.width, bottom:this.height, left:0 };
		this.oncomplete();
	}
	this.clipto(clippingObj);
}


DhtmlObj.prototype.wipeOutVerticalFromMiddle = function () {
	this.elapsed = new Date().getTime() - this.starttime;
	var distance = (this.height/2)*(this.elapsed/this.duration);
	var clippingObj;
	if (this.elapsed < this.duration) {
		clippingObj	= { top:distance, right:this.width, bottom:this.height-distance, left:0 };
	} else {
		clippingObj	= { top:this.height/2, right:this.width, bottom:this.height/2, left:0 };
		this.oncomplete();
	}
	this.clipto(clippingObj);
}


DhtmlObj.prototype.wipeInFromCenter = function () {
	this.elapsed = new Date().getTime() - this.starttime;
	var hdistance = (this.width/2)*(this.elapsed/this.duration);
	var vdistance = (this.height/2)*(this.elapsed/this.duration);
	var clippingObj;
	if (this.elapsed < this.duration) {
		clippingObj	= { top:(this.height/2)-vdistance, right:(this.width/2)+hdistance, bottom:(this.height/2)+vdistance, left:(this.width/2)-hdistance };
	} else {
		clippingObj	= { top:0, right:this.width, bottom:this.height, left:0 };
		this.oncomplete();
	}
	this.clipto(clippingObj);
}


DhtmlObj.prototype.wipeOutFromCenter = function () {
	this.elapsed = new Date().getTime() - this.starttime;
	var hdistance = (this.width/2)*(this.elapsed/this.duration);
	var vdistance = (this.height/2)*(this.elapsed/this.duration);
	var clippingObj;
	if (this.elapsed < this.duration) {
		clippingObj	= { top:vdistance, right:this.width-hdistance, bottom:this.height-vdistance, left:hdistance };
	} else {
		clippingObj	= { top:this.height/2, right:this.width/2, bottom:this.height/2, left:this.width/2 };
		this.oncomplete();
	}
	this.clipto(clippingObj);
}


DhtmlObj.prototype.wipeInFromTopLeft = function () {
	this.elapsed = new Date().getTime() - this.starttime;
	var hdistance = this.width*(this.elapsed/this.duration);
	var vdistance = this.height*(this.elapsed/this.duration);
	var clippingObj;
	if (this.elapsed < this.duration) {
		clippingObj	= { top:0, right:hdistance, bottom:vdistance, left:0 };
	} else {
		clippingObj	= { top:0, right:this.width, bottom:this.height, left:0 };
		this.oncomplete();
	}
	this.clipto(clippingObj);
}


DhtmlObj.prototype.wipeOutFromTopLeft = function () {
	this.elapsed = new Date().getTime() - this.starttime;
	var hdistance = this.width*(this.elapsed/this.duration);
	var vdistance = this.height*(this.elapsed/this.duration);
	var clippingObj;
	if (this.elapsed < this.duration) {
		clippingObj	= { top:0, right:this.width-hdistance, bottom:this.height-vdistance, left:0 };
	} else {
		clippingObj	= { top:this.height, right:0, bottom:0, left:this.width };
		this.oncomplete();
	}
	this.clipto(clippingObj);
}


DhtmlObj.prototype.wipeInFromTopRight = function () {
	this.elapsed = new Date().getTime() - this.starttime;
	var hdistance = this.width*(this.elapsed/this.duration);
	var vdistance = this.height*(this.elapsed/this.duration);
	var clippingObj;
	if (this.elapsed < this.duration) {
		clippingObj	= { top:0, right:this.width, bottom:vdistance, left:this.width-hdistance };
	} else {
		clippingObj	= { top:0, right:this.width, bottom:this.height, left:0 };
		this.oncomplete();
	}
	this.clipto(clippingObj);
}


DhtmlObj.prototype.wipeOutFromTopRight = function () {
	this.elapsed = new Date().getTime() - this.starttime;
	var hdistance = this.width*(this.elapsed/this.duration);
	var vdistance = this.height*(this.elapsed/this.duration);
	var clippingObj;
	if (this.elapsed < this.duration) {
		clippingObj	= { top:0, right:this.width, bottom:this.height-vdistance, left:hdistance };
	} else {
		clippingObj	= { top:0, right:this.width, bottom:0, left:this.width };
		this.oncomplete();
	}
	this.clipto(clippingObj);
}


DhtmlObj.prototype.wipeInFromBottomRight = function () {
	this.elapsed = new Date().getTime() - this.starttime;
	var hdistance = this.width*(this.elapsed/this.duration);
	var vdistance = this.height*(this.elapsed/this.duration);
	var clippingObj;
	if (this.elapsed < this.duration) {
		clippingObj	= { top:this.height-vdistance, right:this.width, bottom:this.height, left:this.width-hdistance };
	} else {
		clippingObj	= { top:0, right:this.width, bottom:this.height, left:0 };
		this.oncomplete();
	}
	this.clipto(clippingObj);
}


DhtmlObj.prototype.wipeOutFromBottomRight = function () {
	this.elapsed = new Date().getTime() - this.starttime;
	var hdistance = this.width*(this.elapsed/this.duration);
	var vdistance = this.height*(this.elapsed/this.duration);
	var clippingObj;
	if (this.elapsed < this.duration) {
		clippingObj	= { top:vdistance, right:this.width-hdistance, bottom:this.height-vdistance, left:hdistance };
	} else {
		clippingObj	= { top:this.height, right:this.width, bottom:this.height, left:this.width };
		this.oncomplete();
	}
	this.clipto(clippingObj);
}


DhtmlObj.prototype.wipeInFromBottomLeft = function () {
	this.elapsed = new Date().getTime() - this.starttime;
	var hdistance = this.width*(this.elapsed/this.duration);
	var vdistance = this.height*(this.elapsed/this.duration);
	var clippingObj;
	if (this.elapsed < this.duration) {
		clippingObj	= { top:this.height-vdistance, right:hdistance, bottom:this.height, left:0 };
	} else {
		clippingObj	= { top:0, right:this.width, bottom:this.height, left:0 };
		this.oncomplete();
	}
	this.clipto(clippingObj);
}


DhtmlObj.prototype.wipeOutFromBottomLeft = function () {
	this.elapsed = new Date().getTime() - this.starttime;
	var hdistance = this.width*(this.elapsed/this.duration);
	var vdistance = this.height*(this.elapsed/this.duration);
	var clippingObj;
	if (this.elapsed < this.duration) {
		clippingObj	= { top:vdistance, right:this.width, bottom:this.height, left:hdistance };
	} else {
		clippingObj	= { top:this.height, right:this.width, bottom:this.height, left:this.width };
		this.oncomplete();
	}
	this.clipto(clippingObj);
}


DhtmlObj.prototype.wipe = function ( wipe, duration, oncomplete, endclip ) {
	
	this.oncomplete = function () 
	{
		clearInterval(this.wipeIntervalID);
		this.wiping = false;
		eval(this.fcomplete);
	}
	
	
	if (this.wiping != true) {
			
		this.duration 	= duration;
		this.fcomplete	= oncomplete;
		this.endclip 	= endclip;
		this.interval 	= 35;
		this.starttime 	= new Date().getTime() + this.interval;
		this.elapsed	= 0;
		this.wiping 	= true;
		
		this.wipeIntervalID;
		
		if (this.endclip != null) {
			this.clipvalues = this.getClippingValues();
			this.wipeWidth = Math.abs(this.endclip.right-this.clipvalues.right) + Math.abs(this.endclip.left-this.clipvalues.left);
			this.wipeHeight = Math.abs(this.endclip.bottom-this.clipvalues.bottom) + Math.abs(this.endclip.top-this.clipvalues.top);
		}
		
		var clippingObj;
		
		switch (wipe) {
			case "infromtop" :
				this.wipeIntervalID = setInterval(this.objRef+'.wipeInFromTop()', this.interval);
				break;
			case "outfromtop" :
				this.wipeIntervalID = setInterval(this.objRef+'.wipeOutFromTop()', this.interval);
				break;
			case "infrombottom" :
				this.wipeIntervalID = setInterval(this.objRef+'.wipeInFromBottom()', this.interval);
				break;
			case "infrombottom" :
				this.wipeIntervalID = setInterval(this.objRef+'.wipeInFromBottom()', this.interval);
				break;
			case "outfrombottom" :
				this.wipeIntervalID = setInterval(this.objRef+'.wipeOutFromBottom()', this.interval);
				break;
			case "inhorizontalfrommiddle" :
				this.wipeIntervalID = setInterval(this.objRef+'.wipeInHorizontalFromMiddle()', this.interval);
				break;
			case "outhorizontalfrommiddle" :
				this.wipeIntervalID = setInterval(this.objRef+'.wipeOutHorizontalFromMiddle()', this.interval);
				break;
			case "inverticalfrommiddle" :
				this.wipeIntervalID = setInterval(this.objRef+'.wipeInVerticalFromMiddle()', this.interval);
				break;
			case "outverticalfrommiddle" :
				this.wipeIntervalID = setInterval(this.objRef+'.wipeOutVerticalFromMiddle()', this.interval);
				break;
			case "infromcenter" :
				this.wipeIntervalID = setInterval(this.objRef+'.wipeInFromCenter()', this.interval);
				break;
			case "outfromcenter" :
				this.wipeIntervalID = setInterval(this.objRef+'.wipeOutFromCenter()', this.interval);
				break;
			case "infromright" :
				this.clipto( { top:0, right:this.width, bottom:this.height, left:this.width } );
				this.wipeIntervalID = setInterval(this.objRef+'.wipeInFromRight()', this.interval);
				break;
			case "outfromright" :
				this.wipeIntervalID = setInterval(this.objRef+'.wipeOutFromRight()', this.interval);
				break;
			case "infromleft" :
				this.clipto( { top:0, right:0, bottom:this.height, left:0 } );
				this.element.style.visibility = 'visible';
				this.wipeIntervalID = setInterval(this.objRef+'.wipeInFromLeft()', this.interval);
				break;
			case "outfromleft" :
				this.wipeIntervalID = setInterval(this.objRef+'.wipeOutFromLeft()', this.interval);
				break;
			case "infromtopright" :
				this.wipeIntervalID = setInterval(this.objRef+'.wipeInFromTopRight()', this.interval);
				break;
			case "outfromtopright" :
				this.wipeIntervalID = setInterval(this.objRef+'.wipeOutFromTopRight()', this.interval);
				break;
			case "infromtopleft" :
				this.wipeIntervalID = setInterval(this.objRef+'.wipeInFromTopLeft()', this.interval);
				break;	
			case "outfromtopleft" :
				this.wipeIntervalID = setInterval(this.objRef+'.wipeOutFromTopLeft()', this.interval);
				break;
			case "fromAtoB" :
				this.wipeIntervalID = setInterval(this.objRef+'.wipeFromAtoB()', this.interval);
				break;
			default: break;
		}
		
	}
}


/*
	StraightLineAnimation
	
	Description:
	
*/

StraightLineAnimation = function (id,origin,dest,speed,interval) {
	
	/* private instance variables */
	var self = this;
	
	var id 		= id;
	var origin 	= origin;
	var dest 	= dest;
	
	var element;		// DOM element
	var x, y;			// current x,y position
	var dX, dY; 		// final delta x and delta y
	var dXY;			// straight line distance from origin to dest
	var xStep, yStep;	// pixels per interval
	var	dx, dy;			// x y change at each interval
	
	
	/* public instance variables */
	this.intervalID;
	this.interval = interval;
	
	
	/* private methods */
	
	function init() {
		
		// dom element
		element = document.getElementById(id);
		element.style.position = 'absolute';
		
		// start position
		setX(origin.x);
		setY(origin.y);
		
		// zero delta x,y
		dx = dy = 0;
		
		// total change in position 
		dX = Math.abs(dest.x - origin.x);
		dY = Math.abs(dest.y - origin.y);
		dXY= Math.sqrt(Math.pow((origin.x-dest.x),2)+Math.pow((origin.y-dest.y),2));
		
		// calculate x y steps from speed and straightline distance
		xStep = (dX/dXY)*speed;
		yStep = (dY/dXY)*speed;
	}
	
	
	function setX (x) { 
		X = x;
		element.style.left = X +'px'; 
	}
	
	function setY (y) { 
		Y = y;
		element.style.top = Y +'px'; 
	}
	
	
	/* priveledged methods */
	
	this.animate = function () {
		
		// if the distance traveled is less than the total continue
		if ((dx + xStep < dX) || (dy + yStep < dY)) {
			
			if (dx + xStep < dX) {
				dx += xStep;
				setX(X+xStep);
			}	
			
			if (dy + yStep < dY) {
				dy += yStep;
				setY(Y+yStep);
			}
			
		} else {
			
			setX(dest.x);
			setY(dest.y);
			self.stop();
		}
	}
	
	init();
	return this;
}


StraightLineAnimation.prototype.start = function () {
	
	this.intervalID = setInterval(this.animate,this.interval);
}


StraightLineAnimation.prototype.stop = function () {
	
	clearInterval(this.intervalID);
}

