/*   ¶¯»­¹¹ÔìÀà
 *   desc : like jquery¡®s Animate
 *   author : kong @ 249717835@qq.com
 **/
var kong_flash = function (selector) {
        var f = function (selector) {
        
                this['dom'] = $(selector);
                this.timers=[];
                this.timerId=null;
            } ;
        f.prototype = kong_flash.prototype ;
        return new f(selector) ;
    } ;
    
kong_flash.prototype.getCss = function (name) {
    try {
        return parseInt(window.getComputedStyle(this.dom, null).getPropertyValue(name)) ;
    }catch (e){
        return parseInt(this.dom.currentStyle[name]) ;
    }
}

kong_flash.prototype.anim = function(prop, d, easing, callback) {
    for (var name in prop) {
            var config = {
                //'dom': this.dom,
                'd': d,
                'easing': easing,
                'callback':callback,
                'name': name,
                'from': this.getCss( name),
                'to': prop[name]
            } ;
    }
    for (var i in config) {
        this[i] = config[i] ;
    }
    this.custom();
}
kong_flash.browser = {
    version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
    safari: /webkit/.test( userAgent ),
    opera: /opera/.test( userAgent ),
    msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
    mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
};

kong_flash.prototype.custom = function () {
    this.startTime = + new Date() ;
    var self = this ;
    function fn() {
        self.step();
    }
    fn();
    this.timers.push(fn);
    if (this.timerId) return  ;
    this.timerId = setInterval(function(){
            t_fun(self);
        }, 16); 
    
} ;
var t_fun = function(obj){
    var timers = obj.timers;
    timerId = obj.timerId;
    if (obj.timers.length > 0){
        for (var i = 0, k ; k = obj.timers[i] ; i++ ) {
            k();
        }
    }else {
        clearInterval(obj.timerId);
        obj.timerId = null ;
        
    }
}

kong_flash.prototype.step = function () {
    var n = (+ new Date())- this.startTime ;
    if (n > this.d){
        //alert('aaa');
        if(this.name == 'left' || this.name == 'top' ||
           this.name == 'width' || this.name == 'height') {
            this.dom.style[this.name] = this.to + 'px' ;
        } else if(this.name == 'opacity'){
            if(kong_flash.browser.msie ) {
                if( this.to == 1) this.to = 100;
                    this.dom.style['filter'] = 'alpha(opacity='+this.to+')';
            } else {
                this.dom.style['opacity']  = this.to ;
            }
            if(this.to == 0) {
                this.dom.style.display='none';
            } else {
                this.dom.style.display='block';
            }
        }
        // remove this anim
        /*
        for (var i = 0, k ; k = this.timers[i] ; i++ ) {
            if (k === this){
                //this.timers.splice(i--, 1);
            }
            //this.timers.splice(i--, 1);
        }*/
        this.timers.shift()
        if (this.callback && typeof this.callback == 'function'){
            this.callback();
        }
    }else {
        var ratio = n/this.d ;
        if(this.name == 'left' || this.name == 'top' || this.name == 'width' || this.name == 'height') {
            var temp = this.eas['swing'](ratio, n, 0, 1) ;
            var change = this.from + temp*(this.to-this.from) ;
            //change = parseInt(change);
            if(change >= 0)
                this.dom.style[this.name]  = change + 'px';
        } else {
            if(kong_flash.browser.msie) {
                if(this.to==0) {
                    this.from = 100;
                    to = 0;
                }
                else {
                    this.from = 0;
                    to = 100;
                }
                var temp = this.eas['swing'](ratio, n, 0, 1) ;
                var change = this.from + temp*(to-this.from) ;
                this.dom.style['filter'] = 'alpha(opacity='+change+')';
            } else {
                var temp = this.eas['linear'](ratio, n, 0, 1) ;
                var change = this.from + temp*(this.to-this.from) ;
                this.dom.style['opacity']  = change ;
            }
        }
    }
} ;

kong_flash.prototype.eas = {
    linear: function( p, n, firstNum, diff ) {
        return firstNum + diff * p;
    },
    swing: function( p, n, firstNum, diff ) {
        return ((-Math.cos(p*Math.PI)/2) + 0.5) * diff + firstNum;
    }
}
 