/*
 * anm8 v0.1 by bluntworks 2011 | dwtfywi license
 */
(function() {
  
  //Fader
  $.fn.fader = function(options) { 
    var o = {
            duration: 3000
          , indx: 0
          , holdFor: 8000
          , nav: true
        }

      , self = $(this)
      , items = self.children().clone()
      , plateA
      , plateB
      , plate
      , len = items.length -1
      , theLoop
      , under   = { 'z-index': 10 }
      , over    = { 'z-index': 100, 'opacity': 0 } 
      , overOn  = { 'z-index': 100 } 
      , opac    = { 'opacity': 0 }

    var 
    init = function() {
      self.children().remove();
      self.append('<div id="pa" class="PlateA"></div><div id="pb" class="PlateB"></div>');

      plateA = self.find('.PlateA'); 
      plateB = self.find('.PlateB');
      self.children().css( { position: 'absolute', top: 0, left:0 } ); 

      plateA.css(under);
      plateB.css(overOn);

      items.each(function(i, el) { $(el).css('display','block') } );
      
    },

    swapPlates = function(item, toBot, toTop) { 
      toTop.children().remove();
      toTop.css(over);
      toTop.append(item);
       
      plate = toTop;

      toBot.css(under);   
    },

    loopLogic = function(that, i, loop) { 
      (plate.hasClass('PlateA'))
        ? swapPlates(that[i], plateA, plateB)
        : swapPlates(that[i], plateB, plateA);

      plate
        .filter(':not(:animated)')
        .animate({ opacity: 1 }, o.duration, function() { loop(); } );
    }, 

    override = function(v) { 
      theLoop.override(v);
    }
    

    if(typeof(options) == 'object')
      $.extend(o, options);
   
    
    //Loop It
    theLoop = items.loopIt( { 
        indx: o.indx
      , holdfor: o.holdFor
      , max: len
      , start: function(i, loop) { 
          this.trigger('tictok', [i,'start']);

          plateB.append(this[i]);
          plate = plateB;
          loop();
        }

      , next: function(i, loop) { 
          this.trigger('tictok', [i,'next']);
          loopLogic(this, i, loop);
        }

      , back: function(i, loop) { 
          this.trigger('tictok', [i, 'back']);
          loopLogic(this, i, loop);
      }

      , reset: function(i, loop) { 
          this.trigger('tictok', [i, 'reset']);

          plateA.css(under).children().remove();
          plateB.css(overOn).children().remove();
          plateB.append(this[i]);
          plate = plateB;
          loop();
      }

    });

    init();
    theLoop.start();

    return { 
      self:this, 
      override: override,
      items: items
    }; 

  }

  $.fn.loopIt = function(options) { 
    var o  = { 
       indx: 0
    ,  holdFor: 8000
    ,  max: 5
    ,  start: function(i, loop) { loop(); }
    ,  next:  null
    ,  back: function() { return false; }
    ,  reset: function() { return false; }
    ,  chck: function( i, m ) { return ( i < m ) ? true : false; }
    };

    if(typeof(options) == 'object')
      $.extend(o, options);

    $.log('max: '+o.max);

    var self= $(this)
      , indx = o.indx
      , max  = o.max
      , toId

    var loop= function() { 
      (o.chck(indx, o.max)) ? indx++  : indx = o.indx;
      (indx ==  o.indx) ? que(o.back) : que(o.next);
    };

    var que= function(fn) { 
      toId = setTimeout(function() { fn.call(self, indx, loop) } , o.holdFor);  
    };

    var start= function() { 
      o.start.call(self, indx, loop);
    };

    var override= function(v) { 
      clearTimeout(toId);
      indx = v;
      $(this).stop();
      o.reset.call(self, indx, loop);
    };

    return { 
      override : override,
      start: start
    }
  };

  //General Helpers
  $.log = function(m) { 
    if(window.console) 
       console.log(m);
  };

  $.num = function (v) {
      return parseInt(v, 10) || 0;
  };

})(jQuery);

