$(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)
	{// alert(window.location.pathname);
		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()
	{	
		var page = window.location.hash ? window.location.hash.toString().replace('#','') : window.location.pathname; //alert(page);
		var pageContent = window.location.hash.toString().replace('#', ''); //alert(pageContent);
		if (pageContent == "") {
			pageContent = "/home/";
		}
		pageContent = pageContent.replace(new RegExp('/', 'g'), '-'); 
		pageContent = pageContent.replace('-', ''); 
		pageContent = pageContent.replace(/^(.*)-(.*?)$/, '$1$2') + ".php"; 
	
		var ajaxOptions =
		{
			url:pageContent,
			dataType:'html',
			success:function(data)
			{ 
				$('#content').html(data).delay(1e2).fadeIn(25e1,function()
				{
					//alert(data);
					ht = $(document).height() - 20;
					$('#nav_bg').css({height: ht+'px'});
					if(window.scrollOnLoad)
					{
						$('#content').scrollTo($('#'+window.scrollOnLoad),500);
						window.scrollOnLoad = null;
					}
				});
			},
			error: function(xhr, ajaxOptions, thrownError)
			{  
				
				$('#content').html('We are currently under construction.  Please check back later.').delay(1e2).fadeIn(25e1);
				//$('#content').html(thrownError).delay(1e2).fadeIn(25e1);
			}
		};
		$.ajax(ajaxOptions); 
		

		var $menuItem = $('#navWrapper').find('a[href="'+page+'"]').last(); 

		$('#navWrapper div.menuBlock').removeClass('current section');
		$('#navWrapper img').trigger('mouseout');
		$menuItem.parents('div.menuBlock').siblings().find('ul:visible').hide(250);
		$menuItem.parents('div.menuBlock').addClass('current').find('img').trigger('mouseover');
		
		

		/*if (page == '/products/') {
			$('#navWrapper li a').removeClass('current');
			
			var tmp = "/products/passenger-car-oils/";
			
			var $tmpItem = $('#navWrapper').find('a[href="'+tmp+'"]').last();
			$tmpItem.parents('ul').show(250);
		} else {*/
			$('#navWrapper 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%'});
			// to adjust scrollbar left inc 930 to adjust rt dec 930
			var contentWidth = ($(this).width() - 840) / 2 + 760;  
			if(contentWidth < 760)
			{
				//contentWidth = 760;
			}
			
			// to adjust viewable height
			contentHeight = ($(this).height() - 125);
			$('#content').css({height: contentHeight+'px', width: contentWidth+'px'}); 
			//$('#content').css({'overflow-y': 'auto'});
			contentHeight = contentHeight + 15; 
			$('#siteWrapper').css({height: contentHeight+'px'});
		}
	});
	//when the page first loads do the above for the first time.
	//$(window).trigger('resize');

});// end onready

function clearNavs()
{ 
	//makes it so that nothing in the left nav is currently selected.
	$('#navWrapper *').removeClass('current').mouseleave();
	$('#navWrapper ul').hide(250);
}

$(function()
{	
	if (window.location.hash != "") {
		var page = window.location.hash ? window.location.hash.toString().replace('#','') : window.location.pathname;
		var pageContent = window.location.hash.toString().replace('#', '');  
		pageContent = pageContent.replace(new RegExp('/', 'g'), '-'); 
		pageContent = pageContent.replace('-', ''); 
		pageContent = pageContent.replace(/^(.*)-(.*?)$/, '$1$2') + ".php"; 
		//alert("2: "+pageContent);

		document.getElementById('start').src = 'images/menu_home_off.png';

		var ajaxOptions =
		{
			url:pageContent,
			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 = $('#navWrapper').find('a[href="'+page+'"]').last(); 

		$('#navWrapper div.menuBlock').removeClass('current section');
		$('#navWrapper img').trigger('mouseout');
		$menuItem.parents('div.menuBlock').siblings().find('ul:visible').hide(250);
		$menuItem.parents('div.menuBlock').addClass('current').find('img').trigger('mouseover');

		$('#navWrapper li a').removeClass('current');
		$menuItem.addClass('current').parents('ul').show(250);	
			
	}
});


