/*
 * jQuery newsTicker plugin
 *
 * Released: 2009-11-23
 * Version: 1.0
 *
 * Copyright (c) 2009 Fabian de Rijk, Total Active Media
 * Dual licensed under the MIT and GPL licenses.
 * http://docs.jquery.com/License
 */
/*
 * heavily modified by assmann@skygate.de 20100323 
 */
(function($){
     $.fn.extend({ 
         newsTicker: function(options) {
            /*
             * Usage:
             * $('#divname').newsTicker();
             * Available options:
             *     showTime: The time a single news item is showed in milliseconds, defaults to 5000
             *     childDivClass: The classname of the divs where the actual news is in, defaults to tickerText
             *     activeClass: The class added to the shown news item, defaults to activeTicker
             *     effectTime: The time in milliseconds it takes to do each step in the effect, defaults to 500
             *     
             * Example with full options
             * $('#divname').newsTicker({
             *     showTime: 10000,
             *     childDivClass: 'childClass',
             *     activeClass: 'activeClass',
             *     effectTime: 1000
             * });
             */
            var defaults = {
                showTime: 5000,
                childDivClass: 'tickerText',
                activeClass: 'activeTicker',
                effectTime: 500
            }
            var options =  $.extend(defaults, options);
            var obj = $(this);
            var childLength = obj.children('.'+options.childDivClass).length;
            var children = obj.children('.'+options.childDivClass);
            var firstChild = obj.children('.'+options.childDivClass+':first');
            var t;
            
            ///////////////////////////////////////////////////////////////
            // states for the animation
            var runningKey='running'; 
            obj.data(runningKey,true); // true=running, false=stopped
            var animationKey='animation'; 
            obj.data(animationKey,true); // true=fadeIn, false=fadeOut
            
            
            ///////////////////////////////////////////////////////////////
            // pause / resume on click
            obj.click(function() {
                
                var active = obj.children('.'+options.activeClass);
                var animated = obj.children(':animated');
                var next=animated.next();
                if(next.length == 0){
                    next = firstChild;
                }
                // pause or resume
                if(obj.data(runningKey)){
                    
                    // stop and show at full opacity
                    animated.stop(true,true);
                    animated.show();
                    
                    // if we are in fadeIn animation state, 
                    // we need to undo the move of the active class
                    if(obj.data(animationKey)){
                        animated.addClass(options.activeClass);
                        next.removeClass(options.activeClass);
                    }
                    clearTimeout(t);
                }else{
                    startTimer();
                }
                
                // flip state
                obj.data(runningKey,!obj.data(runningKey));
                
                // do nothing else
                return false;
              });
              startTimer();
              
            function startTimer(){
                var delay = options.showTime;
                t = setTimeout(function() {
                    animateTicker();
                }, delay);
            }
            
            function animateTicker() {
                // determine active and its next
                var active = obj.children('.'+options.activeClass);
                if(active.length == 0) {
                    active = firstChild;
                    active.addClass(options.activeClass);
                }
                var next = active.next();
                if(next.length == 0) {
                    next = firstChild;
                }
                
                // hide all except active
                children.each(function() {
                    if(!$(this).hasClass(options.activeClass)) {
                        $(this).hide();
                    }
                });
                
                // fade in
                if(obj.data(animationKey)){ 
                    active.fadeIn(options.effectTime, function() {
                        obj.data(animationKey,false);
                        clearTimeout(t);
                        startTimer();
                    });
                // fade out
                }else{ 
                    active.removeClass(options.activeClass);
                    active.fadeOut(options.effectTime, function() {
                        obj.data(animationKey,true); 
                        next.addClass(options.activeClass);
                        clearTimeout(t);
                        startTimer();
                    });
                }
            }
      }
    });
    
})(jQuery);
