/*
DW-Rotator - JQUERY PLUGIN
PLUGIN Author - d3sign-worx
PLUGIN URL - http://www.d3sign-worx.com
VERSION - 0.1

*/
(function($) {
    $.fn.extend({
        rotate: function(options) {
            var defaults = {
                autoSlide: true, //Auto slide
                time: 3000, // time between slides
                prevButton: [], //any buttons for previous (IDS)
                nextButton: [], //any buttons for next (IDS)
                showDesc: false, // are we showing title attributes
                descHolder: '', // The holder to show description (ID)
                descAttribute: 'title' // what attribute holds the  description
            };


            var options = $.extend(defaults, options);
            var plug = $(this);
          //  alert(plug.attr('id'));
            return this.each(function() {
                var index = 0;
                var intervalID;

                var container = $(this);

                var count = $(this).children().size();
                $(container).children().each(function() {
                    $(this).css('display', 'none');
                });
                $(container).children().eq(0).css('display', 'block');
                $(container).children().each(function() {

                    var obj = $(this).children()[index];

                    $(this).mouseover(function() {
                        if (options.showDesc == true) {
                            $(options.descHolder).html($(this).attr(options.descAttribute)).fadeIn();
                        }
                        if (options.autoSlide == true) {
                            window.clearInterval(intervalID);
                        }
                    });
                    $(this).mouseout(function() {
                        $(options.descHolder).fadeOut();
                        if (options.autoSlide == true) {
                            intervalID = window.setInterval(autoRotate, options.time);
                        }
                    });

                }); // kid cont each

                $(options.prevButton).click(function() {
                    window.clearInterval(intervalID);
                    var prev;
                    if (index == 0) {
                        prev = count - 1;
                    } else {
                        prev = index - 1;
                    }
               
                    $(container).children().eq(index).fadeOut('slow', function() {
                        $(container).children().eq(prev).fadeIn('slow');
                        index = prev;
                    }); // fadeout func                           
                }); //options next click

                $(options.nextButton).click(function() {
                    window.clearInterval(intervalID);
                    var next = index + 1;
                    if (next > count - 1) {
                        next = 0;
                    }
                    $(container).children().eq(index).fadeOut('slow', function() {
                        $(container).children().eq(next).fadeIn('slow');
                        index = next;
                    }); // fadeout func                           
                }); //options next click

                function autoRotate() {
                    var next = index + 1;
                    if (next > count - 1) {
                        next = 0;
                    }
                    $(container).children().eq(index).fadeOut('slow', function() {
                        $(container).children().eq(next).fadeIn('slow');
                        index = next;
                    }); // fadeout func
                }
                if (options.autoSlide == true) {
                    intervalID = window.setInterval(autoRotate, options.time);
                }

            }); //this each

        }

    })
})(jQuery);
// End of plugin
