﻿
// start Carousel
var Carousel = Class.create ({
    initialize: function(content,items,prev,next,links,options)
    {
		this.content = content;
		this.items = items;
			this.length = this.items.size();
		this.prev = prev;
		this.next = next;
		this.links = links;
		this.options = Object.extend(
		{
			initialIndex: 0,
			itemsPerGroup: 5,
			speed: 0.8,
			activeItemClassName: 'active', //used for testing
			disabledLinkClassName: 'disabled'
		}, options || {});
		if (this.options.initialIndex >= this.length)
		{
			this.options.initialIndex = 0;
		}
		this.groups = (this.length / this.options.itemsPerGroup).ceil();
		this.scrollAmt = (this.items[0].getWidth() * this.options.itemsPerGroup) * -1;
		this.groupIndex = (((this.options.initialIndex + 1) / this.options.itemsPerGroup).ceil()) -1;
		this.isAnimating = false;
        this.prev.observe('click', this.__ClickPrev.bindAsEventListener(this));
        this.next.observe('click', this.__ClickNext.bindAsEventListener(this));
		var boundLinkClick = this.__ClickItem.bindAsEventListener(this);
        this.links.invoke('observe', 'click', boundLinkClick);
		//set initial position
		this.MoveIt(this.groupIndex);
    },
    __ClickPrev: function(e)
    {
        e.stop();
        if ((this.groupIndex != 0) && !this.isAnimating)
        {
			this.groupIndex = this.groupIndex -1;
			this.MoveIt(this.groupIndex);
        }
    },
    __ClickNext: function(e)
    {
        e.stop();
        if ((this.groupIndex != this.groups -1) && !this.isAnimating)
        {
			this.groupIndex = this.groupIndex +1;
			this.MoveIt(this.groupIndex);
        }
    },
    __ClickItem: function(e)
    {
        e.stop();
        var element = e.element();
        //element = element.up('a');
        for (var i=0; i<this.length; i++)
        {
	        if (this.links[i] == element)
	        {
		        this.groupIndex = i;
		        this.MoveIt(i);
		        break;
	        }
        }
    },
    MoveIt: function(index)
    {
		this.moveEffect = new Effect.Move(this.content, {
			x: this.scrollAmt * index,
			y: 0,
			mode: 'absolute',
			fps: 100,
			duration: this.options.speed,
			transition: Effect.Transitions.easeOutExpo,
            beforeStart: function() {
                this.isAnimating = true;
                this.updateLinks();
            }.bind(this),
            afterFinish: function() {
                this.isAnimating = false;
            }.bind(this)
		});
        for (var i=0; i<this.length; i++)
        {
	        if (i == index)
	        {
		        this.links[i].up().addClassName(this.options.activeItemClassName);
	        } else {
	            this.links[i].up().removeClassName(this.options.activeItemClassName);
	        }
        }
    },
    updateLinks: function()
    {
		this.prev.removeClassName(this.options.disabledLinkClassName);
		this.next.removeClassName(this.options.disabledLinkClassName);

        if (this.groupIndex == 0)
        {
			this.prev.addClassName(this.options.disabledLinkClassName);
        }
        if (this.groupIndex == this.groups -1)
        {
			this.next.addClassName(this.options.disabledLinkClassName);
        }
    }
});
// end Carousel

