/**
 * @author patrickteng
 */
var vis_tour = {
	
	arr_tourNav_li: '',
	current_sectionId: 'tour_nav_campaigns', // Starts off at the login pane
	slideDuration: .5,
	
	init: function()
	{
		this.arr_tourNav_li = $$('#tour_nav_list li');
		
		this.bind_tourNav(); 
		//this.bind_PrevNextBtns();
		this.UICheckLogic();
	},
	
	bind_tourNav: function()
	{		
		// Assign event handler for the tour navigation buttons.
		for(i = 0, liCount = this.arr_tourNav_li.length; i < liCount; ++i)
		{
			this.arr_tourNav_li[i].select('a')[0].observe('click', function(e) 
			{	
				vis_tour.slideToSection(Event.element(e).parentNode.id);						
				return false;
			});
			this.arr_tourNav_li[i].select('a')[0].onclick = function(){return false;};
		}
	},
	
	bind_PrevNextBtns: function()
	{		
		$('right_arrow').observe('click', function(e) {
			vis_tour.handlePrevNextBtns(true);
			return false;
		});
		$('right_arrow').onclick = function(){return false;};
		
		$('left_arrow').observe('click', function(e) {
			vis_tour.handlePrevNextBtns(false);
			return false;
		});
		$('left_arrow').onclick = function(){return false;};
	},
	
	UICheckLogic: function()
	{	
		/**	
		$('right_arrow').show();
		$('left_arrow').show();
		
		// Check which tour section we're at, so I can hide Prev/Next Buttons
		if(this.current_sectionId == 'tour_nav_shortcodes')
			$('right_arrow').hide();
		
		if(this.current_sectionId == 'tour_nav_campaigns')
			$('left_arrow').hide();
		**/
	},
	
	handlePrevNextBtns: function(goNext)
	{
		for(i = 0, arrCount = this.arr_tourNav_li.length; i < arrCount; ++i)
		{
			if(this.arr_tourNav_li[i].id == this.current_sectionId)
			{
				if (goNext && i+1 < this.arr_tourNav_li.length) 
				{
					this.slideToSection(this.arr_tourNav_li[i + 1].id);
					break;
				}
				else 
				{
					if (!goNext && i != 0) 
					{
						this.slideToSection(this.arr_tourNav_li[i - 1].id);
					}	
					break;
				}
			}
		}
	},
	
	getMoveAmount: function(str_sectionID)
	{
		var elem_destContent = $($(str_sectionID).select('a')[0].readAttribute('href').split('#')[1]);
		var destCurrPosition = elem_destContent.cumulativeOffset();

		var tour_containerPos = $('tour_container').cumulativeOffset();
		var XzeroPt = tour_containerPos[0]+27;
		var moveAmount = XzeroPt - destCurrPosition[0];
		
		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.
		$(this.current_sectionId).className = 'tour_nav_inactive';
		$(str_destSectionID).className = 'tour_nav_active';
		this.current_sectionId = str_destSectionID;
	
		var moveAmount = this.getMoveAmount(str_destSectionID);
		
		new Effect.Move('tour_content_container', { 
			x: moveAmount, 
			y: 0, 
			duration: this.slideDuration, 
			queue: {scope: 'slide_tour_content_queue', limit:1},
			afterFinish: function()
			{
				var moveAmount = vis_tour.getMoveAmount(vis_tour.current_sectionId)
				if(moveAmount !== 0)
				{
					vis_tour.slideToSection(vis_tour.current_sectionId, true);
				}		
				vis_tour.UICheckLogic();
			}
		});
			
		return true;
	}
}
document.observe('dom:loaded', vis_tour.init.bindAsEventListener(vis_tour));
