var availID = "";

$(document).ready(function() {
	/* $("<div id=\"popUp\" class=\"xWin\">Square added. <a href=\"/confirm.asp\">You can pay for these squares now</a> or close this window to continue adding more.</div>").appendTo("body"); */

	$("#thegrid a, #minigrid a").Tooltip({
		track: true,
		extraClass: "bubble",
		showURL: false,
		fixPNG: true,
		showBody: "|",
		delay: 50
	});
	
	$("#thegrid a.taken").attr("target","_blank").click(function() {
		// AJAX CLICK TRACKER
		squareID = $(this).attr("rel");
		$.post("/track.asp",{
			SquareID: squareID
		},function(response) {
			if (response == "OK") {
				return true;
			}
		});
	});
	
	$("#thegrid a.avail").click(function() {
		availID = $(this).id();
		var theDate = new Date();
		
		$.ajax({
			type: "GET",
			url: $(this).href() + "&js=1&cb=" + theDate.getTime(),
			dataType: "html",
			success: function(resp) {
				switch (resp) {
					case "BadN":
					case "NaN":
						alert("Invalid selection");
					break;
					case "SqOwned":
						alert("The square you chose has been taken");				
					break;
					case "Selected":
						alert("You have already chosen that square");				
					break;
					case "OK":
						var theDate = new Date();
						$("#" + availID).addClass("selected");
						$("#popUp").xWin({
							show: "si",
							title: "Thank you",
							status: "milliondollarwomen.co.uk",
							width: 300,
							height: 100,
							start: "center",
							resizable: "no",
							moveable: "no"
						});
					break;
					default:
						alert("Sorry, but an error has occured.");
					break;
				}
			}
		});
		
		return false;
	});
});
