/**
 * @author patrickteng
 */
var vis_features = {
	
	arr_tourNav_li: '',
	current_sectionId: 'features_nav_shortcodes', // Starts off at the login pane
	slideDuration: .5,
	
	init: function()
	{
		this.arr_nav_li = $$('#features_nav_list li');	
		this.bind_tourNav(); 
	},
	
	bind_tourNav: function()
	{		
		// Assign event handler for the tour navigation buttons.
		for(i = 0, liCount = this.arr_nav_li.length; i < liCount; ++i)
		{
			this.arr_nav_li[i].select('a')[0].observe('click', function(e) 
			{	
				if( Event.element(e).tagName.toLowerCase() == 'img')
					vis_features.slideToSection(Event.element(e).parentNode.parentNode.id);
				else
					vis_features.slideToSection(Event.element(e).parentNode.id);
				return false;
			});
			this.arr_nav_li[i].select('a')[0].onclick = function() { return false; };
		}
	},
	
	getMoveAmount: function(str_sectionID)
	{
		var tour_containerPos = $('features_container').cumulativeOffset();
		var YzeroPt = tour_containerPos[1]+0; // Change the +0 to another amount to tweak the slide offset amount.
		
		var elem_destContent = $($(str_sectionID).select('a')[0].readAttribute('href').split('#')[1]);
		var destCurrPosition = elem_destContent.cumulativeOffset();
		
		var moveAmount = YzeroPt - destCurrPosition[1];
		
		return moveAmount;		
	},

	slideToSection: function(str_destSectionID, errorCorrection) {
		if (str_destSectionID == this.current_sectionId && !errorCorrection) return;
		
		// Swap classNames to active show/hide the background nav image of the li elements.
		$(vis_features.current_sectionId).className = '';
		$(str_destSectionID).className = 'selected';
		vis_features.current_sectionId = str_destSectionID;
		
		var moveAmount = this.getMoveAmount(str_destSectionID);
		
		new Effect.Move('features_content_container', { 
			x: 0, 
			y: moveAmount, 
			duration: this.slideDuration, 
			queue: {scope: 'slide_tour_content_queue', limit:1},
			afterFinish: function()
			{
				var moveAmount = vis_features.getMoveAmount(vis_features.current_sectionId)
				if(moveAmount !== 0)
				{
					vis_features.slideToSection(vis_features.current_sectionId, true);
				}		
			}
		});		
		return true;
	}
}
document.observe('dom:loaded', vis_features.init.bindAsEventListener(vis_features));