/* Copyright (c) 2009-2010 Petr Vostrel (http://www.pisi.cz/)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://www.vostrel.cz/jquery/reel/
 * Version: 1.0.4 "Touchy"
 * Updated: 2010-04-24
 *
 * Requires jQuery 1.4.x or higher
 */
(function(b){b.fn.reel=function(g){var i={footage:6,frame:1,frames:36,horizontal:true,hotspot:undefined,hint:"",indicator:0,klass:"",loops:true,reversed:false,saves:false,sensitivity:20,spacing:0,stitched:undefined,next:undefined,previous:undefined,speed:100,suffix:"-reel",tooltip:""},e="jquery-reel",d=(function(j){var k=[];j.filter("img").each(function(m){var o=b(this),p=o.attr("src"),n=a(o.css("width")),l=a(o.css("height"));if(!p||p==""||!n||!l){return}k.push(o)});j.filter("div."+e).each(function(l){k.push(b(this))});return b(k)})(this),h=[],f=(/iphone|ipod|ipad|android/i).test(navigator.userAgent);c("mousewheel disableTextSelect".split(/ /));d.each(function(){var n=b(this),o=b.extend(i,g),m=b(document),k=function(p,q){n.data(p,q);n.trigger("store");return q},l=function(p){var q=n.data(p);n.trigger("recall");return q},j={setup:function(){if(n.hasClass(e)){return}var v=n.attr("src"),w=n.attr("id"),r=n.attr("class"),s=n.attr("style"),u=v.replace(/^(.*)\.(jpg|jpeg|png|gif)$/,"$1"+o.suffix+".$2"),q={x:a(n.css("width")),y:a(n.css("height"))},t=b("<div>").attr("class",r).addClass(e).addClass(o.klass),p=f||!o.saves?{display:"none"}:{opacity:0};h.push((n=n.attr("id","").wrap(t).css(p).parent().attr("id",w).bind(j).css({display:"block",width:q.x+"px",height:q.y+"px",backgroundImage:"url("+u+")"}))[0]);k("frames",o.frames);k("spacing",o.spacing);k("offset",n.offset());k("dimensions",q);k("backup",{id:w,"class":r||"",style:s||""});n.trigger("start")},teardown:function(q){n=n.unbind(j).find(".indicator").remove().end().find("img").attr(n.data("backup")).unwrap().bind("setup",function p(r){n.unbind("setup");j.setup()})},start:function(s){n.css({position:"relative"});var q;if(o.next!=undefined){o.next.bind("mouseover",function(){q=setInterval(function(){n.trigger("frameChange",l("frame")+1)},o.speed)});o.next.bind("mouseout",function(){clearInterval(q)})}if(o.previous!=undefined){o.previous.bind("mouseover",function(){q=setInterval(function(){if(l("frame")==1){n.trigger("frameChange",o.frames-1)}else{n.trigger("frameChange",l("frame")-1)}},o.speed)});o.previous.bind("mouseout",function(){clearInterval(q)})}var p=o.hotspot?o.hotspot:n,r=l("dimensions");p.css({cursor:"ew-resize"}).mouseenter(function(u){n.trigger("enter")}).mouseleave(function(u){n.trigger("leave")}).mousemove(function(u){n.trigger("over",[u.clientX,u.clientY])}).mousewheel(function(u,v){n.trigger("wheel",[v]);return false}).dblclick(function(u){n.trigger("animate")}).mousedown(function(u){n.trigger("down",[u.clientX,u.clientY])}).disableTextSelect().each(function t(){f&&y(this,{touchstart:x,touchmove:v,touchend:u,touchcancel:u,click:w,gesturestart:w,gesturechange:w,gestureend:w});function y(A,z){b.each(z,function B(C){A.addEventListener(C,this,false)})}function w(z){z.cancelable&&z.preventDefault();return false}function x(B){var D=B.touches[0],A=k("clicked",true),z=k("clicked_location",D.clientX),C=k("last_frame",k("clicked_on_frame",l("frame")));return w(B)}function v(z){var A=z.touches[0];n.trigger("drag",[A.clientX,A.clientY]);return w(z)}function u(A){var z=k("clicked",false);return w(A)}});(o.hint||o.tooltip)&&p.attr("title",o.hint||o.tooltip);o.indicator&&n.append(b("<div/>").addClass("indicator").css({width:o.indicator+"px",height:o.indicator+"px",top:(r.y-o.indicator)+"px",position:"absolute",backgroundColor:"#000"}));n.trigger("frameChange",o.frame)},animate:function(p){},down:function(s,p,u){var r=k("clicked",true),q=k("clicked_location",p),t=k("last_frame",k("clicked_on_frame",l("frame")));m.mousemove(function(v){n.trigger("drag",[v.clientX,v.clientY])}).mouseup(function(v){n.trigger("up")})},up:function(q){var p=k("clicked",false);m.unbind("mousemove mouseup")},drag:function(t,w,v){var z=l("clicked_location"),q=l("clicked_on_frame"),u=l("frames"),r=f?o.sensitivity*0.6:o.sensitivity,p=Math.round((z-w)/r),s=(o.reversed?-1:1)*(o.stitched?-1:1),q=k("frame",q-s*p);n.trigger("frameChange")},wheel:function(q,u){var s=l("frame"),r=l("frames"),t=Math.ceil(Math.sqrt(Math.abs(u))),t=u<0?-t:t,p=o.reversed?-1:1,s=k("frame",s-p*t);n.trigger("frameChange");return false},frameChange:function(I,D){var D=!D?l("frame"):k("frame",D),K=l("last_frame"),C=l("frames"),J=l("dimensions"),t=l("spacing"),D=!o.loops&&D>C?C:D,D=!o.loops&&D<1?1:D,D=D-Math.floor(D/C)*C,D=k("last_frame",k("frame",D<1?C:D)),L=D-K,L=Math.abs(L)>10?0-L:L,r=k("reversed",L!=0?(L>0):l("reversed"));if(!o.stitched){var E=Math.floor(D/o.footage),q=D-E*o.footage-1,E=q==-1?E+q:E,q=q==-1?o.footage+q:q,F=o.horizontal?J.y:J.x,p=o.horizontal?J.x:J.y,B=-E*(t+F),z=-q*(t+p),A=Math.ceil(C/o.footage),s=A*F+(A-1)*t,B=r&&o.horizontal?B-s:B,z=r&&!o.horizontal?z-s:z,H=o.horizontal?z+"px "+B+"px":B+"px "+z+"px"}else{var w=o.loops?o.stitched:o.stitched-J.x,G=o.loops?C:C-1,u=w/G,B=Math.round((D-1)*u),z=0,H=-B+"px "+z+"px"}var v=((J.x-o.indicator)/(C-1)*(D-1))+"px";n.css({backgroundPosition:H}).find(".indicator").css({left:v})}};n.ready(j.setup)});return b(h)};function a(d){return parseInt(d)}function c(d){b.each(d,function(){if(!b.fn[this]){b.fn[this]=function(){return this}}})}})(jQuery);
