var TrodonCarousel = Class.create();

TrodonCarousel.prototype = {

	initialize: function(args) {
		this.carousel = args.carousel;
		this.nextButton = args.nextButton;
		this.previousButton = args.previousButton;
		this.itemsPerPage = args.itemsPerPage;

		this.page = 0;
		
		this.carouselRow = $(this.carousel).down('.carousel-row');
		
		// calculate width of carousel row
		var anItem = $(this.carousel).down('.carousel-item');		
		
		/*var itemWidth = parseInt($(anItem).getWidth()) +
			parseInt($(anItem).getStyle('margin-left')) +
			parseInt($(anItem).getStyle('margin-right')) +		
			parseInt($(anItem).getStyle('padding-left')) +
			parseInt($(anItem).getStyle('padding-right')) +		
			parseInt($(anItem).getStyle('border-left-width')) +
			parseInt($(anItem).getStyle('border-right-width'))
		;*/
		
		this.itemWidth = 174;
		this.numItems = $(this.carousel).select('.carousel-item').length;
		this.slideWidth = this.itemWidth * this.itemsPerPage;

		this.firstItem = $(this.carousel).down('.first-item');
		
		$(this.nextButton).observe('click', this.next.bind(this));
		$(this.previousButton).observe('click', this.previous.bind(this));
		
		$(this.previousButton).addClassName('disabled');
	},
	
	slide: function(page) {
		//var currentLeft = parseInt($(this.carouselRow).getStyle('left'));
		
		//alert(this.slideWidth);
		
		//var newLeft = -(page * this.slideWidth);
		var newLeft = -(page * this.itemWidth);
		
		//alert('left:' + newLeft + 'px');
		
		$(this.carouselRow).morph({'left': newLeft + 'px'});
	},

	next: function() {
		if (this.page >= this.numItems-this.itemsPerPage) {
			// ta frŒn bšrjan och lŠgg pŒ
			var clone = $(this.carousel).down('.carousel-item', this.page-this.itemsPerPage-1).cloneNode(true);
			//var clone = $(this.firstItem).next(this.page-this.itemsPerPage-2).cloneNode(true);
			
			//$(clone).removeClassName('first-item');
			$(this.carousel).down('.carousel-row').insert($(clone));
			//Element.insert($(this.carousel).down('.carousel-row'), { 'top': clone });
		}
		
		this.page++;
		this.slide(this.page);
		$(this.previousButton).removeClassName('disabled');
	},

	previous: function() {
		/*if (this.page <= 0) {
			//var lastItem = $(this.carousel).down('.carousel-item', this.numItems).cloneNode(true);
			var clone = $(this.firstItem).next(this.numItems-2).cloneNode(true);
			$(clone).removeClassName('first-item');
			//$(this.carousel).down('.carousel-row').insert($(clone), 'top');
			Element.insert($(this.firstItem), { 'before': clone });
		}*/
		
		if (this.page > 0) {
			this.page--;
			this.slide(this.page);
		}
		
		if (this.page <= 0)  {
			$(this.previousButton).addClassName('disabled');
		}
	}

};
Effect.Scroll = Class.create();
Object.extend(Object.extend(Effect.Scroll.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    var options = Object.extend({
      x:    0,
      y:    0,
      mode: 'absolute'
    } , arguments[1] || {}  );
    this.start(options);
  },
  setup: function() {
    if (this.options.continuous && !this.element._ext ) {
      this.element.cleanWhitespace();
      this.element._ext=true;
      this.element.appendChild(this.element.firstChild);
    }
   
    this.originalLeft=this.element.scrollLeft;
    this.originalTop=this.element.scrollTop;
   
    if(this.options.mode == 'absolute') {
      this.options.x -= this.originalLeft;
      this.options.y -= this.originalTop;
    } else {
   
    }
  },
  update: function(position) {   
    this.element.scrollLeft = this.options.x * position + this.originalLeft;
    this.element.scrollTop  = this.options.y * position + this.originalTop;
  }
});
