function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}



$(document).ready(function(){
	if($("#nav")) {
		$("#nav dd").hide();
		$("#nav dt b").click(function() {
			if(this.className.indexOf("clicked") != -1) {
				$(this).parent().next().slideUp(200);
				$(this).removeClass("clicked");
			}
			else {
				$("#nav dt b").removeClass();
				$(this).addClass("clicked");
				$("#nav dd:visible").slideUp(200);
				$(this).parent().next().slideDown(500);
			}
			return false;
		});
		
	}
	// Ive added a new jquery function, its ajax/geturlvar.js.
	// It read out the vars from the url, and here it reads ut the
	// ?game=
	if ($.getUrlVar('game') != '')
	{
		// Show the 2nd parent of the li, the dd.
		$("#game"+$.getUrlVar('game')).parent().parent().show();
		// It changes the child of the li, the a.
		// So that the css corresponds to a hovered link item.
		$("#game"+$.getUrlVar('game'))
			.children()
			.css("background-image", "url(../_img/navbgs/rollover.png)")
			.css("background-repeat", "no-repeat")
			.css("background-position", "left")
			.css("color", "#000")
			.css("padding", "0 10px 2px 10px")
			.css("display", "block");
	}
	
//});
//$(document).ready(function () {
									
		// Hide the screen-overlay div when the document is loaded.
		$("#screen-overlay").hide();
		$("#container").hide();
		
		// Set the window variable 'checkChallenge' to 'true'. (if you type window before a variable it can
		// be accessed in a function also!
		window.checkChallenge = true;
		
		// Set an interval so the function "connect()" will be called every 2000 millisecond.
		window.setInterval(function () { connect(); }, 3000);
		
		// The function to connect to the page select.php
		function connect() {
			// Here it will get the JSON from the select page, and then put all the data in the
			// variable "data".
			
			//$.getJSON('/popupchallenge.php', function(data) {
														
					$.getJSON('/popupchallenge.php', function(data) {
					if (data) {
					 var isChal = data.action;
					}
					else {
					 var isChal = 'nochallenge';
					}
					if ((isChal == 'challenge') && (window.checkChallenge == true)) {
					
				// Here we check that if the "action" value from the select.php page is
				// "challenge" AND that window.checkChallenge is true.
				//if ((data.action == 'challenge') && (window.checkChallenge == true)) {
					
					// Write some html inside the screen-overlay div.
					$("#title").html(data.title);
					$("#challenge").html(data.name);
					$("#level").html(data.level);
					$("#country").html("Battling From: " + data.country);
					$("#buttons").html(data.buttons);
					$("#game").html(data.game);
					$("#console").html(data.console);
					// Then we fade in the #screen-overlay div
					$("#screen-overlay").fadeIn("slow");
					$("#container").fadeIn("slow");
					$("#container").css('z-index', '999');
					$("#container").css('position', 'absolute');
					
					// Start the countdown function
					countdownTimeout(data.timeout);
					
					// Now that we already got a challenge, set the variable checkChallenge
					// to false so that if statement is false.
					window.checkChallenge = false;
					
					// Here we set an timeout on how long the div will be shown.
					window.setTimeout( function () {
						// This is what to do after the data.timeout time has passet.
						// In this case, we fadeOut the div slow.
						$("#screen-overlay").fadeOut("slow");
						$("#container").fadeOut("slow");
						$("#container").css('z-index', '1');
						$("#container").css('position', 'relative');
						
						// Now that we want to check for new challenges, set the
						// checkChallenge variable to "true"
						window.checkChallenge = true;
						
						// data.timeout comes from the select.php document.
						// So that we can specify an timeout from outside this script.
					}, data.timeout);
				} 
			});
		}
		
		// The function for the timeout count
		function countdownTimeout(timeout) {
			
			// This will start the displayTimeout function
			displayTimeout(timeout);
			
			// The function to write out the time left.
			function displayTimeout(sec) {
				// Put the htlm inside the timeout div. The / 1000 is because everything is in
				// milliseconds.
				$("#timeout").html("Popup Closes in " + sec / 1000 + " Seconds")
				// As long as the variable sec is larger then 0, continue to count.
				if (sec > 0) {
					// Set a timeout so it will only write it the time left every 1000ms (1s)
					setTimeout(function () { countdownTimeout(sec - 1000); }, 1000);
				}
			}
			
		}
		
	});

