// jQuery Carousel plugin v1.2 - By Daniel Thomson

// version 1.1 - just a bit of optimisation of the code here, no functional changes
// version 1.2 - added new option to start at a nominated position

(function(d){d.fn.carousel=function(a){var b={itemWidth:50,itemHeight:80,scrollNext:".next",scrollPrev:".prev",scrollSpeed:"1000",scrollNum:5,scrollVisible:3,circular:false,vertical:false,startPoint:0};b&&d.extend(b,a);this.each(function(){var c=d(this);c.counter=1;c.scrollPane=c.children("div");c.theList=c.children("div").children("ul");c.carouselSize=c.theList.children("li").size();b.circular==true&&d.fn.carousel.addTail(c,b);c.listItems=c.children("div").children("ul").children("li");c.itemWidth= b.itemWidth;c.itemHeight=b.itemHeight;c.scrollNum=b.scrollNum;c.carouselWidth=c.carouselSize*c.itemWidth;c.carouselHeight=c.carouselSize*c.itemHeight;c.itemDimension=b.vertical==true?c.itemHeight:c.itemWidth;c.scrollLength=c.scrollNum*c.itemDimension;c.scrollNext=d(b.scrollNext);c.scrollPrev=d(b.scrollPrev);c.scrollVisible=b.scrollVisible;c.scrollPos=0;d.fn.carousel.styleList(c,b);d.fn.carousel.setStartPos(c,b);d(b.scrollNext).click(function(){d.fn.carousel.findScrollPos(c,b,"next");b.circular==false? d.fn.carousel.findEndPos(c,b,"next"):d.fn.carousel.findEndPosCircular(c,b,"next");d.fn.carousel.animateList(c,b);c.animationEnd=c.scrollPos;return false});d(b.scrollPrev).click(function(){d.fn.carousel.findScrollPos(c,b,"prev");b.circular==false?d.fn.carousel.findEndPos(c,b,"prev"):d.fn.carousel.findEndPosCircular(c,b,"prev");d.fn.carousel.animateList(c,b);c.animationEnd=c.scrollPos;return false})});return this};d.fn.carousel.setStartPos=function(a,b){if(b.startPoint!=0){if(Math.abs(b.startPoint)> a.carouselSize&&b.circular==true)if(b.startPoint>0){var c=Math.floor(b.startPoint/a.carouselSize);c=b.startPoint-a.carouselSize*c}else{c=Math.ceil(b.startPoint/a.carouselSize);c=b.startPoint-a.carouselSize*c+a.carouselSize}else c=(b.startPoint>a.carouselSize||b.startPoint<0)&&b.circular==false?1:b.startPoint>0?b.startPoint:b.startPoint+a.carouselSize;a.counter=c;if(b.circular==true)c=(a.counter+a.scrollVisible)*a.itemDimension-a.itemDimension;else{if(a.counter>a.carouselSize-a.scrollVisible){a.counter= a.carouselSize-a.scrollVisible+1;d(b.scrollNext).addClass("disabled")}a.counter>1&&d(b.scrollPrev).removeClass("disabled");c=a.counter*a.itemDimension-a.itemDimension}b.vertical==true?a.theList.css("top",-c+"px"):a.theList.css("left",-c+"px");a.scrollPos=-c;a.animationEnd=a.scrollPos}};d.fn.carousel.findEndPos=function(a,b,c){if(c=="next"){if(b.scrollVisible+a.counter+a.scrollNum>a.carouselSize){b.vertical==false&&a.theList.css("left",a.animationEnd);a.scrollPos=b.scrollVisible*a.itemDimension-a.carouselSize* a.itemDimension;d(b.scrollNext).addClass("disabled");a.counter=a.carouselSize-b.scrollVisible+1}else a.counter+=a.scrollNum;d(b.scrollPrev).removeClass("disabled")}else{if(a.counter<=a.scrollNum){a.scrollPos=0;a.counter=1}else a.counter-=a.scrollNum;d(b.scrollNext).removeClass("disabled");a.counter==1&&d(b.scrollPrev).addClass("disabled")}};d.fn.carousel.findEndPosCircular=function(a,b,c){if(c=="next"){if(a.counter+a.scrollNum>a.carouselSize){a.counter-=a.carouselSize;c=a.scrollPos+(a.carouselSize+ a.scrollNum)*a.itemDimension;a.scrollPos=c-a.scrollLength;b.vertical==false?a.theList.css("left",c):a.theList.css("top",c)}a.counter+=a.scrollNum}else{if(a.counter<1){a.counter+=a.carouselSize;c=a.scrollPos-(a.carouselSize+a.scrollNum)*a.itemDimension;a.scrollPos=c+a.scrollLength;b.vertical==false?a.theList.css("left",c):a.theList.css("top",c)}a.counter-=a.scrollNum}};d.fn.carousel.findScrollPos=function(a,b,c){if(b.vertical==false){a.theList.stop();a.theList.css("left",a.animationEnd);b=parseInt(a.theList.css("left")); a.scrollPos=c=="next"?b-a.scrollLength:b+a.scrollLength}else{a.theList.stop();a.theList.css("top",a.animationEnd);b=parseInt(a.theList.css("top"));a.scrollPos=c=="next"?b-a.scrollLength:b+a.scrollLength}};d.fn.carousel.animateList=function(a,b){b.vertical==false?a.theList.animate({left:a.scrollPos},b.scrollSpeed):a.theList.animate({top:a.scrollPos},b.scrollSpeed)};d.fn.carousel.addTail=function(a,b){for(i=0;i<b.scrollVisible;i++){lastIndex="li:eq("+(a.carouselSize-1)+")";firstIndex="li:eq("+i*2+")"; appendage=a.theList.children(firstIndex).clone();prependage=a.theList.children(lastIndex).clone();appendage.appendTo(a.theList);prependage.prependTo(a.theList)}};d.fn.carousel.styleList=function(a,b){a.css({position:"relative"});a.scrollPane.css({position:"relative",left:"0px",overflow:"hidden","z-index":"2"});a.theList.css({"list-style-type":"none",margin:"0px",padding:"0px",position:"relative","z-index":"1",height:a.carouselHeight+"px",width:a.itemWidth+"px",left:"0px",top:"0px"});a.listItems.css({cssFloat:"left", overflow:"hidden",display:"block",height:a.itemHeight+"px",width:a.itemWidth+"px"});carouselWidth=a.listItems*a.scrollVisible;if(b.vertical==true){a.scrollPane.css({height:a.itemHeight*a.scrollVisible+"px",width:a.itemWidth+"px"});a.theList.css({height:a.carouselHeight+"px",width:a.itemWidth+"px"});b.circular==true&&a.theList.css({top:0-a.scrollVisible*a.itemHeight+"px",height:a.carouselHeight+a.scrollVisible*a.itemHeight*2+"px"});a.animationEnd=a.theList.css("top")}else{a.scrollPane.css({height:a.itemHeight, width:a.itemWidth*a.scrollVisible+"px"});a.theList.css({height:a.itemHeight+"px",width:a.carouselWidth+"px"});b.circular==true&&a.theList.css({left:0-a.scrollVisible*a.itemWidth+"px",width:a.carouselWidth+a.scrollVisible*a.itemWidth*2+"px"});a.animationEnd=a.theList.css("left")}b.circular==false&&d(a.scrollPrev).addClass("disabled")}})(jQuery);