// * home page flippy combines flippy with scroller
var HomeFlippy = Class.create(Flippy, {
	initialize: function($super, items, itemsNav, duration, scrollerObj) {
		$super(items, itemsNav, duration);
		this.rotator = new HomeScroller(scrollerObj);
		
		document.observe('scroll:right', this.scrollListener.bind(this));
		document.observe('scroll:left', this.scrollListener.bind(this));
	},
	
	restart: function() {
		// * restart auto change
		this.stop();
		this.timer = null;
		this.start();
	},
	
	next: function($super, event) {
		$super();		
		// * if it's an auto change (not triggered by native event)
		if (!event) {
			// * move scroller
			this.rotator.scroll(null, 1);
			this.restart();
		}
	},
	
	prev: function($super, event) {
		$super();		
		// * if it's an auto change (not triggered by native event)
		if (!event) {
			// * move scroller
			this.rotator.scroll(null, -1);
			this.restart();
		}
  },

	showItem: function($super, event) {
		$super(event);
		var element = event.findElement('a');
		var itemToShow = this.items.find(function(item){ return item.id == element.rel });
		this.currentIndex = this.items.indexOf(itemToShow);
		// this.restart();
		this.stop();
		this.timer = null;
	},
	
	scrollListener: function(event) {
		if(event.eventName.indexOf('right')!=-1) {
			// move to right
			this.clearSelected();
			this.next(event);
			this.restart();
		} else {
			// move to left
			this.clearSelected();
			this.prev(event);
			this.restart();
		}
	}
});

var HomeScroller = Class.create(scroller, {
	initialize: function($super, obj) {
		$super(obj);
	},
	scroll: function($super, event, dir) {
		$super(event, dir);
		if (!event) return;
		if (event.type == 'click') {
			if (dir==1) {
				document.fire('scroll:right');
			} else {
				document.fire('scroll:left');
			}
		}
	}
});
