$(function() {
	initOverLabels();
	$("ul.piped-list>li:not(:last-child)").append("<span>|</span>");
	// add classes to items that can't be selected with CSS because of damn IE6
	$("#primary-navigation li.selected>ul>li:last>a:not(.parent-open), #primary-navigation li.selected:last-child>a:not(.parent-open), #primary-navigation li.selected:last:not(:has(ul))>a.parent-open").addClass("last-child");
	$("#primary-navigation li.selected>a.child").addClass("selected-child");
	var $selected = $("#primary-navigation li.selected");
	var $selectedAfter;
	if ($selected.next("li").length > 0)
	{
		$selectedAfter = $selected.next("li");
	}
	else if ($selected.parent().next("li").length > 0)
	{
		$selectedAfter = $selected.parent().next("li");
	}
	else if ($selected.parent().parent().next("li").length > 0)
	{
		$selectedAfter = $selected.parent().parent().next("li");
	}
	if ($selectedAfter && $selectedAfter.length > 0)
		$selectedAfter.addClass("selected-after");
	
});

// 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

function initOverLabels() {
	$("label.overlabel[for]").each(function() {
		var label = $(this);
		label.attr("class", "overlabel-apply");
		var field = $("#" + label.attr("for"));
		if (field.val() != "")
			label.hide();
		field.focus(function() {
			label.hide();
		}).blur(function() {
			if (field.val() == "")
				label.show();
		});
		label.click(function() {
			field.trigger("focus");
		});
	});	
}

// used for links to objects like video. This will overlay the video on the screen. Requires jqModal.js.
$.fn.objectOverlay = function(w, h) {
	$(this).click(function() {
		var vid = $(this).attr("href");
		var objtag = "<div class=\"jqmWindow\">";
		objtag += "<a id=\"object-close\" class=\"jqmClose\" href=\"javascript:void(0);\">close X</a>";
		objtag += "<object id=\"vid\" width=\"" + w + "\" height=\"" + h + "\" data=\"" + vid + "\">";
		objtag += "<param name=\"src\" value=\"" + vid + "\"/>";
		objtag += "<p>Video for " + vid + "</p>";
		objtag += "</object></div>";
		$(objtag).appendTo("body").jqm({onHide: function(hash) {
			hash.w.remove();
			hash.o.remove();
			if ($.browser.msie)
				document.location = document.location;
		}}).jqmShow();
		return false;
	});
};

function pluralize(word, count) 
{
	var plural = "";
	if (count != 1) 
	{
		switch (word)
		{
			case "match":
				plural = "es";
				break;
			default:
				plural = "s";
				break;
		}
	}
	return count + " " + word + plural;
}

$.extend({
    inArray: function( elem, array, start ) {
        if (start == null)
            start = 0;
            
		for ( var i = start, length = array.length; i < length; i++ )
			if ( array[ i ] == elem )
				return i;

		return -1;
	},
	
	inArrayAll: function( elem, array ) {
	    var indices = [];
        var idx = $.inArray(elem, array, 0);
        while (idx != -1)
        {
            indices.push(idx);
            idx = $.inArray(elem, array, idx + 1);
        }
        
        return indices;
	}
});