/**
 * Various JavaScript elements of space pages
 * 
 * @package reservations
 */

$(document).ready(function ()
{
	/* TABS */
		$('#space-tabs').tabs(
		{
			hideClass: 'hide'
		});
		
		$('a[href="#layouts"]').click(function ()
		{
			$('#space-tabs ul.tabs-nav').tabs('select', 'layouts');
		});
	/* END TABS */
	
	// Initiate rotating gallery in upper-right hand corner
	$('#space_photos').cycle(
	{
		delay: 5000,
		speed: 500,
		fit: 1
	});
	
	// Light boxes
	$('a[rel="space-photo"]').colorbox({transition: 'elastic'});
	$('a.layout_link').colorbox({
		transition: 'elastic',
		width: '750px',
		height: '540px',
		iframe: true,
		initialWidth: '750px',
		initialHeight: '540px'
	});
	
	// Smooth scrolling between same-page links
	enableSmoothScroll();
	
	// Set easing settings
	jQuery.easing.def = 'easeInOutQuart';
	
	// Move sidebar details
	if ($('div.sidebar_details').length)
	{
		var sidebar_handler = new sidebarHandler;
		sidebar_handler.setPositionAnchorTop($('#scroller_anchor').position().top);
		
		$('div.sidebar_details a').click(function ()
		{
			$('div.sidebar_details').fadeOut(200);
		});
		
		$(window).bind('scroll', sidebar_handler.moveForm);
	}
	
	// Tooltips
	$('div#features ul li span').tooltip();
});

/**
 * Smooth scrolling for same page links
 * http://www.learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links
 * 
 * @author Karl Swedberg, Paul Armstrong, Zachary Johnson
 */
function enableSmoothScroll()
{
	/**
	 * @param {String} string
	 * @type String
	 */
	function filterPath(string)
	{
		return string
		    .replace(/^\//, '')
		    .replace(/(index|default).[a-zA-Z]{3,4}$/, '')
		    .replace(/\/$/, '');
	}
	
	var locationPath = filterPath(location.pathname);
	var scrollElement = 'html, body';
	
	$('html, body').each(function ()
	{
		var initScrollTop = $(this).attr('scrollTop');
		$(this).attr('scrollTop', initScrollTop + 1);
		
		if ($(this).attr('scrollTop') == initScrollTop + 1)
		{
			scrollElement = this.nodeName.toLowerCase();
			$(this).attr('scrollTop', initScrollTop);
			
			return false;
		}
	});
	
	// Don't scroll UI tabs navigation
	$('a[href*=#]').not($(".ui-tabs-nav a[href*=#]")).each(function ()
	{
		var thisPath = filterPath(this.pathname) || locationPath;
		
		if (locationPath == thisPath
			&& (location.hostname == this.hostname || !this.hostname)
			&& this.hash.replace(/#/, ''))
		{
			if ($(this.hash).length)
			{
				$(this).click(function (event)
				{
					var targetOffset = $(this.hash).offset().top;
					var target = this.hash;
					event.preventDefault();
					
					$(scrollElement).animate({ scrollTop: targetOffset }, 500, function ()
					{
						location.hash = target;
					});
				});
			}
		}
	});
}

/**
 * Move sidebar details
 * Used with permission from Paul Armstrong Designs.
 * Adapted by Kamran Ayub
 * 
 * @author Paul Armstrong
 */
function sidebarHandler()
{
	var self = this;
	
	var _positionAnchorTop;
	var _formMove;
	
	/**
	 * Move sidebar details
	 */
	this.moveForm = function ()
	{
		clearTimeout(_formMove);
		
		$('div.sidebar_details').css('top', $('div.sidebar_details').position().top + 'px');
		var dist = _calcTop();
		
		if (($('div.sidebar_details').position().top <= $(document).scrollTop() 
				|| $('div.sidebar_details').position().top >= _positionAnchorTop)
			&& dist + $('div.sidebar_details').height() < $(document).height())
		{
			_formMove = setTimeout(function ()
			{
				var time = (dist / 0.6);
				
				$('div.sidebar_details').dequeue().animate(
					{ top: _calcTop() + 'px' },
					1000,
					'easeInOutQuart',
					 _endScroll());
			}, 300);
		}
	}
	
	/**
	 * Set the anchor's position from top
	 * 
	 * @param {Number} distance
	 */
	this.setPositionAnchorTop = function (distance)
	{
		_positionAnchorTop = distance;
	}
	
	/**
	 * Calculate the position from the top
	 * 
	 * @private
	 * @type Number
	 */
	function _calcTop()
	{
		if ($('div.sidebar_details').position().top <= $(document).scrollTop()
			|| $('div.sidebar_details').position().top >= _positionAnchorTop)
		{
			var newTop = ($(document).scrollTop() > _positionAnchorTop) ? $(document).scrollTop() : _positionAnchorTop;
			
			if ($('div.sidebar_details').height() + newTop >= $(document).height() - $('#umnFooter').height()
				&& newTop >= _positionAnchorTop + $('div.sidebar_details').height())
			{
				newTop = newTop;
			}
			
			return newTop;
		}
	}
	
	/**
	 * End scroll
	 * 
	 * @private
	 */
	function _endScroll()
	{
		if (_calcTop() <= (_positionAnchorTop + 15))
		{
			$('div.sidebar_details').fadeOut(200);
		}
		else
		{
			$('div.sidebar_details').fadeIn(200);
		}
	}
}