
Card = function(options) {

  this.options = jQuery.extend({
    id:0,
    count:0,
    width:0,
    height:0,
    scrollSpeed:1000,
    gap:40,
    bottomPadding:60,
    moveControls:1
  },options);
  
  this.scrollPositions = [];

}

Card.prototype.init = function()
{
  var self = this;
  
  //hide and absolute
  $('#cards .wrapper').css('position','absolute');
  $('#cards .card').css('position','absolute');
  
  if(this.options.moveControls)
  {
    $('#cards .card .controls').css('position','absolute');
    $('#cards .card .controls').css('bottom','0px');
  }
  else
  {
    this.options.bottomPadding = 0;
  }
  
  $('#cards .card .controls').show();
  
  //fix widths
  if(this.options.width == 0)
  {
    this.options.width = $('#cards').width();
  }
  

  for(var ii=0; ii<= this.options.count; ii++)
  {
    $('#card'+ii).show();
    
    if(this.options.height == 0)
    {
      if($('#card'+ii).height() > this.options.height)
      {
        this.options.height = $('#card'+ii).height();
      }
    }
    
    $(".controls p.prev a",$('#card'+ii)).bind("click", function(e){
      self.moveTo(parseInt($(this).attr('class'))-1);
    });
    $(".controls p.next a",$('#card'+ii)).bind("click", function(e){
      self.moveTo(parseInt($(this).attr('class'))+1);
    });
    
    if(ii > 0)
    {
      $('#card'+ii).css('left',
        (this.options.width*ii)+(this.options.gap*ii)
      );
    }
  }
  
  //console.log(this.options.width);
  //console.log(this.options.height);
  
  var height = this.options.height + this.options.bottomPadding;
  $('#cards').css('height',height);
  $('#cards .wrapper').css('height',height);
  $('#cards .wrapper .card').css('height',height);
  
  //Animate active
  if($('.stages.animated'))
  {
    this.animateActive();
  }
}


Card.prototype.animateActive = function()
{
  $('.stage.active').animate({
    paddingTop: '-0px'
  });
  
  $('.stage:not([class~=active])').animate({
    paddingTop: '10px'
  });

}


Card.prototype.moveTo = function(pos)
{
  var scrollSpeed = this.options.scrollSpeed;
  var left = (this.options.width*pos)+(this.options.gap*pos);
  $('#cards .wrapper').animate({ left:"-"+left+"px" }, scrollSpeed);
  
  //Check if slide class exists
  if($('.stage'))
  {
    $('.stage').removeClass('active');
    $('#stage'+pos).addClass('active');
  }
  
  //Animate active
  if($('.stages.animated'))
  {
    this.animateActive();
  }
  
  if(this.scrollPositions[pos])
  {
    $('.stageScroll').animate({
      backgroundPosition: this.scrollPositions[pos][1]+"px "+this.scrollPositions[pos][0]+"px"
    });
  }
}


Card.prototype.setScrollPositions = function(positions)
{
  this.scrollPositions = positions;
}

