/**
 *  jQuery Rotator Plugin
 *  @requires jQuery v1.2.6 or greater
 *  http://hernan.amiune.com/labs
 *
 *  Copyright (c)  Hernan Amiune (hernan.amiune.com)
 *  Licensed under MIT license:
 *  http://www.opensource.org/licenses/mit-license.php
 *
 *  Version: 2.0
 */

(function($){
    $.fn.rotator = function(options){

        var defaults = {
            ms: 2000,
            n: 1,
            autoHeight: false,
            detectHeight: true // proposed by Cliff Pruitt
        };

        var options = $.extend(defaults, options);

        return this.each(function(index) {

            var $this = $(this);

            var initialHeight = 0;
            $this.children().filter(":lt("+options.n+")").each(function(index,item){
                initialHeight += $(item).height();
            });

            if(options.detectHeight)$this.height(initialHeight); // proposed by Cliff Pruitt

            function beginRotation(){
                $this.data("timer", setInterval(function(){

                    var childHeight = $this.children().filter(":first-child").height();
                    var animParams = {
                        scrollTop: (childHeight) + "px"
                    };
                    var autoHeight = 0;
                    if(options.autoHeight){
                        $this.children().filter(":lt("+(options.n+1)+")").each(function(index,item){
                            if(index>0)autoHeight += $(item).height();
                        });
                        animParams = $.extend({
                            height:(autoHeight) + "px"
                        }, animParams);
                    }


                    $this.animate(animParams, 500, function(){
                        $this.append($this.children().filter(":first-child"));
                        $this.remove($this.children().filter(":first-child"));
                        $this.scrollTop(0);
                    });


                }, options.ms));
            }

            $this.mouseover(function(){
                clearTimeout($this.data("timer"));
            });

            $this.mouseout(function(){
                beginRotation();
            });

            beginRotation();

        });


    }
})(jQuery);






$(window).load(function() { //start after HTML, images have loaded


    var InfiniteRotator =
    {
        init: function()
        {
            //initial fade-in time (in milliseconds)
            var initialFadeIn = 1000;

            //interval between items (in milliseconds)
            var itemInterval = 5000;

            //cross-fade time (in milliseconds)
            var fadeTime = 2500;

            //count number of items
            var numberOfItems = $('.rotating-item').length;

            //set current item
            var currentItem = 0;

            //show first item
            $('.rotating-item').eq(currentItem).fadeIn(initialFadeIn);

            //loop through the items
            var infiniteLoop = setInterval(function(){
                $('.rotating-item').eq(currentItem).fadeOut(fadeTime);

                if(currentItem == numberOfItems -1){
                    currentItem = 0;
                }else{
                    currentItem++;
                }
                $('.rotating-item').eq(currentItem).fadeIn(fadeTime);

            }, itemInterval);
        }
    };

    InfiniteRotator.init();




});
