$(document).ready(function() {
	if ($("#UserUsername").length > 0 && $("#UserPassword").length > 0) {
		$("#UserUsername").field();
		$("#UserPassword").field();
	}

	$("#logout .apply-move").click(function() {return confirm("Are you sure you wish to apply the current mode public move to the current public game?");});
	$("#sidebar .tabs").tabs({selected: $("#Type").val() == "private" ? 0 : 1});
	$("#chatter .paginator a").paginator($("#chatter"));
	$("#games-list .paginator a").paginator($("#games-list"));
	$("#wait-list .paginator a").paginator($("#wait-list"));

	$(".remove-game").click(function() {
		if (confirm("Are you sure you wish to remove this game?")) {
			window.location = $(this).attr("href");
		}

		return false;
	});

	$(".button").hover(
		function() {
			$(this).addClass("hover").find("a").addClass("hover");
		},
		
		function() {
			$(this).removeClass("hover").find("a").removeClass("hover");
		}
	).mousedown(function() {
		$(this).addClass("active").find("a").addClass("active");
	}).mouseout(function() {
		$(this).removeClass("active").find("a").removeClass("active");
	}).click(function() {
		if ($(this).is(".clickable")) {
			window.location = $(this).find("a").attr("href");
			return false;
		}
	});
});

$.fn.field = function() {
	this.focus(function() {
		if (this.value == this.defaultValue) {
			this.value = ""; 
		}
	}).blur(function() {
		if (!this.value.length) {
			this.value = this.defaultValue;
		}
	});
	
	return this;
};

$.fn.paginator = function(container) {
	this.click(function() {
		var href = $(this).attr("href");

		container.css({overflow: "hidden"});
		container.animate({height: "0px", paddingTop: "0px", paddingBottom: "0px"}, 500, "linear", function() {
			$.ajax({
				method: "get",
				url: href,

				success: function(html) {
					var height = container.html(html).css({height: "auto"}).height();

					container.css({height: "0px"});
					container.animate({height: height, paddingTop: "10px", paddingBottom: "0px"}, 250);
					container.css({overflow: "visible"});
					container.find(".paginator a").paginator(container);
				}
			});
		});
	
		return false;
	});
	
	return this;
};