/* Copyright (c) 2008 Gabe Antypas (gabe@mezzagrille.com)
 * MIT (http://www.opensource.org/licenses/mit-license.php) licensed.
 *
 * Title: Rotating Elements
 * Version: 1.0
 *
 * Requires:
 *   jQuery 1.2+
 */

(function($) {
    jQuery.fn.mezza_rotation = function(options){
        
        // default values
        var defaults = {
            r_el_name: "hours_",        //This defines the base id of the elemnts that are rotating ex.) if id is hours_1, base id value is hours_.
            num_elements: 3,            //This defines total number of elements to rotate through.
            fout_speed: 300,            //Fade out speed.
            fin_speed: 400,             //Fade in speed.
            autoplay_speed: 5000        //This defines the amount of time before the next slide loads.
        };
        //--

        //jQuery extend
        var options = $.extend(defaults, options);
        //--
        
        return this.each(function() {  
         
            //init - below is initialized on page load.
            //This is the first element out of the elements to rotate through.   
            var r_el = $(this); 

            //Internal Global Variables
            var cur_elem = 1;
            var timer;
            //--
            
            //Start Element Rotation
            r_el.fadeIn(options.fin_speed);
            var apMethod = "$('#" + options.r_el_name + cur_elem + "').trigger('click', '1');"; 
            timer = window.setInterval(apMethod, options.autoplay_speed);
            //--
            // --------------
            
            //Events            
            $('#' + options.r_el_name + cur_elem).click(function (e, auto_triggered) {
                if(!auto_triggered) {clearInterval(timer)}; //stops auto play if triggered from non time interval call
                cur_elem++;
                if(cur_elem > options.num_elements) {
                  cur_elem = 1;
                }
                changeElem();
            });
            
            //the below events must manually be added util I update this plugin
            //the events allow for the user to click a different element 
            //and move to the next one
            $('#' + options.r_el_name + "2").click(function () {
                clearInterval(timer);
                $('#' + options.r_el_name + "1").click(); 
            });
            
            $('#' + options.r_el_name + "3").click(function () {
                clearInterval(timer);
                $('#' + options.r_el_name + "1").click(); 
            });
            //--

            // --------------

            //Methods
            var autoPlay = function(method, speed) {
                timer = window.setInterval(method, speed);
            };
            
            var changeElem = function(next_elem){
                if(next_elem){ cur_elem = next_elem;} 
                r_el.fadeOut(options.fout_speed, function(){
                    r_el = $("#" + options.r_el_name + cur_elem);
                    r_el.fadeIn(options.fin_speed);
                });
            };
            // --------------
        });
    }; // end : jQuery.fn.mezza_rotation
})(jQuery);
