// Constants for Main Room
var MIN_POLL_INTERVAL = 500;
var MAX_POLL_INTERVAL = 3000;
var POLL_INTERVAL_STEP = 200; // Each time there's no update, increase polling interval by fixed amt
var ROOM_HEIGHT = 590;
var ROOM_WIDTH = 770;
var EMBED_IMAGE_WIDTH = 300;

// Constants for Chat Room
var LIST_UPDATE_INTERVAL = 60000;
var UPDATE_CLEAR_MESSAGE_DELAY = 5000;
var NEWEST = 0;
var MOST_POPULAR = 1;

/**
 * Creates an XMLHttpRequest object in a cross-browser compatible way. 
 * @return a valid XMLHttpRequest object for making ajax calls.
 */
function XMLRequest() {
  if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    request = new ActiveXObject("Msxml2.XMLHTTP");
    if (!request) {
      request = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return request;
}

/**
 * Sends off an HTTP request to update the user's nickname. Momentarily replaces
 * the "Change Nick" link with a status message telling the user that the nick 
 * has been changed.
 */
 
function changeNick() {
	newNick = $("nick").value;	
	var date = new Date();
	var request = XMLRequest();
	if (request) { 
	   	request.onreadystatechange = function() {
			if (request.readyState == 4) {
        		if (request.status == 200) {
          			$("nickStatus").innerHTML = "Nick changed";
					$("changeNickLink").style.display = "none";
					setTimeout(restoreChangeNickLink, UPDATE_CLEAR_MESSAGE_DELAY);
        		}
			}
    	}; 
		var date = new Date();	
		request.open("GET", "/nick?n="+encodeURIComponent(newNick)+"&z="+date.getTime(), true);
		request.send("");
  	}
}

function restoreChangeNickLink() {
	$("nickStatus").innerHTML = "";
	$("changeNickLink").style.display = "";
}