	var stopPolling = false;
	function checkForMessages () {
		}
	
	function checkCallback (str) {
		var pair = str.split(",");
		for (i = 0; i < pair.length; i++) {
			var user = pair[i].split(";")[0];
			var room = pair[i].split(";")[1];
			var time = pair[i].split(";")[2];
			
			// If messenger is turned off
			if (user == 'stopPolling') {
				stopPolling = true;
			}
			//
			
			room = "" + room; //Convert to JavaScript string
			var message = time;
			user = urldecode(user);
			if (user && room) {
				// Tjek query success
				if (user.indexOf("Query failed") == -1) {
					popit(user, message, 'normal', room);
				} else {
					break;
				}
			}
		}
	}
	
	function rejectConversation (username) {
	
		var url = "http://www.playtopia.com/ajax/im.php?userName=&p=true&rejectedUsername=" + username + "&action=rejectConversation";
		makeHttpRequest(url, "dummy");
	}
	
	var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	
	function encode64(input) {
	   var output = "";
	   var chr1, chr2, chr3;
	   var enc1, enc2, enc3, enc4;
	   var i = 0;
	
	   do {
	      chr1 = input.charCodeAt(i++);
	      chr2 = input.charCodeAt(i++);
	      chr3 = input.charCodeAt(i++);
	
	      enc1 = chr1 >> 2;
	      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
	      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
	      enc4 = chr3 & 63;
	
	      if (isNaN(chr2)) {
	         enc3 = enc4 = 64;
	      } else if (isNaN(chr3)) {
	         enc4 = 64;
	      }
	
	      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
	         keyStr.charAt(enc3) + keyStr.charAt(enc4);
	   } while (i < input.length);
	   
	   return output;
	}
	
	function decode64(input) {
	   var output = "";
	   var chr1, chr2, chr3;
	   var enc1, enc2, enc3, enc4;
	   var i = 0;
	
	   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
	   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
	
	   do {
	      enc1 = keyStr.indexOf(input.charAt(i++));
	      enc2 = keyStr.indexOf(input.charAt(i++));
	      enc3 = keyStr.indexOf(input.charAt(i++));
	      enc4 = keyStr.indexOf(input.charAt(i++));
	
	      chr1 = (enc1 << 2) | (enc2 >> 4);
	      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
	      chr3 = ((enc3 & 3) << 6) | enc4;
	
	      output = output + String.fromCharCode(chr1);
	
	      if (enc3 != 64) {
	         output = output + String.fromCharCode(chr2);
	      }
	      if (enc4 != 64) {
	         output = output + String.fromCharCode(chr3);
	      }
	   } while (i < input.length);
	
	   return output;
	}
	
	function cleanStr(winName) {
		var rgx = /[^a-zA-Z0-9]/;
		while (rgx.test(winName)) {
			winName = winName.replace(rgx, '');
		}
		
		return winName;
	}
	
	function openIm (receiverid, roomname) {
		var w			= 480;
		var h 			= 380;
		if (screen.width) {
			var winl = (screen.width-w)/2;
			var wint = (screen.height-h)/2;
		} else {
			winl = 0;
			wint = 0;
		}
		if (winl < 0) {
			winl = 0;
		}
		if (wint < 0) { 
			wint = 0; 
		}
		settings = 'scrollbars = 0,'; 
		settings += 'resizable = 0,';
		settings += 'width=' + w + ',';
		settings += 'height=' + h + ',';
		settings += 'top=' + wint + ',';
		settings += 'left=' + winl + ',';
		settings += 'status=1';
		
		var winName = 'im' + cleanStr(receiverid);
		window.open('/im/messenger.php?receiverid=' + receiverid + '&room=' + roomname + '', winName, settings);
	}
	
	function InviteUserToIm (senderid, receiverid) {
			var senderid = senderid.toString();
		var receiverid = receiverid.toString();
		d = new Date();
		unix = Math.floor(d.getTime()/1000);
		var roomname 	= "IM" + cleanStr(senderid) + cleanStr(receiverid);
		openIm (receiverid, roomname);
		}
	
	// IM - START
	
	var ar = new Array();
	
	function inArrayIM(array,test) {
	    for (var i = 0; i < array.length; i++) {
	        if(test == array[i]) {
	            return true;
	            break;
			}
		}
		return false;
	}
	
	function addToArrayIM (val) {
		if (!inArrayIM(ar, val)) {
			ar[ar.length] = val;
		}
	}
	
	function remFromArrayIM (val, which) {
	    for (var i = 0; i < ar.length; i++) {
	        if (val == ar[i]) {
				ar.splice(i,1);
			}
		}
		rePosition(which);
	}
	
	function getPosArray (val) {
	    for (var i = 0; i < ar.length; i++) {
	        if (val == ar[i]) {
				pos = i;
				return pos;
				break;
			}
		}
	}
	
	function rePosition (which) {
		if (navigator.appName == "Microsoft Internet Explorer"){
			var lefty = Math.round(((document.body.offsetWidth))-250);
		}
		if (navigator.appName == "Netscape"){
			var lefty = Math.round(((window.innerWidth))-250);
		}
		for (var i = 0; i < ar.length; i++) {
			var elm = document.getElementById(cleanStr(ar[i]));
			elm.style.bottom = i*120;
		}
	}
	
	var popupIM = new Object();
	popupIM.closed = true;
	function popit(userName, message, which, room) { // OK
		if (which == 'normal') {
			if (!popupIM.closed) {
				popupIM.popit(userName, message, 'game', room);
			}
		}
		var obj = document.getElementById(userName);
		
		message = message.substring(0, 50);
		if (!obj) {
			pos = getPosArray(userName);
			var bottomVar = -120;
			addToArrayIM(userName);
	
			$(document).ready(function () {
				$('div#msgHolder').show();
			});
			
			var html = document.getElementById('msgHolder').innerHTML;
			var divHTML = '<div id="'+cleanStr(userName)+'" style="z-index: 1000; position: absolute; display: none; width: 230px; height: 120px; right: 15px; bottom: '+bottomVar+'px;">';
			divHTML 	+= '<iframe src="/messenger_slider.php?userName='+userName+'&message='+message+'&bottom='+bottomVar+'&room='+room+'" name="iframe'+cleanStr(userName)+'" id="iframe'+cleanStr(userName)+'" WIDTH="230" HEIGHT="120" MARGINWIDTH="0" MARGINHEIGHT="0" FRAMEBORDER="0" SCROLLING="no"></iframe>';
			divHTML 	+= '</div>';
			document.getElementById('msgHolder').innerHTML = html + divHTML;
			document.getElementById(cleanStr(userName)).style.display = 'block';
			move_div_up(userName, which);
		} else {
			document.getElementById(cleanStr(userName)).style.display = 'block';
		}
	}
	
	function show_popup(userName, room, which) { // �bner selve beskedvinduet FRA slideren
		if (which == 'normal') {
			move_div_down(userName, which);
			if (!popupIM.closed) { // Popup er �ben
				if (popupIM.document.getElementById(cleanStr(userName)).style.display == 'block') {
					popupIM.move_div_down(userName, 'game');
				}
			}
			openIm(userName, room);
		} else {
			if (opener) {
				if (!opener.closed){ // �ben
					opener.show_popup(userName, 'normal');
				} else {
					move_div_down(userName, 'game');
					openIm(userName, room);
				}
			} else {
				move_div_down(userName, 'game');
				openIm(userName, room);
			}
		}
	}
	
	function move_div_up(userName, which) {
		var bott = document.getElementById(cleanStr(userName));
		var pos = getPosArray(userName);
		var tmpNum = ((pos)*120);
		if (bott) {
			var bTmp = bott.style.bottom;
			if ((bTmp.substr(0,bTmp.length-2)*1) < tmpNum) {
				bott.style.bottom = (bTmp.substr(0,bTmp.length-2)*1) + (1*8) + 'px';
				setTimeout("move_div_up('" + userName + "', '" + which + "');",20);
			} else {
				rePosition(which);
			}
		} else {
			setTimeout("move_div_up('" + userName + "', '" + which + "');",1000);
		}
	}
	
	function move_div_down(userName, which) { // OK
		var userName = userName;
		var which = which;
		var bott = document.getElementById(cleanStr(userName));
	
		if (bott) {
			if (bott.style.bottom > '-120') {
				bott.style.bottom = (bott.style.bottom.substr(0,bott.style.bottom.length-2)*1) - (1*8) + 'px';
				setTimeout('move_div_down(\''+userName+'\', \''+which+'\')', 10);
			} else {
				bott.parentNode.removeChild(bott);
				bott.innerHTML = '';
				bott.outerHTML = '';
				bott.style.display = 'none';
				remFromArrayIM(userName, which);
			}
		} else {
			setTimeout('move_div_down(\''+userName+'\', \''+which+'\')',1000);
		}
	}
	
	
	function switch_gfx(w,user) { // OK
		var obj = document.getElementById('x_knap'+user);
		if (w == 'hover') {
			obj.src = '../../images/im/close_x_hover.gif';
		} else {
			obj.src = '../../images/im/close_x_normal.gif';
		}
	}
	
	function inThisChat(userName, room) {
		parent.makeHttpRequest("http://www.playtopia.com/ajax/im.php?userName=&p=true&room="+room+"&action=inThisChat", "window.frames['iframe"+cleanStr(userName)+"'].isInRoom");
		setTimeout("inThisChat('"+userName+"', '"+room+"');", 10000);
	}
	
	function isInRoom(str) {
		if (str == 'YES') {
			parent.move_div_down('', 'normal');
		}
	}
	// IM - SLUT
	
	
	function urldecode( str ) {
	    // http://kevin.vanzonneveld.net
	    // +   original by: Philip Peterson
	    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	    // +      input by: AJ
	    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	    // +   improved by: Brett Zamir (http://brettz9.blogspot.com)
	    // +      input by: travc
	    // +      input by: Brett Zamir (http://brettz9.blogspot.com)
	    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	    // +   improved by: Lars Fischer
	    // %          note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
	    // *     example 1: urldecode('Kevin+van+Zonneveld%21');
	    // *     returns 1: 'Kevin van Zonneveld!'
	    // *     example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
	    // *     returns 2: 'http://kevin.vanzonneveld.net/'
	    // *     example 3: urldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a');
	    // *     returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a'
	    
	    var histogram = {};
	    var ret = str.toString();
	    
	    var replacer = function(search, replace, str) {
	        var tmp_arr = [];
	        tmp_arr = str.split(search);
	        return tmp_arr.join(replace);
	    };
	    
	    // The histogram is identical to the one in urlencode.
	    histogram["'"]   = '%27';
	    histogram['(']   = '%28';
	    histogram[')']   = '%29';
	    histogram['*']   = '%2A';
	    histogram['~']   = '%7E';
	    histogram['!']   = '%21';
	    histogram['%20'] = '+';
	    histogram['\u00DC'] = '%DC';
	    histogram['\u00FC'] = '%FC';
	    histogram['\u00C4'] = '%D4';
	    histogram['\u00E4'] = '%E4';
	    histogram['\u00D6'] = '%D6';
	    histogram['\u00F6'] = '%F6';
	    histogram['\u00DF'] = '%DF'; 
	    histogram['\u20AC'] = '%80';
	    histogram['\u0081'] = '%81';
	    histogram['\u201A'] = '%82';
	    histogram['\u0192'] = '%83';
	    histogram['\u201E'] = '%84';
	    histogram['\u2026'] = '%85';
	    histogram['\u2020'] = '%86';
	    histogram['\u2021'] = '%87';
	    histogram['\u02C6'] = '%88';
	    histogram['\u2030'] = '%89';
	    histogram['\u0160'] = '%8A';
	    histogram['\u2039'] = '%8B';
	    histogram['\u0152'] = '%8C';
	    histogram['\u008D'] = '%8D';
	    histogram['\u017D'] = '%8E';
	    histogram['\u008F'] = '%8F';
	    histogram['\u0090'] = '%90';
	    histogram['\u2018'] = '%91';
	    histogram['\u2019'] = '%92';
	    histogram['\u201C'] = '%93';
	    histogram['\u201D'] = '%94';
	    histogram['\u2022'] = '%95';
	    histogram['\u2013'] = '%96';
	    histogram['\u2014'] = '%97';
	    histogram['\u02DC'] = '%98';
	    histogram['\u2122'] = '%99';
	    histogram['\u0161'] = '%9A';
	    histogram['\u203A'] = '%9B';
	    histogram['\u0153'] = '%9C';
	    histogram['\u009D'] = '%9D';
	    histogram['\u017E'] = '%9E';
	    histogram['\u0178'] = '%9F';
	 
	    for (replace in histogram) {
	        search = histogram[replace]; // Switch order when decoding
	        ret = replacer(search, replace, ret) // Custom replace. No regexing   
	    }
	    
	    // End with decodeURIComponent, which most resembles PHP's encoding functions
	    ret = decodeURIComponent(ret);
	 
	    return ret;
	}
