/*
 * jQuery infinitecarousel plugin
 * @author admin@catchmyfame.com - http://www.catchmyfame.com
 * @version 1.2.2
 * @date August 31, 2009
 * @category jQuery plugin
 * @copyright (c) 2009 admin@catchmyfame.com (www.catchmyfame.com)
 * @license CC Attribution-Share Alike 3.0 - http://creativecommons.org/licenses/by-sa/3.0/
 */

(function($){
    $.fn.extend({
        infiniteCarousel: function(options)
        {
            var defaults =
            {
                transitionSpeed : 100,
                displayTime : 6000,
                textholderHeight : .5,
                displayProgressBar : 1,
                displayThumbnails: 0,
                displayThumbnailNumbers: 1,
                displayThumbnailBackground: 1,
                thumbnailWidth: '20px',
                thumbnailHeight: '20px',
                thumbnailFontSize: '.7em'
            };
        var options = $.extend(defaults, options);

            return this.each(function(i) {
                var randID = Math.round(Math.random()*100000000);
                var o=options;
                var obj = $(this);
                var curr = 1;

                var numImages = $('li', obj).length; // Number of images
                var imgWidth = 372;
                var autopilot = 0;

                $('p#title', obj).hide(); // Hide any text paragraphs in the carousel
                $(obj).width(imgWidth);

                // Build progress bar
                if(o.displayProgressBar)
                {
                    $(obj).append('<div id="progress'+randID+'" style="position:absolute;bottom:0;background:#bbb;left:'+$(obj).css('paddingLeft')+'"></div>');
                    $('#progress'+randID).width(imgWidth).height(5).css('opacity','.5');
                }

                // Move last image and stick it on the front
                $(obj).css({'overflow':'hidden','position':'relative'});
                $('li:last', obj).prependTo($('ul', obj));
                $('ul', obj).css('left',-imgWidth+'px');
                $('ul',obj).width(9999);

                $('ul',obj).css({'list-style':'none','margin':'0','padding':'0','position':'relative'});
                $('li',obj).css({'display':'inline','float':'left'});


                // Prev/next button(img)
                html = '<div id="btn_rt'+randID+'" style="position:absolute;z-index:50;left:302px;top:100px"><a href="javascript:void(0);"><img style="border:none;margin-right:2px;position:absolute;z-index:100;" src="static/images/gp/adherent/diapo/rt.jpg" /></a></div>';
                html += '<div id="btn_lt'+randID+'" style="position:absolute;z-index:50;left:0px;top:100px"><a href="javascript:void(0);"><img style="border:none;margin-left:2px" src="static/images/gp/adherent/diapo/lt.jpg" /></a></div>';
                $(obj).append(html);


                // Left and right arrow image button actions
                $('#btn_rt'+randID).click(function(){
                    autopilot = 0;
                    anim('next');
                });
                $('#btn_lt'+randID).click(function(){
                    autopilot = 0;
                    anim('prev');
                });

                function thumbclick(event)
                {
                    target_num = this.id.split('_'); // we want target_num[1]
                    if(curr != target_num[1])
                    {
                        $('#thumb'+randID+'_'+curr).css({'border-color':'#ccc'});
                        $('#progress'+randID).stop().fadeOut();
                        $('#thumbs'+randID+' div').css({'cursor':'default'}).unbind('click'); // Unbind the thumbnail click event until the transition has ended
                        autopilot = 0;
                   }
                    if(target_num[1] > curr)
                    {
                        diff = target_num[1] - curr;
                        anim('next',diff);
                    }
                    if(target_num[1] < curr)
                    {
                        diff = curr - target_num[1];
                        anim('prev', diff);
                    }
                }

                function anim(direction,dist)
                {
                    if(direction == "next")
                    {
                        if(curr==numImages) curr=0;
                        if(dist>1)
                        {
                            $('li:lt(2)', obj).clone().insertAfter($('li:last', obj));
                            $('ul', obj).animate({left:-imgWidth*(dist+1)},o.transitionSpeed,function(){
                                $('li:lt(2)', obj).remove();
                                for(j=1;j<=dist-2;j++)
                                {
                                    $('li:first', obj).clone().insertAfter($('li:last', obj));
                                    $('li:first', obj).remove();
                                }
                                $(this).css({'left':-imgWidth});
                                curr = curr+dist;

                            });
                        }
                        else
                        {
                            $('#thumbs'+randID+' div').css({'cursor':'default'}).unbind('click'); // Unbind the thumbnail click event until the transition has ended
                            // Copy leftmost (first) li and insert it after the last li
                            $('li:first', obj).clone().insertAfter($('li:last', obj));
                            // Update width and left position of ul and animate ul to the left
                            $('ul', obj)
                                .animate({left:-imgWidth*2},o.transitionSpeed,function(){
                                    $('li:first', obj).remove();
                                    $('ul', obj).css('left',-imgWidth+'px');
                                    curr=curr+1;

                                });
                        }
                    }
                    if(direction == "prev")
                    {
                        if(dist>1)
                        {
                            $('li:gt('+(numImages-(dist+1))+')', obj).clone().insertBefore($('li:first', obj));
                            $('ul', obj).css({'left':(-imgWidth*(dist+1))}).animate({left:-imgWidth},o.transitionSpeed,function(){
                                $('li:gt('+(numImages-1)+')', obj).remove();
                                curr = curr - dist;

                            });
                        }
                        else
                        {
                            $('#thumbs'+randID+' div').css({'cursor':'default'}).unbind('click'); // Unbind the thumbnail click event until the transition has ended
                            // Copy rightmost (last) li and insert it after the first li
                            $('li:last', obj).clone().insertBefore($('li:first', obj));
                            // Update width and left position of ul and animate ul to the right
                            $('ul', obj)
                                .css('left',-imgWidth*2+'px')
                                .animate({left:-imgWidth},o.transitionSpeed,function(){
                                    $('li:last', obj).remove();
                                    curr=curr-1;
                                    if(curr==0) curr=numImages;

                                });
                        }
                    }
                }
        });
        }
    });
})(jQuery);
