var collection = function() {
	this.obj = window;
	
	this.setopacity = function (elem, opacityAsInt)
 {
    var opacityAsDecimal = opacityAsInt;
    if (opacityAsInt > 100)
      opacityAsInt = opacityAsDecimal = 100; 
      else if (opacityAsInt < 0)
          opacityAsInt = opacityAsDecimal = 0; 
      
     opacityAsDecimal /= 100;
     if (opacityAsInt < 1)
         opacityAsInt = 1; // IE7 bug, text smoothing cuts out if 0
     
     elem.style.opacity = (opacityAsDecimal);
     elem.style.filter  = "alpha(opacity=" + opacityAsInt + ")";
 }	
	
	this.updown = function(obj,limit,step) {
		  height = obj.offsetHeight;
		  if (height<limit) {
				obj.style.height = height+step+'px';
				return true;
			}
			return false;
	}
	
	this.leftright = function(obj,limit,step) {
		  width = obj.offsetWidth;
		  if (width<limit) {
				obj.style.width = width+step+'px';
				return true;
			}
			return false;
	}
	this.rightleft = function(obj,limit,step) {
		  width = obj.offsetWidth;
		  if (width>limit) {
			  	if (width<step) obj.style.width = '0px';
				else obj.style.width = width-step+'px';
				return true;
			}
			obj.style.width = limit+'px';
			return false;
	}
	
	this.downup = function(obj,limit,step) {
		   height = obj.offsetHeight;
		   if (height>limit) {
			   	if (height<step)height = 0;
	        	else height=height-step;
				obj.style.height = height+'px';
			return true;
		  }
		   obj.style.height = limit + 'px';
		   //alert("downup done");
		  return false;
		  
	}
	
	this.fadeout = function(obj) {
		op = obj.style.opacity;
		op=(op*100)-10;
		if (op>=0) {
			this.setopacity(obj,op);
			return true;
		}
		obj.style.display='none';
		return false; 		
	}
	this.fadein = function(obj) {
		op = obj.style.opacity;
		op=(op*100)+10;
		if (op<=100) {
			obj.style.display='block';
			this.setopacity(obj,op);
			return true;
		} return false;		
	}
}
var movement = new collection();

var Animatable = function(obj) {
		this.obj = obj;
		this.animatable = true;
		this.step = function() {};
}

var UpDown = function(obj) {
		this.obj = obj;
		this.obj.style.display='block';
		this.obj.style.height = this.obj.offsetHeight+'px';
		this.animatable = true;
		this.limit = 0;
		this.setLimit = function(limit) { 
			this.limit = limit;
			this.steps = Math.round((limit-this.obj.offsetHeight)/this.stepHeight);
		}
		this.stepHeight=10;
		this.steps = 0;
		this.getSteps = function() {if (this.steps>0) return this.steps; return 1;}
		this.step = function() {return movement.updown(this.obj,this.limit,this.stepHeight);}	
}

var DownUp = function(obj) {
		this.obj = obj;
		this.obj.style.display='block';
		this.obj.style.height = this.obj.offsetHeight+'px';
		this.animatable = true;
		this.limit = 0;
		this.stepHeight = 10;
		this.setLimit = function(limit) { 
			this.limit = limit;
			this.steps = Math.round((this.obj.offsetHeight-limit)/this.stepHeight);
		}
		this.steps = Math.round(this.obj.offsetHeight/this.stepHeight);
		this.getSteps = function() {if (this.steps>0) return this.steps; return 1;}
		this.step = function() {return movement.downup(this.obj,this.limit,this.stepHeight);}	
}

var LeftRight = function(obj) {
		this.obj = obj;
		this.obj.style.display='block';
		this.obj.style.width = this.obj.offsetWidth+'px';
		this.animatable = true;
		this.limit = 0;
		this.setLimit = function(limit) { 
			this.limit = limit;
			this.steps = Math.round((limit-this.obj.offsetWidth)/this.stepWidth);
		}
		this.stepWidth=10;
		this.steps = 0;
		this.getSteps = function() {if (this.steps>0) return this.steps; return 1;}
		this.step = function() {return movement.leftright(this.obj,this.limit,this.stepWidth);}	
}


var RightLeft = function(obj) {
		this.obj = obj;
		this.obj.style.display='block';
		this.obj.style.width = this.obj.offsetWidth+'px';
		this.animatable = true;
		this.limit = 0;
		this.stepWidth = 10;
		this.setLimit = function(limit) { 
			this.limit = limit;
			this.steps = Math.round((this.obj.offsetWidth-limit)/this.stepWidth);
		}
		this.steps = Math.round(this.obj.offsetWidth/this.stepWidth);
		this.getSteps = function() {if (this.steps>0) return this.steps; return 1;}
		this.step = function() {return movement.rightleft(this.obj,this.limit,this.stepWidth);}	
}

var FadeOut = function(obj) {
		this.obj = obj;
		this.animatable = true;
		this.getSteps = function() {return 10;}
		this.step = function() {return movement.fadeout(this.obj);}
}

var FadeIn = function(obj) {
		this.obj = obj;
		this.obj.style.display='block';
		this.obj.style.opacity=0;
		this.animatable = true;
		this.steps = 10;
		this.getSteps = function() {return this.steps;}
		this.step = function() {return movement.fadein(this.obj);}
}

var Animator = function(Animatable) {
	this.Animatable = Animatable;
	this.timer = null;
	this.duration = 0;
	

	this.start = function() {
		var self = this;
		this.timer = setTimeout(function() {self.stepAnimation();},5);
	}
	
	this.stepAnimation = function() {
		if (this.animation()) this.start();
	}
	
	this.animation = function() {
				if (!this.Animatable.step()) {this.stop();return false;} 
				else return true;
	}
	
	this.stop = function(){if (this.timer) clearTimeout(this.timer); this.timer = null;}
	
	this.getSpeed = function() {
		if (!this.Animatable.getSteps()) return this.duration;
		var speed = this.duration/this.Animatable.getSteps();
		if (speed < 1) return 1;
		return speed;
	}
}

var AnimatorCue = function() {
	this.Animators = new Array();
	this.addAnimator = function(Animator) {this.Animators.push(Animator);}
	this.timer = null;
	this.counter = 0;
	this.execute = function() {
		var self = this;
		var Animator;
		if (self.Animators[self.counter]) {
				Animator = self.Animators[self.counter];
		}
		else {self.timer = null;return null;}
		//alert(Animator.getSpeed());
		self.timer = setTimeout(function() {self.stepAnimation(Animator);},Animator.getSpeed());
	}
	this.stepAnimation = function(Animator) {
		if (Animator.animation())
			{			 
			 this.execute();
		 	}
		else {
			this.counter++;
			this.execute();
			}
	}
	this.pause = function() {clearTimeout(this.timer);this.timer = null;}
	this.stop = function() {clearTimeout(this.timer);this.timer = null;this.counter=0;}
	this.clear = function() {this.Animators = new Array();};
	
}
