//File Information:
//Version: $Revision: 974 $
//Last Changed By: $Author: cfrommann $ on $Date: 2006-07-02 23:19:51 -0400 (Sun, 02 Jul 2006) $

// lib/animate.js
//  JS animation library

/* iWebPress - A Newspaper Administration tool
 * http://www.iwebpress.com
 * Copyright (C) 2002-2006 iWebPress, Inc., et al except where otherwise noted
 *
 * This program is protected by copyright.  Redistribution 
 * is prohibited without express written consent of the
 * authors.  You can find contact information on iwebpress.com.
 *
 * This program is distributed in the hope that it will be 
 * useful, but WITHOUT ANY WARRANTY; without even the 
 * implied warranty of MERCHANTABILITY or FITNESS FOR A 
 * PARTICULAR PURPOSE.
 */

function Animator(obj) {
	this.obj = obj;
	
	this.changeDimensions = function(widthf, heightf, steps, intervals, powr, nextOp) {
		var curStep = 0;
		var obj = this.obj;
		var widthi = parseInt(getStyle(this.obj,'width'));
		var heighti = parseInt(getStyle(this.obj,'height'));
		
		if( !(widthi || widthf) && !(heighti || heightf) ) return false;
		
		if(obj.dimensionsInterval)
			window.clearInterval(obj.dimensionsInterval);
		obj.dimensionsInterval = window.setInterval(
			function() {
				if(widthi) 
					obj.style.width = _easeInOut(widthi,widthf,steps,curStep++,powr) + 'px';
				if(heighti)
					obj.style.height = _easeInOut(heighti,heightf,steps,curStep++,powr) + 'px';
				if (curStep > steps) {
					window.clearInterval(obj.dimensionsInterval);
					if(nextOp)
						setTimeout(nextOp,1);
				}
			} 
		,intervals);
	}
	
	this.changeHeight = function(heightf, steps, intervals, powr, nextOp) {
		var curStep = 0;
		var obj = this.obj;
		var heighti = parseInt(getStyle(this.obj,'height'));

		if(!(heighti || heightf)) return false;
		
		if(obj.dimensionsInterval)
			window.clearInterval(obj.dimensionsInterval);
		obj.dimensionsInterval = window.setInterval(
			function() {
				obj.style.height = _easeInOut(heighti,heightf,steps,curStep++,powr) + 'px';
				if (curStep > steps) {
					window.clearInterval(obj.dimensionsInterval);
					if(nextOp)
						setTimeout(nextOp,1);
				}
			} 
		,intervals);
	}
	
	this.setDimensions = function(width,height) {
		this.obj.style.width = width + 'px';
		this.obj.style.height = height + 'px';
	}
	
	this.setHeight = function(height) {
		this.obj.style.height = height + 'px';
	}

	this.changeOpacity = function(opacityf, steps, intervals, powr, nextOp) {
		if( opacityf < 0 && opacityf > 100 ) return false;
		
		var curStep = 0;
		var obj = this.obj;
		var opacityi = this.getOpacity()==null ? 100 : this.getOpacity();
		
		if(obj.opacityInterval)
			window.clearInterval(obj.opacityInterval);
		obj.opacityInterval = window.setInterval(
			function() {
				var opac = _easeInOut(opacityi,opacityf,steps,curStep++,powr);
				if(document.all) {
					obj.style.filter = 'alpha(opacity=' + opac + ')';
				} else {
					if(ua.indexOf("safari") != -1 || ua.indexOf("konqueror") != -1) {
						obj.style.opacity = opac / 100; //DOM3
					} else if(ua.indexOf("gecko") != -1) {
						obj.style.MozOpacity = opac / 100;
					}
				}
				if (curStep > steps) {
					window.clearInterval(obj.opacityInterval);
					if(nextOp)
						setTimeout(nextOp,1);
				}
			} 
		,intervals);
	}
	
	this.setOpacity = function(opac) {
		if(document.all) {
			this.obj.style.filter = 'alpha(opacity=' + opac + ')';
		} else {
			if(ua.indexOf("safari") != -1 || ua.indexOf("konqueror") != -1) {
				this.obj.style.opacity = opac / 100; //DOM3
			} else if(ua.indexOf("gecko") != -1) {
				this.obj.style.MozOpacity = opac / 100;
			}
		}
	}
	
	this.getOpacity = function() {
		if(document.all) {
			try {
				return this.obj.style.filter.alpha.opacity;
			} catch(e) {
				return 100;
			}
		} else {
			if(ua.indexOf("safari") != -1 || ua.indexOf("konqueror") != -1) {
				return getStyle(this.obj,'opacity') * 100;
			} else if(ua.indexOf("gecko") != -1) {
				return getStyle(this.obj,'MozOpacity') * 100;
			}
		}
	}
	
	this.movePositionCartesian = function(xf, yf, steps, intervals, powr, nextOp) {			
		var curStep = 0;
		var obj = this.obj;
		var xi = parseInt(getStyle(this.obj,'left'));
		var yi = parseInt(getStyle(this.obj,'top'));
		
		if(!getStyle(this.obj,'position'))
			this.obj.style.position = 'relative';

		if(obj.positionInterval)
			window.clearInterval(obj.positionInterval);
		obj.positionInterval = window.setInterval(
			function() {
				if(xi || xf)
					obj.style.left = _easeInOut(xi,xf,steps,curStep++,powr) + 'px';
				if(yi || yf)
					obj.style.top = _easeInOut(yi,yf,steps,curStep++,powr) + 'px';
				if (curStep > steps) {
					window.clearInterval(obj.positionInterval);
					if(nextOp)
						setTimeout(nextOp,1);
				}
			} 
		,intervals);
	}
	
	this.oscillateX = function(times, distance, steps, intervals, powr, nextOp) {
		var curTime = 0;
		var curStep = 0;
		var obj = this.obj;
		var xi = parseInt(getStyle(this.obj,'left'));
		var xf = xi - (distance / 2);
		
		if(!getStyle(this.obj,'position'))
			this.obj.style.position = 'relative';

		if(obj.positionInterval)
			window.clearInterval(obj.positionInterval);
		obj.positionInterval = window.setInterval(
			function() {
				obj.style.left = _easeInOut(xi,xf,steps,curStep++,powr) + 'px';
				if (curStep > steps) {
					if(curTime > times) {
						window.clearInterval(obj.positionInterval);
						if(nextOp)
							setTimeout(nextOp,1);
					} else {
						curTime++;	
						if(curTime % 2 == 0) {
							xi = xf;
							xf = xi - ((curTime>times) ? distance/2 : distance);
						} else {
							xi = xf;
							xf = xi + ((curTime>times) ? distance/2 : distance);
						}
						curStep = 0;
					}
					
				}
			}
		,intervals);
	}
	
	this.setPosition = function(x, y) {
		if(!getStyle(this.obj,'position'))
			this.obj.style.position = 'relative';
		this.obj.style.left = x + 'px';
		this.obj.style.top = y + 'px';
	}
	
	this.movePositionPolar = function(r, thetaf, steps, intervals, powr, nextOp) {
		var curStep = 0;
		var obj = this.obj;
		var xi = parseInt(getStyle(this.obj,'left'));
		var yi = parseInt(getStyle(this.obj,'top'));
		
		if(!getStyle(this.obj,'position'))
			this.obj.style.position = 'relative';

		if(obj.positionInterval)
			window.clearInterval(obj.positionInterval);
		obj.positionInterval = window.setInterval(
			function() {
				var theta = _easeInOut(1,thetaf*1000,steps,curStep++,powr)/1000;
				obj.style.left = ((eval(r) * Math.cos(theta))+xi) + 'px';
				obj.style.top = (-(eval(r) * Math.sin(theta))+yi) + 'px';
				
				if (curStep > steps) {
					window.clearInterval(obj.positionInterval);
					if(nextOp)
						setTimeout(nextOp,1);
				}
			} 
		,intervals);
	}
	
	this.hide = function(nextOp) {
		this.obj.style.visibility = 'hidden';
		if(nextOp)
			setTimeout(nextOp,1);
	}
	
	this.show = function(nextOp) {
		this.obj.style.visibility = 'visible';
		if(nextOp)
			setTimeout(nextOp,1);
	}
	
	this.collapse = function(nextOp) {
		this.obj.style.display = 'none';
		if(nextOp)
			setTimeout(nextOp,1);
	}
	
	this.expand = function(nextOp) {
		this.obj.style.display = 'block';
		if(nextOp)
			setTimeout(nextOp,1);
	}
	
	this.stop = function() {
		if(this.obj.dimensionsInterval)
			window.clearInterval(this.obj.dimensionsInterval);
		if(this.obj.positionInterval)
			window.clearInterval(this.obj.positionInterval);
		if(this.obj.opacityInterval)
			window.clearInterval(this.obj.opacityInterval);
	}
	
	_easeInOut = function(min,max,totalSteps,actualStep,powr) { 
		//Generic Animation Step Value Generator By www.hesido.com 
		var delta = max - min; //Æ
		var stepp = min + (Math.pow((actualStep / totalSteps), powr) * delta);
		return Math.ceil(stepp);
	} 
}

/****************************************************************************************
*			Methods below makes use of animation										*
*****************************************************************************************/

/*
* Modified from iWP Code to use divs instead of lis (I don't have time to tame them...again)
*/

function Paginate(node,instance) {
	this.node = node;
	this.current = null;
	this.expanding = false;
	
	var tab,contents,height,eContents;
	for(var i = 0; i < this.node.childNodes.length; i++) {
		if(this.node.childNodes[i].nodeName == 'DIV' && 
			this.node.childNodes[i].childNodes.length > 0 &&
			this.node.childNodes[i].firstChild.nodeName == 'A') {
				div = this.node.childNodes[i];
				anchor = this.node.childNodes[i].firstChild;
				contents = document.getElementById(anchor.getAttribute('tab'));
				if(contents) {
					height = parseInt(getStyle(contents,'height'));
					defineAttribute(anchor,'contentHeight',height);
					eContents = new Animator(contents);
					eContents.setHeight(document.all ? 3 : 1);
					eContents.setOpacity(0);
					
					div.onmouseover = function() {
						a = new Animator(this);
						if(!eval(instance).current)
							a.changeDimensions(220, 180, 50, 2, .5, function() {});
						a.setOpacity(99);
						//this.style.backgroundImage = getStyle(this,"background-image").replace(/\.gif/g,"l.gif");
					}
					div.onmouseout = function() {
						a = new Animator(this);
						if(!eval(instance).current)
							a.changeDimensions(150, 180, 50, 2, .5, function() {});
						a.setOpacity(85);
					}
					div.onclick = function() {
						a = new Animator(this);
						p = new Animator(this.parentNode);
						
						if(this == eval(instance).current) return;
						var anchor = this.firstChild;
						var height = parseInt(anchor.getAttribute('contentHeight')) + 20; //+20 for padding
						
						eContents = new Animator(document.getElementById(anchor.getAttribute('tab')));
						
						if(eval(instance).current) {
							prev = new Animator(eval(instance).current);
							prev.changeDimensions(150, 180, 50, 2, .5, function() {});
							a.changeDimensions(220, 180, 50, 2, .5, function() {});
							cContents = new Animator(document.getElementById(eval(instance).current.firstChild.getAttribute('tab')));
							cContents.changeOpacity(0,10,50,2,function() {
								cContents.setHeight(document.all ? 3 : 1);
								eContents.setHeight(height);
								eContents.changeOpacity(85,10,50,2);
							});
						} else {
							p.movePositionCartesian(0, 25, 10, 2, 1.5, function() {});
							//a.changeDimensions(150, 180, 50, 2, .5, function() {});
							a.setOpacity(99);
							eContents.setHeight(height);
							eContents.changeOpacity(85,10,50,2);
						}
						
						eval(instance).current = this;
						return false;
					}
				} 
			}
	}
}
