/* jquery-rotator 1.0 - jQuery Plugin
 * written by Kenneth Frank
 * http://www.claremontmckenna.edu/ITS/
 * 
 * Built for jQuery Library
 * http://jquery.com
 * 
 * markup example for $("#rotator").rotator();
 *      <div id="rotator_wrapper"> 
 * 		<ul id="rotator">
 *			<li id="photo_1"><img src="./_common/PARENTSbanner.jpg" alt="Photo" /></li>
 *			<li id="photo_2"><img src="./_common/SUMMERbanner.jpg" alt="Photo" /></li>
 *			<li id="photo_3"><img src="./_common/RIMbanner.jpg" alt="Photo" /></li>
 *			<li id="photo_4"><img src="./_common/RISbanner.jpg" alt="Photo" /></li>
 *		</ul>
 *	</div>
 * 
 */

;
(function($){
    $.fn.rotator = function(options){
        var defaults = {
            first_visible:       true,
            first_selector:      'rotator_first',
            prev_selector:       'rotator_prev',
            next_selector:       'rotator_next',
            last_visible:        true,
            last_selector:       'rotator_last',
            play_pause_visible:  true,
            play_pause_selector: '#rotator_play_pause',
            numeric_controls:    true,
            controls_selector:   '#rotator_controls',
            speed:               1000,
            delay:               6000,
            pause:               false
        };
            
        var opts = $.extend({}, defaults, options);
    
        return this.each(function(){
            // Get the current item reference we are working with and cache it.
            var $obj = $(this);
            var $controlsObj = $(opts.controls_selector);
            
            // Rotator function.
            function rotate(currentElement, nextElement) {
                if(currentElement === null){
                    currentElement = $obj.find('li:visible:first');
                }

                // Either the next / first <li>.
                var $next_li = (nextElement ? nextElement : ($(currentElement).next('li').length ? $(currentElement).next('li') : $obj.find('li:first')));

                // If we have opted for numeric controls, update the active number.
                if(opts.numeric_controls === true){
                    // Move the current indicator.
                    $controlsObj.find('a.current').removeClass('current');
                    $controlsObj.find('li#' + $next_li.attr('id') + ' a').addClass('current');
                }

                // Continue.
                function doIt() {
                    rotate($next_li, null);
                };

                // Fade out <li>.
                $(currentElement).fadeOut(opts.speed);

                // Show next <li>.
                $($next_li).fadeIn(opts.speed, function() {
                    // Only schedule the next one if we are set to play.
                    if(opts.pause === false){
                        // Slight delay.
                        $obj.timer = setTimeout(doIt, opts.delay);
                    }
                });
            };

            function SetPause(mode) {
                switch(mode){
                    case 'PAUSE' :
                        if($obj.timer){
                        clearTimeout($obj.timer);
                    }
                    $(opts.play_pause_selector).html('PLAY');
                        opts.pause = true;
                        break;
                    case 'PLAY'  :
                        $(opts.play_pause_selector).html('PAUSE');
                        opts.pause = false;
                        rotate($obj.find('li:visible:first'), null);
                        break;
                    default      :
                        SetPause('PLAY');
                }
            };

            ////////////////////////////////////////////////////////////////////
            // Initialize the plugin for this instance.                       //
            ////////////////////////////////////////////////////////////////////
            if(opts.numeric_controls === false){
                $controlsObj.hide();
            }else{
                // Numeric controls are requested, generate the html and put it in the context.
                $obj.find('li').each(function(i, liObj){
                    $(document.createElement("li"))
                    .attr('id', $(liObj).attr('id'))
                    .html('<a rel='+ i +' href=\"javascript:void(0)\">' + (i+1) + '</a>')
                    .appendTo($controlsObj)
                    .click(function(){
                        SetPause('PAUSE');
                        rotate(null, $(liObj)); // Go to the first item for now.
                    });
                });
            }

            if(opts.first_visible === true){
                $(document.createElement("a"))
                .attr('id', opts.first_selector)
                .attr('href', "javascript:void(0)")
                .text("|<")
                .appendTo('#rotator_wrapper')
                .click(function(){
                   SetPause('PAUSE');
                   rotate(null, $obj.find('li:first'));
                });
            }

            $(document.createElement("a"))
            .attr('id', opts.prev_selector)
            .attr('href', "javascript:void(0)")
            .text("<")
            .appendTo('#rotator_wrapper')
            .click(function(){
               SetPause('PAUSE');
               var list = $obj.find('li');
               var curr = $obj.find('li:visible:first');
               if(list.index(curr) == 0){
                    // If we are already at the first item, wrap it around to the last.
                    rotate(null, $obj.find('li:last'));
               }else{
                    rotate(null, $obj.find('li:visible:first').prev('li'));
               }
            });

            $(document.createElement("a"))
            .attr('id', opts.next_selector)
            .attr('href', "javascript:void(0)")
            .text(">")
            .appendTo('#rotator_wrapper')
            .click(function(){
               SetPause('PAUSE');
               var list = $obj.find('li');
               var curr = $obj.find('li:visible:first');
               var last = $obj.find('li:last');
               if(list.index(curr) == list.index(last)){
                    // If we are already at the last item, wrap it around to the first.
                    rotate(null, $obj.find('li:first'));
               }else{
                   rotate(null, $obj.find('li:visible:first').next('li'));
               }
            });

            if(opts.last_visible === true){
                $(document.createElement("a"))
                .attr('id', opts.last_selector)
                .attr('href', "javascript:void(0)")
                .text(">|")
                .appendTo('#rotator_wrapper')
                .click(function(){
                   SetPause('PAUSE');
                   rotate(null, $obj.find('li:last'));
                });
            }
            
            if(opts.play_pause_visible === true){
                $(document.createElement("a"))
                .attr('id', 'rotator_play_pause')
                .attr('href', "#")
                .text('PAUSE')
                .appendTo('#rotator_wrapper')
                    .click(function(){
                    // What does the button say?
                    if ($(this).html() === 'PAUSE') {
                        SetPause('PAUSE');
                    } else {
                        SetPause('PLAY');
                    }

                    this.blur();
                    return false;
                });                    
            }

            // Hide all but first <li>.
            $obj.find('li:first').show();

            if(opts.pause === false){
                // Wait for page load.
                $(window).load(function() {
                    // Begin rotation.
                    if($.browser.msie){
                        //  IE has a timer bug so this is a little fix, just send 0 seconds so it doesn't double it.'
                        $obj.timer = setTimeout(rotate, 0, null);
                    }else{
                        $obj.timer = setTimeout(rotate, opts.delay, $obj.find('li:visible:first'));
                    }
                });            
            }
        });
    };
    
})(jQuery);

