﻿function Accelimation(from, to, time, zip,src) {
if (typeof zip == "undefined") zip = 0;
if (typeof unit == "undefined") unit = "px";
this.x0 = from;
this.x1 = to;
this.dt = time;
this.zip = -zip;
this.unit = unit;
this.timer = null;
this.sender=src;
this.onend = new Function();
this.onframe = new Function();
}




Accelimation.prototype.start = function() {
this.t0 = new Date().getTime();
this.t1 = this.t0 + this.dt;
var dx = this.x1 - this.x0;
this.c1 = this.x0 + ((1 + this.zip) * dx / 3);
this.c2 = this.x0 + ((2 + this.zip) * dx / 3);
Accelimation._add(this);
}

Accelimation.prototype.stop = function() {
Accelimation._remove(this);
}




Accelimation.prototype._paint = function(time) {
	if (time < this.t1) {
	var elapsed = time - this.t0;
	this.onframe(Accelimation._getBezier(elapsed/this.dt,this.x0,this.x1,this.c1,this.c2));
	}
	else this._end();
}

Accelimation.prototype._end = function() {
	Accelimation._remove(this);
	this.onframe(this.x1);
	this.onend();
}




Accelimation._add = function(o) {
	var index = this.instances.length;
	this.instances[index] = o;	
	if (this.instances.length == 1) {
	this.timerID = window.setInterval("Accelimation._paintAll()", this.targetRes);
	}
}

Accelimation._remove = function(o) {
		for (var i = 0; i < this.instances.length; i++) 
		{
			if (o == this.instances[i]) {
				this.instances = this.instances.slice(0,i).concat( this.instances.slice(i+1) );
				break;
			}
		}

		if (this.instances.length == 0) {
			window.clearInterval(this.timerID);
			this.timerID = null;
	    }
}

Accelimation._paintAll = function() {
	var now = new Date().getTime();
	for (var i = 0; i < this.instances.length; i++)
	{
		this.instances[i]._paint(now);
	}
}

Accelimation._B1 = function(t) { return t*t*t }
Accelimation._B2 = function(t) { return 3*t*t*(1-t) }
Accelimation._B3 = function(t) { return 3*t*(1-t)*(1-t) }
Accelimation._B4 = function(t) { return (1-t)*(1-t)*(1-t) }

Accelimation._getBezier = function(percent,startPos,endPos,control1,control2) {
return endPos * this._B1(percent) + control2 * this._B2(percent) + control1 * this._B3(percent) + startPos * this._B4(percent);
}



Accelimation.instances = [];
Accelimation.targetRes = 10;
Accelimation.timerID = null;

var effect=new Object();
effect.utility={
    GetHeight:function(obj)
    {
//        var div=document.createElement("DIV");
//        div.style.visibility="hidden";
//        div.innerHTML="<table cellpadding=\"0\" cellspacing=\"0\" height=\"100%\"><tr><td>"+obj.innerHTML+"</td></tr></table>";
//        document.appendChild(div);
//        alert(div.innerHTML);
        var height=obj.firstChild.clientHeight;
//        document.removeChild(div);
//        ggggg();
        return height;
    }
}
effect.slideout={
    target:"",
    onEndArgs:"",
    onEnd:new Function(),
    onEnterFrame:function(x)
    {
    
        this.sender.target.style.height=x+"px";
    },
    
    start:function()
    {
        
        
        var heigth=this.target.clientHeight;
        if(heigth==0)
        {
            heigth=this.target.parentNode.clientHeight;
        }
       
        var e=new Accelimation(heigth,0,600,2,this);
        e.onframe=this.onEnterFrame;
        e.onend=this.onEnd;
        e.start();
    }
}
effect.slidein={
    target:"",
    onEnterFrame:function(x)
    {
        x=Math.abs(x);
        //alert(x);
        this.sender.target.style.height=x+"px";
    },
    start:function()
    {
           
         var endHeight=parseInt(this.target.style.height);
        
         if(!endHeight)
         {
            if(this.target.clientHeight)
            {
                endHeight=this.target.clientHeight;
            }
            else
            {
                endHeight=effect.utility.GetHeight(this.target);
            }
         }
        //alert(endHeight);
        var e=new Accelimation(0,endHeight,600,2,this);
        e.onframe=this.onEnterFrame;
        e.start();
    }
}
effect.shake={
    target:"",
    e:"",
    counter:5,
    ox:"",
    oy:"",
    onEnterFrame:function(x)
    {
        x=Math.abs(x);
        this.sender.target.style.left=x+"px";
    },
    onEnd:function()
    {
         this.sender.counter--;
         //alert(this.sender.counter);
         if(this.sender.counter>0)
         {
            this.sender.e.start();
         }
         else
         {
           this.sender.target.style.left=this.sender.ox+"px";
           this.sender.counter=5; 
         }
    },
    start:function()
    {
        
        var startx=parseInt(this.target.style.left);
        this.ox=startx;
        this.e=new Accelimation(startx,startx+20,50,2,this);
        this.e.onframe=this.onEnterFrame;
        this.e.onend=this.onEnd;
        this.e.start();
       
    }
}