(function(key, url, update, timer, statusArea, ircInfoArea, currentCount, ircInfoWrap, referrer, isLocalReferrer) {
	/*
	referrer = ((document.referrer || document.referer || '') + '').toLowerCase().replace(/^(?:[a-z]+:\/\/)?([^\/?#]+).*$/gim, '$1');
	isLocalReferrer = (referrer===location.hostname || referrer===location.host);
	if (window.console) {
		console.log(referrer, isLocalReferrer);
	}
	*/
	referrer = ((document.referrer || document.referer || '') + '').toLowerCase();
	isLocalReferrer = !!referrer.match(/^(http|https)\:\/\/([a-z0-9.-]*\.)?amoebaos\.org\//gim);
	
	// Make a JSONp request, and pass the response to a function
	var getJSONp = function(url, callback) {
		var script, id;
		id = 'jsonpcallback'+(this.count=(this.count || 0)+1);
		window[id] = function() {
			callback && callback.apply(callback, arguments);
			setTimeout(function() {
				window[id] = null;
				try { delete window[id]; }catch(err){}
				script.parentNode.removeChild(script);
				script = null;
			}, 10);
		};
		script = document.createElement("script");
		script.setAttribute("async", "async");
		script.setAttribute("src", url.replace('{callback}', id));
		(this.head || (this.head=document.getElementsByTagName("head")[0])).appendChild(script);
		return {
			stop : function() {
				window[id] = null;
				try { delete window[id]; }catch(err){}
				script.parentNode.removeChild(script);
				script = null;
			}
		};
	};
	
	
	key = "3d2763b0eda900e7f0287830700a6009";
	url = "http://api.mibbit.com/" + key + ".jsonp?callback={callback}";
	statusArea = document.getElementById("irc_users_display");
	ircInfoArea = document.getElementById("irc_info");
	ircInfoWrap = document.getElementById("irc_info_wrap");
	
	if (isLocalReferrer) {
		ircInfoWrap.style.top = '0px';
		ircInfoWrap.style.opacity = 0.8;
		ircInfoWrap.style.filter = 'alpha(opacity=80)';
	}
	else {
		(function(frame, timer, startTime, duration, startVal, endVal) {
			startTime = new Date().getTime();
			duration = 500;
			startVal = -100;
			endVal = 0;
			frame = function() {
				var time = new Date().getTime(),
					frac = (time-startTime)/duration,
					eased = (1 - Math.cos(frac*Math.PI))/2;
				if (frac<1) {
					timer = setTimeout(frame, 10);
				}
				else {
					frac = eased = 1;
					frame = timer = null;
				}
				ircInfoWrap.style.top = ((endVal-startVal)*eased + startVal) + 'px';
				ircInfoWrap.style.opacity = eased * 0.8;
				ircInfoWrap.style.filter = 'alpha(opacity=' + Math.round(eased*80) + ')';
			};
			timer = setTimeout(frame, 10);
		}());
	}
	
	
	update = function() {
		getJSONp(url, function(response) {
			var count = parseInt(response.mibbitians, 10) + 1;
			if (count!==currentCount) {
				statusArea.innerHTML = count + ' user' + (count===1?'':'s') + ' online';
				var i = 0, j = 1, interval;
				interval = setInterval(function() {
					i += j;
					if (i<=10) {
						//ircInfoArea.style.backgroundColor = "#" + Math.round(255-i*4).toString(16) + (255-i*2).toString(16) + (255-i*6).toString(16);
						ircInfoArea.style.backgroundColor = "#" + Math.round(255-i*6).toString(16) + (255-i*4).toString(16) + (255-i*2).toString(16);
					}
					if (j===1 && i>=15) {
						j = -1;
					}
					if (j===-1 && i<=0) {
						ircInfoArea.style.backgroundColor = "#ffffff";
						clearInterval(interval);
					}
				}, 50);
				currentCount = count;
			}
		});
	};
	
	timer = setInterval(update, 30*1000);		// once every 30 seconds
	update();
}());

