﻿/*
*  jQuery EZ Rotator
*
*  Iterates through the selected element's children elements 
*  to create a simple content rotator. 
*
*  Sample Usage with Default Options:
*  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"> </script>
*  <script type="text/javascript"> 
*	 	$('ul#rotate').ezrotator({
*	       play: true,
*	       interval: 5000,
*	       speed: "slow",
*	       transition: "fade",
*	       pagination: false,
*	       cssNext: 'ez-next',
*	       cssPrev: 'ez-prev'
*	   });
*  </script> 
* Copyright (c) 2010 Frederick King
*  
*/
(function ($) {
    var ezrotator;
    if (!ezrotator) ezrotator = function () { }
    $.fn.ezrotator = function (options) {
        var defaults = {
            play: true,
            interval: 5000,
            speed: "slow",
            transition: "fade",
            pagination: false,
            cssNext: 'ez-next',
            cssPrev: 'ez-prev'
        }
        var opts = $.extend(defaults, options);
        var elem = $(this);
        var size = elem.children().size();
        var count;
        var controls;
        var next;
        var prev;
        /* 

        */
        ezrotator.init = function () {
            //console.profile();
            elem.children().hide();
            elem.children().eq(0).show();
            count = 0;
            if (opts.play) {
                setInterval(function () {
                    ezrotator.rotate();
                }, opts.interval);
            }
            if ((!opts.play) || (opts.pagination)) {
                ezrotator.addControls();
            }
            //console.profileEnd();
        }
        /* 

        */
        ezrotator.rotate = function () {
            //console.profile();
            switch (opts.transition) {
                case 'fade':
                    elem.children().eq(count).fadeOut(opts.speed);
                    if (count == size - 1) {
                        count = 0;
                    }
                    else {
                        count = count + 1;
                    }
                    elem.children().eq(count).fadeIn(opts.speed);
                    break;
                case 'slide':
                    elem.children().eq(count).animate({
                        left: '-450px',
                        opacity: 0

                    });
                    if (count == size - 1) {
                        prev.hide();
                        count = 0;
                    }
                    else {
                        count = count + 1;
                        next.show();
                        prev.show();
                    }
                    if (count === size - 1) {
                        next.hide();
                    }
                    elem.children().eq(count).show().css({ 'left': '450px' }).animate({
                        opacity: 1,
                        left: '0'
                    });
                    break;
            }
            //console.profileEnd();
        }
        /* 

        */
        ezrotator.rotateBack = function () {
            //console.profile();
            switch (opts.transition) {
                case 'fade':
                    elem.children().eq(count).fadeOut(opts.speed);
                    if (count === 0) {
                        count = size - 1;
                    }
                    else {
                        count = count - 1;
                    }
                    elem.children().eq(count).fadeIn(opts.speed);
                    break;
                case 'slide':

                    elem.children().eq(count).animate({
                        left: '450px',
                        opacity: 0
                    });
                    if (count === 0) {
                        count = size - 1;
                        next.hide();

                    }
                    else {
                        count = count - 1;
                        prev.show();
                        next.show();
                    }
                    if (count === 0) {
                        prev.hide();
                    }
                    elem.children().eq(count).css({ 'left': '-450px' }).animate({
                        opacity: 1,
                        left: '0'
                    });
                    break;
            }
            //console.profileEnd();
        }

        /* 

        */
        ezrotator.addControls = function () {
            //console.profile();
            controls = $('<div id="ez-controls" />');
            prev = $('<a class="' + opts.cssPrev + '">Prev</a>').bind('click', function () {
                //console.log(elem.data('count'));
                ezrotator.rotateBack();
            }).hide();
            next = $('<a class="' + opts.cssNext + '">Next</a>').bind('click', function () {
                //console.log(elem.data('count'));
                ezrotator.rotate();
            });
            var container = elem.parent('div');
            controls.append(prev).append(next).prependTo(container);
            //console.profileEnd();
        }
        /* 

        */
        return ezrotator.init();
    }
})(jQuery);

