var video_playing_stream = null;
var video_current_stream = null;

function video_start(stream, pos, vid)
{
	var so = new SWFObject("/images/video.swf?version7", "video_player", "480", "340", "9.0.115", "#000000");
	so.addParam("allowScriptAccess", "sameDomain");
	so.addParam("allowFullScreen", "true");
	if (video_auto_play)
		so.addVariable("autoStart", "1");
	so.addVariable("disableLogo", "1");
	so.addVariable("sWidth", screen.width);
	so.addVariable("sHeight", screen.height);
	so.addVariable("initXML", plus_escape("/data/video-player.php?video=" + vid + "&stream=" + stream + (pos ? "&pos=" + pos : "") + (video_show_featured ? "&featured=1" : "")));

	if (video_live_stream)
		so.addVariable("live", 1);

	if (pos)
		so.addVariable("goto", pos);

	so.write("video_player_content");

	document.getElementById("video-high-link").className = "";
	document.getElementById("video-low-link").className = "";
	document.getElementById("video-" + video_current_quality + "-link").className = "selected";

	video_playing_stream = stream;
	video_auto_play = true;

	var image = new Image();
	image.src = "/data/video-track.php?vid=" + escape(stream) + "&t=" + new Date().getTime();
}

function video_set_jump(pos)
{
	var can_jump = false;

	if (video_playing_stream == video_primary_stream.high || video_playing_stream == video_primary_stream.low)
		can_jump = true;

	if (can_jump)
		video_start_primary(video_playing_stream, pos);
	else
	{
		if (video_primary_stream[video_current_quality])
			video_start_primary(video_primary_stream[video_current_quality], pos);
		else if (video_primary_stream.high)
			video_start_primary(video_primary_stream.high, pos);
		else if (video_primary_stream.low)
			video_start_primary(video_primary_stream.low, pos);
	}
}

function video_interview_jump(id, pos)
{
	video_current_stream = video_interview_streams[id];

	if (!video_current_stream.high || !video_current_stream.low)
		document.getElementById("video-low-high-links").style.display = "none";
	else
		document.getElementById("video-low-high-links").style.display = "";

	if (video_current_stream[video_current_quality])
		video_start(video_current_stream[video_current_quality], pos, video_current_stream.id);
	else if (video_current_stream.high)
		video_start(video_current_stream.high, pos, video_current_stream.id);
	else if (video_current_stream.low)
		video_start(video_current_stream.low, pos, video_current_stream.id);
}

function video_start_interview(id)
{
	video_current_stream = video_interview_streams[id];

	if (!video_current_stream.high || !video_current_stream.low)
		document.getElementById("video-low-high-links").style.display = "none";
	else
		document.getElementById("video-low-high-links").style.display = "";

	if (video_current_stream[video_current_quality])
		video_start(video_current_stream[video_current_quality], 0, video_current_stream.id);
	else if (video_current_stream.high)
		video_start(video_current_stream.high, 0, video_current_stream.id);
	else if (video_current_stream.low)
		video_start(video_current_stream.low, 0, video_current_stream.id);
}

function video_start_primary(id, pos)
{
	video_current_stream = video_primary_stream;

	if (!video_current_stream.high || !video_current_stream.low)
		document.getElementById("video-low-high-links").style.display = "none";
	else
		document.getElementById("video-low-high-links").style.display = "";

	video_start(id, pos, video_current_stream.id);
}

function video_switch_low()
{
	video_current_quality = "low";
	video_start(video_current_stream[video_current_quality], 0, video_current_stream.id);
}

function video_switch_high()
{
	video_current_quality = "high";
	video_start(video_current_stream[video_current_quality], 0, video_current_stream.id);
}

function video_select_tab(tab)
{
	var tabs = ["interview", "setlist", "more"];

	for (var i = 0; i < tabs.length; i++)
	{
		if (!document.getElementById("video-tab-" + tabs[i] + "-button"))
			continue;

		document.getElementById("video-tab-" + tabs[i] + "-button").src = document.getElementById("video-tab-" + tabs[i] + "-button").src.replace(/_on/, "_off");
		document.getElementById("video-tab-" + tabs[i]).style.display = "none";
	}

	document.getElementById("video-tab-" + tab + "-button").src = document.getElementById("video-tab-" + tab + "-button").src.replace(/_(over|off)/, "_on");
	document.getElementById("video-tab-" + tab).style.display = "block";

	video_tab_go(tab, 0);
}

function video_tab_up(tab)
{
	var tab_el = document.getElementById("video-tab-" + tab);
	video_tab_go(tab, tab_el._currentPos - 1);
}

function video_tab_down(tab)
{
	var tab_el = document.getElementById("video-tab-" + tab);
	video_tab_go(tab, tab_el._currentPos + 1);
}

function video_tab_go(tab, n)
{
	var tab_el = document.getElementById("video-tab-" + tab);
	var uls = tab_el.getElementsByTagName ? tab_el.getElementsByTagName("ul") : tab_el.all.tags.ul;

	if (n < 0 || n >= uls.length)
		return;

	for (var i = 0; i < uls.length; i++)
		uls[i].style.display = "none";
	uls[n].style.display = "block";

	tab_el._currentPos = n;

	document.getElementById("video-tab-" + tab + "-up").style.display = n <= 0 ? "none" : "";
	document.getElementById("video-tab-" + tab + "-down").style.display = n + 1 >= uls.length ? "none" : "";
}

function video_show_overlay(id)
{
	var overlay = document.getElementById("video-overlay");
	if (!overlay)
	{
		overlay = document.createElement("div");
		overlay.id = "video-overlay";
		document.body.appendChild(overlay);

		var overlay_holder = document.createElement("div");
		overlay_holder.id = "video-overlay-holder";
		document.body.appendChild(overlay_holder);
	}

	overlay.style.display = "block";

	document.getElementById("video-overlay-holder").style.left = document.getElementById("container").offsetLeft + "px";

	function overlay_resize()
	{
		if (document.getElementById("video-overlay").clientHeight < document.getElementById("container").clientHeight)
			document.getElementById("video-overlay").style.height = document.getElementById("container").clientHeight + "px";
		if (document.getElementById("video-overlay").clientWidth < document.getElementById("container").clientWidth)
			document.getElementById("video-overlay").style.width = document.getElementById("container").clientWidth + "px";
	}

	window.setTimeout(overlay_resize, 0);

	overlay.onclick = function ()
	{
		video_hide_overlay();
	}

	document.getElementById("video_player").style.visibility = "hidden";

	var x, y;

	if (self.innerHeight)
		y = self.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight)
		y = document.documentElement.clientHeight;
	else if (document.body)
		y = document.body.clientHeight;

	function later()
	{
		function reposition()
		{
			var top = window.pageYOffset ? window.pageYOffset : document.body.scrollTop;
			if (!top)
				top = document.documentElement.scrollTop;

			document.getElementById(id).style.top = ((y - document.getElementById(id).clientHeight) / 2 + top) + "px";
		}

		reposition();
		document.getElementById(id).style.visibility = "visible";

		var temp_onscroll = window.onscroll;
		window.onscroll = function ()
		{
			reposition();
			if (temp_onscroll)
				return temp_onscroll();
		}
	}

	try
	{
		var overlay_page = document.getElementById(id);
		document.getElementById("content").removeChild(overlay_page);
		document.getElementById("video-overlay-holder").appendChild(overlay_page);
	}
	catch (e)
	{
	}

	// Tehcnically overdoing it, but for safety.
	document.getElementById("video-popup-feedback-captcha").src = "/images/captcha.png?t=" + new Date().getTime();

	document.getElementById(id).style.display = "block";
	document.getElementById(id).style.visibility = "hidden";
	window.setTimeout(later, 0);
}

function video_hide_overlay()
{
	document.getElementById("video-popup-help").style.display = "none";
	document.getElementById("video-popup-specs").style.display = "none";
	document.getElementById("video-popup-feedback").style.display = "none";

	document.getElementById("video_player").style.visibility = "visible";
	document.getElementById("video-overlay").style.display = "none";
}

var video_player_kbits = 0;
function video_feedback_submit()
{
	var form = document.getElementById("video-popup-feedback");
	var request = "location=" + plus_escape(form.location.value) + "&specs=" + plus_escape(form.specs.value) + "&comments=" + plus_escape(form.comments.value) + "&captcha=" + plus_escape(form.captcha.value) + "&video=" + plus_escape(video_playing_stream);

	function step2(kbits)
	{
		// Avoid running the test again.
		video_player_kbits = kbits;
		request += "&kbits=" + kbits;

		function async(response)
		{
			var xml = xml_first_child(response.responseXML, "data");

			document.getElementById("video-popup-feedback-captcha").src = "/images/captcha.png?t=" + new Date().getTime();
			form.captcha.value = '';

			if (xml.getAttribute("ok") == "1")
				video_hide_overlay();
			else
				alert(xml_text_content(xml_first_child(xml, "message")));
		}

		http_post("/video-player/feedback/save.html", request, async);
	}

	if (video_player_kbits == 0)
		startDownloadTest(step2);
	else
		step2(video_player_kbits);
}

var testRunning = false;
var testRequest = null;
var testLength = 0;
var testStarted = 0;

function startDownloadTest(cb)
{
	if (testRunning)
		return;
	testRunning = true;

	testLength = 200 * 1024;

	testRequest = new XMLHttpRequest();
	testRequest.open("GET", "/video-player/feedback/bandwidth.html?download=" + testLength, true);
	testRequest.onreadystatechange = function ()
	{
		if (testRequest.readyState == 4)
			cb(completeDownloadTest());
	};

	testStarted = new Date().getTime();
	testRequest.send(null);
}

function completeDownloadTest()
{
	var now = new Date().getTime();

	var ms = now - testStarted;
	var bytes_per_s = (testLength / ms) * 1000;
	var kbits = Math.floor(bytes_per_s * 8 / 1024);

	testRunning = false;

	return kbits;
}

function video_purchase_play_mp3(mp3, link)
{
	hidden_play_mp3(mp3, link);
}

function video_purchase_addcart(item_id, type)
{
	function async(response)
	{
		var xml = xml_first_child(response.responseXML, "data");

		if (type != "bsh_track")
		{
			document.getElementById("buy-stuff-cart-error_" + item_id).innerHTML = xml_text_content(xml_first_child(xml, "message"));
			document.getElementById("buy-stuff-cart-error_" + item_id).style.display = "block";
		}

		if (xml.getAttribute("ok") == "1")
		{
			// Customize the message for this page.
			if (type != "bsh_track")
				document.getElementById("buy-stuff-cart-error_" + item_id).innerHTML = 'This item has been added to your <a href="/buy-stuff2/cart.html">shopping cart</a>.';

			window.location.href = "/buy-stuff2/cart.html";
		}
		else if (type == "bsh_track")
			alert(xml_text_content(xml_first_child(xml, "message")));
	}

	http_get("/buy-stuff2/cart.html?xml=add&item=" + item_id + "&type=" + type, async);

	return false;
}


function videos2_tab_select(tab)
{
	var tabs = ["new", "all", "featured", "upcoming"];

	for (var i = 0; i < tabs.length; i++)
		document.getElementById("videos2-tab-" + tabs[i] + "-content").style.display = "none";

	document.getElementById("videos2-tab-bar").className = "videos2-tab-" + tab + "-selected";
	document.getElementById("videos2-tab-" + tab + "-content").style.display = "block";

	if (typeof(window["videos2_tab_" + tab + "_setup"]) != "undefined")
		window["videos2_tab_" + tab + "_setup"]();

	function track()
	{
//		if (typeof(urchinTracker) != "undefined")
//			urchinTracker("/live-shows/videos/tab/" + tab + ".html");
	}
	window.setTimeout(track, 250);
}
function videos2_tab_all_setup()
{
	videos2_nav_page('tab-all', 1);
}

var videos2_nav_current = {};
function videos2_nav_page_rel(id, delta)
{
	videos2_nav_page(id, videos2_nav_current[id] + delta);
	return false;
}

function videos2_nav_page(id, p)
{
	videos2_nav_current[id] = p;

	var i = 1;
	while (document.getElementById("videos2-" + id + "-page" + i))
	{
		document.getElementById("videos2-" + id + "-page" + i).style.display = "none";
		i++;
	}

	if (document.getElementById("videos2-" + id + "-page" + p))
		document.getElementById("videos2-" + id + "-page" + p).style.display = "block";

	if (document.getElementById("videos2-" + id + "-previous"))
		document.getElementById("videos2-" + id + "-previous").style.display = p <= 1 ? "none" : "";
	if (document.getElementById("videos2-" + id + "-next"))
		document.getElementById("videos2-" + id + "-next").style.display = p >= i - 1 ? "none" : "";

	return false;
}

function videos2_interviews_setup()
{
	videos2_nav_page('interviews', 1);
}
function videos2_setlist_setup()
{
	videos2_nav_page('setlist', 1);
}

function video_comment_reset_captcha(div)
{
	var imgs = div.getElementsByTagName ? div.getElementsByTagName("img") : div.all.tags.img;

	if (imgs.length > 0)
		imgs[0].src = "/images/captcha.png?t=" + new Date().getTime();
}

function video_comment_reset(form)
{
	if (form.comment.alreadyFocused)
	{
		form.comment.alreadyFocused = false;
		form.comment.value = '';
	}
	form.website.value = '';
	form.author.value = '';
	form.captcha.value = '';

	video_comment_reset_captcha(form);
}

function video_comment_new_show()
{
	var el = document.getElementById("comments-form-container");
	var forms = el.getElementsByTagName ? el.getElementsByTagName("form") : el.all.tags.form;

	el.style.display = "block";
	document.getElementById("comments-new-header").style.display = "block";

	if (forms.length > 0)
		video_comment_reset(forms[0]);
}

function video_comment_new_hide()
{
	document.getElementById("comments-form-container").style.display = "none";
	document.getElementById("comments-new-header").style.display = "none";
}

function video_comments_submit(form)
{
	var request = "author=" + plus_escape(form.author.value) + "&website=" + plus_escape(form.website.value) + "&comment=" + plus_escape(form.comment.value) + "&captcha=" + plus_escape(form.captcha.value) + "&reply_to=" + plus_escape(form.reply_to.value);

	if (!form.comment.alreadyFocused)
	{
		alert('Please enter a comment.');
		return;
	}

	function async(response)
	{
		var xml = xml_first_child(response.responseXML, "data");

		video_comment_reset_captcha(form);
		form.captcha.value = '';

		if (xml.getAttribute("ok") == "1")
		{
			if (form.reply_to.value == "0")
			{
				video_comments_cancel(form);
				video_comments_page(1);
			}
			else
			{
				video_comments_cancel(form);
				video_comments_page(video_comments_page_current);
			}
		}
		else
			alert(xml_text_content(xml_first_child(xml, "message")));
	}

	http_post(form.action, request, async);
}

function video_comments_cancel(form)
{
	if (form.reply_to.value != "0")
	{
		var rid = parseInt(form.reply_to.value);
		if (rid > 0)
		{
			document.getElementById("comment-box-post-reply-" + rid).innerHTML = '';
			document.getElementById("comment-box-reply-link-" + rid).style.display = "block";

			video_comments_last_shown_reply = 0;
		}

		video_comment_new_show();
	}
	else
		video_comment_reset(form);
}

function video_comments_toggle(finish)
{
	var first = document.getElementById("comments-first-post");
	var area = document.getElementById("comments-expand-area");

	if (first.style.display != "none")
	{
		first.style.display = "none";
		area.style.display = "block";

		video_comments_page(1, finish);
		video_comment_new_show();
	}
	else
	{
		first.style.display = "block";
		area.style.display = "none";
	}
}

var video_comments_page_current = 0;
function video_comments_page(n, finish)
{
	video_comments_page_current = n;

	function async(response)
	{
		 document.getElementById("comments-dynamic-area").innerHTML = response.responseText;
		 window.location.href = "#comments";

		if (finish)
			finish();
	}

	http_get(comments_link + "?page_n=" + n, async);
}

function video_comments_show_initial_reply(id)
{
	function finish()
	{
		video_comments_show_reply(id, id);
	}

	video_comments_toggle(finish);
}

var video_comments_last_shown_reply = 0;
function video_comments_show_reply(id, reply_to)
{
	var html = '';

	video_comment_new_hide();

	if (video_comments_last_shown_reply > 0)
	{
		document.getElementById("comment-box-post-reply-" + video_comments_last_shown_reply).innerHTML = '';
		document.getElementById("comment-box-reply-link-" + video_comments_last_shown_reply).style.display = "block";
	}
	video_comments_last_shown_reply = id;

	html = '<div style="color: #ffff00; font-weight: bold; padding: 10px;">Reply To This Comment</div>';
	html += document.getElementById("comments-form-container").innerHTML;

	document.getElementById("comment-box-post-reply-" + id).innerHTML = html;
	document.getElementById("comment-box-reply-link-" + id).style.display = "none";

	function finish()
	{
		var el = document.getElementById("comment-box-post-reply-" + id);
		var forms = el.getElementsByTagName ? el.getElementsByTagName("form") : el.all.tags.form;

		if (forms.length > 0)
		{
			forms[0].reply_to.value = reply_to;
			video_comment_reset_captcha(forms[0]);
		}
	}

	window.setTimeout(finish, 0);
}

function plus_escape(str)
{
	return escape(str).replace(/\+/g, '%2B');
}