/**
 * CentredScroller: A jQuery plugin for creating a scrollable slideshow that loops
 * The currently active slide is always centred
 *
 * Dependencies: jquery-1.4.2+
 *
 * @project ca.thomasjbradley.applications.centredscroller
 * @author Thomas J Bradley <hey@thomasjbradley.ca>
 * @link http://thomasjbradley.ca/labs/centred-scroller
 * @link http://github.com/thomasjbradley/centred-scroller
 * @copyright Copyright MMXI–, Thomas J Bradley
 * @license New BSD License
 * @version 1.0.0
 */

(function(g){function A(J,p){var w,k,r;function h(){return x.children(f.slideGroup)}function m(){return h().children().eq(0).width()}function s(){var a=m(),b=x.width(),a=Math.ceil(b/a);a+=a%2===0?1:0;a+=2;return a}function E(){var a=m(),b=0,n=0,d=0,e=0,o=0,c=null,i=0,e=0;g("#centredScroller-left-measure").width(a);b=g("#centredScroller-left-measure").offset().left-x.offset().left;n=s();d=Math.floor(n/2);e=t(j);o=q(j);c=h().clone();c.html("");c.append(l.eq(e).clone());c.append(l.eq(j).clone());c.append(l.eq(o).clone());
for(i=0;i<d-1;i++)e=t(e),c.prepend(l.eq(e).clone());for(e=0;e<d-1;e++)o=q(o),c.append(l.eq(o).clone());g("["+f.dataAttr+"="+j+"]",c).addClass(f.currentClass);c.css({width:a*n,left:b-a*d});h().replaceWith(c)}function u(a,b){B()&&h().css({"-moz-transition":"left "+f.duration+"ms ease-in-out","-o-transition":"left "+f.duration+"ms ease-in-out","-webkit-transition":"left "+f.duration+"ms ease-in-out",transition:"left "+f.duration+"ms ease-in-out"});h().children().removeClass(f.currentClass);h().position();
B()?(h().css({left:a}),w=setTimeout(function(){y(b)},f.duration)):h().animate({left:a},f.duration,"swing",function(){y(b)})}function y(a){clearTimeout(w);w=null;C();E();jQuery.isFunction(i[a])&&i[a].apply(D)}function C(){h().css({"-moz-transition":"left 0s linear","-o-transition":"left 0s linear","-webkit-transition":"left 0s linear",transition:"left 0s linear"})}function B(){var a=document.createElement("slideshow").style;return typeof a.transitionProperty!=="undefined"||typeof a.WebkitTransitionProperty!==
"undefined"||typeof a.MozTransitionProperty!=="undefined"||typeof a.OTransitionProperty!=="undefined"||typeof a.msTransitionProperty!=="undefined"||typeof a.KhtmlTransitionProperty!=="undefined"?!0:!1}function A(a,b){var n=h(),d=0,e=0,o=!1,c=0,k=c=c=0,d=d=0;if(!(a>l.children().length-1)&&(d=g("."+f.currentClass,n),parseInt(d.attr(f.dataAttr),10)!==a))if(e=g("["+f.dataAttr+"="+a+"]",n),e.length>0){if(e.each(function(){g(this).next().hasClass(f.currentClass)?(F(b),o=!0):g(this).prev().hasClass(f.currentClass)&&
(G(b),o=!0)}),!o)e.eq(0).index()>d.index()?(c=s(),c=Math.floor(c/2),c-=n.children().length-1-e.eq(0).index(),k=m(),d=h().position().left-(e.eq(0).index()-d.index())*k,H(c)):(c=s(),c=Math.floor(c/2),c-=e.eq(0).index(),d=h().position().left,I(c)),i.display=b,j=a,u(d,"display")}else a>j?(d=parseInt(n.children().last().attr(f.dataAttr),10),c=s(),c=Math.floor(c/2),c+=a-d,k=m(),d=h().position().left-c*k,H(c)):(d=parseInt(n.children().first().attr(f.dataAttr),10),c=s(),c=Math.floor(c/2),c+=d-a,d=h().position().left,
I(c)),i.display=b,j=a,u(d,"display")}function G(a){var b=m(),b=h().position().left-b;i.next=a;j=q(j);u(b,"next")}function F(a){var b=m(),b=h().position().left+b;i.prev=a;j=t(j);u(b,"prev")}function K(){var a=m(),a=h().position().left-a;jQuery.isFunction(i.onautoadvancebefore)&&i.onautoadvancebefore.apply(D);j=q(j);u(a,"onautoadvanceafter")}function v(a){var b=f.autoAdvance;clearInterval(k);typeof a!=="undefined"&&(b=a);b>99&&(k=setInterval(K,b))}function z(){clearInterval(k);k=null}function q(a){return a+
1>=l.length?0:a+1}function t(a){return a-1<0?l.length-1:a-1}function H(a){var b=h(),g=m(),d=parseInt(b.children().last().attr(f.dataAttr),10),d=q(d),e=0;if(!(a<=0)){for(e=0;e<a;e++)b.append(l.eq(d).clone()),d=q(d);b.css({width:b.width()+a*g})}}function I(a){var b=h().clone(),g=h().position().left,d=m(),e=parseInt(b.children().first().attr(f.dataAttr),10),e=t(e),i=0;if(!(a<=0)){for(i=0;i<a;i++)b.prepend(l.eq(e).clone()),e=t(e);b.css({width:d*b.children().length,left:g-d*a});h().replaceWith(b)}}var D=
this,f=g.extend({},g.fn.centredScroller.defaults,p),x=g(J),j=0,l=h().children().clone();w=null;r=null;k=null;var i={next:null,prev:null,display:null,onautoadvanceafter:null,onautoadvancebefore:null};g(window).bind("resize",function(){C();clearTimeout(r);r=setTimeout(y,10)});typeof window.onorientationchange!=="undefined"&&window.addEventListener("orientationchange",function(){C();clearTimeout(r);r=setTimeout(y,10)},!1);(function(){l.each(function(a){g(this).attr(f.dataAttr,a);g(this).removeClass(f.currentClass)})})();
x.prepend('<div id="centredScroller-left-measure-parent" style="position:absolute; top:0px; width:100%;"><div id="centredScroller-left-measure" style="width:'+m()+'px; margin:0 auto;"></div></div>');E();v(f.autoAdvance);g.extend(D,{getIndex:function(){return j},display:function(a,b){z();A(a,b);k=setTimeout(v,f.interactionDelay)},next:function(a){z();G(a);k=setTimeout(v,f.interactionDelay)},prev:function(a){z();F(a);k=setTimeout(v,f.interactionDelay)},startAutoAdvance:function(a){v(a)},stopAutoAdvance:function(){z()},
onAutoAdvanceBefore:function(a){i.onautoadvancebefore=null;if(jQuery.isFunction(a))i.onautoadvancebefore=a},onAutoAdvanceAfter:function(a){i.onautoadvanceafter=null;if(jQuery.isFunction(a))i.onautoadvanceafter=a},cssTransitionsAvailable:function(){return B()}})}g.fn.centredScroller=function(g){var p=[];this.each(function(){p.push(new A(this,g))});return p.length==1?p[0]:p};g.fn.centredScroller.defaults={autoAdvance:0,duration:400,interactionDelay:5E3,currentClass:"current",slideGroup:".slides",dataAttr:"data-centredscroller-index"}})(jQuery);

