/*
 *  nano FX Plugin v1.0
 *  http://www.nanojs.org/plugins/fx
 *
 *  Copyright (c) 2010 James Watts (SOLFENIX)
 *  http://www.solfenix.com
 *
 *  This is FREE software, licensed under the GPL
 *  http://www.gnu.org/licenses/gpl.html
 */

if(nano){nano.plugin({fx:function fx(type,args,callback){args=args||{};switch(type.toLowerCase()){case'create':return(nano.fx[type])?new nano.fx[type](this,args,callback):false;case'fade':new nano.fx.fade(this,args,callback).start();return this;case'fadein':args.end=1;new nano.fx.fade(this,args,callback).start();return this;case'fadeout':args.end=0;new nano.fx.fade(this,args,callback).start();return this;case'toggle':if(this.visible()){this.fx('fadeout',args,function(){this.hide();});}else{this.opacity(0).show().fx('fadein');}
return this;case'move':new nano.fx.move(this,args,callback).start();return this;case'resize':new nano.fx.resize(this,args,callback).start();return this;case'transform':this.fx('resize',args,callback);this.fx('move',args);return this;case'bounce':callback=callback||function(){};this.fx('move',{time:200,y:this.y()-30},function(){this.fx('move',{time:250,y:this.y()+30},function(){this.fx('move',{time:200,y:this.y()-20},function(){this.fx('move',{time:200,y:this.y()+20},function(){this.fx('move',{time:200,y:this.y()-10},function(){this.fx('move',{time:100,y:this.y()+10},callback);});});});});});return this;case'jump':callback=callback||function(){};this.fx('move',{time:100,y:this.y()+5},function(){this.fx('move',{time:250,y:this.y()-35},function(){this.fx('move',{time:200,y:this.y()+30},callback);});});return this;case'shake':callback=callback||function(){};this.fx('move',{time:100,x:this.x()-15},function(){this.fx('move',{time:200,x:this.x()+30},function(){this.fx('move',{time:150,x:this.x()-25},function(){this.fx('move',{time:150,x:this.x()+20},function(){this.fx('move',{time:100,x:this.x()-15},function(){this.fx('move',{time:100,x:this.x()+5},callback);});});});});});return this;case'vibrate':callback=callback||function(){};this.fx('move',{time:50,y:this.y()-3},function(){this.fx('move',{time:50,y:this.y()+3},function(){this.fx('move',{time:50,y:this.y()-3},function(){this.fx('move',{time:50,y:this.y()+3},function(){this.fx('move',{time:50,y:this.y()-3},function(){this.fx('move',{time:50,y:this.y()+3},callback);});});});});});return this;case'punch':callback=callback||function(){};this.fx('transform',{time:200,x:parseInt(nano.style(this,'left'))-5,y:parseInt(nano.style(this,'top'))-5,w:parseInt(nano.style(this,'width'))+10,h:parseInt(nano.style(this,'height'))+10},function(){this.fx('transform',{time:300,x:parseInt(nano.style(this,'left'))+5,y:parseInt(nano.style(this,'top'))+5,w:parseInt(nano.style(this,'width'))-10,h:parseInt(nano.style(this,'height'))-10},callback);});return this;case'pinch':callback=callback||function(){};this.fx('transform',{time:200,x:this.x()+6,y:this.y()+6,w:parseInt(nano.style(this,'width'))-10,h:parseInt(nano.style(this,'height'))-10},function(){this.fx('transform',{time:300,x:this.x()-5,y:this.y()-5,w:parseInt(nano.style(this,'width'))+10,h:parseInt(nano.style(this,'height'))+10},callback);});return this;case'pulse':callback=callback||function(){};var time=args.time||300;var rate=args.rate||30;var start=(nano.isset(args.start))?args.start:1;var end=(nano.isset(args.end))?(args.end<0.1)?1:0:(this.opacity()<0.1)?1:0;this.fx('fade',{time:time,rate:rate,start:start,end:end},function(){this.fx('fade',{time:time,rate:rate,start:end,end:start},callback);});return this;case'blink':callback=callback||function(){};var time=args.time||300;var obj=this;obj.toggle();setTimeout(function(){obj.toggle();callback();},time);return this;case'flash':callback=callback||function(){};var time1=((args.time/5)*4)||800;var time2=(args.time/5)||200;var obj=this;obj.opacity(0);setTimeout(function(){obj.fx('fade',{time:time2,start:0,end:1},function(){this.fx("fade",{time:time2,start:1,end:0},callback);});},time1);return this;case'drop':callback=callback||function(){};this.fx('move',{time:args.time||600,rate:args.rate||30,y:this.y()+this.h()});this.fx('fade',{time:args.time||600,rate:args.rate||30,start:this.opacity(),end:0.0},callback);return this;case'sink':callback=callback||function(){};this.fx('move',{time:args.time||900,rate:args.rate||30,y:this.y()+this.h()-1});this.fx('resize',{time:args.time||900,rate:args.rate||30,h:1});this.fx('fade',{time:args.time||900,rate:args.rate||30,start:this.opacity(),end:0.0},callback);return this;case'melt':callback=callback||function(){};var obj=this;obj.fx('move',{time:args.time||1800,rate:args.rate||30,x:this.x()-this.w()/2,y:this.y()+this.h()-1});obj.fx('resize',{time:args.time||1800,rate:args.rate||30,w:this.w()*2,h:1});setTimeout(function(){obj.fx('fade',{time:((args.time)?(args.time/2):900),rate:args.rate||30,start:obj.opacity(),end:0.0},callback);},((args.time)?(args.time/2):900));return this;case'shrink':callback=callback||function(){};this.fx('move',{time:args.time||900,rate:args.rate||30,x:this.x()+this.w()/4,y:this.y()+this.h()/2});this.fx('resize',{time:args.time||900,rate:args.rate||30,w:this.w()/2,h:this.h()/2});this.fx('fade',{time:args.time||900,rate:args.rate||30,start:this.opacity(),end:0.0},callback);return this;case'grow':callback=callback||function(){};this.fx('move',{time:args.time||900,rate:args.rate||30,x:this.x()-this.w()/2,y:this.y()-this.h()});this.fx('resize',{time:args.time||900,rate:args.rate||30,w:this.w()*2,h:this.h()*2});this.fx('fade',{time:args.time||900,rate:args.rate||30,start:this.opacity(),end:0.0},callback);return this;case'fold':callback=callback||function(){};this.fx('move',{time:args.time||600,rate:args.rate||30,y:this.y()+((this.h()/2)-1)});this.fx('resize',{time:args.time||600,rate:args.rate||30,h:2});this.fx('fade',{time:args.time||600,rate:args.rate||30,start:this.opacity(),end:0.0},callback);return this;case'pack':callback=callback||function(){};var time=args.time||600;var rate=args.rate||30;this.fx('resize',{time:time,rate:args.rate,w:this.w()/2},function(){this.fx('resize',{time:time,rate:rate,h:this.h()/2},function(){this.fx('fade',{time:time,rate:rate,start:this.opacity(),end:0.0},callback)});});return this;case'evaporate':callback=callback||function(){};this.fx('move',{time:args.time||1000,rate:args.rate||30,x:this.x()-this.w()/20,y:this.y()-this.h()/2});this.fx('resize',{time:args.time||1000,rate:args.rate||30,w:this.w()+this.w()/10,h:this.h()+this.h()/10});this.fx('fade',{time:args.time||1000,rate:args.rate||30,start:this.opacity(),end:0.0},callback);return this;case'implode':callback=callback||function(){};this.fx('move',{time:args.time||600,rate:args.rate||30,x:this.x()+this.w()/4,y:this.y()+this.h()/4});this.fx('resize',{time:args.time||600,rate:args.rate||30,w:this.w()/2,h:this.h()/2});this.fx('fade',{time:args.time||600,rate:args.rate||30,start:this.opacity(),end:0.0},callback);return this;case'explode':callback=callback||function(){};this.fx('move',{time:args.time||600,rate:args.rate||30,x:this.x()-this.w()/2,y:this.y()-this.h()/2});this.fx('resize',{time:args.time||600,rate:args.rate||30,w:this.w()*2,h:this.h()*2});this.fx('fade',{time:args.time||600,rate:args.rate||30,start:this.opacity(),end:0.0},callback);return this;case'morph':callback=callback||function(){};var obj=nano(args.id);this.fx('move',{time:args.time||900,rate:args.rate||30,x:obj.x(),y:obj.y()});this.fx('resize',{time:args.time||900,rate:args.rate||30,w:obj.w(),h:obj.h()},callback);return this;default:return this;}}},function(){this.fx={cache:{},counter:0,UID:function(){return'fx_'+this.counter++;},start:function(id){this.cache[id].interval=setInterval('nano.fx.cache["'+id+'"].run();',this.cache[id].rate);this.cache[id].paused=false;this.cache[id].stopped=false;},pause:function(id){clearInterval(this.cache[id].interval);this.cache[id].paused=true;this.cache[id].stopped=false;},stop:function(id){clearInterval(this.cache[id].interval);this.cache[id].paused=false;this.cache[id].stopped=true;},fade:function(node,args,callback){this.id=nano.fx.UID();nano.fx.cache[this.id]=this;this.node=node;this.time={elapsed:0,end:(nano.isset(args.time))?args.time:1000};this.rate=1000/((nano.isset(args.rate))?args.rate:30);if(typeof callback==='function'){this.callback=callback;}
eval("this.start = function() { nano.fx.start('"+this.id+"'); }; this.pause = function() { nano.fx.pause('"+this.id+"'); }; this.stop = function() { nano.fx.stop('"+this.id+"'); };");this.opacity={first:(nano.isset(args.start))?args.start:this.node.opacity(),last:(nano.isset(args.end))?args.end:0.5};if(nano.isset(args.start)){this.opacity.first=args.start;}
this.opacity.difference=this.opacity.last-this.opacity.first;this.run=function(){this.time.elapsed+=this.rate;if(this.time.elapsed>=this.time.end){this.time.elapsed=this.time.end;}
var scale=this.time.elapsed/this.time.end;var new_opacity=this.opacity.first+(scale*this.opacity.difference);this.node.opacity(new_opacity);if(this.time.elapsed==this.time.end){this.stop();if(this.callback){this.callback.call(this.node);}}};},move:function(node,args,callback){this.id=nano.fx.UID();nano.fx.cache[this.id]=this;this.node=node;this.time={elapsed:0,end:(nano.isset(args.time))?args.time:1000};this.rate=1000/((nano.isset(args.rate))?args.rate:30);if(typeof callback==='function'){this.callback=callback;}
eval("this.start = function() { nano.fx.start('"+this.id+"'); }; this.pause = function() { nano.fx.pause('"+this.id+"'); }; this.stop = function() { nano.fx.stop('"+this.id+"'); };");this.x={first:this.node.x(),last:(nano.isset(args.x))?args.x:this.node.x()};this.y={first:this.node.y(),last:(nano.isset(args.y))?args.y:this.node.y()};this.node.css.display='block';this.node.css.position='absolute';this.run=function(){this.time.elapsed+=this.rate;if(this.time.elapsed>=this.time.end){this.time.elapsed=this.time.end;}
this.x.distance=Math.floor(this.x.last)-this.x.first;this.y.distance=Math.floor(this.y.last)-this.y.first;var scale=this.time.elapsed/this.time.end;var move_x=this.x.first+(scale*this.x.distance);var move_y=this.y.first+(scale*this.y.distance);this.node.moveTo(move_x,move_y);if(this.time.elapsed==this.time.end){this.stop();if(this.callback){this.callback.call(this.node);}}};},resize:function(node,args,callback){this.id=nano.fx.UID();nano.fx.cache[this.id]=this;this.node=node;this.time={elapsed:0,end:(nano.isset(args.time))?args.time:1000};this.rate=1000/((nano.isset(args.rate))?args.rate:30);if(typeof callback==='function'){this.callback=callback;}
eval("this.start = function() { nano.fx.start('"+this.id+"'); }; this.pause = function() { nano.fx.pause('"+this.id+"'); }; this.stop = function() { nano.fx.stop('"+this.id+"'); };");this.w={first:this.node.w(),last:(nano.isset(args.w))?args.w:this.node.w()};this.h={first:this.node.h(),last:(nano.isset(args.h))?args.h:this.node.h()};this.run=function(){this.time.elapsed+=this.rate;if(this.time.elapsed>=this.time.end){this.time.elapsed=this.time.end;}
this.w.distance=Math.floor(this.w.last)-this.w.first;this.h.distance=Math.floor(this.h.last)-this.h.first;var scale=this.time.elapsed/this.time.end;var resize_w=this.w.first+(scale*this.w.distance);var resize_h=this.h.first+(scale*this.h.distance);this.node.resizeTo(resize_w,resize_h);if(this.time.elapsed==this.time.end){this.stop();if(this.callback){this.callback.call(this.node);}}};}};});}

