/**
 * Cufon
 */
$(function() {
	if(!$.browser.msie) {
		Cufon.set('fontFamily', 'Akzidenz Grotesk').replace(
			'h2, p, #content ul, a.paging, #project h3, #contact h3, \
			#standard #content h3, #teammate #subnav h3, #teammate #main h3'
		);
		Cufon.set('fontFamily', 'Akzidenz Grotesk Medium').replace(
			'#header a, #header p, p.summary, #background h3, \
			#projects #content ul em, #project #content p.location, \
			#contact dl, #contact .directions, #contact .directions a, \
			#contact p.prettyPlease, #team #content ul a, \
			#teammate #subnav a',
			{hover: true}
		);
	}
});


/**
 * IE Helpers
 */
$(function() {
	if($.browser.msie) {
		$(':first-child').addClass('first-child');
		$(':last-child').addClass('last-child');
	}
})


/**
 * Randomly reorder an array
 */
Array.prototype.shuffle = function() {
	this.sort(function() {
		return Math.round(Math.random()) ? -1 : 1;
	});
	return this;
}


/**
 * Nullify links that resolve to only '#'. It's annoying seeing that thing
 * in the addressbar during development!
 */
function nullifyEmptyLinks() {
	$('a').each(function() {
		if($(this).attr('href') == '#') {
			$(this).click(function() {
				return false;
			});
		}
	});
}


/**
 * Controller: Nav
 */
$(function() {

	var fadeSpeed = 250;
	var timeoutId;
	var $currentMenu;

	$('#header>ul>li>a').each(function() {

		var $header = $(this);
		var $child = $(this).parent().find('ul');
		var index = $header.parent().index();

		$header.mouseover(showMenu);
		$header.mouseout(initTimeout);

		$child.mouseover(showMenu);
		$child.mouseout(initTimeout);

		function showMenu() {
			clearTimeout(timeoutId);

			// Fade out other menus
			$('#header>ul>li>ul').each(function() {
				if($(this).parent().index() != index) {
					$(this).fadeOut(fadeSpeed);
				}
			});

			$child.fadeIn(fadeSpeed);
		}

		function initTimeout() {
			clearTimeout(timeoutId);

			timeoutId = setTimeout(function() {
				$child.fadeOut(fadeSpeed);
			}, 500);
		}

	});

	// Fixes an IE 7 bug where nav menus appear below page content. Annoying!
	// Super special thanks to: http://richa.avasthi.name/blogs/tepumpkin/2008/01/11/ie7-lessons-learned/
	// Also: http://www.quirksmode.org/bugreports/archives/2006/01/Explorer_z_index_bug.html
	if($.browser.msie && parseInt($.browser.version, 10) == 7) {
		$('#header>ul>li>ul').parents().each(function() {
			$(this).addClass('on-top');
		});
	}

});


/**
 * Controller: Home
 */
$(function() {

	if($('#home').size() == 0) return;

	var fadeSpeed = 500;
	var timeoutId;

	var images = [
		'/home/1.jpg',
		'/home/2.jpg',
		'/home/3.jpg',
		'/home/4.jpg',
		'/home/5.jpg',
		'/home/6.jpg',
		'/home/7.jpg',
		'/home/8.jpg',
		'/home/9.jpg',
		'/home/10.jpg',
		'/home/11.jpg'
	].shuffle();

	function loadNext() {
		clearTimeout(timeoutId);

		var img = new Image();
		img.onload = function() {
			showNextImage(img);
		}
		img.src = images[0];

		images.push(images.shift());
	}

	function showNextImage(img) {

		var $lastImg = $('#home #content img');

		$(img).css('z-index', 1).hide();

		$('#home #content').append(img);
		$(img).fadeIn(fadeSpeed, function() {
			$lastImg.remove();
			$(img).css('z-index', 0);
		});

		timeoutId = setTimeout(loadNext, 3000);
	}

	loadNext();
});


/**
 * Controller: Background Section
 */
$(function() {

	var $pages = $('#content>div');

	$('a.paging').click(function() {

		var $currentPage, $otherPage;

		// Determine which 'page' is visible
		if( $(this).text().toLowerCase() == 'next' ) {
			$currentPage = $pages.eq(0);
			$nextPage = $pages.eq(1);
		} else {
			$currentPage = $pages.eq(1);
			$nextPage = $pages.eq(0);
		}

		// Fade out current page then fade in next page
		$currentPage.fadeOut(200, function() {
			$nextPage.fadeIn(200);
		});
	});

	// Show first page
	$pages.eq(0).fadeIn(200);

});



/**
 * Controller: Projects Sections
 */
$(function() {

	// Move projects after the fourth into a new column
	$('#projects #content li').each(function() {
		if($(this).index() > 3) {

			var top = ($(this).index() - 4) * $(this).height();
			if($(this).index() > 4) top += 3;

			$(this).css({
				position: 'absolute',
				top: top,
				left: $(this).width() + 20
			});
		}
	});

});


/**
 * Controller: Project Section
 */
$(function() {

	var images = [], loadedImages = [], currentImage, ie7 = false;

	if($.browser.msie && parseInt($.browser.version, 10) == 7) {
		ie7 = true;
	}

	$('#project #content ol a').each(function() {
		images.push(this.href);
		if(!ie7) $(this).css('opacity', 0);
	}).click(function() {
		return false;
	});

	preloadImages();

	function preloadImages() {
		var i = 0;

		while(images.length > 0) {
			img = new Image();
			$(img).data('index', i++);
			img.onload = function() {
				
				if(ie7) return;
				
				$(this).hide().appendTo('#project #content');
				//$('#project #content').append(this); // TODO: make sure we can take this out

				$('#project #content ol a').eq($(this).data('index'))
					.data('index', $(this).data('index'))
					.animate({opacity: 1}, {duration: 250, queue: false})
					.click(function() {

						$('#project #content ol a').removeClass('active');
						$(this).addClass('active');

						if(currentImage != loadedImages[$(this).data('index')]) {
							$(currentImage).fadeOut(250);
							currentImage = loadedImages[$(this).data('index')];
							$(currentImage).fadeIn(250);
						}
					});

				loadedImages[$(this).data('index')] = this;

				if($(this).data('index') == 0) {
					currentImage = loadedImages[0];
					$(currentImage).fadeIn(250);
					$('#project #content ol a').eq(0).addClass('active');
				}
			};
			img.src = images.shift();
			
			// Gross IE7 fix
			// TODO: clean this up. This is nearly a duplication of img.onload() above.
			// ----------
			if(ie7) {
				$(img).hide().appendTo('#project #content');
				$('#project #content ol a').eq($(img).data('index'))
					.data('index', $(img).data('index'))
					.click(function() {
						$('#project #content ol a').removeClass('active');
						$(this).addClass('active');
						if(currentImage != loadedImages[$(this).data('index')]) {
							$(currentImage).fadeOut(250);
							currentImage = loadedImages[$(this).data('index')];
							$(currentImage).fadeIn(250);
						}
					});
					loadedImages[$(img).data('index')] = img;

					if($(img).data('index') == 0) {
						currentImage = loadedImages[0];
						$(currentImage).fadeIn(250);
						$('#project #content ol a').eq(0).addClass('active');
					}
			}
			// ----------

		}
	}

	$('#project #content a.next').click(function() {
		var indexes = $('#project #content ol a').size();
		var currentIndex = parseFloat($('#project ol a.active').data('index'));
		var nextIndex = (currentIndex > indexes - 2) ? 0 : currentIndex + 1;

		$('#project #content ol a').removeClass('active');
		$('#project #content ol a').eq(nextIndex).addClass('active');
		$(currentImage).fadeOut(250);
		currentImage = loadedImages[nextIndex];
		$(currentImage).fadeIn(250);

		return false;
	});
});

/**
 * Init
 */
$(nullifyEmptyLinks);

