$(document).ready(function() {

	/* photo nav */
	var photonavWidth = 0;
	var navControls = $('#nav-left, #nav-right').hide();
	var primaryLinks = $('#primary-links');
	var james = $('#james');
	
	function resize() {
	
		var imgNav = $('#image-nav');
		var imgCont = $('#image-cont');		
		var currentImg = $('#image-cont').find('img.current');

		// Resize Navigation Slider
		photonavWidth = 0;
		$('img', imgNav).each(function() {
			photonavWidth = photonavWidth + $(this).attr('width') + 2;
		});
		imgNav.width(photonavWidth + 2);

		// Resize container window
		var height = $(window).height() - 130;
		imgCont.height(height);
		imgCont.find('ul, li').css({ width: imgCont.width(), height: imgCont.height() });

		$('#img-prev, #img-next').css('top', height / 2);

		// Resize image
		var ratio = currentImg.parent().attr('origheight') / currentImg.parent().attr('origwidth');

		var newWidth = imgCont.width();
		var newHeight = imgCont.width() * ratio;

		if (newHeight > imgCont.height()) {
			newHeight = imgCont.height();
			newWidth = newHeight / ratio;

		}
		currentImg.css({ width: newWidth, height: newHeight, marginTop: (imgCont.height() / 2) - (newHeight / 2) });

		// Show/hide the nav bittons
		if ($('#image-nav-container').width() > imgNav.width()) {
	
			navControls.fadeOut('fast');
		}
		else {
			navControls.fadeIn('fast');
		}
		
		
		if (james.offset().top - 40 < primaryLinks.offset().top + primaryLinks.height())
			james.css({ top: james.offset().top, bottom: 'auto' });
		else 
			james.css({ top: 'auto', bottom: '16px' });
	}
	
	function startSlideShow() {
	
		var $slideshow = $('#image-inner'); 
		var i = 0;

		var imgNav = $('#image-nav');
		
		var paused = false;
		
		function loadNext(next, f) {

			next = $('li[rel=' + next.attr('rel') + ']', $slideshow);

			if (!next.data('loading')) {

				next.data('loading', true);
				
				var img = $('<img class="current" />')
					.load(function() {
						if (next.attr('imgstyle') != undefined) img.attr('style', next.attr('imgstyle'));
						next.append( img );
						resize();
						if (typeof f == 'function') f();
					})
					.attr('src', next.attr('imgsrc'));
			}
		}

		$slideshow.cycle({ 
			delay: 0, 
			timeout: 3000, 
			fx: 'fade', 
			prev: '#img-prev', 
			next: '#img-next', 
			pager: '#image-nav',
			pagerAnchorBuilder: function(idx, slide) { 

				var navItem = $('li img[rel=' + $(slide).attr('rel') + ']', imgNav).closest('li');
				var navHTML = navItem.html();
				navItem.remove();
				return '<li>' + navHTML + '</li>';
		    },
	//		pauseOnPagerHover: true,
			before: function(currSlideElement, nextSlideElement) {

				$('#image-cont img.current').removeClass('current');
				$('img', nextSlideElement).addClass('current');
				
				loadNext($(nextSlideElement));
				
				$('li', imgNav).removeClass('current');
				$('img[rel=' + $(nextSlideElement).attr('rel') + ']', imgNav).closest('li').addClass('current');
				resize();
			},
			after: function(currSlideElement, nextSlideElement) {
			//	$('img', nextSlideElement).removeClass('current');

				var next = $(nextSlideElement).next();
				
				loadNext(next);
				/* next = $('li[rel=' + next.attr('rel') + ']', $slideshow);
				if (next.find('img').length == 0) {
					var img = $('<img />').attr('src', next.attr('imgsrc'));
					if (next.attr('imgstyle') != undefined) img.attr('style', next.attr('imgstyle'));
					next.append( img );
				} */
			}
		});
		
		imgNav.hover(
			function() {

				$slideshow.cycle('pause');
				$(this).data('hover', true);
			}, 
			function() {

				$(this).data('hover', false);
				if (!paused) $slideshow.cycle('resume');
			}
		);
		
	/*	$('a', imgNav).click(function(e) {
			
			var $slideshow = $('#image-inner');
	
			next = $('li[rel=' + $('img', this).attr('rel') + ']', $slideshow);
			if (next.find('img').length == 0) {
	
				$slideshow.cycle('pause');
				paused = true;

			/*	loadNext(next, function() {
					if (!imgNav.data('hover')) $slideshow.cycle('resume');
					paused = false;
				});

			}
		});	*/
	}
	
	
	resize();
	$(window).resize(resize);
	startSlideShow();
	
	navControls.live('click', function() {

		var currentPos = parseInt($('#image-nav').css('left'));

		if ($(this).attr('id') == 'nav-right') {
			var navWrapper = $('#image-nav-inside');

			if (photonavWidth - (-1 * currentPos) - navWrapper.width() > 400) {

				$('#image-nav').animate({ left: '-=400' });
			}
			else {
				$('#image-nav').animate({ left: '-' + (photonavWidth - navWrapper.width()) });
			}
		}
		else {

			if (currentPos + 400 < 0) {
				$('#image-nav').animate({ left: '+=400' });
			}
			else {
				$('#image-nav').animate({ left: '0' });
			}
		}
	});
	
	
	
	/* Main Nav */
	
	$('.secondary-links').each(function() {
		
		$(this).css({ marginBottom: 0 });

		if (!$(this).closest('li').hasClass('current')) {
			$(this).data('height', $(this).height() + 20);
			$(this).css({ height: 0 });
		}
		else {
			$(this).data('height', $(this).height() - 20);
			$(this).css({ height: $(this).height() });
		}
	});
	
	function openNav(that) {
		var newNav = $('.secondary-links', $(that).parent());

		newNav.animate({ height: newNav.data('height') + 20 }, 500, function() {
		//	$(this).closest('li').addClass('current');
		});
	}
	
	var links = $('#primary-links ul');
	$('#primary-links ul li > a')
	
		.live('click', function(e) {
		
			e.preventDefault();

			if ($(this).attr('href') != $('li.current > a', links).attr('href')) {

				var that = this;

				var current = $('.current .secondary-links', links);
				
				if (current.length == 0) openNav(that);

				$('li.current', links).removeClass('current');
				$(that).closest('li').addClass('current');
				current.animate({ height: 0 }, 500, function() {

					openNav(that);
				});
	
			}
			
		});
	
	
	var content = $('#content');
	$('.secondary-links a, #contact a').click(function(e) {
		
		e.preventDefault();
		
		var that = this;
		
		$('.secondary-links a.current').removeClass('current');
		$(this).addClass('current');

		content.fadeOut('fast', function() {

			content.load($(that).attr('href') + ' #content', function() {
				$('#image-nav li:first-child', content).addClass('current');
				navControls = $('#nav-left, #nav-right');
				navControls.hide();
				content.css({ display: 'block', visiblity: 'hidden' });
				resize();
				content.fadeIn('fast');
				startSlideShow();
			});
		});
	});
	

	

	
});
