﻿/*  Copyright 2007 Ian Robson (www.byian.co.uk)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    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.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
var Transition = Class.create();
Transition.prototype = {
	initialize : function (opts){
		this.options = {
			duration: 500,
			stages: 25,
			movement: "halfsine"
		}
		Object.extend(this.options, opts || {});
		this.options.stepDuration = this.options.duration / this.options.stages;
		this.moving = false;
		this.queue = [];
	},
	addTransition : function(movements){
		if (movements.push == null)
			movements = [movements];
		this.queue.push(movements);
	},
	begin : function(){
		if (!this.moving && this.queue.length > 0){
			this.currentMovement = this.queue.shift();
			var toGo = false;
			for (var i=0; i<this.currentMovement.length; i++){
				var it = this.currentMovement[i];
				if (it.elem){
					if (it.instant){
						if (it.action == "remove")
							it.elem.parentNode.removeChild(it.elem);
						else
							it.elem[it.attrib] = it.end + it.suffix;
					}else{
						toGo = true;
						if (it.start == null)
							it.start = parseInt(it.elem[it.attrib]);
						it.step = (it.end - it.start) / this.options.stages;
					}
				}
			}
			if (toGo){
				this.currentStage = 0;
				this.moving = true;
				this.inter = setInterval(this.tick.bind(this), this.options.stepDuration);
			}
		}
	},
	doTransition : function(movements){
		this.addTransition(movements);
		this.begin();
	},
	tick : function(){
		this.currentStage++;
		for (var i=0; i<this.currentMovement.length; i++){
			var it = this.currentMovement[i];
			var val;
			if (it.elem != null){
				if (this.options.movement == "linear")
					val = it.start + Math.round(this.currentStage * it.step);
				else if(this.options.movement == "sine")
					val = (it.start + Math.round(((Math.sin((Math.PI * (this.currentStage / this.options.stages)) - (Math.PI/2))+1)/2) * (it.end - it.start)));
				else if(this.options.movement == "halfsine")
					val = it.start + Math.round(Math.pow(Math.sin(Math.PI * (this.currentStage / (this.options.stages * 2))), 0.5) * (it.end - it.start));
				it.elem[it.attrib] = it.suffix == null ? val : val.toString() + it.suffix;
			}
		}
		if (this.currentStage == this.options.stages){
			clearInterval(this.inter);
			this.currentStage = 0;
			this.moving = false;
			this.begin();
		}
	}
}