/**
 * @author Ricky Pattillo
 */

$(function() {
	var nubinTier1 = null;
	var nubinTier3 = null;
	
	var linksTier1 = $('ul.tier1 li a');
	var linksTier3 = $('ul.tier3 li a');
	
	/* Search box stuff */
	$('input#s').focus(function() {
		if($(this).val().toLowerCase() == 'search')
		{
			$(this).val('');
		}
	});
	$('input#s').blur(function() {
		$(this).val('Search');
	});

	/* Nubins in navigation - setup */
	if(linksTier1.length)
	{
		nubinTier1 = $('<img id="nubinTier1" src="/img/nubin-tier1.png">');
		nubinTier1.load(function(){initNubin(nubinTier1, $('ul.tier1 li.active a'), 'x')});
		$('nav#main').prepend(nubinTier1);
		
		linksTier1.hover(
			function ()
			{
				animateNubin(nubinTier1, $(this));
			},
			function ()
			{
				animateNubin(nubinTier1, $('ul.tier1 li.active a'));
			}
		);
	}
	
	if(linksTier3.length)
	{
		nubinTier3 = $('<img id="nubinTier3" src="/img/nubin-tier3.gif">');
		nubinTier3.load(function(){initNubin(nubinTier3, $('ul.tier3 li.active a'), 'x')});
		$('nav#main').prepend(nubinTier3);
		
		linksTier3.hover(
			function () 
			{
				animateNubin(nubinTier3, $(this));
			},
			function ()
			{
				animateNubin(nubinTier3, $('ul.tier3 li.active a'));
			}
		);
	}
});

function initNubin(nubin, anchor, axis)
{
	axis = axis.toLowerCase();
	nubin.data('axis', axis);
	
	if(axis == 'x')
	{
		nubin.data('property', 'left');
	}
	else if(axis == 'y')
	{
		nubin.data('property', 'top');
	}
	
	nubin.css(nubin.data('property'), getOffsetWithRespectToAnchor(nubin, anchor, nubin.data('axis')));
}

function animateNubin(nubin, anchor)
{
	if(nubin.filter(':animated'))
	{
		nubin.stop();
	}
	
	var property = nubin.data('property');
	var offset = getOffsetWithRespectToAnchor(nubin, anchor, nubin.data('axis'));
	var map = [];
	map[property] = offset;
	nubin.animate(map, 300, 'swing');
}

/**
 * Calculates an offset for a element that centers it with respect to an anchor
 * element. The element and anchor must share the same positioning parent. Both
 * must be fully loaded before calling this method. Images must be complete and
 * styles must be applied.
 *
 * @author Ricky Pattillo
 * 
 * @param element jQuery object representing a single element.
 * @param anchor jQuery object representing a single element.
 * @param axis either 'x' or 'y'
 * 
 * @return int the offset that centers the image 
 */
function getOffsetWithRespectToAnchor(element, anchor, axis)
{
	if(!anchor.size())
	{
		return 0;
	}
	
	var anchorCoord = anchor.position();	//coordinates
	var realAnchorOffset = 0;	// offset on the relavent axis, including margin
	var anchorDimension = 0;	// dimension on the relavent axis; height or width
	var elementDimension = 0;	// dimension on the relative axis; height or width
		
	if(axis.toLowerCase() == 'x')
	{
		realAnchorOffset = anchorCoord.left + parseInt(anchor.css('margin-left'));
		anchorDimension = anchor.width();
		elementDimension = element.width();
	}
	else if(axis.toLowerCase() == 'y')
	{
		realAnchorOffset = anchorCoord.top + parseInt(anchor.css('margin-top'));
		anchorDimension = anchor.height();
		elementDimension = element.height();
	}
	
	return realAnchorOffset + anchorDimension / 2 - elementDimension / 2;
}

