Cufon.replace('.cufon', {hover: true});

$(document).ready(function() {
	initMenu();
	initSlideShow();
	initPortfolio();
});

function initMenu() {
	var links = $('#menu > ul > li > a');
	var active = links.filter('.active');
	var rollover = $('#active');
	var menu = $('#menu');

	function slide(to) {
		var target = $(to);
		var target_pos = target.position();
		var final_pos = target_pos.left + 15; //calculate edge of link
		if(rollover.is(':visible')) {
			rollover.dequeue(); //kill any effects in progress
			rollover.animate({left: final_pos}, 500, 'swing').css('opacity', 1.0);
		} else {
			rollover.css({left: final_pos, opacity: 1.0});
		}
	}

	links.each(function(e) {
		$(this).mouseenter(
			function() {
				slide(this); //slide to this link on mouseenter
			}
		);
	});

	if(active.length) {
		slide(active); //start on active link
		rollover.fadeIn('slow');
		menu.mouseleave(
			function() {
				slide(active); //slide to active link on mouseleave
			}
		);
	} else {
		links.mouseenter(
			function() {
				rollover.fadeIn();
			}
		);
		menu.mouseleave(
			function() {
				rollover.fadeOut();
			}
			);
	}
}

function initSlideShow() {
	var slideshow = $('.spotlight');
	var slides = slideshow.children('.item');
	var fadeNum = 0;
	
	//if at least one slide:
	if(slides.length != 0) {
		//wrap each with a div and overlay the title
		slides.each(function(index) {
			var slide = $(this).detach();
			var wrap = $('<div class="slide" />').appendTo(slideshow);
			wrap.append(slide).css({
				position: 'absolute',
				zIndex: (slides.length-index)
			});
		});
	}
	
	//if more than one slide:
	if(slides.length > 1) {
		//create the menu
		var menu = $('.pagination');
		slides.each(function(index) {
			//add buttons for each slide
			if(index == 0) {
				menu.append($('<a href="#" class="cufon active">' + (index + 1) + '</a>'));
			} else {
				menu.append($('<a href="#" class="cufon">' + (index + 1) + '</a>'));
			}
			//add id's to each slide
			$(this).parent().attr('id', 'slide' + (index + 1));
		});
		//add events to each button
		var buttons = menu.children('a');
		Cufon.replace(buttons, {hover: true});
		buttons.each(function(index) {
			$(this).click(function() {
				fadeNum = index;
				nextFade();
				return false;					   
			});
		});
		var auto_fade = null;
		function nextFade() {
			if(auto_fade != null) {
				clearInterval(auto_fade);
			}
			var slide_items = $('.slide');
			var fadeLength = slide_items.length;
			//fade in target slide
			$(slide_items[fadeNum]).fadeIn();
			//fade out everything except target slide
			$(slide_items).filter(function(index) {
				return index != fadeNum;						 
			}).fadeOut();																 													 
			//set active menu button
			buttons.removeClass('active');
			buttons.eq(fadeNum).addClass('active');
			Cufon.replace(buttons);
			//find next slide
			if (fadeNum < (fadeLength-1)) {
				fadeNum++;
			} else {
				fadeNum = 0;
			}
			auto_fade = setInterval(nextFade, 5000);
		}
		nextFade();		
	}
}

function initPortfolio() {
	var pages = $('.page').css({width: 960});
	var wrap = $('<div class="page_wrap" />').css({width: pages.length*960});
	
	pages.css('float', 'left').wrapAll(wrap);
	pages.each(function(index, page) {
		var button = $('<a href="#" />').html(index+1);
		if(index == 0) button.addClass('active');
		$('.pagination').append(button);
		Cufon.replace(button, {hover: true});
		
		$(button).click(function() {
			$('.page_wrap').animate({
				marginLeft: -index*960
			}, 1000);
			$('.pagination a').removeClass('active');
			$(this).addClass('active');
			Cufon.replace('.pagination a');
			return false;
		});
	});
}

