/*  Caroussel version 1.0
 *  (c) 2009 Oblik Software - http://www.obliksoftware.com
 *  created by: martin.plante@obliksoftware.com
 *
 *  Require Prototype Library - http://www.prototypejs.org/
 *
 *--------------------------------------------------------------------------*/
var caroussel = Class.create();

caroussel.prototype = {
	
	m_carousselDiv: null,
	m_itemHeight: null,
	m_itemWidth: null,
	m_visibleHeight: null,
	m_visibleItems: 2,
	m_items: null,
	m_itemsCount: null,
	m_index: 0,
	m_pe: null,
	m_transitionPe: null,
	m_transitionDuration: 0.05,
	m_duration: 3,
	m_scrollTarget: 0,
	m_currentScroll: 0,
	
	initialize: function(carousselId, itemHeight, itemWidth, visibleItemsCount) {
		
		this.m_carousselDiv = $(carousselId);
		this.m_itemHeight = itemHeight;
		this.m_itemWidth = itemWidth;
		this.m_visibleItems = visibleItemsCount;
		
		this.m_items = this.m_carousselDiv.getElementsByTagName("div");
		this.m_itemsCount = this.m_items.length;
		
		if (this.m_itemsCount <= this.m_visibleItems) return;
		
		var bottomClones = new Array(this.m_visibleItems);
		var topClones = new Array(this.m_visibleItems);
		
		/* Duplicate head and tail of the newsitem list */
		for (var i = 0; i < this.m_visibleItems; i++) {
			bottomClones[i] = this.m_items[this.m_itemsCount - 1 - i].cloneNode(true);
			topClones[i] = this.m_items[i].cloneNode(true);
		}
		/* Grow the list */
		for (var i = 0; i < this.m_visibleItems; i++) {
			this.m_carousselDiv.insertBefore(bottomClones[i], this.m_items[0]);
			this.m_carousselDiv.appendChild(topClones[i]);
		}
	
		this.m_visibleHeight = (this.m_visibleItems * this.m_itemHeight);
		this.m_carousselDiv.style.height = this.m_visibleHeight + 'px';
		this.m_carousselDiv.style.width = this.m_itemWidth + 'px';
		this.m_carousselDiv.style.overflow = 'hidden';
		this.m_index = 0;
		
		this.m_currentScroll = (this.m_index * this.m_itemHeight) + (this.m_visibleItems * this.m_itemHeight);
		this.m_scrollTarget = this.m_currentScroll;
		this.m_carousselDiv.scrollTop = this.m_currentScroll;
		
		this.m_pe = new PeriodicalExecuter(this.scrollUp.bind(this), this.m_duration);
		
	},
	
	scrollUp: function() {
		this.m_pe.stop();
		
		this.m_index++;
		
		this.m_scrollTarget += this.m_itemHeight;
		this.m_transitionPe = new PeriodicalExecuter(this.animateUp.bind(this), this.m_transitionDuration);
	},
	
	animateUp: function() {
		this.m_transitionPe.stop();
		this.m_currentScroll += 10;
		
		if (this.m_currentScroll > this.m_scrollTarget) {
			this.m_currentScroll = this.m_scrollTarget;
			this.m_carousselDiv.scrollTop = this.m_currentScroll;
			
			if (this.m_index > (this.m_itemsCount-1)) {
				this.m_index = 0;
				this.m_currentScroll = (this.m_index * this.m_itemHeight) + (this.m_visibleItems * this.m_itemHeight);
				this.m_scrollTarget = this.m_currentScroll;
				this.m_carousselDiv.scrollTop = this.m_currentScroll;
			}
			
			this.m_pe = new PeriodicalExecuter(this.scrollUp.bind(this), this.m_duration);
		} else {
			this.m_carousselDiv.scrollTop = this.m_currentScroll;
			this.m_transitionPe = new PeriodicalExecuter(this.animateUp.bind(this), this.m_transitionDuration);
		}
	}
}
