
var featureSet = new Object;
var featureSetSm = new Object;
var featureCount;
var nextCount;
var progressCount;
var maxProgressCount = 9;
var loadedFeatures = 0;
var pt;
var running = 1;

var frontFeatures = {
  
  /*
   * firstCall
   *
   * Starts the Play
   * Preps the DOM elements
   *
   */
  firstCall : function(){
    
    frontFeatures.rotationPlay();
    
    $(featureSet).not(':eq(0)').css('opacity', 0).removeClass('hidden');   
    // $(featureSetSm).attr('href', '#');
    $(featureSetSm).eq(0).parent('li').addClass('active');
  },
  
  /*
   * rotationPlay
   *
   * Our main counter
   * has just a little logic to figure out 'what' gets fired 'when'
   *
   */
  rotationPlay : function(){ 
    
    if(progressCount == undefined || progressCount == maxProgressCount){
      progressCount = 0;
      frontFeatures.rotationC();
      frontFeatures.UIDelegate();
      
    } else {
      progressCount += 1;
    }  
    if(running == 1) {
      pt = setTimeout(frontFeatures.rotationPlay, 650);
    }
    
  },
  
  /*
   * rotationC
   *
   * 'gets' and 'sets' which feature gets focus
   *
   */
  rotationC : function(){ 
    if(featureCount == undefined){
      featureCount = 0;
    } else {
      featureCount = (featureCount+1 == featureSet.length) ? 0 : featureCount+1;
    }
    nextCount = (featureCount+1 == featureSet.length) ? 0 : featureCount+1;
    
  },
  
  /*
   * UIDelegate
   *
   * fires most of the UI related stuffs
   *
   */
  UIDelegate : function(){        

      if($('#frf-lg .frf-cur-img').length == 0){
        $(featureSet).eq(featureCount).addClass('frf-cur-img').css('z-index', 99);
      } else {
        
        $(featureSet).eq(featureCount).css('z-index', 100);
        $(featureSet).eq(featureCount).animate({
          opacity:1
        }, 200, function(){
          $('#frf-lg a.frf-cur-img').css({'z-index': 0, 'opacity': 0}).removeClass('frf-cur-img');
          $(this).addClass('frf-cur-img').css('z-index', 99);
          frontFeatures.UIActions();
        });
      }
    
  },
  
  /*
   * UIActions()
   *
   * animates out / removes / inserts info text
   * then pause the timer
   * animate in newly populated dl
   * fade in the appropriate .frf-ui and restart our timer
   */
  UIActions : function(){   
    // console.log('inside uiactiions');
    $('#frf-sm li.active').animate({
      opacity:1
    }, 200, function(){
      $(this).removeClass('active');
    });
      
      frontFeatures.rotationPause();
      // frontFeatures.unBinds();
      
      $("#frf-sm li:eq("+featureCount+")").animate({
        opacity:1
      }, 200,function(){
        // debug.log('running in UIshow '+running);
        if(running !=3) running = 1;
        
        // put some logic here to confirm that we are ready to unpause
        frontFeatures.rotationPlay();
        // frontFeatures.binds();
        
        $(this).addClass('active');
      });

      progressCount = 0;

  },
  
  
  /*
   * rotationPause
   *
   * set running to 0 if we're 'paused'
   *
   */
  rotationPause : function(){
    
    running = 0;
    clearTimeout(pt);
  },
  unBinds : function(){
    $(featureSetSm).unbind('.frf');
  },
  binds : function(){
    $(featureSetSm).bind('click.frf.update', function(e){
      li = $(e.target).parent('li');
      clicked = $('#frf-sm li').index(li);
      
      if($(e.target).parent('li').hasClass('active')){
        // console.log('dont do anything');
      } else {
        frontFeatures.rotationPause();
        
        progressCount = maxProgressCount;
        featureCount = $('#frf-sm li').index(li)-1;
        
        frontFeatures.rotationPlay();
      }
      return false;
    });
    
  }
  

}


$(window).load(function(){
  
  featureSet = $('#frf-lg a');
  featureSetSm = $('#frf-sm li a');

  frontFeatures.firstCall();
  // frontFeatures.binds();
  
  
});
