var chapterCarousel;
var chaptersIndex = {};

Ext.onReady(function() {


	// index chapters
	var i = 0;
	Ext.select('.chapter').each(function(chapterEl) {
		chaptersIndex[chapterEl.dom.id] = i++;
		chapterEl.dom.id = '_'+chapterEl.dom.id;
	});


	// create carousel
	var codaSlider = new Ext.ux.CodaSlider('chaptersMenu', 'chaptersWrapper', {
		animateHeight: false
		,navSelector:'li'
		,activeButtonClass: 'selected'
	});
				
	
	// wire all links
	Ext.select('a').on('click', function(e, target) {
		
		if(!target.hash || (target.pathname != window.location.pathname))
		{
			return;
		}
		
		// cancel link
		e.stopEvent();
		
		// switch slide
		codaSlider.onTabChange(chaptersIndex[target.hash.substr(1)]);
		
		// set hash
		window.location.hash = target.hash;

	});
	
	// set initial page
	var index;
	if(window.location.hash && (index = chaptersIndex[window.location.hash.substr(1)]))
	{
		codaSlider.onTabChange(index);
		window.scroll(0,Ext.fly('Menu').getTop());
	}
	else
	{
		codaSlider.onTabChange(0);
	}

});


