$(function()
{

	//handles clicks of all links and attempts determines if the link should go through as written or changes the hash, triggering an ajax load.
	$('a').live('click',function(e)
	{
		var $this = $(this);
		//checking for && !($this.attr('href').indexOf('http') === 0) will fail in IE7 because it rewrites some links.
		if(window.location.pathname != '/' && $this.attr('href') && $this.attr('target') != '_blank' && !($this.attr('href').indexOf('javascript:') === 0) && !$this.hasClass('noAjax'))
		{
			e.preventDefault();
			var link = $this.attr('href');

			if(link.lastIndexOf('#') >= 0)
			{
				var linkArray = link.split('#');
				link = linkArray[0];
				window.scrollOnLoad = linkArray[1];
			}
			else if($this.attr('href').indexOf('#') === 0)
			{
				//just scrollto and return
				$('#content').scrollTo($('#content '+link));
			}


			if(window.location.hash == '#'+link)
			{
				//should scroll to an ID
				if(window.scrollOnLoad)
				{
					$('#content').scrollTo($('#content #'+window.scrollOnLoad),500);
					window.scrollOnLoad = null;
				}
			}else{
				//page content will change hash and load via ajax...
				$('#content').scrollTo({top:0,left:0},100).fadeOut(250); //scroll to the top and hide out content
				//IE6-7 will add the domain name to some absolute links
				link = link.replace(window.location.protocol+'//'+window.location.hostname,'');
				if(link)
				{
					window.location.hash = link;
				}
			}
		}else if($this.attr('href') && $this.attr('href').indexOf('http') === 0)
		{
			$this.attr('target','_blank');
		}
	});

	$('.subNavLink').live('click',function()
	{
		$(this).parent().siblings().find('a').removeClass('current');
		$(this).addClass('current');
	});


	$(window).hashchange(function()
	{
		if(window.location.hash == '#/cart')
		{
			return;
		}
		var page = window.location.hash ? window.location.hash.toString().replace('#','') : window.location.pathname;
		var ajaxOptions =
		{
			url:page,
			dataType:'html',
			success:function(data)
			{
				$('#content').html(data).delay(1e2).fadeIn(25e1,function()
				{
					if(window.scrollOnLoad)
					{
						$('#content').scrollTo($('#'+window.scrollOnLoad),500);
						window.scrollOnLoad = null;
					}
				});
			},
			error: function()
			{
				$('#content').html('Sorry, could not find that page.');
			}
		};
		$.ajax(ajaxOptions);

		var $menuItem = $('#leftNav').find('a[href="'+page+'"]').last();

		$('#leftNav div.menuBlock').removeClass('current section');
		$('#leftNav img').trigger('mouseout');
		$menuItem.parents('div.menuBlock').siblings().find('ul:visible').hide(250);
		$menuItem.parents('div.menuBlock').addClass('current').find('img').trigger('mouseover');

		if($menuItem.hasClass('topNavLink'))
		{
			if($menuItem.siblings('ul').find('li').length)
			{
				$menuItem.siblings('ul').find('li a').eq(0).addClass('current');
				$menuItem.siblings('ul').show(250);
			}
		}
		else
		{
			$('#leftNav li a').removeClass('current');
			$menuItem.addClass('current').parents('ul').show(250);
		}
	});

	//check for when the window resizes so that the content area can be resized to fit
	$(window).resize(function()
	{
		if(window.location.pathname != '/')
		{
			$('html,body').css({overflow: 'hidden', height: '100%'});
			var contentWidth = ($(this).width() - 980) / 2 + 830;
			if(contentWidth < 830)
			{
				contentWidth = 830;
			}
			$('#content').css({height: ($(this).height() - 155)+'px', width: contentWidth+'px'});
		}
	});
	//when the page first loads do the above for the first time.
	$(window).trigger('resize');

	//not happy about this.  But it sets the size of any product loaded onto the page to Large.
	$(window).load(function()
	{
		$('#merch_fit_l').click();
	});

});// end onready

function clearNavs()
{
	//makes it so that nothing in the left nav is currently selected.
	$('#leftNav *').removeClass('current').mouseleave();
	$('#leftNav ul').hide(250);
}
