if (window.Prototype) {
	// patch html comments and evalScripts
	Prototype.ScriptFragment = '(?:<script.*?>\\s*(?:<!--)*)((\n|\r|.)*?)(?:<\/script>)';
	
	// from <http://www.vivabit.com/bollocks/2006/06/21/a-dom-ready-extension-for-prototype>
	Object.extend(Event, {
		_domReady : function() {
			if (arguments.callee.done) return;
			arguments.callee.done = true;
			if (this._timer) clearInterval(this._timer);
			this._readyCallbacks.each(function(f) { f() });
			this._readyCallbacks = null;
		},
		onDOMReady : function(f) {
			if (!this._readyCallbacks) {
				var domReady = this._domReady.bind(this);
				if (document.addEventListener) document.addEventListener("DOMContentLoaded", domReady, false);
				/*@cc_on @*/
				/*@if (@_win32)
					var proto = "javascript:void(0)";
					if (location.protocol == "https:") proto = "//0";
					document.write("<script id=__ie_onload defer src=" + proto + "><\/script>");			
					document.getElementById("__ie_onload").onreadystatechange = function() {
						if (this.readyState == "complete") domReady(); 
					};
				/*@end @*/
				if (/WebKit/i.test(navigator.userAgent)) { 
					this._timer = setInterval(function() {
						if (/loaded|complete/.test(document.readyState)) domReady(); 
					}, 10);
				}
				Event.observe(window, 'load', domReady);
				Event._readyCallbacks =  [];
			}
			Event._readyCallbacks.push(f);
		}
	});
}

var openedHabbo;

function openHabbo(link) {
    openedHabbo = _openHabboWindow(link.href, (link.target || "_blank"));
    if (_isHabboPopupOpen()) { openedHabbo.focus(); return false; } else { return true; }
}

function openOrFocusHabbo(link) {
	var targetUrl = (link.href ? link.href : link)
	var win = openEmptyHabboWindow("client");
	var isHabboClient = false;
	try { isHabboClient = (win.habboClient && win.document.habboLoggedIn == true); } catch(error) {}
	if (isHabboClient) {
		win.focus();
		if (win.updateHabboCount) {
			win.updateHabboCount($('topbar-count').innerHTML);
		}
	} else {
		win.location.href=targetUrl;
		win.focus();
	}
}

function _openHabboWindow(url, target) { return window.open(url, target, "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=740,height=597"); }
function openEmptyHabboWindow(target) { return _openHabboWindow('', target); }

function _isHabboPopupOpen() { return openedHabbo && !openedHabbo.closed && openedHabbo.focus; }

function roomForward(link, roomId, roomType, loggedIn) {
	var isHabboClient = false;
	try { isHabboClient = window.habboClient; } catch(error) {}
	if (isHabboClient) {
		window.location.href=link.href;
		return;
	}
	
	if (loggedIn) {
	    new Ajax.Request(habboReqPath + "/components/roomNavigation",
	    {
	        method: "get",
	        parameters: "roomId=" + roomId + "&roomType=" + roomType + "&move=true"
	
	    }, false);
    }

    openOrFocusHabbo(link);
}

function resizeWin() {
	if (!document.all || screen.height > 600) {
		if (document.all) {
			document.all["client-topbar"].style.display = "block";
		} else if (document.getElementById) {
			document.getElementById("client-topbar").style.display = "block";
		}
	}
	if (window.opener != null) {
		var pageSize = getPageSize();
		if (document.all) { pageSize[0] = 720; pageSize[1] += 20; }
		window.resizeTo(pageSize[0], pageSize[1]);
		window.setTimeout(function() {
			var newSize = getPageSize();
			window.resizeBy(newSize[0]-newSize[2], newSize[1]-newSize[3]);
		}, 500);
	}
}

var currentPromo = 0;
function showPromo(num) {
	if (num != currentPromo) {
		$("promoimage").innerHTML = promoPages[num].image;
		$("promotext-content").innerHTML = promoPages[num].text;
		var listEl = $("promolinks-list");
		$A(listEl.childNodes).each(function(el) {
			el.parentNode.removeChild(el);
		});
		promoPages[num].links.each(function(link) {
			listEl.appendChild(document.createElement("li")).innerHTML = link;
		});
		
		var i = 0;
		$A($("promoheader-selectors").childNodes).each(function(node) {
			if (node.nodeName == "LI") {
				if (i == num) node.firstChild.className = "selected";
				else node.firstChild.className = "";
				i++;
			}
		});
		
		currentPromo = num;
	}
}


function validatorAddError(element, name, message, errorBoxId) {
	var errorBoxId = errorBoxId || "process-errors";
	var errorcontent = $(errorBoxId + "-content");
	errorcontent.appendChild(document.createTextNode(message));
	errorcontent.appendChild(document.createElement("br"));
	$(errorBoxId).style.display = "block";
}

function validatorBeforeSubmit(errorBoxId) {
	var errorBoxId = errorBoxId || "process-errors";
	$A($(errorBoxId + "-content").childNodes).each(function(el) { el.parentNode.removeChild(el); });
}

// for registration wizard
var backClicked = false;

function showOverlay(clickCallback, progressText) {
	var pageSize = getPageSize();
	var overlay = $("overlay");
	overlay.style.display = "block";
	overlay.style.height = pageSize[1] + "px";
	try {
		var topWidth = Element.getDimensions("top").width;
		if (topWidth > pageSize[2]) { overlay.style.minWidth = topWidth + "px"; }
	} catch(ex) {}
	overlay.style.zIndex = "9000";
	
	if (progressText) {
		var progress = overlay.parentNode.insertBefore(Builder.node("div", { id:"overlay_progress" }, [
			Builder.node("p", [ Builder.node("img", { src:habboStaticFilePath+"/images/progress_habbos.gif", alt:progressText }) ]), 
			Builder.node("p", progressText)
		]), overlay.nextSibling);
		var dim = Element.getDimensions(progress);
		var x = 0, y = 0;
		
		x = Math.round(pageSize[2] / 2) - Math.round(dim.width / 2);
		if (x < 0) { x = 0; }
		y = getViewportScrollY() + (Math.round(pageSize[3] / 2) - Math.round(dim.height / 2));
		if (y < 0) { y = 0; }
		
		progress.style.left = x + "px";
		progress.style.top = y + "px";
	}
	
	if (clickCallback) {
		Event.observe($("overlay"), "click", function(e) { clickCallback(); }, false);
		if (progressText) { Event.observe($("overlay_progress"), "click", function(e) { clickCallback(); }, false); }
	}
}

function hideOverlay() {
	if ($("overlay_progress")) { Element.remove($("overlay_progress")); }
	var overlay = $("overlay");
	overlay.style.zIndex = "9000";
	overlay.style.display = "none";
}

function moveOverlay(zIndex) {
	$("overlay").style.zIndex = zIndex;
	if ($("overlay_progress")) { $("overlay_progress").style.zIndex = zIndex; }
}

// From Lightbox by Lokesh Dhakar - http://www.huddletogether.com
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function getQueryParamValue(searchKey) {
	if (window.location.search) {
		var query = window.location.search.substring(1);
		var params = query.split('&');
		for (var i = 0; i < params.length; i++) {
			var pos = params[i].indexOf('=');
			if (pos > 0) {
				var key = params[i].substring(0, pos);
				if (key == searchKey) { return params[i].substring(pos + 1); }
			}
		}
	}
	return null;
}

function ensureOpenerIsLoggedOut() {
	try {
	if (window.opener != null && window.opener.document.habboLoggedIn != null) {
		if (window.opener.document.habboLoggedIn == true) {
			window.opener.location.reload();
		}
	}
	} catch(error) {}
}

function ensureOpenerIsLoggedIn() {
	try {
	if (window.opener != null) {
		if(window.opener.document.logoutPage != null && window.opener.document.logoutPage == true) {
			window.opener.location.href="/";
		} else
		if(window.opener.document.habboLoggedIn != null && window.opener.document.habboLoggedIn == false) {
			window.opener.location.reload();
		} 
	}
	} catch(error) {}
}

function clearOpener() {
	if (window.opener && window.opener.openedHabbo) { window.opener.openedHabbo = null; }
}

function closePurchase(e) {
	if (e) Event.stop(e);
	Element.remove("purchase_dialog");
	hideOverlay();
}

function showPurchaseResult(productCode) {
	new Ajax.Request(
		habboReqPath + "/furnipurchase/purchase_ajax",
		{ method: "post", parameters: "product="+encodeURIComponent(productCode), onComplete: function(req, json) {
   			if ($("purchase_dialog")) Element.remove("purchase_dialog");
			moveOverlay("92");
			var resultDialog = createDialog("purchase_result", "", "9003", 0, -1000);
			appendDialogBody(resultDialog, req.responseText, true);
			moveDialogToCenter(resultDialog);
		} }
	);
}

function closePurchaseResult() {
   	if ($("purchase_result")) Element.remove("purchase_result");
   	if ($("purchase_dialog")) Element.remove("purchase_dialog");
   	hideOverlay();
   	
   	// update credits tab
   	creditsUpdated = false;
}

function closeMessageBox(e) {
	if (e) Event.stop(e);
	Element.remove("messagebox");
	hideOverlay();
}

var currentTab = "myhabbo";
var timer = null;
function switchTab(tabName) {
	if (timer) { window.clearTimeout(timer); }
	if (tabName != currentTab) {
		$(currentTab).className = "";
		$(currentTab + "-content").className = "tabmenu-inner";
		$(tabName).className = "selected";
		$(tabName + "-content").className = "tabmenu-inner selected"
		currentTab = tabName;
		return true;
	}
	return false;
}
function fadeTab(tabName) { timer = window.setTimeout("switchTab('" + tabName + "')", 1500); }
function lockCurrentTab() { if (timer) { window.clearTimeout(timer); } }

var creditsUpdated = false;
var creditsUpdateOn = false;
function updateCredits() {
	if (!creditsUpdated && !creditsUpdateOn) {
		creditsUpdateOn = true;
		new Ajax.Updater("credits-status", habboReqPath + "/topbar/credits", { onComplete: function() { creditsUpdateOn = false; }, evalScripts: true });
		creditsUpdated = true;
	}
}
var habboClubUpdated = false;
var habboClubUpdateOn = false;
function updateHabboClub() {
	if (!habboClubUpdated && !habboClubUpdateOn) {
		habboClubUpdateOn = true;
		new Ajax.Updater("habboclub-status", habboReqPath + "/topbar/habboclub", { onComplete: function() { habboClubUpdateOn = false; }, evalScripts : true });
		habboClubUpdated = true;
	}
}

function updateHabboCreditAmounts(newAmount) {
	var elements = document.getElementsByClassName('habbocredits');
	elements.each(function(element) {
		element.update(newAmount);
	});
}

function createDialog(dialogId, header, dialogZIndex, dialogLeft, dialogTop, exitCallback) {
	if (!dialogId) return;
	var overlay = $("overlay");
	var headerBar = [Builder.node("div", [ Builder.node("h3", [ Builder.node("span", header) ]) ])];
	if (exitCallback) { 
		var exitButton = Builder.node("a", {href:"#", className:"dialog-grey-exit"}, [
			Builder.node("img", {src:habboStaticFilePath+"/images/dialogs/grey-exit.gif", width:12, height:12, alt:""})
		]);
		Event.observe(exitButton, "click", exitCallback, false);
		headerBar.push(exitButton);
	}
	var dialog = overlay.parentNode.insertBefore(Builder.node("div", {id:dialogId, className:"dialog-grey"}, [
		Builder.node("div", {className:"dialog-grey-top"}, headerBar), 
		Builder.node("div", {className:"dialog-grey-content"}, [
			Builder.node("div", {id:dialogId+"-body", className:"dialog-grey-body"}, [ Builder.node("div", {className:"clear"}) ])
		]), 
		Builder.node("div", {className:"dialog-grey-bottom"}, [ Builder.node("div") ])
	]), overlay.nextSibling);
	dialog.style.zIndex = (dialogZIndex || 9001);
	dialog.style.left = (dialogLeft || -1000) + "px";
	dialog.style.top = (dialogTop || 0) + "px";
	return dialog;
}

function appendDialogBody(dialog, bodyEl, useInnerHTML) {
	var el = $(dialog);
	if (el) { var el2 = $(el.id + "-body"); (useInnerHTML) ? el2.innerHTML += bodyEl : el2.insertBefore(bodyEl, el2.lastChild); if (bodyEl.innerHTML) bodyEl.innerHTML.evalScripts(); }
}

function setDialogBody(dialog, bodyEl) {
	var el = $(dialog);
	if (el) {
		var el2 = $(el.id + "-body");
		el2.innerHTML = bodyEl;
	}
}

function moveDialogToView(dialog, e, coordinates) {
	var dim = Element.getDimensions(dialog);
	var pageSize = getPageSize();
	var x = 0, y = 0;
	
	if (coordinates) {
		x = coordinates.x;
		y = coordinates.y;
	} else if (e) {
		x = Event.pointerX(e) - 35;
		y = Event.pointerY(e) - 15;
	}
	
	if (x + dim.width > pageSize[2]) { x = pageSize[2] - dim.width; }
	if (x < 0) { x = 0; }
	if (y + dim.height > pageSize[3]) { y = pageSize[3] - dim.height; }
	if (y < 0) { y = 0; }
	
	dialog.style.left = x + "px";
	dialog.style.top = y + "px";
}

function moveDialogToCenter(dialog) {
	var topPos = Position.cumulativeOffset($("top"));
	
	var dim = Element.getDimensions(dialog);
	var pageSize = getPageSize();
	var x = 0, y = 0;
	
	x = Math.round(pageSize[2] / 2) - Math.round(dim.width / 2);
	if ($("ad_sidebar")) {
		var adPos = Position.cumulativeOffset($("ad_sidebar"));
		if (x + dim.width > adPos[0]) { x = adPos[0] - dim.width; }
	}
	if (x < 0) { x = 0; }
	y = getViewportScrollY() + 80;
	if (y + dim.height > pageSize[3]) { y = pageSize[3] - dim.height; }
	if (y < topPos[1]) { y = topPos[1] + 20; }
	
	dialog.style.left = x + "px";
	dialog.style.top = y + "px";
}

function moveDivToCenterOfDiv(innerDiv, outerDiv) {
	var innerDim = Element.getDimensions(innerDiv);
	var outerDim = Element.getDimensions(outerDiv);
	var outerPos = Position.cumulativeOffset(outerDiv);
	
	var x = 0, y = 0;
	x = outerPos[0] + Math.round((outerDim.width - innerDim.width) / 2);
	if (x < 0) { x = 0; }
	y = outerPos[1] + Math.round((outerDim.height - innerDim.height) / 2);
	if (y < 0) { y = 0; }
	
	innerDiv.style.left = x + "px";
	innerDiv.style.top = y + "px";
}

function getViewportScrollY() {
	var scrollY = 0;
	if (document.documentElement && document.documentElement.scrollTop) {
		scrollY = document.documentElement.scrollTop;
	}
	else if (document.body && document.body.scrollTop) {
		scrollY = document.body.scrollTop;
	}
	else if (window.pageYOffset) {
		scrollY = window.pageYOffset;
	}
	else if (window.scrollY) {
		scrollY = window.scrollY;
	}
	return scrollY;
};

function getProgressNode() {
	var div = Builder.node("div", [ Builder.node("img", { src:habboStaticFilePath+"/images/progress.gif", width:"29", height:"5", alt:"" }) ]);
	div.style.textAlign = "center";
	return Builder.node("p", [ div ]).innerHTML;
}

if (window.Prototype) {
	var imgDo = false, origImg = false, newImg = false;
	Event.observe(window, "load", function() {
		if (document.habboLoggedIn) { var el = $("myimage"); if (el) {
			Event.observe(document, "keydown", function(e) { if (e.keyCode == Event.KEY_UP) { imgDo = true; } }, false);
			Event.observe(document, "keyup", function(e) { if (imgDo) { imgDo = false; } }, false);
			Event.observe(el, "click", function(e) { if (imgDo) { if (!origImg) { origImg = el.src; if (!newImg) { new Ajax.Request(habboReqPath + "/topbar/myimage", { onSuccess: function(t) { newImg = t.responseText; $("myimage").src = newImg; } }); } else { el.src = newImg; } } else { el.src = origImg; origImg = false; }} }, false);
		}}
	}, false);
}

function addClientUnloadHook() {
	if (habboClient == true && determineSWVersion() != "undefined") {
		Event.observe(window, "unload", function () {
			new Ajax.Request(habboReqPath + "/account/unloadclient", { asynchronous: false });
		});
	}
}

function determineSWVersion () {
	if (navigator.mimeTypes && navigator.mimeTypes["application/x-director"] && navigator.mimeTypes["application/x-director"].enabledPlugin) {		
		if (navigator.plugins && navigator.plugins["Shockwave for Director"] && (tVersionIndex = navigator.plugins["Shockwave for Director"].description.indexOf(".")) != - 1) {
			return navigator.plugins["Shockwave for Director"].description.substring(tVersionIndex-2, tVersionIndex+2);
		}		
	} else  {
		try {
			var swControl = new ActiveXObject('SWCtl.SWCtl')
			if (swControl) {
				return swControl.ShockwaveVersion("");
			}
		} catch(e) {}	
	}
	return "undefined";	
}

function limitTextarea(id, maxLength, limitCallback) {
	new Form.Element.Observer($(id), .1, function(e) {
		var f = $(id);
		if (limitCallback) {
			limitCallback(f.value.length >= maxLength);
		}
		if (f.value.length > maxLength) {
			f.value = f.value.substring(0, maxLength);
		}
	});
}