/*
 * jCarousel - Riding carousels with jQuery
 *   http://sorgalla.com/jcarousel/
 *
 * Copyright (c) 2006 Jan Sorgalla (http://sorgalla.com)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Built on top of the jQuery library
 *   http://jquery.com
 *
 * Inspired by the "Carousel Component" by Bill Scott
 *   http://billwscott.com/carousel/
 *   
 *   Modified for vBulletin:
 *   - added destroy method
 *   - changed reset method by adding a flag to call setup method
 *   - added check for undefined instance in the main jcarousel function
 */
(function(C){var D={vertical:false,rtl:false,start:1,offset:1,size:null,scroll:3,visible:null,animation:"normal",easing:"swing",auto:0,wrap:null,initCallback:null,setupCallback:null,reloadCallback:null,itemLoadCallback:null,itemFirstInCallback:null,itemFirstOutCallback:null,itemLastInCallback:null,itemLastOutCallback:null,itemVisibleInCallback:null,itemVisibleOutCallback:null,animationStepCallback:null,buttonNextHTML:"<div></div>",buttonPrevHTML:"<div></div>",buttonNextEvent:"click",buttonPrevEvent:"click",buttonNextCallback:null,buttonPrevCallback:null,itemFallbackDimension:null},B=false;C(window).bind("load.jcarousel",function(){B=true});C.jcarousel=function(I,F){this.options=C.extend({},D,F||{});this.locked=false;this.autoStopped=false;this.container=null;this.clip=null;this.list=null;this.buttonNext=null;this.buttonPrev=null;this.buttonNextState=null;this.buttonPrevState=null;if(!F||F.rtl===undefined){this.options.rtl=(C(I).attr("dir")||C("html").attr("dir")||"").toLowerCase()=="rtl"}this.wh=!this.options.vertical?"width":"height";this.lt=!this.options.vertical?(this.options.rtl?"right":"left"):"top";var M="",K=I.className.split(" ");for(var H=0;H<K.length;H++){if(K[H].indexOf("jcarousel-skin")!=-1){C(I).removeClass(K[H]);M=K[H];break}}if(I.nodeName.toUpperCase()=="UL"||I.nodeName.toUpperCase()=="OL"){this.list=C(I);this.clip=this.list.parents(".jcarousel-clip");this.container=this.list.parents(".jcarousel-container")}else{this.container=C(I);this.list=this.container.find("ul,ol").eq(0);this.clip=this.container.find(".jcarousel-clip")}if(this.clip.size()===0){this.clip=this.list.wrap("<div></div>").parent()}if(this.container.size()===0){this.container=this.clip.wrap("<div></div>").parent()}if(M!==""&&this.container.parent()[0].className.indexOf("jcarousel-skin")==-1){this.container.wrap('<div class=" '+M+'"></div>')}this.buttonPrev=C(".jcarousel-prev",this.container);if(this.buttonPrev.size()===0&&this.options.buttonPrevHTML!==null){this.buttonPrev=C(this.options.buttonPrevHTML).appendTo(this.container)}this.buttonPrev.addClass(this.className("jcarousel-prev"));this.buttonNext=C(".jcarousel-next",this.container);if(this.buttonNext.size()===0&&this.options.buttonNextHTML!==null){this.buttonNext=C(this.options.buttonNextHTML).appendTo(this.container)}this.buttonNext.addClass(this.className("jcarousel-next"));this.clip.addClass(this.className("jcarousel-clip")).css({position:"relative"});this.list.addClass(this.className("jcarousel-list")).css({overflow:"hidden",position:"relative",top:0,margin:0,padding:0}).css((this.options.rtl?"right":"left"),0);this.container.addClass(this.className("jcarousel-container")).css({position:"relative"});if(!this.options.vertical&&this.options.rtl){this.container.addClass("jcarousel-direction-rtl").attr("dir","rtl")}var J=this.options.visible!==null?Math.ceil(this.clipping()/this.options.visible):null;var L=this.list.children("li");var N=this;if(L.size()>0){var E=0,G=this.options.offset;L.each(function(){N.format(this,G++);E+=N.dimension(this,J)});this.list.css(this.wh,(E+100)+"px");if(!F||F.size===undefined){this.options.size=L.size()}}this.container.css("display","block");this.buttonNext.css("display","block");this.buttonPrev.css("display","block");this.funcNext=function(){N.next()};this.funcPrev=function(){N.prev()};this.funcResize=function(){if(N.resizeTimer){clearTimeout(N.resizeTimer)}N.resizeTimer=setTimeout(function(){N.reload()},100)};if(this.options.initCallback!==null){this.options.initCallback(this,"init")}if(!B&&C.browser.safari){this.buttons(false,false);C(window).bind("load.jcarousel",function(){N.setup()})}else{this.setup()}};var A=C.jcarousel;A.fn=A.prototype={jcarousel:"0.2.8"};A.fn.extend=A.extend=C.extend;A.fn.extend({setup:function(){this.first=null;this.last=null;this.prevFirst=null;this.prevLast=null;this.animating=false;this.timer=null;this.resizeTimer=null;this.tail=null;this.inTail=false;if(this.locked){return }this.list.css(this.lt,this.pos(this.options.offset)+"px");var E=this.pos(this.options.start,true);this.prevFirst=this.prevLast=null;this.animate(E,false);C(window).unbind("resize.jcarousel",this.funcResize).bind("resize.jcarousel",this.funcResize);if(this.options.setupCallback!==null){this.options.setupCallback(this)}},reset:function(E){this.list.empty();this.list.css(this.lt,"0px");this.list.css(this.wh,"10px");if(this.options.initCallback!==null){this.options.initCallback(this,"reset")}if(E){this.setup()}},reload:function(){if(this.tail!==null&&this.inTail){this.list.css(this.lt,A.intval(this.list.css(this.lt))+this.tail)}this.tail=null;this.inTail=false;if(this.options.reloadCallback!==null){this.options.reloadCallback(this)}if(this.options.visible!==null){var G=this;var H=Math.ceil(this.clipping()/this.options.visible),F=0,E=0;this.list.children("li").each(function(I){F+=G.dimension(this,H);if(I+1<G.first){E=F}});this.list.css(this.wh,F+"px");this.list.css(this.lt,-E+"px")}this.scroll(this.first,false)},lock:function(){this.locked=true;this.buttons()},unlock:function(){this.locked=false;this.buttons()},size:function(E){if(E!==undefined){this.options.size=E;if(!this.locked){this.buttons()}}return this.options.size},has:function(F,G){if(G===undefined||!G){G=F}if(this.options.size!==null&&G>this.options.size){G=this.options.size}for(var E=F;E<=G;E++){var H=this.get(E);if(!H.length||H.hasClass("jcarousel-item-placeholder")){return false}}return true},get:function(E){return C(">.jcarousel-item-"+E,this.list)},add:function(I,M){var J=this.get(I),G=0,F=C(M);if(J.length===0){var L,H=A.intval(I);J=this.create(I);while(true){L=this.get(--H);if(H<=0||L.length){if(H<=0){this.list.prepend(J)}else{L.after(J)}break}}}else{G=this.dimension(J)}if(F.get(0).nodeName.toUpperCase()=="LI"){J.replaceWith(F);J=F}else{J.empty().append(M)}this.format(J.removeClass(this.className("jcarousel-item-placeholder")),I);var K=this.options.visible!==null?Math.ceil(this.clipping()/this.options.visible):null;var E=this.dimension(J,K)-G;if(I>0&&I<this.first){this.list.css(this.lt,A.intval(this.list.css(this.lt))-E+"px")}this.list.css(this.wh,A.intval(this.list.css(this.wh))+E+"px");return J},remove:function(E){var F=this.get(E);if(!F.length||(E>=this.first&&E<=this.last)){return }var G=this.dimension(F);if(E<this.first){this.list.css(this.lt,A.intval(this.list.css(this.lt))+G+"px")}F.remove();this.list.css(this.wh,A.intval(this.list.css(this.wh))-G+"px")},next:function(){if(this.tail!==null&&!this.inTail){this.scrollTail(false)}else{this.scroll(((this.options.wrap=="both"||this.options.wrap=="last")&&this.options.size!==null&&this.last==this.options.size)?1:this.first+this.options.scroll)}},prev:function(){if(this.tail!==null&&this.inTail){this.scrollTail(true)}else{this.scroll(((this.options.wrap=="both"||this.options.wrap=="first")&&this.options.size!==null&&this.first==1)?this.options.size:this.first-this.options.scroll)}},scrollTail:function(E){if(this.locked||this.animating||!this.tail){return }this.pauseAuto();var F=A.intval(this.list.css(this.lt));F=!E?F-this.tail:F+this.tail;this.inTail=!E;this.prevFirst=this.first;this.prevLast=this.last;this.animate(F)},scroll:function(F,E){if(this.locked||this.animating){return }this.pauseAuto();this.animate(this.pos(F),E)},pos:function(S,F){var G=A.intval(this.list.css(this.lt));if(this.locked||this.animating){return G}if(this.options.wrap!="circular"){S=S<1?1:(this.options.size&&S>this.options.size?this.options.size:S)}var P=this.first>S;var U=this.options.wrap!="circular"&&this.first<=1?1:this.first;var X=P?this.get(U):this.get(this.last);var R=P?U:U-1;var V=null,Q=0,N=false,W=0,T;while(P?--R>=S:++R<S){V=this.get(R);N=!V.length;if(V.length===0){V=this.create(R).addClass(this.className("jcarousel-item-placeholder"));X[P?"before":"after"](V);if(this.first!==null&&this.options.wrap=="circular"&&this.options.size!==null&&(R<=0||R>this.options.size)){T=this.get(this.index(R));if(T.length){V=this.add(R,T.clone(true))}}}X=V;W=this.dimension(V);if(N){Q+=W}if(this.first!==null&&(this.options.wrap=="circular"||(R>=1&&(this.options.size===null||R<=this.options.size)))){G=P?G+W:G-W}}var K=this.clipping(),M=[],E=0,L=0;X=this.get(S-1);R=S;while(++E){V=this.get(R);N=!V.length;if(V.length===0){V=this.create(R).addClass(this.className("jcarousel-item-placeholder"));if(X.length===0){this.list.prepend(V)}else{X[P?"before":"after"](V)}if(this.first!==null&&this.options.wrap=="circular"&&this.options.size!==null&&(R<=0||R>this.options.size)){T=this.get(this.index(R));if(T.length){V=this.add(R,T.clone(true))}}}X=V;W=this.dimension(V);if(W===0){throw new Error("jCarousel: No width/height set for items. This will cause an infinite loop. Aborting...")}if(this.options.wrap!="circular"&&this.options.size!==null&&R>this.options.size){M.push(V)}else{if(N){Q+=W}}L+=W;if(L>=K){break}R++}for(var J=0;J<M.length;J++){M[J].remove()}if(Q>0){this.list.css(this.wh,this.dimension(this.list)+Q+"px");if(P){G-=Q;this.list.css(this.lt,A.intval(this.list.css(this.lt))-Q+"px")}}var I=S+E-1;if(this.options.wrap!="circular"&&this.options.size&&I>this.options.size){I=this.options.size}if(R>I){E=0;R=I;L=0;while(++E){V=this.get(R--);if(!V.length){break}L+=this.dimension(V);if(L>=K){break}}}var H=I-E+1;if(this.options.wrap!="circular"&&H<1){H=1}if(this.inTail&&P){G+=this.tail;this.inTail=false}this.tail=null;if(this.options.wrap!="circular"&&I==this.options.size&&(I-E+1)>=1){var O=A.intval(this.get(I).css(!this.options.vertical?"marginRight":"marginBottom"));if((L-O)>K){this.tail=L-K-O}}if(F&&S===this.options.size&&this.tail){G-=this.tail;this.inTail=true}while(S-->H){G+=this.dimension(this.get(S))}this.prevFirst=this.first;this.prevLast=this.last;this.first=H;this.last=I;return G},animate:function(I,E){if(this.locked||this.animating){return }this.animating=true;var F=this;var G=function(){F.animating=false;if(I===0){F.list.css(F.lt,0)}if(!F.autoStopped&&(F.options.wrap=="circular"||F.options.wrap=="both"||F.options.wrap=="last"||F.options.size===null||F.last<F.options.size||(F.last==F.options.size&&F.tail!==null&&!F.inTail))){F.startAuto()}F.buttons();F.notify("onAfterAnimation");if(F.options.wrap=="circular"&&F.options.size!==null){for(var K=F.prevFirst;K<=F.prevLast;K++){if(K!==null&&!(K>=F.first&&K<=F.last)&&(K<1||K>F.options.size)){F.remove(K)}}}};this.notify("onBeforeAnimation");if(!this.options.animation||E===false){this.list.css(this.lt,I+"px");G()}else{var J=!this.options.vertical?(this.options.rtl?{right:I}:{left:I}):{top:I};var H={duration:this.options.animation,easing:this.options.easing,complete:G};if(C.isFunction(this.options.animationStepCallback)){H.step=this.options.animationStepCallback}this.list.animate(J,H)}},startAuto:function(F){if(F!==undefined){this.options.auto=F}if(this.options.auto===0){return this.stopAuto()}if(this.timer!==null){return }this.autoStopped=false;var E=this;this.timer=window.setTimeout(function(){E.next()},this.options.auto*1000)},stopAuto:function(){this.pauseAuto();this.autoStopped=true},pauseAuto:function(){if(this.timer===null){return }window.clearTimeout(this.timer);this.timer=null},buttons:function(G,F){if(G==null){G=!this.locked&&this.options.size!==0&&((this.options.wrap&&this.options.wrap!="first")||this.options.size===null||this.last<this.options.size);if(!this.locked&&(!this.options.wrap||this.options.wrap=="first")&&this.options.size!==null&&this.last>=this.options.size){G=this.tail!==null&&!this.inTail}}if(F==null){F=!this.locked&&this.options.size!==0&&((this.options.wrap&&this.options.wrap!="last")||this.first>1);if(!this.locked&&(!this.options.wrap||this.options.wrap=="last")&&this.options.size!==null&&this.first==1){F=this.tail!==null&&this.inTail}}var E=this;if(this.buttonNext.size()>0){this.buttonNext.unbind(this.options.buttonNextEvent+".jcarousel",this.funcNext);if(G){this.buttonNext.bind(this.options.buttonNextEvent+".jcarousel",this.funcNext)}this.buttonNext[G?"removeClass":"addClass"](this.className("jcarousel-next-disabled")).attr("disabled",G?false:true);if(this.options.buttonNextCallback!==null&&this.buttonNext.data("jcarouselstate")!=G){this.buttonNext.each(function(){E.options.buttonNextCallback(E,this,G)}).data("jcarouselstate",G)}}else{if(this.options.buttonNextCallback!==null&&this.buttonNextState!=G){this.options.buttonNextCallback(E,null,G)}}if(this.buttonPrev.size()>0){this.buttonPrev.unbind(this.options.buttonPrevEvent+".jcarousel",this.funcPrev);if(F){this.buttonPrev.bind(this.options.buttonPrevEvent+".jcarousel",this.funcPrev)}this.buttonPrev[F?"removeClass":"addClass"](this.className("jcarousel-prev-disabled")).attr("disabled",F?false:true);if(this.options.buttonPrevCallback!==null&&this.buttonPrev.data("jcarouselstate")!=F){this.buttonPrev.each(function(){E.options.buttonPrevCallback(E,this,F)}).data("jcarouselstate",F)}}else{if(this.options.buttonPrevCallback!==null&&this.buttonPrevState!=F){this.options.buttonPrevCallback(E,null,F)}}this.buttonNextState=G;this.buttonPrevState=F},notify:function(E){var F=this.prevFirst===null?"init":(this.prevFirst<this.first?"next":"prev");this.callback("itemLoadCallback",E,F);if(this.prevFirst!==this.first){this.callback("itemFirstInCallback",E,F,this.first);this.callback("itemFirstOutCallback",E,F,this.prevFirst)}if(this.prevLast!==this.last){this.callback("itemLastInCallback",E,F,this.last);this.callback("itemLastOutCallback",E,F,this.prevLast)}this.callback("itemVisibleInCallback",E,F,this.first,this.last,this.prevFirst,this.prevLast);this.callback("itemVisibleOutCallback",E,F,this.prevFirst,this.prevLast,this.first,this.last)},callback:function(I,L,E,J,H,G,F){if(this.options[I]==null||(typeof this.options[I]!="object"&&L!="onAfterAnimation")){return }var M=typeof this.options[I]=="object"?this.options[I][L]:this.options[I];if(!C.isFunction(M)){return }var N=this;if(J===undefined){M(N,E,L)}else{if(H===undefined){this.get(J).each(function(){M(N,this,J,E,L)})}else{var O=function(P){N.get(P).each(function(){M(N,this,P,E,L)})};for(var K=J;K<=H;K++){if(K!==null&&!(K>=G&&K<=F)){O(K)}}}}},create:function(E){return this.format("<li></li>",E)},destroy:function(){this.reset();C(window).unbind(".jcarousel");this.list.removeData("jcarousel");this.container.removeData("jcarousel");this.buttonNext.removeData("jcarouselstate").remove();this.buttonPrev.removeData("jcarouselstate").remove();return this},format:function(H,G){H=C(H);var F=H.get(0).className.split(" ");for(var E=0;E<F.length;E++){if(F[E].indexOf("jcarousel-")!=-1){H.removeClass(F[E])}}H.addClass(this.className("jcarousel-item")).addClass(this.className("jcarousel-item-"+G)).css({"float":(this.options.rtl?"right":"left"),"list-style":"none"}).attr("jcarouselindex",G);return H},className:function(E){return E+" "+E+(!this.options.vertical?"-horizontal":"-vertical")},dimension:function(G,H){var F=C(G);if(H==null){return !this.options.vertical?(F.outerWidth(true)||A.intval(this.options.itemFallbackDimension)):(F.outerHeight(true)||A.intval(this.options.itemFallbackDimension))}else{var E=!this.options.vertical?H-A.intval(F.css("marginLeft"))-A.intval(F.css("marginRight")):H-A.intval(F.css("marginTop"))-A.intval(F.css("marginBottom"));C(F).css(this.wh,E+"px");return this.dimension(F)}},clipping:function(){return !this.options.vertical?this.clip[0].offsetWidth-A.intval(this.clip.css("borderLeftWidth"))-A.intval(this.clip.css("borderRightWidth")):this.clip[0].offsetHeight-A.intval(this.clip.css("borderTopWidth"))-A.intval(this.clip.css("borderBottomWidth"))},index:function(E,F){if(F==null){F=this.options.size}return Math.round((((E-1)/F)-Math.floor((E-1)/F))*F)+1}});A.extend({defaults:function(E){return C.extend(D,E||{})},intval:function(E){E=parseInt(E,10);return isNaN(E)?0:E},windowLoaded:function(){B=true}});C.fn.jcarousel=function(G){if(typeof G=="string"){var E=C(this).data("jcarousel"),F=Array.prototype.slice.call(arguments,1);if(typeof E!="undefined"){return E[G].apply(E,F)}return this}else{return this.each(function(){var H=C(this).data("jcarousel");if(H){if(G){C.extend(H.options,G)}H.reload()}else{C(this).data("jcarousel",new A(this,G))}})}}})(jQuery);