(function($)
{
	
	$(function()
	{
		var footer = $('#FooterContainer');
		
		var teaser = $('#TeaserContainer');
		teaser.hide().removeClass('hide');
		
		var teaserTrigger = $('#latestNewsTrigger');
		teaserTrigger.data('animating', 'no');
		
		var nav = $('#MainNavHolder');
		var search = $('#SearchHolder');
		
		var content = $('#ContentContainer');
		var main = $('#MainContainer');
		main.removeClass('hide');
		
		
		if($('body').attr('id') == 'HomePage')
		{
			if(content.length < 1)
			{
				var content = $('#HomePageLead');
			}
			
			nav.css({ 'top': '-200px', 'opacity': 0 });
			search.css({ 'position': 'absolute', 'top': '300px', 'opacity': 0 });
			
			
			footer.hide().removeClass('hide');
			content.hide().removeClass('hide');
			
			nav.animate({
				'top': 0,
				'opacity': 1
			}, {
				'duration': 1200,
				'easing': 'easeOutSine',
				'complete': function()
				{
					
				}
			});
			
			search.delay(1000).animate({
				'top': '390px',
				'opacity': 1
			}, {
				'duration': 600,
				'easing': 'easeOutSine',
				'complete': function()
				{
				}
			});
			
			footer.fadeIn(800);
			content.delay(2000).fadeIn(500);
		}
		else
		{
			footer.show();
		}
		
		Shadowbox.init();
		
		
		teaser.find('.CloseCross').click(function(e)
		{
			e.preventDefault();
			
			if(teaserTrigger.data('animating') == 'no')
			{
				teaserTrigger.data('animating', 'yes');
				
				teaser.stop().fadeOut(1000, function()
				{
					teaserTrigger.data('animating', 'no');
				});
			}
		});
		
		teaserTrigger.click(function(e)
		{
			e.preventDefault();
			
			if(teaserTrigger.data('animating') == 'no')
			{
				teaserTrigger.data('animating', 'yes');
				teaser.stop().fadeToggle(1000, function()
				{
					teaserTrigger.data('animating', 'no');
				});
			}
		});
		
		
		
		$('#SearchHolder input:text').focus(function()
		{
			$('#SearchHolder').animate({
				'width': '168px'
			}, {
				'duration': 200,
				'easing': 'easeOutExpo'
			});
			
			$('#SearchHolder input:text').animate({
				'width': '139px'
			}, {
				'duration': 200,
				'easing': 'easeOutExpo'
			});
			
		});
		
		
		$('#SearchHolder input:text').blur(function()
		{
			$('#SearchHolder').animate({
				'width': '118px'
			}, {
				'duration': 400,
				'easing': 'easeOutSine'
			});
			
			$('#SearchHolder input:text').animate({
				'width': '89px'
			}, {
				'duration': 400,
				'easing': 'easeOutSine'
			});
			
		});
		
		
		$('.contactDetails .secondaryNavigation a').click(function() {
			
			var address = $(this).parents('.contactDetails').find('.address').text();
			var href = $(this).attr('href').replace(/^[^#]+/, '');
			var content = $(href);
			var html = content.html();
			
			Shadowbox.open({
				player: 'html',
				content: html,
				height: 590,
				width: 680,
				options: {
					onFinish: function(item) {
						var container = jQuery('#' + Shadowbox.playerId).find('.sb-map').get(0);
						var options = {
							zoom: 15,
							mapTypeId: google.maps.MapTypeId.ROADMAP
						}
						
						var geocoder = new google.maps.Geocoder();
						var map = new google.maps.Map(container, options);
						
						geocoder.geocode({
							'address': address
						}, function(results, status) {
							if(status == google.maps.GeocoderStatus.OK)
							{
								map.setCenter(results[0].geometry.location);
								
								var marker = new google.maps.Marker({
									map: map,
									position: results[0].geometry.location
								});
							}
							else
							{
								alert("Geocode was not successful for the following reason: " + status);
							}
						});
					}
				}
			});
			
		});
		
		
		$('#ProjectList a').click(function() {
			
			var href = $(this).attr('href').replace(/^[^#]+/, '');
			var content = $(href);
			var html = content.html();
			
			Shadowbox.open({
				player: 'html',
				content: html,
				height: 790,
				width: 680,
				options: {
					onFinish: function(item) {
						
						var container = jQuery('#' + Shadowbox.playerId).find('.PortfolioEntrySlideshow');
						
						container.before('<ul id="SlideshowNav">').cycle({
							fx: 'fade',
							speed: 'fast',
							timeout: 0,
							pager: '#SlideshowNav',
							
							// callback fn that creates a thumbnail to use as pager anchor 
							pagerAnchorBuilder: function(idx, slide)
							{
								return '<li><a href="#"></a></li>';
							}
						});
						
					}
				}
			});
			
		});
		
	});
	
	
	function repositionFooter()
	{
		if($(window).height() < (810 + $('#FooterHolder').height()))
		{
			$('#FooterHolder').css({
				'position': 'absolute',
				'top': '810px',
				'bottom': 'auto'
			});
		}
		else
		{
			$('#FooterHolder').css({
				'position': 'fixed',
				'top': 'auto',
				'bottom': '0'
			});
		}
	}
	
	
	$(function()
	{
		$(window).resize(function() { repositionFooter(); });
		
		
		$("#MainNav li:not(:first)").mouseenter(function() { $(this).addClass("hover"); });
		$("#MainNav li:not(:first)").mouseleave(function() { $(this).removeClass("hover"); });
		
	});
	
	
})(jQuery);
