function changeChannel(type, channel) {
	// If we opened a popup window (click & hold), don't change the main video
	if (popoutOpenedWindow == true) {
		popoutOpenedWindow = false;
		return;
	}
	if (type == 'jtv') {
		document.getElementById("embed").innerHTML = '<object id="objvideo" type="application/x-shockwave-flash" width="' + embedwidth + '" height="' + embedheight + '" id="live_embed_player_flash" data="http://www.justin.tv/widgets/live_embed_player.swf?channel=' + jsonData[0].jtv[channel].jtvname + '" bgcolor="#000000"><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="allowNetworking" value="all" /><param name="movie" value="http://www.justin.tv/widgets/live_embed_player.swf" /><param name="flashvars" value="channel=' + jsonData[0].jtv[channel].jtvname + '&auto_play=true&start_volume=25" /><param name="wmode" value="transparent" /></object>';
		setTitle(jsonData[0].jtv[channel].title);
		ishighlight = false;
	}
	if (type == 'jtvhighlight') {
		document.getElementById("embed").innerHTML = '<object id="objvideo" type="application/x-shockwave-flash" height="' + embedheight + '" width="' + embedwidth + '" id="clip_embed_player_flash" data="http://www.justin.tv/widgets/archive_embed_player.swf" bgcolor="#000000"><param name="movie" value="http://www.justin.tv/widgets/archive_embed_player.swf" /><param name="allowScriptAccess" value="always" /><param name="allowNetworking" value="all" /><param name="allowFullScreen" value="true" /><param name="autoplay" value="true" /><param name="flashvars" value="auto_play=true&start_volume=25&title=LordKaT Highlights&channel=lordkat&archive_id=' + jsonData[1].highlights[channel].id + '" /><param name="wmode" value="transparent" /></object>';
		setTitle(jsonData[1].highlights[channel].title);
		ishighlight = true;
	}
	currentstream = type;
	currentchannel = channel;
	return;
}

function buildChannels() {
	var jtv = document.getElementById("jtv");
	jtv.innerHTML = "<center>";
	for (var i = 0; i <	jsonData[0].jtv.length; i++) {
		if (jsonData[0].jtv[i].online == 1)
			jtv.innerHTML += "<a href=\"javascript:changeChannel('jtv', '" + i + "');\" onMouseOver=\"showTooltipImage(event, '" + jsonData[0].jtv[i].screen + "');\" onMouseOut=\"hideTooltip();\" onMouseDown=\"popoutDown('video', 'jtv', '" + jsonData[0].jtv[i].jtvname + "');\" onMouseUp=\"popoutUp();\"><img src=\"" + jsonData[0].jtv[i].image + "\"></a>";
		else
			jtv.innerHTML += "<a href=\"javascript:changeChannel('jtv', '" + i + "');\" onMouseOver=\"showTooltip(event, '" + jsonData[0].jtv[i].title + "');\" onMouseOut=\"hideTooltip();\" onMouseDown=\"popoutDown('video', 'jtv', '" + jsonData[0].jtv[i].jtvname + "');\" onMouseUp=\"popoutUp();\"><img src=\"" + jsonData[0].jtv[i].image + "\" class=\"offline\"></a>";
		if ((i+1) % 5 == 0)
			jtv.innerHTML += "<br />";
	}
	jtv.innerHTML += "</center>";
	return;
}

function buildHighlights() {
	var highlights = document.getElementById("jtvhighlights");
	highlights.innerHTML = "";
	for (var i = 0; i < jsonData[1].highlights.length; i++) {
		highlights.innerHTML += "<div class=\"highlight\"><a href=\"javascript:changeChannel('jtvhighlight', '" + i + "');\" class=\"highlight\" onMouseDown=\"popoutDown('video', 'jtvhighlight', '" + jsonData[1].highlights[i].id + "');\" onMouseUp=\"popoutUp();\">" + jsonData[1].highlights[i].title + "</a></div>";
	}
	return;
}

function buildSocial() {
	var social = document.getElementById("social");
	social.innerHTML = "";
	for (var i = 0; i < jsonData[2].social.length; i++) {
		social.innerHTML += "<div class=\"plusbox\"><img src=\"" + jsonData[2].social[i].image + "\" align=\"left\" width=\"50\" height=\"50\" onMouseOut=\"hideTooltip();\" onMouseOver=\"showTooltip(event, '" + jsonData[2].social[i].name + "');\" /><a href=\"" + jsonData[2].social[i].url + "\" class=\"plusbox\" target=\"_gplus\">" + jsonData[2].social[i].content + "</a></div><br clear=\"all\" />";
	}
	return;
}

function buildBanners() {
	var d = new Date();
	if (currentbanner + 1 >= jsonData[3].banners.length)
		currentbanner = 0;
	else
		currentbanner++;
	document.getElementById("banners").innerHTML = "<img src=\"" + jsonData[3].banners[currentbanner] + "?bs=" + d.getTime() + "\" />";
	return;
}

function loadJSON() {
	var xmlhttp;
	if (window.XMLHttpRequest) {
 		xmlhttp = new XMLHttpRequest();
	}
	else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			jsonData = eval('(' + xmlhttp.responseText + ')');
			buildBanners();
			buildChannels();
			buildHighlights();
			buildSocial();
			if (firstload == true) {
				if (sizepref != null && sizepref != "" && sizepref != "normal")
				resizeFlash(sizepref);
				firstload = false;
			}
			if (ishighlight == false)
				setTitle(jsonData[0].jtv[currentchannel].title);
			document.getElementById("pageloader").style.display = "none";
		}
	}

	// We have to add some bullshit at the end to get IE to stop retrieving cached data
	var d = new Date();
	xmlhttp.open("GET", "data.json?bs=" + d.getTime(), true);
	xmlhttp.send();
	setTimeout("loadJSON()", 30000);
	return;
}

function loadIRC(width, height) {
	document.getElementById("irccontainer").innerHTML = '<object id="objchat" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '" data="http://flashirc.geekshed.net/tflash.php?embed=1&amp;joinonconnect=lordkat&amp;chatonly=0&amp;isrestricted=0&amp;altfont=0&amp;key=&amp;nick="><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="http://flashirc.geekshed.net/tflash.php?embed=1&amp;joinonconnect=lordkat&amp;chatonly=0&amp;isrestricted=0&amp;altfont=0&amp;key=&amp;nick="><param name="quality" value="high"></object>';
	return;
}

function resizeFlash(size) {
	var width = 640;
	var height = 400;
	var objvideo = document.getElementById("objvideo");
	var objchat = document.getElementById("objchat");
	var embed = document.getElementById("embed");
	var irc = document.getElementById("irccontainer");
	var container = document.getElementById("container");

	embed.style.visibility = "hidden";
	irc.style.visibility = "hidden";

	if (size == 'xxsmall') {
		width = 320;
		height = 200; 
	}

	if (size == 'xsmall') {
		width = 480;
		height = 300; 
	}

	if (size == 'small') {
		width = 520;
		height = 325;
	}
	if (size == 'normal') {
		width = 640;
		height = 400;
	}
	if (size == 'large') {
		width = 800;
		height = 500;
	}
	if (size == 'huge') {
		width = 1024;
		height = 640;
	}

	embedwidth = width;
	embedheight = height; 

	container.style.width = "" + (width * 2) + "px";
	embed.style.width = "" + width + "px";
	embed.style.height = "" + height + "px";
	embed.style.float = "left";
	irc.style.width = "" + width + "px";
	irc.style.height = "" + height+ "px";
	irc.style.float = "right";		

	if (objvideo != null) {
		objvideo.setAttribute("width", width);
		objvideo.setAttribute("height", height);
	}

	if (size == 'small' || size == 'xsmall' || size == 'xxsmall') {
		document.getElementById("social").style.visibility = "hidden";
		document.getElementById("social").style.width = "0px";
		document.getElementById("social").style.height = "0px";
		document.getElementById("cal").style.visibility = "hidden";
		document.getElementById("cal").style.width = "0px";
		document.getElementById("cal").style.height = "0px";
		document.getElementById("banners").style.visibility = "hidden";
		document.getElementById("banners").style.width = "0px";
		document.getElementById("banners").style.height = "0px";
	}
	else {
		document.getElementById("social").style.visibility = "visible";
		document.getElementById("social").style.width = "250px";
		document.getElementById("social").style.height = "200px";
		document.getElementById("cal").style.visibility = "visible";
		document.getElementById("cal").style.width = "250px";
		document.getElementById("cal").style.height = "200px";
		document.getElementById("banners").style.visibility = "visible";
		document.getElementById("banners").style.width = "560px";
		document.getElementById("banners").style.height = "95px";			
	}

	changeChannel(currentstream, currentchannel);
	loadIRC(embedwidth, embedheight);
	embed.style.visibility = "visible";
	irc.style.visibility = "visible";
	setCookie("size", size, 1024);
	return;
}

function main() {
	sizepref = getCookie("size");
	loadJSON();
	loadIRC(embedwidth, embedheight);
	return;
}

