$(document).ready(function(){

	// instantiate a few variables
	$.toggle_show_title = "Show information overlay";
	$.toggle_hide_title = "Hide information overlay";
 	$.default_email_text = "Your e-mail";

	// focus-blur on newsletter input
	$.input_email = $(".newsletter #duuyiu-duuyiu");
	$.input_email.attr("value", $.default_email_text);
	$.input_email.focus(function(){  
		if($(this).attr("value") == $.default_email_text) $(this).attr("value", "");
		$(this).addClass("active");
	});
	$.input_email.blur(function(){  
		if($(this).attr("value") == "") $(this).attr("value", $.default_email_text);
		$(this).removeClass("active");
	});

	// div-level hover (home)
	$(".button img").hover(
		function () {$(this).parents(".button").addClass("hover");},
		function () {$(this).parents(".button").removeClass("hover");}
	);

	// phase hover
	$(".phase img").hover(
		function () {$(this).parents(".phase").addClass("hover");},
		function () {$(this).parents(".phase").removeClass("hover");}
	);

	// billboard.home
	$('#billboard.home #slides').cycle({
		fx: 'fade',
		speed: 1000,
		timeout: 5000,
		//before: onBefore,
		after: onAfter
	});
	// billboard.default
	$('#billboard.default #slides').cycle({
		fx: 'scrollHorz',
		speed: 500,
		timeout: 0,
		prev: '#controls .prev',
		next: '#controls .next',
		//before: onBefore,
		after: onAfter
	});
	// billboard counter
	function onBefore(curr,next,opts) {
		var caption = (opts.currSlide + 1) + ' / ' + opts.slideCount;
		$('.counter').html(caption);
	}
	function onAfter(curr,next,opts) {
		var caption = (opts.currSlide + 1) + ' / ' + opts.slideCount;
		$('.counter').html(caption);
	}


	// billboard.home overlay toggle
	$.overlay_toggle = "<div class=\"toggle\"><a href=''></a></div>";
	$("#billboard.home #slides").after($.overlay_toggle);
	$(".toggle a").attr("title", $.toggle_hide_title);
	$(".toggle a").click(function(event){
		$.my_target = $(this).parents("#billboard.home").children("#slides").children(".slide").children(".overlay");
		$.drawer_speed = 400;
		if ($(this).attr("class")=="hidden") {
			$(this).attr("title", $.toggle_hide_title);
			$.my_target.show("slide", {direction: "right"}, $.drawer_speed);
		} else {
			$(this).attr("title", $.toggle_show_title);
			$.my_target.hide("slide", {direction: "right"}, $.drawer_speed);
		}
		$(this).toggleClass("hidden");
		return false;
	});

	// quick lightbox link rewrite
	//$(".slideshow span").text("View Slideshow");

	// fancybox - single images
	$('.larger-image a').fancybox({
		'hideOnContentClick': true,
		'imageScale': true,
		'zoomSpeedIn': 500,
		'zoomSpeedOut': 500,
		'overlayShow': true,
		'overlayOpacity': .8
	});

	// fancybox - video
	$('.video a').fancybox({
		'hideOnContentClick': false,
		'frameWidth': 640,
		'frameHeight': 480,
		'zoomSpeedIn': 500,
		'zoomSpeedOut': 0,
		'overlayShow': true,
		'overlayOpacity': .8
	});

	// fancybox - slideshow
	$('.slideshow a').fancybox({
		'padding': 17,
		'hideOnContentClick': false,
		'imageScale': true,
		'zoomSpeedIn': 500,
		'zoomSpeedOut': 500,
		'overlayShow': true,
		'overlayOpacity': .8
	});

	// fancybox - full bio
	$('.bio h2 a').fancybox({
		'hideOnContentClick': false,
		'frameWidth': 480,
		'frameHeight': 480,
		'zoomSpeedIn': 500,
		'zoomSpeedOut': 0,
		'overlayShow': true,
		'overlayOpacity': .8
	});

	// tabbed subpages
	$("#subpages").tabs({fx:{opacity: 'toggle'}});

	// Textile help for comments
	$("#comment-form .textile").hide();
	$("#comment-form span.help").wrapInner("<a href='' title=''></a>");
	$("#comment-form span.help a").click(function(event){
		$("#comment-form .textile").slideToggle("fast");
		return false;
	});

	// FAQs
	$.show_hide_link = "<p class=\"show-hide\"><a href=\"\" title=\"\"></a></p>"; // <-- construct this
	$.show_link_text = "Show Answer";
	$.hide_link_text = "Hide Answer";
	$.show_all_link_text = "Show All Answers";
	$.hide_all_link_text = "Hide All Answers";
	
	$("#faqs").before($.show_hide_link);
	//$("#faqs").after($.show_hide_link);
	
	$(".faq .answer").hide();
	$("h3.question").wrapInner("<a href=''></a>");
	$('h3.question a').each(function() {
	    $(this).attr("title", $.show_link_text);
	});
	$("h3.question a").addClass("hidden");
	$("h3.question a").click(function(event){
		$(this).parents(".faq").children(".answer").slideToggle("fast");
		//$(this).parents(".faq").children(".answer").toggle();
		$(this).toggleClass("hidden");
		if ($(this).attr("class")=="hidden") {
			$(this).attr("title", $.show_link_text);
		} else {
			$(this).attr("title", $.hide_link_text);	
		}
		return false;
	});
	
	$("p.show-hide a").text($.show_all_link_text);
	$("p.show-hide a").attr("title", $.show_all_link_text);
	
	$("p.show-hide a").click(function(event){
		if ($(this).is(":contains('Show')")) {
			$(".faq .answer").slideDown("fast");
			$("h3.question a").removeClass("hidden");
			$("h3.question a").attr("title", $.hide_link_text);
			$('p.show-hide a').each(function() {
				$(this).text($.hide_all_link_text);
				$(this).attr("title", $.hide_all_link_text);
			});
		} else {
			$(".faq .answer").hide("fast");
			$("h3.question a").addClass("hidden");
			$("h3.question a").attr("title", $.show_link_text);
			$('p.show-hide a').each(function() {
				$(this).text($.show_all_link_text);
				$(this).attr("title", $.show_all_link_text);
			});
		}
		return false;
	});

	Cufon.replace('h1', {hover:true})('#billboard.home .slide .overlay h2')('#phases .phase h2', {hover:true})('.media-gallery h2');

});