$(document).ready(function() {

	if ($('#home').length > 0) $page = 'home';
	else if ($('#gallery').length > 0) $page = 'gallery';
	else if ($('#findus').length > 0) $page = 'findus';
	else if ($('#venuehire').length > 0) $page = 'venue';
	
	function resize() {

		var height = 575 - Math.round($(window).height() / 2);
		$('#outer-wrapper').css({marginTop: (-1 * height), minHeight: $(window).height() + height });
	}

	if (!(($.browser.msie) && ($.browser.version == '6.0'))) {
		resize();
		$(window).resize(resize);
	}
	
	if ($.browser.msie) {
		$nav = $('#nav');

		$('li a', $nav).css('background-image', 'url(../images/transparent.gif)');
		$('#navgallery', $nav).css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'../images/nav-gallery.png\',sizingMethod=\'scale\')alpha(opacity=60);');
		$('#navfindus', $nav).css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'../images/nav-findus.png\',sizingMethod=\'scale\')alpha(opacity=60);');
		$('#navhome', $nav).css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'../images/nav-home.png\',sizingMethod=\'scale\')alpha(opacity=100);');
		$('#navmenu', $nav).css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'../images/nav-menu.png\',sizingMethod=\'scale\')alpha(opacity=60);');
		$('#navvenue', $nav).css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'../images/nav-venue.png\',sizingMethod=\'scale\')alpha(opacity=60);');
	}

	var $contentLarge = $('.content-large');
	var $content = $('.content');


	
	if ($page == 'home') {
		// cache other pages //
		
	//	$contentLarge.append('<div id="findusContainer" class="inner-wrapper"><div id="mapcontainer"></div>Click through to <a target="_blank" href="http://maps.google.co.uk/maps?f=q&amp;source=embed&amp;hl=en&amp;geocode=&amp;q=two+floors+W1B+5PD&amp;sll=51.512044,-0.138595&amp;sspn=0.002337,0.008256&amp;ie=UTF8&amp;hq=two+floors&amp;hnear=London+W1B+5PD,+United+Kingdom&amp;cid=12948572599676747348&amp;ll=51.520013,-0.138273&amp;spn=0.016022,0.07596&amp;z=14&amp;iwloc=A" style="color:#FFFFFF;text-align:left">Google Maps</a></div>');

		$('<div></div>').load('/gallery.php #galleryContainer', function() {
			$contentLarge.append($('#galleryContainer', this));
			$galleryContainer = $('#galleryContainer', $contentLarge);
			$galleryContainer.css({ opacity: '0', display: 'block'});

/*			var totalImages = $('img', $galleryContainer).length;
			var i = 0;
			$('.inner-wrapper ul li img', $contentLarge).bind('load', function() {																			 
				i++;
				if (i == totalImages) { setupGallery(); }
			}); */
		});
		
		$('<div></div>').load('/venuehire.php #venueContainer', function() {
			$content.append($('#venueContainer', this));
		});
		// ***************** //

		var currentPage = 'home';
		var navlinks = $('a', '#nav');
		var transitioning = false;

		navlinks
			.hover(function() {

				$(this).stop(true, true).animate({ opacity: '1' });

			}, function() {

				if ($(this).attr('rel') != currentPage) $(this).stop(true, true).animate({ opacity: '0.6' });

			})
			.filter(':not(#nav' + currentPage + ')')
			.css({ opacity: '0.6' })
			.addClass('jdown');
			

		var maploaded = false;
		function switchPage(page) {

			var i = 0;
			$('.inner-wrapper').fadeOut('fast', function() {

				i++; 
				if (i > 4) {
					if ($.inArray(page, ['gallery', 'findus']) > -1) {
						$content.fadeOut('fast', function() {
							$('.bg', $contentLarge).fadeIn(300);
							$contentLarge.animate({ height: '508px', marginTop: '-190px' }, 300, function() {
																										  
								transitioning = false;
								$('#' + page + 'Container' , $contentLarge).fadeIn('fast');
								$('#' + page + 'Container' , $contentLarge).animate({ opacity: 1 });
								if (page === 'gallery') {
									//alert('asdas');

									$('.gallery', $contentLarge).animate({ opacity: 1 });
									setupGallery();
								}
								else {
									
									if (!maploaded) {
										$('#mapcontainer', $contentLarge).gMap({ zoom: 15, latitude: 51.512044, longitude: -0.138525, markers: [{latitude: 51.512044, longitude: -0.138525}] });
										maploaded = true;
									}
								}
							});
						});
					} else {

						$('.bg', $contentLarge).fadeOut(300);
						$contentLarge.animate({ height: '153px', marginTop: '-38px' }, 300, function() {
							$content.fadeIn('fast', function() {
								$('#' + page + 'Container' , $content).fadeIn('fast');
								transitioning = false;
							});
						});
					}
				}
			});
		}
		

		navlinks.filter(':not(#navmenu)').click(function(e) {

			e.preventDefault();
			if (transitioning) return false;
			if (currentPage != $(this).attr('rel')) {
				transitioning = true;
				currentPage = $(this).attr('rel');
				$(this).css( 'opacity', '1' );
				navlinks.filter(':not(#nav' + currentPage + ')').stop(true, true).animate({ opacity: '0.6' });
				switchPage(currentPage);
			}
	
		});


	}
	
	if ($page == 'gallery') {
		$galleryContainer = $('#galleryContainer', $contentLarge);
		setupGallery();
	}

	if ($page == 'findus') {
		$('#mapcontainer', $contentLarge).gMap({ zoom: 15, latitude: 51.512044, longitude: -0.138525, markers: [{latitude: 51.512044, longitude: -0.138525}] });
	}

	var galleryIsSetup = false;
	function setupGallery() {

		if (galleryIsSetup) return false;

		var $ul = $('ul', $galleryContainer).jcarousel({ 

			scroll: 2,
			buttonNextCallback: function(i, e, f) {

				if (!f) $(e).css('opacity', 0.4);
				else $(e).css('opacity', 1);

			},
			buttonPrevCallback: function(i, e, f) {

				if (!f) $(e).css('opacity', 0.4);
				else $(e).css('opacity', 1);						
			},
			initCallback: function() {
				if ($.browser.msie) {
					
					$('.jcarousel-prev', $galleryContainer).css( 'background-image', 'none').css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'../images/gallery-left.png\',sizingMethod=\'scale\')alpha(opacity=0);');
					$('.jcarousel-next', $galleryContainer).css( 'background-image', 'none').css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'../images/gallery-right.png\',sizingMethod=\'scale\')alpha(opacity=0);');
				}
			}
		});
		
		galleryIsSetup = true;
		
		if ( ($page == 'home') && (currentPage != 'gallery') ) {
			$ul.css( 'opacity', 0 );
			if ($page == 'home') $galleryContainer.css('display', 'none');
		}
	}

});
