/* 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.3
 * Updated: 2010-03-29
 *
 * Requires jQuery 1.3.x or higher
 */
(function(k){function n(p){return+p.toString().replace(/[^0-9]+/,"")}k.fn.reel=function(p){var s={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,suffix:"-reel",tooltip:""};return function(c){var b=[];c.filter("img").each(function(){var m=k(this),f=m.attr("src"),h=n(m.css("width")),o=n(m.css("height"));!f||f==""||!h||!o||b.push(m)});return k(b)}(this).each(function(){var c=
k(this),b=k.extend(s,p),m=k(document),f=function(d,a){c.data(d,a);c.trigger("store");return a},h=function(d){d=c.data(d);c.trigger("recall");return d},o={init:function(){var d=c.attr("class"),a=c.attr("src").replace(/^(.*)\.(jpg|jpeg|png|gif)$/,"$1"+b.suffix+".$2"),e={x:n(c.css("width")),y:n(c.css("height"))};d=k("<div>").attr("class",d).addClass("jquery-reel").addClass(b.klass);var l=b.saves?{opacity:0}:{display:"none"};c=c.wrap(d).css(l).parent().css({width:e.x+"px",height:e.y+"px",backgroundImage:"url("+
a+")"});for(var j in o)c.bind(j,o[j]);k.each(["mousewheel","disableTextSelect"],function(q,g){k.fn[g]||(k.fn[g]=function(){return this})});c.trigger("setup")},setup:function(){var d={x:n(c.css("width")),y:n(c.css("height"))};f("frames",b.frames);f("spacing",b.spacing);f("offset",c.offset());f("dimensions",d);c.trigger("start")},start:function(){var d=b.hotspot?b.hotspot:c,a=h("dimensions");d.css({cursor:"ew-resize"}).mouseenter(function(){c.trigger("enter")}).mouseleave(function(){c.trigger("leave")}).mousemove(function(e){c.trigger("over",
[e.clientX,e.clientY])}).mousewheel(function(e,l){c.trigger("wheel",[l]);return false}).dblclick(function(){c.trigger("animate")}).mousedown(function(e){c.trigger("down",[e.clientX,e.clientY])}).disableTextSelect();if(b.hint||b.tooltip)d.attr("title",b.hint||b.tooltip);b.indicator&&c.append(k("<div>").addClass("indicator").css({width:b.indicator+"px",height:b.indicator+"px",top:a.y-b.indicator+"px",position:"relative",backgroundColor:"#000"}));c.trigger("frameChange",b.frame)},animate:function(){},
down:function(d,a){f("clicked",true);f("clicked_location",a);f("last_frame",f("clicked_on_frame",h("frame")));m.mousemove(function(e){c.trigger("drag",[e.clientX,e.clientY])}).mouseup(function(){c.trigger("up")})},up:function(){f("clicked",false);m.unbind("mousemove mouseup")},drag:function(d,a){var e=h("clicked_location");d=h("clicked_on_frame");h("frames");a=Math.round((e-a)/b.sensitivity);f("frame",d-(b.reversed?-1:1)*a);c.trigger("frameChange")},frameChange:function(d,a){a=!a?h("frame"):f("frame",
a);var e=h("last_frame");d=h("frames");var l=h("dimensions"),j=h("spacing");a=!b.loops&&a>d?d:a;a=!b.loops&&a<1?1:a;a-=Math.floor(a/d)*d;a=f("last_frame",f("frame",a<1?d:a));e=a-e;e=Math.abs(e)>10?0-e:e;e=f("reversed",e!=0?e>0:h("reversed"));if(b.stitched){g=a*(b.stitched/d);i=0;j=g+"px 0"+i+"px"}else{g=Math.floor(a/b.footage);i=a-g*b.footage-1;g=i==-1?g+i:g;i=i==-1?b.footage+i:i;var q=b.horizontal?l.y:l.x,g=-g*(j+q),i=-i*(j+(b.horizontal?l.x:l.y)),r=Math.ceil(d/b.footage);j=r*q+(r-1)*j;g=e&&b.horizontal?
g-j:g;i=e&&!b.horizontal?i-j:i;j=b.horizontal?i+"px "+g+"px":g+"px "+i+"px"}a=(l.x-b.indicator)/(d-1)*(a-1)+"px";c.css({backgroundPosition:j}).find(".indicator").css({left:a})},wheel:function(d,a){d=h("frame");h("frames");var e=Math.ceil(Math.sqrt(Math.abs(a)));e=a<0?-e:e;f("frame",d-(b.reversed?-1:1)*e);c.trigger("frameChange");return false}};c.ready(o.init)})}})(jQuery);

