$(function()
{
	
	// self explanatory...
	loadBGImage();
	
	var imgCount = $('#imageCount').attr('value');
	$('#imageControlLeft').click(function()
	{
		var imgNumber = $('#imageNumber').attr('value');
		if ( imgNumber ) 
		{
			img = imgNumber - 1;
			if ( img == 0 )
			{
				img = imgCount;
			}
		}
		loadBGImage(img);
		return false;
	});
	
	$('#imageControlRight').click(function()
	{
		var imgNumber = $('#imageNumber').attr('value');
		if ( imgNumber ) 
		{
			img = parseInt(parseInt(imgNumber)+parseInt(1));
			if ( img > imgCount )
			{
				img = 1;
			}
		}
		else
		{
			img = 2;
		}
		loadBGImage(img);
		return false;
	});
	
	// mouseovers and outs are below, in the if NOT ie6 block
	
	


	
	// external links
	$('.external').click(function()
	{
		$(this).attr('target', '_blank');
	});
	
	
	



	
	// if you are NOT using IE Shix.
	if ( $.browser.version != 6 )
	{
		
		$('#imageControlLeft').mouseover(function()
		{
			var src = $(this).attr('src');
			src_o = src.replace('left', 'left_o');
			$(this).attr('src', src_o);
		}).mouseout(function()
		{
			var src = $(this).attr('src').replace('left_o', 'left');
			$(this).attr('src', src);
		});
		
		$('#imageControlRight').mouseover(function()
		{
			var src = $(this).attr('src');
			src_o = src.replace('right', 'right_o');
			$(this).attr('src', src_o);
		}).mouseout(function()
		{
			var src = $(this).attr('src').replace('right_o', 'right');
			$(this).attr('src', src);
		});
		
		
		// main header
		$('h1').css('cursor', 'pointer').click(function()
		{
			window.location = '/';
		});
		
		
		// navigation animation
		$('#navigation a').text('')
		.mouseover(function()
		{
			$(this).animate({paddingLeft:'10px'}, 100);
			// ie7 cant handle fading in a png (not surprised)
			if ( $.browser.msie && $.browser.version >= 7 )
			{
				$('img', $(this).parent()).css('display', 'block');
			}
			else
			{
				$('img', $(this).parent()).fadeIn(300);
			}
		})
		.mouseout(function()
		{
			$(this).animate({paddingLeft:'0'}, 300);
			// ie7 cant handle fading in a png (not surprised)
			if ( $.browser.msie && $.browser.version >= 7 )
			{
				$('img', $(this).parent()).css('display', 'none');
			}
			else
			{
				$('img', $(this).parent()).fadeOut(300);
			}
		});
		
		
		
		
		// h1 header / logo
		$('h1').html('<img src="/img/logo/langandpotter.png" alt="Lang &amp; Potter" />');
		
		var area = $('#control h2').text().toLowerCase().replace(/ /, '') || 'index';
		
		// h2 area titles
		$('#control h2').html( '<img src="/img/titles/' + area + '.png" alt="' + $('#control h2').text() + '" />');
	
		$('#thumbControl img').click(function()
		{
			$(this).blur();
			return false;
		});

		
			// page navigation
		$('#subNavigation li a:not(".leaveMeAlone"), #footer li a:not(".leaveMeAlone")').click(function()
		{
			
			// alert(area);
			
			if ( area == 'craftsmen' )
			{
				area = 'index';
			}
			
			if ( $(this).attr('href') == '/home' )
			{
				return true;
			}
			
			var href = $(this).attr('href').replace('/', '');
	
			$('#windowWrap').css({'opacity':'0.9'});
			
			$('#windowContent').css('margin-top', '20px');
			
			switch(href)
			{
				case 'aboutus':
					$('#windowContent').load('/content/' + area + '/aboutus.php?area=' + area);
					break;
				case 'whatwedo':
					$('#windowContent').load('/content/' + area + '/whatwedo.php?area=' + area);
					break;
				case 'clientlist':
					$('#windowContent').load('/content/' + area + '/clientlist.php?area=' + area);
					break;
				case 'vacancies':
					$('#windowContent').load('/content/vacancies.php?area=' + area);
					break;
				case 'contact':
					$('#windowContent').load('/content/' + area + '/contact.php?area=' + area);
					break;
				case 'findus':
					$('#windowContent').load('/content/findus.php?area=' + area);
					break;
				case 'sitemap':
					$('#windowContent').load('/content/sitemap.php?area=' + area);
					break;
				case 'accessibility':
					window.location = 'access.php';
					break;

				case 'links':
					$('#windowContent').load('/content/links.php?area=' + area);
					break;



			}
			$('#windowWrap').fadeIn(500);
			return false;
		});
		
		
		
		/*
		// footerNav
		$('#footer a').each(function()
		{
			var tt = $(this).text().toLowerCase().replace(' ', '').replace(' ', '');
			$(this).html('<img src="/img/navigation/footerNav/' + tt + '.png" alt"' + tt + '" />');
		});
		*/
		
		
		$('#indexNavigation li a').each(function()
		{
			var tt = $(this).text().toLowerCase().replace(' ', '');
			$(this).html('<img src="/img/navigation/index/' + tt + '.png" alt="' + tt + '" />')
			.mouseover(function()
			{
				var src_o = $('img', $(this)).attr('src').replace('.png', '_o.png');
				$('img', $(this)).attr('src', src_o).css({'behavior':'url(/css/iepngfix.htc)', 'height':'58px'});
			})
			.mouseout(function()
			{
				var src = $('img', $(this)).attr('src').replace('_o', '');
				$('img', $(this)).attr('src', src).css({'behavior':'url(/css/iepngfix.htc)', 'height':'58px'});
			});
		});

		$('#footerWrap p').css('opacity', '0.5');

	} // end if NOT IE Shix
	
	
	
	
	



	
	
		
		
		
		
	
	
	
	
	// ie6 
	if ( $.browser.msie && $.browser.version == 6 )
	{
		$('div').css({'behavior':'url(/css/iepngfix.htc)'});
		$('img').css({'behavior':'url(/css/iepngfix.htc)'});
		$('#windowWrap').css('background', 'none');

		$('#indexNavigation li').css({'margin':'-3px 0 0 0', 'border-top':'1px solid transparent'});
		
		$('#controlWrap').css({'height':'45px'});
		
		$('#subNavigation li a').css({'font-size':'10px', 'color':'red'});
		
	}
	
	
	// firefox 
	if ( $.browser.mozilla )
	{
		$('#subNavigation').css('margin-top', '-2px');
	}
	
	
	// ie7 
	if ( $.browser.msie && $.browser.version >= 7 )
	{
		$('#subNavigation').css('margin-top', '-2px');
		$('#annotation').css({'padding':'10px 0 10px 0', 'margin':'0 0 0 60px'});
		
		// kill the dropshadow
		$('#windowWrap').css('background', 'none'); // i cant handle a png thats loaded after the dom is. because i suck.
		
		$('#indexNavigation li').css({'margin':'-3px 0 0 0', 'border-top':'1px solid transparent'});

		$('#navigationWrap img').css({'behavior':'url(/css/iepngfix.htc)'});

	}
	

	// google chrome - cant handle the png shadow on content box...
	var isGooglesStupidlyNamedSafariWebkitMozillaChrome = /chrome/.test(navigator.userAgent.toLowerCase());
	if ( isGooglesStupidlyNamedSafariWebkitMozillaChrome )
	{
		$('#windowWrap').css('background', 'none');
	}
	
	
	
	$('#annotationWrap').css('opacity', '0.8');
	
	
	
	
	// ie6 cant handle this
	if ( $.browser.version != 6 )
	{
		if ( $('#annotationFlap') )
		{
			// annotation flap
			$('#annotationFlap').click(function()
			{
				var r = parseFloat($('#annotationWrap').css('right'));
				var w;
				if ( r == 0 )
				{
					w = '-315px';
					$('#annotationFlap').attr('style', 'background:url(/img/annotation/annotationOut.png) no-repeat 50% 50%;');
				}
				else
				{
					w = '0';
					$('#annotationFlap').attr('style', 'background:url(/img/annotation/annotationIn.png) no-repeat 50% 50%;');
				}
				$('#annotationWrap').animate({right:w}, 500);
			});
		}
	}
	
	
	
	
	
	// footer opacity
	$('#footerWrap').css('opacity', '0.7');
	
	
	
});
