Inital Import.
[jquery.git] / fx / fx.js
1 function fx(el,op,ty,tz){
2         var z = this;
3         z.a = function(){z.el.style[ty]=z.now+z.o.unit};
4         z.max = function(){return z.el["io"+ty]||z.el["natural"+tz]||z.el["scroll"+tz]||z.cur()};
5         z.cur = function(){return parseInt($.getCSS(z.el,ty))};
6         z.show = function(){z.ss("block");z.custom(0,z.max())};
7         z.hide = function(){z.el.$o=$.getCSS(z.el,"overflow");z.el["io"+ty]=this.cur();z.custom(z.cur(),0)};
8         z.ss = function(a){if(y.display!=a)y.display=a};
9         z.toggle = function(){if(z.cur()>0)z.hide();else z.show()};
10         z.modify = function(a){z.custom(z.cur(),z.cur()+a)};
11         z.clear = function(){clearInterval(z.timer);z.timer=null};
12         z.el = el.constructor==String?document.getElementById(el):el;
13         var y = z.el.style;
14         z.oo = y.overflow;
15         y.overflow = "hidden";
16         z.o = {
17           unit: "px",
18           duration: (op && op.duration) || 400,
19           onComplete: (op && op.onComplete) || op
20         };
21         z.step = function(f,tt){
22                 var t = (new Date).getTime();
23                 var p = (t - z.s) / z.o.duration;
24                 if (t >= z.o.duration+z.s) {
25                         z.now = tt;
26                         z.clear();
27                         setTimeout(function(){
28                                 y.overflow = z.oo;
29                                 if(y.height=="0px"||y.width=="0px")z.ss("none");
30                                 $.setAuto( z.el, "height" );
31                                 $.setAuto( z.el, "width" );
32                                 if(z.o.onComplete.constructor == Function){z.el.$_ = z.o.onComplete;z.el.$_();}
33                         },13);
34                 } else
35                         z.now = ((-Math.cos(p*Math.PI)/2) + 0.5) * (tt-f) + f;
36                 z.a();
37         };
38         z.custom = function(f,t){
39                 if(z.timer)return;this.now=f;z.a();z.io=z.cur();z.s=(new Date).getTime();
40                 z.timer=setInterval(function(){z.step(f,t);}, 13);
41         };
42 }
43 fx.fn = ["show","hide","toggle"];
44 fx.ty = ["Height","Width","Left","Top"];
45 for(var i in fx.ty){(function(){
46         var c = fx.ty[i];
47         fx[c] = function(a,b){
48                 return new fx(a,b,c.toLowerCase(),c);};
49 })()}
50 fx.Opacity = function(a,b){
51         var o = new fx(a,b,"opacity");
52         o.cur = function(){return parseFloat(o.el.style.opacity);};
53         o.a = function() {
54                 var e = o.el.style;
55                 if (o.now == 1) o.now = 0.9999;
56                 if (window.ActiveXObject)
57                         e.filter = "alpha(opacity=" + o.now*100 + ")";
58                 e.opacity = o.now;
59         };
60         o.io = o.now = 1;
61         o.a();
62         return o;
63 };
64 fx.Resize = function(e,o){
65         var z = this;
66         var h = new fx.Height(e,o);
67         if(o) o.onComplete = null;
68         var w = new fx.Width(e,o);
69         function c(a,b,c){return (!a||a==c||b==c);}
70         for(var i in fx.fn){(function(){
71                 var j = fx.fn[i];
72                 z[j] = function(a,b){
73                         if(c(a,b,"height")) h[j]();
74                         if(c(a,b,"width")) w[j]();
75                 };
76         })()}
77         z.modify = function(c,d){
78                 h.modify(c);
79                 w.modify(d);
80         };
81 };
82 fx.FadeSize = function(e,o){
83         var z = this;
84         var p = new fx.Opacity(e,o);
85         if(o) o.onComplete = null;
86         var r = new fx.Resize(e,o);
87         for(var i in fx.fn){(function(){
88                 var j = fx.fn[i];
89                 z[j] = function(a,b){p[j]();r[j](a,b);};
90         })()}
91 };
92
93 $.speed = function(s,o) {
94   if ( o && o.constructor == Function ) o = { onComplete: o };
95   o = o || {};
96   var ss = {"crawl":1200,"xslow":850,"slow":600,"medium":400,"fast":200,"xfast":75,"normal":400};
97   o.duration = typeof s == "number" ? s : ss[s] || 400;
98   return o;
99 };
100
101 $.fn.hide = function(a,o) {
102   o = $.speed(a,o);
103   return a ? this.each(function(){
104     new fx.FadeSize(this,o).hide();
105   }) : this._hide();
106 };
107
108 $.fn.show = function(a,o) {
109   o = $.speed(a,o);
110   return a ? this.each(function(){
111     new fx.FadeSize(this,o).show();
112   }) : this._show();
113 };
114
115 $.fn.slideDown = function(a,o) {
116   o = $.speed(a,o);
117   return this.each(function(){
118     new fx.Resize(this,o).show("height");
119   });
120 };
121
122 $.fn.slideUp = function(a,o) {
123   o = $.speed(a,o);
124   return this.each(function(){
125     new fx.Resize(this,o).hide("height");
126   });
127 };
128
129 $.fn.fadeOut = function(a,o) {
130   o = $.speed(a,o);
131   return a ? this.each(function(){
132     new fx.Opacity(this,o).hide();
133   }) : this._hide();
134 };
135
136 $.fn.fadeIn = function(a,o) {
137   o = $.speed(a,o);
138   return a ? this.each(function(){
139     new fx.Opacity(this,o).show();
140   }) : this._show();
141 };
142
143 $.fn.center = function(f) {
144   return this.each(function(){
145                 if ( !f && this.nodeName == 'IMG' &&
146                                  !this.offsetWidth && !this.offsetHeight ) {
147                         var self = this;
148                         setTimeout(function(){
149                                 $(self).center(true);
150                         }, 13);
151                 } else {
152                         var s = this.style;
153                         var p = this.parentNode;
154                         if ( $.css(p,"position") == 'static' )
155                                 p.style.position = 'relative';
156                         s.position = 'absolute';
157                         s.left = parseInt(($.css(p,"width") - $.css(this,"width"))/2) + "px";
158                         s.top = parseInt(($.css(p,"height") - $.css(this,"height"))/2) + "px";
159                 }
160   });
161 };
162
163 $.setAuto = function(e,p) {
164   var a = e.style[p];
165   var o = $.css(e,p);
166   e.style[p] = 'auto';
167   var n = $.css(e,p);
168   if ( o != n )
169     e.style[p] = a;
170 };