/**
 * jQuery flashing plugin
 * @name flashing.js
 * @author Maxim Antonov http://www.maxantonov.name/
 * @version 0.1
 * $Revision: 412 $
 * $Author: idler $
 * $Date: 2009-02-26 18:12:20 +0300 (Чтв, 26 Фев 2009) $
 * @category jQuery plugin
 * @copyright (c) 2008 Maxim Antonov (www.maxantonov.name)
 * @license GPL
 */

/**
 * USAGE: 
 * <div rel="1.jpg,2.jpg,3.jpg,4.jpg,5.jpg,6.jpg"></div>
 *
 * --------------------------------------------------------------------------
 *
 *  jQuery(window).ready(function()
 *  {
 *      jQuery('div[rel]').flashing({timeout:2789,fadeSpeed:1600});
 *  });
 */


(function($){
  
  $.fn.flashing = function(options){
  var defaults = {timeout:1800,fadeSpeed:1000};
  var options = $.extend(defaults, options); 
  var ind=1;
    this.each(function(){
      this.images = this.getAttribute('rel').split(',');
      this.counter = 0;
      this.current = this.images[this.counter];

      var self = this;
      this.flash=function(){
        self.current = self.images[self.counter];

       _fadeChange(self,self.current);
        self.counter ++;
          if(self.images.length <= self.counter) self.counter=0;
          setTimeout(self.flash,options.timeout);
      }
      self.flash();
      
      function _fadeChange(el,imgPath){
         $(el).find('.veryOld').remove();
         $(el).find('.old').removeClass('old').addClass('veryOld');
         $(el).append('<img src="'+self.current+'" alt="" class="addition"/>');
         $(el).find('.addition').hide().css({position:'absolute','z-index':(ind++)});
         $(el).find('.addition').fadeIn(options.fadeSpeed,function(){$(this).removeClass('addition').addClass('old'); });
      }
      
    });

  }
  
})(jQuery);// Call and execute the function immediately passing the jQuery object
