;
(function($) {
    $.fn.extend( {
        adqwCarousel : function(options) {
            return this.each(function() {
                new $.AdqwCarousel(this, options);
            });
        }
    });

    $.AdqwCarousel = function(element, options) {
        options=$.extend({},$.AdqwCarousel.defaults,options);
        var $holder=$(element);
        var $ul;
        var $lis;
        var $titleLabel;
        var timer;
        var stopAnimation;
        var currentSelected;
        var direction=-1;
        var $selectorLis;
        var nbItems;
        var timerRestart;
        var $counter;
        var $previous;
        var $next;

        var init = function() {
            $holder.addClass('adqwCarousel');
            $holder.css('overflow','hidden');
            $ul=$holder.children('ul');
            $lis=$ul.children('li').addClass('adqwCarouselItem');

            $lis.css('float','left');
            $ul.after($('<div></div>').css('clear','left'));
            nbItems=$lis.length;
            $ul.width(options.stepWidth*$lis.length+10);
            $holder.width(options.stepWidth);
            $holder.css('position','relative');
            //create title holder
            var $title=$('<div class="adqwCarouselTitle"></div>');
            if (options.showTitle) {
                $title.css('position','absolute');
                if (options.titlePos=='bottom') {
                    $title.css('bottom',0);
                } else {
                    $title.css('top',0);
                }
            }
            $title.height(options.titleHeight);
            $title.width(options.stepWidth);
            //add selectors
            currentSelected=0;
            if (options.showSelectors) {
                var $selectors=$('<ul class="selectors"></ul>').css('position','absolute').css('right','0');
                if ($lis.length>1) {
                    for(var i=0; i<$lis.length; i++) {
                        var $selector;
                        if (currentSelected==i) {
                            $selector=$('<li><img src="'+options.navigatorImgOn+'" alt=""/></li>');
                        } else {
                            $selector=$('<li><img src="'+options.navigatorImgOff+'" alt=""/></li>');
                        }
                        $selector.css('display','inline').css('list_style','none').css('padding','0').css('margin','2px 5px 0 0').css('cursor','pointer').css('font-size','10px').attr('rel',i);
                        $selector.click(function() {
                            if (timerRestart!=undefined) {
                                clearTimeout(timerRestart);
                            }
                            stopAnimation=true;
                            animateTo($(this).attr('rel'));
                            if (options.restartAnimation) {
                                timerRestart=setTimeout(function() {
                                    direction=-1;
                                    stopAnimation=false;
                                },options.restartDelay);
                            }
                        });
                        $selector.appendTo($selectors);
                        $selector=undefined;
                    }
                    $selectors.appendTo($title);
                    $selectorLis=$selectors.children('li');
                }
            }
            if (options.showTitle) {
                //Set title to the one of the first photo
                $titleLabel=$('<h2></h2>');
                $titleLabel.text($lis.eq(0).attr('title'));
                $titleLabel.appendTo($title);
            }
            if (options.titlePos!='bottom') {
                $title.prependTo($holder);
            }
            if (options.showTitle) {
                //create title shade
                var $titleShade=$('<div class="adqwCarouselTitleShade"></div>');
                $titleShade.css('background-color',options.titleShadeColor);
                $titleShade.css('opacity',options.titleShadeOpacity);
                $titleShade.height(options.titleHeight);
                $titleShade.width(options.stepWidth);
                $titleShade.css('position','absolute');
                if (options.titlePos=='bottom') {
                    $titleShade.css('bottom',0);
                    $titleShade.appendTo($holder);
                } else {
                    $titleShade.css('top',0);
                    $titleShade.prependTo($holder);
                }
            }
            if (options.titlePos=='bottom') {
                $title.appendTo($holder);
            }
            if ($lis.length>1) {
                if (options.startAnimation) {
                    stopAnimation=false;
                    timer=setInterval(function() {
                        if (!stopAnimation) {
                            animate();
                        }
                    }, options.delay);
                } else {
                    stopAnimation=true;
                }
            }
            if (options.nextSelector) {
                $next=$(options.nextSelector);
                $next.css('cursor','pointer').click(function() {
                    animateToNext();
                });
            }
            if (options.previousSelector) {
                $previous=$(options.previousSelector);
                $previous.css('cursor','pointer').click(function() {
                    animateToPrevious();
                });
            }
            if (options.counterSelector) {
                $counter=$(options.counterSelector);
                updateCounter();
            }
        }

        var updateCounter=function() {
            $counter.text((currentSelected+1)+'/'+nbItems);
        }

        var animateToNext=function () {
            var toSelect=currentSelected+1;
            animateTo(toSelect);
        }

        var animateToPrevious=function () {
            var toSelect=currentSelected-1;
            animateTo(toSelect);
        }

        var animateTo=function(toSelect) {
            if (toSelect>=nbItems) {
                toSelect=0;
            } else if (toSelect<0) {
                toSelect=nbItems-1;
            }
            if (toSelect!=currentSelected) {
                if (currentSelected>toSelect) {
                    direction=1
                } else {
                    direction=-1;
                }
                animate(Math.abs(currentSelected-toSelect),true);
            }
        }

        var animate=function(nb,quick) {
            var speed;
            if (quick==undefined || !quick) {
                speed=options.speed;
            } else {
                speed=100;
            }
            if (nb==undefined) {
                nb=1;
            }
            if (options.showTitle) {
                //animate title
                var newTitle='';
                if (direction<1) {
                    newTitle=$lis.eq(1).attr('title');
                } else {
                    newTitle=$lis.eq(nbItems-1).attr('title');
                }
                $titleLabel.animate({
                    opacity:0
                },speed/2, function() {
                    $titleLabel.text(newTitle);
                    $titleLabel.animate({
                        opacity:1
                    },speed/2);
                });
            }
            if (options.showSelectors) {
                $selectorLis.eq(currentSelected).find('img').attr('src',options.navigatorImgOff);
            }
            if (direction<1) {
                var $first=$lis.eq(0);
                $first.animate({
                    'margin-left': -options.stepWidth
                },speed,function() {
                    $first.detach();
                    $first.css('margin-left',0);
                    $ul.append($first);
                    $lis=$ul.children('li');
                    currentSelected=currentSelected-direction;
                    if (currentSelected>=nbItems) {
                        currentSelected=0;
                    } else if (currentSelected<0) {
                        currentSelected=nbItems-1;
                    }
                    if (options.showSelectors) {
                        $selectorLis.eq(currentSelected).find('img').attr('src',options.navigatorImgOn);
                    }
                    if (options.counterSelector) {
                        updateCounter();
                    }
                    if (nb>1) {
                        animate(nb-1,quick);
                    }
                });
            } else {
                var $last=$lis.eq(nbItems-1);
                $last.detach();
                $last.css('margin-left',-options.stepWidth);
                $ul.prepend($last);
                $lis=$ul.children('li');
                $last.animate({
                    'margin-left':0
                },speed,function () {
                    currentSelected=currentSelected-direction;
                    if (currentSelected>=nbItems) {
                        currentSelected=0;
                    } else if (currentSelected<0) {
                        currentSelected=nbItems-1;
                    }
                    if (options.showSelectors) {
                        $selectorLis.eq(currentSelected).find('img').attr('src',options.navigatorImgOn);
                    }
                    if (options.counterSelector) {
                        updateCounter();
                    }
                    if (nb>1) {
                        animate(nb-1,quick);
                    }
                });
            }
        }
        init();
    }

    $.AdqwCarousel.defaults= {
        startAnimation: true,
        speed: 1000,
        delay: 5000,
        restartAnimation: true,
        restartDelay: 15000,
        titleShadeColor: '#000000',
        titleShadeOpacity: 0.6,
        titleHeight: 30,
        titlePos: 'bottom',
        navigatorImgOn: '/images/carousel/carouselButtonOn.gif',
        navigatorImgOff: '/images/carousel/carouselButtonOff.gif',
        stepWidth: 800,
        showTitle: true,
        nextSelector: false,
        previousSelector: false,
        counterSelector: false,
        showSelectors: true
    }
})(jQuery)




