// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
	var expires;
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		expires = "; expires=" + date.toGMTString();
	}
	else {
		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) {
		if (word == "match") {
				plural = "es";
		} else {
				plural = "s";
		}
		/* switch is overkill for one comparison
		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;
	}
});

$(function() {
	initOverLabels();
	$("ul.piped-list>li:not(:last-child)").append("<span>|</span>");
});