/**
 * Name: SliderCarousel
 * Author: Jason Gordon
 * Description: Implements a dynamic carousel where the slides scroll instead of
 *              fade in and fade out.
 */
 
SliderCarousel = function(container) {

  var self = this; // We need self because the scope/value of 'this' can change.
  self.rotationInterval = 5000;
  self.container = $(container);
  self.intervals = new Array();
  self.speed = 'medium';

  /**
   * Rotate the carousel from the current slide to the next. If no target
   * slide id is passed in, rotate to the next in line
   */
  self.rotate = function(target){
    var activeId = self.getActiveId();

    // If no target is defined, rotate to the next slide.
    if( target == undefined ){
      target = activeId + 1;

      if( target >= self.container.find('.image').length ) {
        target = 0;
      }
    }

    self.updateControls(target);

    // Scroll the image to the target spot
    var imageOffset = 250; //self.container.find('.images').height();
    var scrollTop = target * imageOffset;
    
    self.container.find('.images').animate({'scrollTop': scrollTop}, self.speed, 'linear', function (){
      self.adjustForIE(target);
    });
  }

  /**
   * Attach the controls to the DOM. Used during class construction
   */
  self.attachControls = function(){
    
    if (self.container == undefined) return;

    // Make links unsuable. We need the links to support browsers w/o javascript
    // tho
    self.container.find('.control .tab a').attr('href', 'javascript:void(0)');

    // Attach rotation to the anchors tabs
    self.container.find('.control .tab').click(function(){
      id = $(this).parent().attr('id').replace('control-', '');
      self.rotate(id);
    }).hover(function(){
      self.pause();
    }, function(){
      self.resume();
    });

    // Pause the carousel when hovering on a tab. Resume after mouse leaves
    self.container.find('.control .summary').hover(function(){
      self.pause();
    }, function(){
      self.resume();
    });
  }

  /**
   * Update the controls after a rotation
   */
  self.updateControls = function(target){
    var activeControl = self.container.find('.active-control');
    var targetControl = self.container.find('#control-' + target);

    // Slide up active control summary and slide down active control tab
    activeControl.find('.summary').slideUp('fast');

    // Slide down the active tab so it is now revealed
    activeControl.find('.tab').slideDown('fast');

    // Slide up target control tab and slide down the target control summary
    targetControl.find('.tab').slideUp('fast');
    targetControl.find('.summary').slideDown('fast', function(){
      activeControl.removeClass('active-control');
      targetControl.addClass('active-control');
    });
  }

  /**
   * Pause the carousel
   */
  self.pause = function (){

    for (i= 0; i < self.intervals.length; i++) {
      window.clearInterval(self.intervals[i]);
    }

    self.intervals = new Array();
  }

  /**
   * Resume rotation
   */
  self.resume = function (){
    
    if (self.rotationInterval) {
      interval = window.setInterval('sliderCarousel.rotate()', self.rotationInterval);
      self.intervals.push(interval);
    }
  }

  /**
   * Get the id of the active slide
   */
  self.getActiveId = function(){
    if( self.container.find('.active-control').attr('id') == undefined ){
      return 0;
    }

    return parseInt(self.container.find('.active-control').attr('id').replace('control-', ''));
  }

  /**
   * Adjust a rotation for IE 6 & 7, which have a weird 3px bug
   */
  self.adjustForIE = function(target){
    return;

    // Only do this in IE 7 & 6
    if (!$.browser.msie) return;
    if (parseInt($.browser.version) > 7) return;


    //if (target < 2) return;

    var images =  self.container.find('.images');

    //target = target-2;
    scrollTop = parseInt($(images).scrollTop()) + (parseInt(target) * 3);
    images.animate({'scrollTop': scrollTop}, 'fast');
  }

  // This is part of the constructor. Weird javascript syntax. What can I say?
  self.attachControls();
  
  self.resume();
}
;
