var MiniCarousel = function(id, cfg) {
  this.element = document.getElementById(id);
  this.currentIndex = cfg.firstVisible ? cfg.firstVisible : 0;
  this.numVisible = cfg.numVisible ? cfg.numVisible : 1;
  this.size = cfg.size;
  this.scrollInc = cfg.scrollInc ? cfg.scrollInc : 1;
  this.animationSpeed = cfg.animationSpeed ? cfg.animationSpeed : 0.5;
  this.animate = cfg.animate ? cfg.animate : true;
  this.animatePhysics = cfg.animatePhysics ? cfg.animatePhysics : false;
  this.itemSize = cfg.itemSize;
  this._isAnimating = false;
  this.loadItemsHandler = cfg.loadItemsHandler;
  this._loadedItems = 0;
  this.prevButtonStateHandler = cfg.prevButtonStateHandler;
  this.nextButtonStateHandler = cfg.nextButtonStateHandler;
  this.onIndexChange = cfg.onIndexChange;
  
  this.omn_page = document.getElementById('omniturePage').value;

  this.init = function() {	  
    if(this.loadItemsHandler) {		
      this.loadItemsHandler(0, this.numVisible);
      this._loadedItems = this.numVisible;
    }	
    this.moveTo(this.currentIndex);
  };

  this.moveTo = function(index) {	  
    if(this._prevent(index)) { return; }
    this._isAnimating = true;
    if(this._loadItemsUpTo(index, function() { this.moveTo(index); })) {
      return;
    }		
    this.element.style.left =  this._calculatePosition(index) + "px";	
	/*alert('1')*/
    this._isAnimating = false;
	/*alert('2')*/
    this._updateStatus(index);
	
	/*alert('3')*/
    this._raiseOnIndexChange();
  };

  this.scrollTo = function(index) {
    if(this._prevent(index)) { return; }
    this._isAnimating = true;
    if(this._loadItemsUpTo(index, function() { this.moveTo(index); })) {
      return;
    }
    var element = this.element;
    var my = this;
    var ret = this.animatePhysics ? ( this.currentIndex < index ? -5 : 5 ) : 0;
    var position = this._calculatePosition(index);
    var animation = new YAHOO.util.Anim(element, { left: { to: (position + ret) }}, this.animationSpeed);
    if(this.animatePhysics) {
      animation.onComplete.subscribe(function() {
        var returnAnimation = new YAHOO.util.Anim(element, { left: { to: position }}, 0.2);
        returnAnimation.onComplete.subscribe(function() {
          my._isAnimating = false;
          my._raiseOnIndexChange();
        });
        returnAnimation.animate();
      });
    }
    else {
      animation.onComplete.subscribe(function() {
        my._isAnimating = false;
        my._raiseOnIndexChange();
      });
    }
    animation.animate();
    this._updateStatus(index);
  };

  this.next = function() {
    if(this.animate) {
      this.scrollTo(this.currentIndex + 1);
    }
    else {
      this.moveTo(this.currentIndex + 1);
    }
	
	omniture.SendBeacon("specialoffersoffer_browse", {
		prop1:"abp:"+omn_page+">specialoffersoffer>browse",
		prop17:"abp:"+omn_page+"_specialoffersoffer",
		prop22:"abp:"+omn_page+">specialoffersoffer>browse",
		eVar5:"abp:"+omn_page+">specialoffersoffer>browse",		
		eVar17:"abp:"+omn_page+"_specialoffersoffer"						
	});	
	
  };

  this.prev = function() {	  
    if(this.animate) {
      this.scrollTo(this.currentIndex - 1);
    }
    else {
      this.moveTo(this.currentIndex - 1);
    }

	omniture.SendBeacon("specialoffersoffer_browse", {
		prop1:"abp:"+omn_page+">specialoffersoffer>browse",
		prop17:"abp:"+omn_page+"_specialoffersoffer",
		prop22:"abp:"+omn_page+">specialoffersoffer>browse",
		eVar5:"abp:"+omn_page+">specialoffersoffer>browse",		
		eVar17:"abp:"+omn_page+"_specialoffersoffer"						
	});	
	
  };

  this._calculatePosition = function(index) {
    if(index < 0) {
      index = 0;
    }
    else {
      index = (index + this.numVisible) < this.size ? index : this.size - this.numVisible;
    }	
    this.currentIndex = index;
    return -(this.itemSize * index);
  };

  this._updateStatus = function(index) {
    if(this.prevButtonStateHandler) {
      this.prevButtonStateHandler(index, (index <= 0));
    }
    if(this.nextButtonStateHandler) {
      this.nextButtonStateHandler(index, (index >= (this.size - 1)));
    }
  };

  this._loadItemsUpTo = function(index, callback) {
    if(!this.loadItemsHandler || (index <= this._loadedItems)) {
      return false;
    }
    this._loadedItems = (index + this.numVisible) >= this.size ? this.size - 1 : (index + this.numVisible);
    this.loadItemsHandler(this._loadedItems + 1, this._loadedItems, callback);
    return true;
  };

  this._prevent = function(index) {
    return this._isAnimating || index < 0 || index >= this.size;
  };

  this._raiseOnIndexChange = function() {
    if(this.onIndexChange) {
      this.onIndexChange(this.currentIndex);
    }
  };
}