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

