var NavBarWidth=720; //CHANGE THIS IF THE NAV ELEMENTS CHANGE - NO WAY TO CALCULATE WHEN HIDDEN BY DEFAULT!
function NavSlider(){
	$('#NavPanel img').click(
		function(){			
			$("#expand").hide();		
			var NavWidth=$('#NavPanel ul').width();
			if(NavWidth=='0'){
				$('#NavPanel').animate(
					{
						'width'	:	(173+NavBarWidth)+'px'
					}
					,750
				)
				
				$('#NavPanel ul:first').animate(
					{
						'width'	:	NavBarWidth+'px'
					}
					,750
					,function(){
						FadeInNavOptions();
					}
				)
				
				$('.HasSubs a').not('.SubNav a').click(
					function(){						
						if($(this).parent().find('ul.SubNav').hasClass('Closed')){													
							$(this).parent().find('ul.SubNav').fadeIn(
								500
								,function(){
									$(this).removeClass('Closed');
								}
							);

						}
						else{
							$(this).removeClass("selected");						
							$(this).parent().find('ul.SubNav').fadeOut(
								500
								,function(){
									$(this).addClass('Closed');
								}
							);

						}
					}
				);				
				return false;
			}
			else{
				document.location = 'http://www.liddell-linens.com';
			}
		}
	)
	
	$('#NavPanel li a').click(
		function(){
			$('#NavPanel li a').removeClass('selected');
			$(this).addClass("selected");
			DoFonts()
			if($(this).parent().hasClass('HasSubs')){
			//	return true;
			}
			if(Panning){
				Panning=false;
				clearTimeout(FadeTimeout);
				$('#Backing1').stop().fadeOut(500,function(){
					$(this).css('left','0px').css('top','0px');
				});				
				$('#Backing2').stop().fadeOut(500,function(){
					$(this).css('left','0px').css('top','0px');
					$('#Backing2').html('<img src="/images/HomepageBackgrounds/blank.png"/>');
					$('#Backing1').html('');
					setTimeout(
						function(){
							LoadContent();
						}
						,10
					);
				});				
			}
			else{
				setTimeout(
					function(){
						LoadContent();
					}
					,100
				);
			}
		}
	)
	
}

function FadeInNavOptions(){
	$('#NavPanel ul:first a.Hidden:first').fadeIn(
		500,
		function(){
			$(this).removeClass('Hidden');
			if ($('#NavPanel ul:first a.Hidden:first').size()>=1){
				setTimeout('FadeInNavOptions()',20);
			}
		}
	);
}

function LoadContent(){
	var pathname = window.location.hash;
	pathname = pathname.replace('#','');
	$.ajax({
		method:	'GET',
		url: '/DATA/'+pathname,
		dataType: 'json',
		success: function(data) {							
			var Content = data.content;
			var Image = data.image;
			$('#ImageBuffer').html(Image);
			$('#PageContentPanel').fadeOut(
				500
				,function(){
					$(this).html(Content);
					$(this).fadeIn(500);
					DoFonts();
				}
			);
			$('#ImageBuffer').waitForImages(function() {
				SwitchBG(Image)
			});			
		},
		error: function(){
			alert('err');
		}
	});
}

function SwitchBG(Image){
	$('#Backing1').html($('#Backing2').html());
	$('#Backing1').css('z-index',-1).css('width','100%').css('height','100%');
	$('#Backing2').css('z-index',-2).css('opacity',1).css('display','block');
	$('#Backing1').css('opacity',1).css('display','block');
	$('#Backing2').html(Image);
	if(FirstLoad){
		$('#Backing2 img').css('display','none');
	}
	setTimeout(function(){
		var Sizes=GetImgSize($('#Backing2 img').attr('src'));
		ImgWidth=Sizes.ImgWidth;
		ImgHeight=Sizes.ImgHeight;
		RatioX=ImgWidth/ImgHeight;
		RatioY=ImgHeight/ImgWidth;
		while (ImgWidth<ScreenWidth || ImgHeight<ScreenHeight){
			ImgWidth+=RatioX;
			ImgHeight+=RatioY;
		}
		ImgWidth=parseInt(ImgWidth);
		ImgHeight=parseInt(ImgHeight);				
		$('#Backing2 img').css('width',ImgWidth+'px');	
		$('#Backing2 img').css('height',ImgHeight+'px');
		if(FirstLoad){
			$('#Backing2 img').fadeIn(300);
			FirstLoad=false;
		}
		$('#Backing1').fadeOut(1500);
	}
	,100
	);
}

