$(document).ready(function(){
	/*Cufon.replace('.stag', {fontFamily: 'Stag Book', hover: true}); 
	Cufon.replace('.stag-light', {fontFamily: 'Stag Light', hover: true}); 
	Cufon.replace('.indicator', {fontFamily: 'Stag Book', hover: true}); 
	Cufon.replace('#more-link', {fontFamily: 'Stag Book', hover: true}); 
	Cufon.replace('#intro p', {fontFamily: 'Stag Book', hover: true});
	Cufon.replace('.policy p', {fontFamily: 'Stag Light'});
	Cufon.replace('.policy li', {fontFamily: 'Stag Light'});
	Cufon.replace('.policy p strong', {fontFamily: 'Stag Book'})
	Cufon.replace('.ethos-text p', {fontFamily: 'Stag Light'})
	Cufon.replace('#project-information-inner p', {fontFamily: 'Stag Light'})
	Cufon.now();*/
	/* Main Navigation */
	$('.category-link').bind('click', expandCategory);
	function expandCategory()
	{
		$this = $(this)
		$this.addClass('current');
		idToExpand = $this.attr('rel');
		$subMenu = $('#sub-navigation');
		$toExpand = $('#'+idToExpand);
		//is the sub-menu open
		if ($subMenu.css('display') == 'block'){
			$subMenu.slideToggle(500, function(){
				//is the selected menu closed?
				if ($toExpand.css('display') == 'none'){
					//open it
					openCategory($toExpand);
				} // else do nothing and the menu just shuts.
			});
		} else {
			openCategory($toExpand);
		}
	}
	function openCategory($el)
	{
		//close all submenus
		$subMenu = $('#sub-navigation');
		$subMenu.find('.sub-menu').css('display', 'none');
		$el.css('display', 'block');
		$subMenu.slideToggle(500);
	}
	/* Home Page Slideshow */
	// Home Page Variables
	totalProjects = $('.project-image').length-1;
	homeCurrentProject = 0;
	homeCurrentProjectImage = 0;
	homeSlideInterval = 3500;
	homeTransitionTime = 1000;
	function startHomeSlide()
	{
		setTimeout(doHomeSlide, homeSlideInterval);
	}
	function doHomeSlide()
	{
		var $currentProject = $('.featured-project.current');
		var $nextProject = $currentProject.next();
		if ($nextProject.length < 1){
			$nextProject = $('.featured-project').first();
		}
		$currentProject.fadeOut(500, function(){
			$(this).removeClass('current');
		});
		$nextProject.fadeIn(500, function(){
			$(this).addClass('current');
		});
		setTimeout(doHomeSlide, homeSlideInterval);	}
	if ($('.featured-project').length > 0){
		startHomeSlide();
	}
	
	/* Project Page Functions */
	// more info button
	$('#more-link').bind('click', toggleInfo);
	function toggleInfo()
	{
		$moreLink = $('#more-link');
		$infoBox = $('#project-information');
		if ($infoBox.css('display') == 'none'){
			$infoBox.fadeIn(250);
			$moreLink.text('less info');
		} else {
			$infoBox.fadeOut(250);
			$moreLink.text('more info');
		}
		//Cufon.refresh('#more-link');
	}
	//next image, previous image
	projectAnimating = false;
	$('#project-next').bind('click', nextProjectImage);
	if ($('.portfolio .images a').length > 1){
		$('.portfolio .images a').bind('click', nextProjectImage);
	}
	$('#project-previous').bind('click', previousProjectImage);
	function nextProjectImage(e)
	{
		e.preventDefault();
		$moreLink = $('#more-link');
		$infoBox = $('#project-information');
		if ($infoBox.css('display') == 'block'){
			$infoBox.fadeOut(250);
			$moreLink.text('more info');
		}
		//Cufon.refresh('#more-link');
		
		if (!projectAnimating){
			projectAnimating = true;
			$currentImage = $('.images.current');
			if ($currentImage.next().hasClass('images')){
				$nextImage = $currentImage.next();
			} else {
				$nextImage = $('.images').first();
			}
			$currentImage.fadeOut(250, function(){
				$(this).removeClass('current');
			})
			$nextImage.fadeIn(250, function(){
				$(this).addClass('current');
				projectAnimating = false;
			})
		}
	}
	function previousProjectImage()
	{
		$moreLink = $(this);
		$infoBox = $('#project-information');
		if ($infoBox.css('display') == 'block'){
			$infoBox.fadeOut(250);
			$moreLink.text('more info');
		}
		//Cufon.refresh('#more-link');
		
		if (!projectAnimating){
			projectAnimating = true;
			$currentImage = $('.images.current');
			if ($currentImage.prev().hasClass('images')){
				$nextImage = $currentImage.prev();
			} else {
				$nextImage = $('.images').last();
			}
			$currentImage.fadeOut(250, function(){
				$(this).removeClass('current');
			})
			$nextImage.fadeIn(250, function(){
				$(this).addClass('current');
				projectAnimating = false;
			})
		}
	}
	//pillars slideshow
	pillarInterval = 5500;
	pillarTransition = 500;
	$pillars = $('.pillars');
	var pillarTimeout;
	
	if ($pillars.length > 0){
		pillarTimeout = setTimeout(nextPillar, pillarInterval);
		$('.pillar-thumbnail').bind('click', changePillar);
	}
	function changePillar(e){
		clearTimeout(pillarTimeout);
		newIndex = $(this).find('img').attr('class');
		$currentPillar = $('.pillar-image.current');
		$currentThumb = $('.pillar-thumbnail.current');
		$nextPillar = $('.pillar-image:nth-child('+newIndex+')')
		$nextThumb = $(this);
		$currentPillar.fadeOut(pillarTransition, function(){
			$(this).removeClass('current');
		})
		$currentThumb.removeClass('current');
		$nextPillar.fadeIn(pillarTransition, function(){
			$(this).addClass('current');
		})
		$nextThumb.addClass('current');
		pillarTimeout = setTimeout(nextPillar, pillarInterval);
	}
	function nextPillar()
	{
		$currentPillar = $('.pillar-image.current');
		$currentThumb = $('.pillar-thumbnail.current');
		$nextPillar = $currentPillar.next();
		$nextThumb = $currentThumb.next();
		if ($nextPillar.length == 0){
			$nextPillar = $('.pillar-image').first();		
			$nextThumb = $('.pillar-thumbnail').first();
		}
		$currentPillar.fadeOut(pillarTransition, function(){
			$(this).removeClass('current');
		})
		$currentThumb.removeClass('current');
		$nextPillar.fadeIn(pillarTransition, function(){
			$(this).addClass('current');
		})
		$nextThumb.addClass('current');
		pillarTimeout = setTimeout(nextPillar, pillarInterval);
	}
	$('#jscroll-previous').bind('click', jscrollUp);
	$('#jscroll-next').bind('click', jscrollDown);
	var jscrollAnimating = false;
	function jscrollUp()
	{
		if (!jscrollAnimating){
			jscrollAnimating = true;
			var $this = $(this);
			var $mask = $this.parent().parent();
			var $content = $this.parent().parent().find('.jscroll-holder');
			var maxOffset = 0;
			var topOffset = parseInt($content.css('top'))*-1;
			var val;
			if (topOffset - 550 >= maxOffset){
				val = ((topOffset-550)*-1);
			} else {
				val = maxOffset;
			}
			$content.animate({ 'top': val+'px'}, 500, function(){
				jscrollAnimating = false;
			});
		}
	}
	function jscrollDown()
	{	
		if (!jscrollAnimating){
			jscrollAnimating = true;
			var $this = $(this);
			var $mask = $this.parent().parent();
			var $content = $this.parent().parent().find('.jscroll-holder');
			var maxOffset = $content.height() - $mask.height();
			var topOffset = parseInt($content.css('top'))*-1;
			var val;
			if (topOffset + 550 <= maxOffset){
				val = ((topOffset+550)*-1);
			} else {
				val = maxOffset*-1;
			}
			$content.animate({ 'top': val+'px'}, 500, function(){
				jscrollAnimating = false;
			});
		}
	}
	$('.label').bind('click', toggleMenu);
	function toggleMenu()
	{
		var $this = $(this);
		var id = $this.attr('rel');
		var $target = $('#'+id);
		var $others = $target.siblings('.portfolio-menu');
		
		//is this one open
		if ($target.hasClass('open')){
			//close it
			$target.slideToggle(500, function(){
				$(this).removeClass('open').addClass('closed');
			})
			//make highlighted
			$this.addClass('current');
		} else {
			//close all others
			$others.each(function(){
				if ($(this).hasClass('open')){
					$(this).slideToggle(500, function(){
						$(this).removeClass('open').addClass('closed');
					})
				}
			})
			//remove all highlights
			$('.label').removeClass('current');
			//Cufon.refresh();
			//open it
			$target.slideToggle(500, function(){
				$(this).removeClass('closed').addClass('open');
			})
			//make highlighted
			$this.addClass('current');
		}
		
	}
})
