/**
 * @author christian
 */
if (!window.console)  {
	window.console =  {
		 log: function( o )  {
			// alert( o );
		 }
	 };
}

HomeCarousel = new Class({
	options: {
		animation: {
			type: "scroll",// fadeNscroll, scroll, fade
			transition: Fx.Transitions.Cubic.easeInOut,
			duration: 500
		},
		autoPlay: false,
		autoPlayInterval: 5000,
		width: -1,
		height: -1
	},
	
	initialize: function(container, pagination, options){
		this.setOptions(options);
		this.scrollableContainer = container.getElement(".carousel_items_container");
		this.scrollableContainer.set('tween', {transition: Fx.Transitions.Quint.easeInOut});
		this.scrollableContainer.setStyle("position", "relative");
		this.scrollableContainer.setStyle("top", 0);
		var items = this.scrollableContainer.getElements(".home_carousel_item");
		this.length = items.length;
		var firstItem = items[0];
		if(this.options.width == -1)
			this.options.width = parseInt(firstItem.getStyle("width"));
		if(this.options.height == -1)
			this.options.height = parseInt(firstItem.getStyle("height"));
		container.setStyle("height", this.options.height);
		container.setStyle("width", this.options.width);
		container.setStyle("overflow", "hidden");
		container.setStyle("position", "relative");
		this.currentIndex = 0;
		// build pagination
		this.buildPagination(pagination);
		this.updatePagination();
		if(this.options.autoPlay){
			this.autoPlayPeriodical = this.showNext.periodical(this.options.autoPlayInterval, this);
		}
	},
	
	showNext: function ()
	{
		this.gotoIndex((this.currentIndex + 1) % this.length);
	},
	
	buildPagination : function(container)
	{
		var p = new Element("p");
		p.innerHTML = "Featured work";
		container.appendChild(p);
		this.buttons = [];
		var ul = new Element("ul");
		var li,span;
		for(var i=0; i<this.length; i++){
			li = new Element("li");
			li.appendChild(this.createButton(i));
			if(i<this.length-1){
				span = new Element("span");
				span.innerHTML = "|";
				li.appendChild(span);
			}
			ul.appendChild(li);
		}
		container.appendChild(ul);
	},
	
	updatePagination: function ()
	{
		var a;
		for(var i=0; i<this.length; i++){
			a = this.buttons[i];
			if(i == this.currentIndex){
				this.log("add class: " + i);
				a.addClass("select");
			}else{
				a.removeClass("select");
			}
		}
	},
	
	createButton: function(index){
		var a = new Element("a");
		a.innerHTML = (index+1) + "";
		var me = this;
		a.addEvent('click', function ()
		{
			$clear(me.autoPlayPeriodical);
			me.gotoIndex(index);
		});
		this.buttons.push(a);
		return a;
	},
	
	gotoIndex: function(newIndex){
		if(newIndex == this.currentIndex)
			return;
		
		this.currentIndex = newIndex;
		this.updatePosition();
		this.updatePagination();
	},
	
	updatePosition: function ()
	{
		var newY = - this.currentIndex * this.options.height;
		this.scrollableContainer.tween("top", newY);
	},
	
	log: function (msg) {
		if (window.console)
			window.console.log(msg);
	}
});
HomeCarousel.implement(new Events); // Implements addEvent(type, fn), fireEvent(type, [args], delay) and removeEvent(type, fn)
HomeCarousel.implement(new Options);// Implements setOptions(defaults, options)