// 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); }

// Create Text Size control bar and build interaction

function textSize() {
	var textSize	= '<span id="textsize">Text Size: <a href="" id="sm">A</a><a href="" id="md">A</a><a href="" id="lg">A</a></span>';
	$("h2:eq(0)").append(textSize);
	$("#textsize a").click(function() {
		var clss	= this.id;
		$(this).parents('span').children('a').removeClass("selected");
		$(this).addClass("selected");
		swapSheet(this.id);
		return false;
	});
}

// Create Page Controls bar and build interaction



// Append inline alternate stylesheets to DOM

function swapSheet(id) {
	 $("link[@rel*=alternate style]").each(function(i) {
		this.disabled = true;
		if (this.getAttribute('title') == id) this.disabled = false;
	});
	createCookie('style', id, 365);
}

// Find cookie and envoke correct stylesheet if set

function cookieExists() {
	var c = readCookie('style');
	if (c) { swapSheet(c); $("a#" + c).addClass("selected");
	} else { $("a#sm").addClass("selected"); }
}

// Remove useless whitespace from all unordered lists in a page
function cleanLists() {
	$("ul").each(function() {
		var node	= this.firstChild;
		while(node) {
			var nextNode	= node.nextSibling;
			if(node.nodeType == 3 && !/\S/.test(node.nodeValue))
			this.removeChild(node);
			node	= nextNode;
		}
		return this;
	});
}

// Return the value of all textfields to their default in the case where a they are blank onBlur
// call the function with the class of the inputs you wish to affect ie. swapDefault("my_class")
function swapDefault(clss) {
	$("input." + clss).each(
		function() {
			$(this).focus(
				function() {
					if(this.defaultValue && this.value == this.defaultValue) this.value = "";
				}
			).blur(function() {
				if(this.defaultValue && !this.value.length) this.value = this.defaultValue;
			}
		);
	});
}

// Open a new window for links with the attribute rel="external"
function targetLinks() {
	$("a[@rel=external]").click(function() {
		window.open(this.href);
		return false;
	});
}

function focusField() {
	$("input[@type=text]").each(function() {
		$(this).focus(function() {
			$(this).addClass("focus");
			// make the Enter/Return key submit the form in IE7
			$(this).keydown(function(e) {
				if(e.keyCode == 13) {
					$(this).parents('form').submit();
		            return false;
				};
			});
		}).blur(function() {
			$(this).removeClass("focus");
		});
	});
}

function smartHover() {	
	$("a").focus(function() { 
		$(this).addClass("hover") 
	}).blur(function() {
		$(this).removeClass("hover")
	});
}

function searchLink() {
	$("#form-search input.textfield").after("<a href='' class='search-submit'></a>");
	$("#form-search a").click(function() {
		this.parentNode.submit();
		return false;
	});
}

function dropNav() {

	// When the child UL is visible, change the class of the parent link.
	$("ul#nav ul").hover(function() {
		$(this).parents("li").children("a").addClass("hover");
	},function() {
		$(this).parents("li").children("a").removeClass("hover");
	}).focus(function() {
		$(this).parents("li").children("a").addClass("hover");
	}).blur(function() {
		$(this).parents("li").children("a").removeClass("hover");
	});
	
	// Add the hover class to the child LIs to shift the left attribute.
	$("ul#nav > li").hover(function() {
		// hide all subnav lists in the case when we are at a second tier
		$(this).parents("ul").children("li").children("ul").css({ left: "-9999px" });
		// reveal the child list of only this nav element
		$(this).children("ul:eq(0)").css("left", "auto");
	},function() {
		$(this).children("ul:eq(0)").css("left", "-9999px");
	}).focus(function() {
		$(this).children("ul:eq(0)").css("left", "auto");
	}).blur(function() {
		$(this).children("ul:eq(0)").css("left", "-9999px");
	});
	
	$("ul#nav").mouseout( function() {
		// reveal the subnav of the selected element when we are at a second tier
		$(this).children("li.selected").children("ul:eq(0)").css("left", "auto");
	});
}

$.fn.extend({
	reset: function() {
		return this.each(function() {
			$(this).is('form') && this.reset();
		})
	}
});

function formLink() {
	$("form:not(#form-search) input[@type=submit]").each(function() {
		$(this).before("<a href='' class='submit'>" + $(this).val() + "</a>");
	});
	$("form input[@type=reset]").each(function() {
		$(this).before("<a href='' class='reset'>" + $(this).val() + "</a>");
	});
	
	$("form a.submit").click(function() { $(this).parents("form").submit(); return false });
	$("form a.reset").click(function()  { $(this).parents("form").reset(); return false });
}

function showhideAbstract() {
	$('a[@rel=showhide]').click(function(event) {
		$('div#' + $(this).attr('href')).slideToggle('slow');
		return false;
	});
}

// Tabs for Projects / People
// Shows the appropriate content when properties tabs are clicked

function toggleProperties() {
	var a = $("div.property div.box-body > ul li a");
	if (a.length == 0) {
	  setTimeout("toggleProperties()", 5000);
	}
  a.click(function() {
		var anchors		= $(this).parents("ul").find("a");
		var container	= $(this).parents("div.box-body");
		container.children("div").hide();
		for (i = 0; i < anchors.length; i++) {
			if (anchors[i] == this) container.children("div:eq("+i+")").show();
		}
		anchors.removeClass("selected");
		$(this).addClass("selected");
		if (window.event) window.event.returnValue = false;
		return false;
	});
}

//	Sort form
function sortForm ()
{
	$("form#news_sort select").change( function(event) {
		$("form#news_sort")[0].submit();
	});
	
	$("ul#year_list li a").click( function(event) {
		$("form#news_sort input[@name='year']").attr( "value", $(this).attr("href") );
		$("form#news_sort")[0].submit();
		return false;
	});
}

//	Tag links
function tagLinks ()
{
	$("span#tag_options a").click(function(event) {
		$("p#cloud_cat").hide();
		$("p#cloud_all").hide();
		$("a#tag_cat").toggleClass("selected");
		$("a#tag_all").toggleClass("selected");
		$("p#cloud_" + $(this).attr("href")).show();
		return false;
	});
}

//	Pops
function pops ()
{
	$("a.pops").click(function(event) {
		window.open($(this).attr("href"), "pop", "width=500,height=600,menubar=no,status=no,toolbar=no");
		return false;
	});
}