(function($) {
    $.fn.contentRotator = function(options) {
        options = $.extend({
            transition: 'fade',
            speed : 400,
            loop : true,
            interval : 5000,
            numbersWrap : true,
            buttonsWidth : 0
        }, options);
        
        return this.each(function() {
            var rotator = $(this),
                count = rotator.children().size(),
                numbering = '<ul>',
                width = rotator.width(),
                height = rotator.height(),
                id = '';
                
            if(rotator.attr('id') != '') id = ' '+rotator.attr('id');
                
            if(options.transition == 'slide') width = width / count;
            
            // wrap the contentRotator class with wrapper and container divs
            rotator.wrap('<div class="contentRotator-wrapper '+options.transition+id+'" style="width: '+width+'px; height: '+height+'px;"><div class="contentRotator-container">');
            
            var wrapper = rotator.parent().parent();
            
            for(var i=1; i<=count; i++){
                var selected = '';
                if(i==1) selected = ' class="selected"';
                numbering += '<li><a'+selected+' href="#">'+i+'</a></li>';
            }
            
            numbering += '</ul>';
            
            $('.contentRotator-wrapper').append('<div class="contentRotator-buttons"><div class="left"></div>'+numbering+'<div class="right"></div></div>');
            
            if(options.buttonsWidth == 0){
                var buttonsWidth = (count * ($('.contentRotator-buttons ul li').width() + 4)) + (2 * $('.contentRotator-buttons div').width());
                $('.contentRotator-buttons').width(buttonsWidth);
            }
            
            if(options.numbersWrap){
                $('<div class="contentRotator-buttons-wrapper">').insertBefore('.contentRotator-buttons');
            }
            
            var left_button = $('.contentRotator-buttons .left');
            var right_button = $('.contentRotator-buttons .right');
            
            // when a button is pressed
            $('.contentRotator-buttons a').click(function() {
                if(!$(this).hasClass('selected')){
                    var contentIndex = $(this).text() - 1;
                    if(options.transition == 'fade'){
                        rotator.find('.contentRotator-content').fadeOut(options.speed);
                        rotator.find('.contentRotator-content:eq('+contentIndex+')').fadeIn(options.speed);
                    }else{
                        var currLeft = parseInt(rotator.css('left').replace('px', ''));
                        var currSel = currLeft / -width;
                        
                        if(contentIndex > currSel){
                            var left = (contentIndex - currSel) * width;
                            rotator.animate({left : '-='+left}, options.speed, function() {
                                var rotator_left = parseInt(rotator.css('left').replace('px', ''));
                                if(rotator_left < 0){
                                    left_button.fadeIn('fast');
                                }
                                
                                if(contentIndex == count - 1){
                                    right_button.fadeOut('fast');
                                }
                            });
                        }else{
                            var left = (currSel - contentIndex) * width;
                            rotator.animate({left : '+='+left}, options.speed, function() {
                                var rotator_left = parseInt(rotator.css('left').replace('px', ''));
                                if(rotator_left == 0){
                                    left_button.fadeOut('fast');
                                }
                                
                                if(contentIndex < count - 1){
                                    right_button.fadeIn('fast');
                                }
                            });
                        }
                    }
                    $('.contentRotator-buttons a.selected').removeClass('selected');
                    $(this).addClass('selected');
                    makeLISelected();
                    clearInterval(looper);
                }
                
                return false;
            });
            
            // when right or left arrows are clicked
            $('.contentRotator-buttons div').click(function() {
                var button = $(this); // the button clicked
                var direction = button.attr('class'); // the direction
                var currIndex = parseInt(button.parent().find('a.selected').text()); // current index of the image
                var selIndex = 0;
                var numSel = 0;
                
                if(button.hasClass('animating') == false){
                    var animated = false;
                    
                    if(direction == 'left' && currIndex > 1){
                        button.addClass('animating');
                        selIndex = currIndex - 2;
                        numSel = selIndex;
                        if(options.transition == 'fade'){
                            rotator.find('.contentRotator-content').fadeOut(options.speed);
                            rotator.find('.contentRotator-content:eq('+selIndex+')').fadeIn(options.speed);
                        }else{
                            rotator.animate({left : '+='+width}, options.speed, function() {
                                var rotator_left = parseInt(rotator.css('left').replace('px', ''));
                                //alert(rotator_left);
                                if(rotator_left == 0){
                                    left_button.fadeOut('fast');
                                }
                                
                                if(selIndex < count - 1){
                                    right_button.fadeIn('fast');
                                }
                            });
                        }
                        animated = true;
                    }
                    
                    if(direction == 'right' && currIndex < count){
                        button.addClass('animating');
                        selIndex = currIndex + 1;
                        numSel = selIndex - 1;
                        if(options.transition == 'fade'){
                            rotator.find('.contentRotator-content').fadeOut(options.speed);
                            rotator.find('.contentRotator-content:eq('+currIndex+')').fadeIn(options.speed);
                        }else{
                            rotator.animate({left : '-='+width}, options.speed, function() {
                                var rotator_left = parseInt(rotator.css('left').replace('px', ''));
                                //alert(rotator_left);
                                if(rotator_left < 0){
                                    left_button.fadeIn('fast');
                                }
                                //alert(currIndex);
                                if(currIndex == count - 1){
                                    right_button.fadeOut('fast');
                                }
                            });
                        }
                        animated = true;
                    }
                    
                    if(animated){
                        wrapper.find('a.selected').removeClass('selected');
                        wrapper.find('.contentRotator-buttons li:eq('+numSel+') a').addClass('selected');
                        button.removeClass('animating');
                        makeLISelected();
                        clearInterval(looper);
                    }
                }
            });
            
            var looper = null;
            if(options.loop){
                looper = setInterval(function() {
                    var currIndex = parseInt(wrapper.find('a.selected').text());
                    //alert(currIndex);
                    
                    if(currIndex == count){
                        currIndex = 0;
                    }
                    
                    if(options.transition == 'fade'){
                        rotator.find('.contentRotator-content').fadeOut(options.speed);
                        rotator.find('.contentRotator-content:eq('+currIndex+')').fadeIn(options.speed);
                    }else if(options.transition == 'slide'){
                        if(currIndex == 0){
                            rotator.animate({left : '0'}, options.speed, function() {
                                left_button.fadeOut('fast');
                                right_button.fadeIn('fast');
                            });
                        }else{
                            rotator.animate({left : '-='+width}, options.speed, function() {
                                if(currIndex > 0) left_button.fadeIn('fast');
                                if(currIndex == count - 1) right_button.fadeOut('fast');
                            });
                        }
                    }
                    wrapper.find('a.selected').removeClass('selected');
                    wrapper.find('.contentRotator-buttons li:eq('+currIndex+') a').addClass('selected');
                    
                    makeLISelected();
                }, options.interval);
            }
            
            function makeLISelected() {
                wrapper.find('.contentRotator-buttons li').removeClass('selected');
                wrapper.find('.contentRotator-buttons a.selected').parent().addClass('selected');
                
                //alert(wrapper.find('.contentRotator-buttons li').index(wrapper.find('.contentRotator-buttons')));
            }
            
            makeLISelected();
        });
    };
})(jQuery);
