/**
 * @category    Workbox jQuery
 * @package     wbRotate
 * @copyright   Copyright (c) 2009-2010 Workbox AB (http://www.workbox.se)
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @version     1.0.0
 */

(function($)
{
    $.fn.extend({
        wbRotate:function(options)
        {
            // Set the default parameters.
            var defaults = {
                rotateInterval:5000,
                rotateIntervalClick:10000,
                shortName:'img',
                shade:'Light'
            };
            // Join the default with the passed options into the o variable.
            var o = $.extend(defaults, options);
            // Set the obj variable to the passed object.
            var obj = $(this);
            // Variable to hold the interval object.
            var intervalHolder = 0;

            /**
             * Return for each the objects.
             *
             * Function to return function called for all the passed objects.
             */
            return this.each(function()
            {
                imgRotator();
            });

            function imgRotator()
            {
                $('ul li', obj).css({opacity:0.0});
                $('ul li:first', obj).css({opacity:1.0});
                if ($('ul li', obj).length > 1) {
                    var unique = 0;
                    $('ul li', obj).each(function()
                    {
                        $(this).attr('rel', o.shortName + unique);
                        $('#' + o.shortName + 'Navigation').append('<a href="#" rel="' + o.shortName + unique
                                           + '" class="' + o.shortName + 'Button">'
                                           + '<img src="/img/jsButtonLightInactive.png" alt=""></a>');
                        unique++;
                    });
                    $('a:first img', '#' + o.shortName + 'Navigation').attr('src', '/img/jsButton' + o.shade + 'Active.png');
                    $('a.' + o.shortName + 'Button').bind('click', goToImg);
                    intervalHolder = setInterval(rotateImg, o.rotateInterval);
                }
            };
            function rotateImg()
            {
                var current = ($('ul li.show', obj)
                            ?  $('ul li.show', obj)
                            : $('ul li:first', obj));
                var next = ((current.next().length)
                          ? ((current.next().hasClass('show'))
                           ? $('ul li:first', obj)
                           : current.next())
                          : $('ul li:first', obj));
                var relCurrent = current.attr('rel');
                var relNext = next.attr('rel');
                $('a.' + o.shortName + 'Button[rel="' + relCurrent + '"] img').attr('src', '/img/jsButton' + o.shade + 'Inactive.png');
                $('a.' + o.shortName + 'Button[rel="' + relNext + '"] img').attr('src', '/img/jsButton' + o.shade + 'Active.png');
                next.css({opacity:0.0})
                    .addClass('show')
                    .animate({opacity:1.0}, 1000);
                current.animate({opacity:0.0}, 1000)
                       .removeClass('show');
            };
            function goToImg()
            {
                clearInterval(intervalHolder);
                var clickRel = $(this).attr('rel');
                $('ul li.show', obj).animate({opacity:0.0}, 1000)
                                                .removeClass('show');
                $('ul li[rel="' + clickRel + '"]', obj).css({opacity:0.0})
                                                                   .addClass('show')
                                                                   .animate({opacity:1.0}, 1000);
                $('a.' + o.shortName + 'Button img').attr('src', '/img/jsButton' + o.shade + 'Inactive.png');
                $('a.' + o.shortName + 'Button[rel="' + clickRel + '"] img').attr('src', '/img/jsButton' + o.shade + 'Active.png');
                intervalHolder = setInterval(resetInterval, o.rotateIntervalClick);
                return false;
            };
            function resetInterval()
            {
                clearInterval(intervalHolder);
                intervalHolder = setInterval(rotateImg, o.rotateInterval);
                rotateImg();
            }
        }
    });
})(jQuery);
