﻿// JScript File

function animacao() {

    var thisObj = this;    
    var arrobjs = new Array();
    
    // properties
    this.clock = 25;
    this.time = 1000;
    this.period = 0.5;
    this.scale = 0.5;
    
    // busy
    this.busy = function(obj) {
        var i;        
        // check array
        for (i = 0; i < arrobjs.length; i++) {
            if (arrobjs[i] == obj) return true;
        }
        return false;
    }
    
    // push
    this.push = function(obj){
        var i;
        for (i = 0; i < arrobjs.length; i++) {
            if (arrobjs[i] == obj) {
                return false;
            }
        }
        arrobjs.push(obj);
    }
    
    // pop
    this.pop = function(obj) {
        // remove from array
        var i;
        for (i = 0; i < arrobjs.length; i++) {
            if (arrobjs[i] == obj) {
                arrobjs.splice(i,1);
                break;
            }
        }
    }
    
    
    // grow
    this.grow = function(obj) {
        if (thisObj.busy(obj)) return false;
        this.stretch(obj, 0, this.period);
    }    
    
    // stretch
    this.stretch = function(obj, per1, per2) {
        
        thisObj.push(obj);
    
        var d1 = new Date();
        var d2;
        var x = 0;
        var y = 0;
        
        var height = obj.offsetHeight;
        var width = obj.offsetWidth;
        var left = obj.offsetLeft;
        var top = obj.offsetTop;
        var h = 0;
        var w = 0;
        
        var position = obj.style.position
        obj.style.left = left + "px";
        obj.style.top = top + "px";
        obj.style.position = "absolute";
                
        var t = setInterval(
            function() {
                d2 = new Date();
                x = (d2.getTime() - d1.getTime()) / thisObj.time;
                if (x > 1) x = 1;
                //y = Math.sin(x * Math.PI * 2 * thisObj.period);
                y = Math.sin ((Math.PI * 2 * per1) + (x * Math.PI * 2 * (per2 - per1)));
                
                h = (height * y * thisObj.scale) + height;
                w = (width * y * thisObj.scale) + width;
                
                Math.ceil(w) ;
                
                obj.style.height = Math.ceil(h) + "px";
                obj.style.top = Math.ceil(top - (h - height)) + "px";
                //obj.style.top = Math.ceil(top - ((h - height) / 2)) + "px";
                obj.style.width = Math.ceil(w) + "px";
                obj.style.left = Math.ceil(left - ((w - width) / 2)) + "px";
                
                if (x == 1) {
                    clearInterval (t);
                    //obj.style.left = Math.ceil(left) + "px";
                    //obj.style.top = Math.ceil(top) + "px";
                    //obj.style.height = Math.ceil(height) + "px";
                    //obj.style.width = Math.ceil(width) + "px";
                    //obj.style.position = position;
                    
                    thisObj.pop(obj);
                }
                
            }, thisObj.clock
        );
        
    }
    
    
    // scroll
    this.scroll = function (obj, inc) {
    
        if (thisObj.busy(obj)) return false;
        thisObj.push(obj);
            
        obj.style.position = "absolute";
        var left = obj.offsetLeft;
        var top = obj.offsetTop;
        obj.style.left = left;
        obj.style.top = top;
        var d1 = new Date();
        var d2;
        var x = 0;
        var pos = 0;
        
        // timer
        var t = setInterval(
            function() {
                d2 = new Date();
                x = (d2.getTime() - d1.getTime()) / thisObj.time;
                if (x > 1) x = 1;
                
                // pos = left + (inc * x);
                pos = left + (inc * Math.sin(x * 2 * Math.PI * 0.25));
                
                if (pos < (-obj.offsetWidth + Math.abs(inc))) pos = -obj.offsetWidth + Math.abs(inc);
                if (pos > 0) pos = 0;
                                
                obj.style.left = pos + "px";
                
                if (x == 1) {
                    clearInterval (t);
                    thisObj.pop(obj);
                }
                
            }, thisObj.clock
        );
        //
        
    }
    
    
    
    // opacity
	function changeOpac (obj, opacity)
	{
		obj.style.opacity = (opacity / 100);
		obj.style.MozOpacity = (opacity / 100);
		obj.style.KhtmlOpacity = (opacity / 100);
		obj.style.filter = "alpha(opacity=" + opacity + ")";
	}
	
}