$(document).ready(function(){
	// Show and clear "search" text in search field
	var search_field = $("#edit-search-block-form-keys");
	search_field.attr("value", "Search");
	search_field.focus(function(){
		if($(this).val() == "Search")
			$(this).attr("value", "");
	});
	search_field.blur(function(){
		if($(this).val() == "")
			$(this).attr("value", "Search");
	});

	// Show/hide full descriptions
	$("#description .short .swap").click(function(){
		$("#description .short").toggle();
		$("#description .long").toggle();
	});

	// Show and animate the intro if we're on the home page, along with other actions.
	if($("#home").length > 0) {
		//Parse the "messages" div and look to see if they've added a signature. If so, 
		// update the message with "please wait" and redirect them to the thank you page.
		if($("div.messages").length > 0){
			var messageContent= $("div.messages").html();
			if(messageContent.match(/<em>Signature<\/em>/gim)){
				$("div.messages").html('Please wait. . . ');
				window.location = '/publications/durhamstatement/thankyou';
				return;
			}
		}
		if(readCookie('berkman_intro_seen') == null) {
			$("#intro").slideDown('slow');
			intro_load();
		} else {
			$("#header .open").click(function(){
				eraseCookie('berkman_intro_seen');
				$("#intro").slideDown('slow');
				intro_load();
			});
		}
	}

});
	
/* Home page intro animation and collapsible block */
var intro_interval = setInterval(intro_rotate, 8000);	
var intro_count = 3;
var current_intro = 0;
var old_intro = 0;

function intro_load() {
	$("#intro .close").click(function(){
		clearInterval(intro_interval);
		$("#intro").slideUp('slow');
		createCookie('berkman_intro_seen', 'TRUE', '90');
	});
	$("#header .open").click(function(){
		eraseCookie('berkman_intro_seen');
		$("#intro").slideDown('slow');
		intro_interval = setInterval(intro_rotate, 8000);	
	});
	$("#intro .fade a").hover(function() {
		clearInterval(intro_interval);
	}, function() {
		intro_interval = setInterval(intro_rotate, 8000);	
		intro_rotate();
	})
}

function intro_rotate() {
	current_intro = (old_intro + 1) % intro_count;
	$("#intro .fade").fadeTo(1000, 0, function(){$(this).css("background-position", "0 -" + current_intro * 108 + "px")}).fadeTo(1000, 1);
	old_intro = current_intro;
}

// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name)
{
	createCookie(name,"",-1);
}
// /cookie functions
