jQuery.animation=function(node,options){this.node=$(node);this.config=$.extend(this.config,options||{});this.frames=node.find(this.config.frameSelector),this.framesCount=this.frames.length,this.currentFrame=0;this.preload();this.initNav();this.animate();this.node.animation=this;$().log(this)};jQuery.animation.prototype={node:false,frames:[],framesCount:0,currentFrame:0,timeoutId:false,nav:false,navItems:[],config:{movePx:150,moveDuration:500,delay:5000,frameSelector:'.frame',titleSelector:'h3',navSelector:'nav'},preload:function(){this.frames.find('img').each(function(){(new Image()).src=$(this).attr('src')})},initNav:function(){var html='',self=this;this.frames.each(function(i){html+='<li><a href="#" data-frame="'+i+'" title="'+$(this).find(self.config.titleSelector).text()+'">'+(i+1)+'</a></li>'});this.nav=this.node.find(this.config.navSelector);this.nav.html(html);this.navItems=this.nav.find('a');this.updateNav();this.nav.click(function(ev){var target=$(ev.target);ev.preventDefault();if(!target.is('a'))return;var selectedFrame=target.attr('data-frame');self.selectFrame(selectedFrame)})},updateNav:function(){this.navItems.removeClass('active').eq(this.currentFrame).addClass('active')},getCurrentFrame:function(){return this.frames.eq(this.currentFrame)},setCurrentFrame:function(id){return this.currentFrame=parseInt(id)%this.framesCount},selectFrame:function(id){this.stop();this.setCurrentFrame(id);this.updateNav();this.frames.hide();this.getCurrentFrame().show();this.animate()},nextFrame:function(){this.setCurrentFrame(this.currentFrame+1)},hideFrame:function(callback){var frame=this.getCurrentFrame();frame.animate({left:this.config.movePx+'px',opacity:0},this.config.moveDuration,function(){frame.hide();if(typeof callback=='function')callback()})},showFrame:function(callback){var frame=this.getCurrentFrame();frame.show().css({left:(-this.config.movePx)+'px',opacity:0}).animate({left:0,opacity:1},this.config.moveDuration,function(){frame.css('filter','');if(typeof callback=='function')callback()})},stop:function(){clearTimeout(this.timeoutId);this.getCurrentFrame().stop();this.frames.css({left:0,opacity:1})},animate:function(){var self=this;this.timeoutId=setTimeout(function(){self.hideFrame(function(){self.nextFrame();self.updateNav();self.showFrame(function(){self.animate()})})},this.config.delay)}};jQuery.fn.animation=function(options){new jQuery.animation($(this),options)};jQuery(function(){jQuery('#promo').animation({frameSelector:'.product',navSelector:'.promo-nav'})})
