/**
 * Rotator
 * Apply to a container element, and add class active to first child
 * @example
 *  <div class="mySlideContainer">
 *      <img src="img1.jpg" class="active" />
 *      <img src="img2.jpg" />
 *      <img src="img3.jpg" />
 *  </div>
 *
 *  <script type="text/javascript">
 *      $('.mySlideContainer').slideShow();
 *  </script>
 * 
 * @author Tommy Johansen
 * 
 */
(function($){
  $.fn.slideShow = function( options ) {

    var settings = {
        'targetTag': 'img',
        'rotateSpeed': 3000, //How long to wait between each image before starting the transition to the next image.
        'fadeSpeed': 1500, //How long time to spend on fading / transitioning between current and next image.
        'fadeActive': false //Fade out the active image when fading in the new? Usually needed for transparrent images.
    };
    
    if (options) { $.extend( settings, options ); }
      
    var container = $(this);
    var children = container.children(settings.targetTag);

    rotate = function() {

        var active = container.children('.active');
        if (active.length == 0) { active = children.last(); }
        var next = (active.next(settings.targetTag).length) ? active.next(settings.targetTag) : children.first();
    

        active.addClass('last-active');
        active.css('z-index', 1);
        
        if(jQuery.browser.msie && jQuery.browser.version <= 8) {

          //next.css({opacity: 0.0, 'z-index': 2});
          next.addClass('active');
  
          if(settings.fadeActive) {
              active.css('visibility', 'hidden')
          }
          
          next.css('visibility', 'visible');
          active.removeClass('active last-active');
          
        } else {

          next.css({opacity: 0.0, 'z-index': 2});
          next.addClass('active');
  
          if(settings.fadeActive) {
              active.animate({opacity: 0}, settings.fadeSpeed);                 
          }
          
          next.animate({opacity: 1.0}, settings.fadeSpeed, function() {
              active.removeClass('active last-active');
          });
          
        }
        

    }

    setInterval(rotate, settings.rotateSpeed );
    
  };
})(jQuery);


