
function SetWCCookie(cookie_name, value)
{
	document.cookie = cookie_name + '=' + value + '; expires=Wed, 1 Jan 2020 12:0:0 GMT;path=/;';
}

function GetWCCookie(cookie_name)
{
	var m = RegExp(cookie_name + '=([^;]*)').exec(document.cookie);
	if (m) return m[1];
	return null;
}

function wcSeek(id, i)
{
	var wcb = document.getElementById('wcVideo'+id).wcb;
	if (wcb.ytplayer && i>=0 && i<wcb.wc.num)
	{
		wcb.ytplayer.seekTo(wcb.wc.times[2*i], true);
	}
}

var wcBars = new Array();
WC_TIMER_ID = null;
WC_TIMER_COUNTER = 0;
function wcTimer()
{
	for (var q=0; q<wcBars.length; q++)
	{
		var wcb = wcBars[q], ytplayer = wcb.ytplayer;
		if (!wcb.ytplayer) continue;
		var state = ytplayer.getPlayerState();
		if (state == 5) return;
		var t = ytplayer.getCurrentTime();
		if (state == 1) wcb.wc.SetTime(t);
		if (WC_TIMER_COUNTER == 0)
		{
			wcb.SetTimeMax(ytplayer.getDuration());
			wcb.SetProgress(t, ytplayer.getVideoStartBytes(), ytplayer.getVideoBytesLoaded(), ytplayer.getVideoBytesTotal());
		}
	}

	if (WC_TIMER_COUNTER == 3) WC_TIMER_COUNTER = 0;
	else WC_TIMER_COUNTER ++;
}

function onYouTubePlayerReady(playerId)
{
	var q = parseInt(playerId);
	var wcb = wcBars[q];
	var ytplayer = document.getElementById('myytplayer'+wcb.id);
	wcb.SetReady(ytplayer);
	if (!WC_EMBEDDING && wcb.id=='') ytplayer.loadVideoById(wcb.videoid, 0);
	else ytplayer.cueVideoById(wcb.videoid, 0);

	if (!WC_TIMER_ID) WC_TIMER_ID = setInterval(wcTimer, 150);

	ytplayer.addEventListener('onStateChange', 'wcBars['+q+'].onStateChange');
	ytplayer.addEventListener('onError', 'wcBars['+q+'].onError');
}


function wcBar(id, videoid, wc)
{
	// called by ytplayer
	this.onStateChange = function onStateChange(newState)
	{
		if (newState==-1 || newState==0 || newState==2 || newState==5) this.SetPlay(1);
		else if (newState==1 || newState==3) this.SetPlay(0);
	}
	this.onError = function onError(errorCode)
	{
		var msg;
		if (errorCode==101 || errorCode==150) msg="Embedding disabled by request";
		else if (errorCode==100) msg="Video has been removed";
		else msg="Video not found";
		var w=document.getElementById('wcCaptions'+this.id);
		w.style.backgroundColor = "black";
		w.style.color = "white";
		w.style.textAlign = "center";
		w.style.height = "50px";
		w.innerHTML = msg;
		this.ytplayer = null;
		UpdateTranscriptHeight(this.id);
	}

	// public
	this.Launch = function Launch()
	{
		if (this.is_launched) return;
		this.is_launched = true;
		var params = { allowScriptAccess: "always", bgcolor: "black" };
		var atts = { id: 'myytplayer' + this.id };
		swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&amp;playerapiid="+this.q, "ytapiplayer"+this.id, '100%', '100%', "8", null, null, params, atts);
	}
	this.SetReady = function SwitchToReady(ytplayer)
	{
		this.bVol.style.display = '';
		this.ytplayer = ytplayer;
		this.SetVolume();
	}
	this.onPlayButton = function onPlayButton()
	{
		if (!this.ytplayer) return;
		this.SetPlay(0);
		this.ytplayer.playVideo();
	}
	this.onPauseButton = function onPauseButton()
	{
		if (!this.ytplayer) return;
		this.SetPlay(1);
		this.ytplayer.pauseVideo();
	}
	this.onWCSwitchButton = function onWCSwitchButton()
	{
		var wc = this.wc;
		if (wc.w)
		{
			wc.Hide();
			this.wc_switch_flag = 0;
		}
		else
		{
			wc.Show();
			this.wc_switch_flag = 1;
		}
		this.DisplayWCSwitch();
		SetWCCookie('wc_switch', this.wc_switch_flag);
	}


	/////////////////////////////////////////////////////////////////////////
	this.SetPlay = function SetPlay(flag) // 0 - pause, 1 - play
	{
		this.play_flag = flag;
		this.DisplayPlay();
	}
	this.SetTimeMax = function SetTimeMax(t_max)
	{
		if (t_max == this.t_max) return;
		if (t_max <= 0) return;
		this.t_max = t_max;
		if (!this.bt4 && this.t_max >= 600)
		{
			this.W -= 12;
			this.bp0.parentNode.style.width = this.W + 'px';
			this.bt0 = this.bt1.cloneNode(false);
			this.bt1.parentNode.insertBefore(this.bt0, this.bt1);
			this.bt4 = this.bt1.cloneNode(false);
			this.bt5.parentNode.insertBefore(this.bt4, this.bt5);
			this.SetProgress(0, 1, 1, 2);
			this.SetProgress(0, 0, 0, 1);
		}
		SetTime(t_max, this.bt4, this.bt5, this.bt6, this.bt7);
	}
	function SetTime(t, b0, b1, b2, b3)
	{
		t = Math.floor(t);
		if (b0)
		{
			if (t>3599) t = 3599;
			var x1 = Math.floor(t/60);
			t -= x1*60;
			var x0 = Math.floor(x1/10);
			x1 -= x0*10;
			b0.style.backgroundImage = 'url(/pic/'+x0+'.gif)';
		}
		else
		{
			if (t>599) t = 599;
			var x1 = Math.floor(t/60);
			t -= x1*60;
		}
		var x2 = Math.floor(t/10);
		t -= x2*10;
		if (b1.d != x1) { b1.style.backgroundImage = 'url(/pic/'+x1+'.gif)'; b1.d = x1; }
		if (b2.d != x2) { b2.style.backgroundImage = 'url(/pic/'+x2+'.gif)'; b2.d = x2; }
		if (b3.d != t ) { b3.style.backgroundImage = 'url(/pic/'+t+'.gif)';  b3.d = t; }
	}
	this.SetProgress = function SetProgress(t, c0, c1, cmax)
	{
		if (t < 0) t = 0;
		if (c0 < 0) c0 = 0;
		if (c1 < 0) c1 = 0;
		if (cmax < 1) cmax = 1;

		if (c0 > cmax) c0 = cmax;
		c1 += c0;
		if (c1 > cmax) c1 = cmax;
		var W0 = Math.floor(this.W*c0/cmax);
		var T = (this.t_max > 0) ? (Math.floor(this.W*t/this.t_max)) : 0;
		var W2 = Math.floor(this.W*c1/cmax);
		var W1 = T;
		if (W1 < W0) W1 = W0;
		else if (W1 > W2) W1 = W2;

		if (W0 != this.W0 || W1 != this.W1 || W2 != this.W2)
		{
			this.W0 = W0;
			this.W1 = W1;
			this.W2 = W2;
			this.bp0.style.width = this.W0 + 'px';
			this.bp1.style.width = (this.W1-this.W0) + 'px';
			this.bp2.style.width = (this.W2-this.W1) + 'px';
			this.bp3.style.width = (this.W-this.W2) + 'px';
		}
		if (T != this.T)
		{
			this.T = T;
			//this.bp4.style.marginLeft = -147 - (this.W - this.T) - ((this.bt0) ? 12:0);
			this.bp4.style.marginLeft = (33 + this.T - this.width) + 'px';
		}

		SetTime(t, this.bt0, this.bt1, this.bt2, this.bt3);
	}
	this.SetVolume = function SetVolume()
	{
		var v = this.ytplayer ? this.ytplayer.getVolume() : 0;
		this.bVol.style.marginLeft = Math.round(-44 + 36*v/100) + 'px';
	}

	// private
	this.DisplayPlay = function DisplayPlay()
	{
		this.bPlay.style.backgroundImage = (this.play_flag == 0) ?
			((this.play_mouse_flag==0) ? 'url(/pic/pause.gif)' : 'url(/pic/pauseRed.gif)') :
			((this.play_mouse_flag==0) ? 'url(/pic/play.gif)'  : 'url(/pic/playRed.gif)');
	}
	this.onProgressClick = function onProgressClick(w, n0, e)
	{
		if (!this.ytplayer) return;
		var t;
		if (e.offsetX) t = parseInt(e.offsetX);
		else
		{
			t = parseInt(e.pageX);
			while (w) { t -= parseInt(w.offsetLeft); w = w.offsetParent; }
		}
		t = this.t_max*(t+n0)/this.W;
		this.ytplayer.seekTo(t, true);
	}
	this.onVolumeClick = function onVolumeClick(w, e)
	{
		if (!this.ytplayer) return;
		var t;
		if (e.offsetX) t = parseInt(e.offsetX);
		else
		{
			t = parseInt(e.pageX);
			while (w) { t -= parseInt(w.offsetLeft); w = w.offsetParent; }
		}
		t = Math.round(t*100/36);
		if (t<0) t = 0;
		if (t>100) t = 100;
		this.ytplayer.setVolume(t);
		this.SetVolume();
        }
	this.DisplayWCSwitch = function DisplayWCSwitch()
	{
		this.bWCSwitch.style.backgroundImage = (this.wc_switch_mouse_flag == 0) ?
			((this.wc_switch_flag==0) ? 'url(/pic/wc_icon0.gif)' : 'url(/pic/wc_icon1.gif)') :
			((this.wc_switch_flag==0) ? 'url(/pic/wc_icon1.gif)' : 'url(/pic/wc_icon0.gif)');
	}

	///////////////////////////////////////////////
	this.q = wcBars.length;
	wcBars[this.q] = this;
	var wcb = 'wcBars[' + this.q + ']';
	var width = parseInt(document.getElementById('wcBar'+id).offsetWidth);
	this.W = width - 211;

	var H = '25px';
	var vert = '<div style="float:left;background-color:#B1B1B1;width:1px;height:'+H+';"></div>';
	var sep1 = '<div style="float:left;background-image:url(/pic/bg.bmp);height:'+H+';width:', sep2 = ';"></div>';
	var s = '';

	s += vert;
	s += sep1 + '4px' + sep2;

	// play
	s += '<div style="float:left; margin-left:2px; margin-top:4px; width:15px; height:'+H+'; background-repeat:no-repeat;" id="wcbPlay'+id+'" onmouseover="if('+wcb+'.play_mouse_flag==0){'+wcb+'.play_mouse_flag=1;'+wcb+'.DisplayPlay();}" onmouseout="if('+wcb+'.play_mouse_flag==1){'+wcb+'.play_mouse_flag=0;'+wcb+'.DisplayPlay();}" onclick="'+wcb+'.play_mouse_flag=0; if ('+wcb+'.play_flag) '+wcb+'.onPlayButton(); else '+wcb+'.onPauseButton();"></div>';

	s += sep1 + '6px' + sep2;
	s += vert;
	s += sep1 + '12px' + ';" onclick="if ('+wcb+'.ytplayer) '+wcb+'.ytplayer.seekTo(0, true)' + sep2;

	// progress bar
	s += '<div style="float:left; width:'+this.W+'px; height:'+H+';">'
	   +   '<div style="float:left;height:'+H+';background-image:url(/pic/bg0.bmp);" id="wcbp0'+id+'" onclick="'+wcb+'.onProgressClick(this,0,event);"></div>'
	   +   '<div style="float:left;height:'+H+';background-image:url(/pic/bg2.bmp);" id="wcbp1'+id+'" onclick="'+wcb+'.onProgressClick(this,'+wcb+'.W0,event);"></div>'
	   +   '<div style="float:left;height:'+H+';background-image:url(/pic/bg1.bmp);" id="wcbp2'+id+'" onclick="'+wcb+'.onProgressClick(this,'+wcb+'.W1,event);"></div>'
	   +   '<div style="float:left;height:'+H+';background-image:url(/pic/bg0.bmp);" id="wcbp3'+id+'" onclick="'+wcb+'.onProgressClick(this,'+wcb+'.W2,event);"></div>'
	   + '</div>';

	s += sep1 + '14px' + sep2;
	//s += vert;

	// time
	s += '<div style="float:left; margin-top:8px; width:6px; height:8px; background-repeat:no-repeat;" id="wcbtime1'+id+'"></div>';
	s += '<div style="float:left; margin-top:8px; width:3px; height:8px; background-repeat:no-repeat; background-image:url(/pic/sc.gif);"></div>';
	s += '<div style="float:left; margin-top:8px; width:6px; height:8px; background-repeat:no-repeat;" id="wcbtime2'+id+'"></div>';
	s += '<div style="float:left; margin-top:8px; width:6px; height:8px; background-repeat:no-repeat;" id="wcbtime3'+id+'"></div>';
	s += '<div style="float:left; margin-top:8px; width:9px; height:8px; background-repeat:no-repeat; background-image:url(/pic/sl.gif);"></div>';
	s += '<div style="float:left; margin-top:8px; width:6px; height:8px; background-repeat:no-repeat;" id="wcbtime5'+id+'"></div>';
	s += '<div style="float:left; margin-top:8px; width:3px; height:8px; background-repeat:no-repeat; background-image:url(/pic/sc.gif);"></div>';
	s += '<div style="float:left; margin-top:8px; width:6px; height:8px; background-repeat:no-repeat;" id="wcbtime6'+id+'"></div>';
	s += '<div style="float:left; margin-top:8px; width:6px; height:8px; background-repeat:no-repeat;" id="wcbtime7'+id+'"></div>';
	s += sep1 + '5px' + sep2;
	s += vert;

	// volume
	s += sep1 + '5px' + sep2;
	s += '<div style="float:left; width:14px; height:15px; margin-top:4px; background-image:url(/pic/vol0.gif); background-repeat:no-repeat;"></div>';
	s += sep1 + '5px' + ';" onclick="if ('+wcb+'.ytplayer) '+wcb+'.ytplayer.setVolume(0);'+wcb+'.SetVolume();' + sep2;
	s += '<div style="float:left; width:36px; height:'+H+'; background-image:url(/pic/volBg.gif);" onclick="'+wcb+'.onVolumeClick(this,event);"></div>';
	s += sep1 + '7px' + ';" onclick="if ('+wcb+'.ytplayer) '+wcb+'.ytplayer.setVolume(100);'+wcb+'.SetVolume();' + sep2;
        s += '<div style="float:left;margin-top:7px;width:3px;height:10px;background-image:url(/pic/volSlider.bmp);background-repeat:no-repeat;display:none;" id="wcbvol'+id+'"></div>';
	s += vert;

	// wc switch
	s += sep1 + '4px' + sep2;
	s += '<div style="float:left; margin-top:5px; width:22px; height:'+H+'; background-repeat:no-repeat;" id="wcbWCSwitch'+id+'" onmouseover="if('+wcb+'.wc_switch_mouse_flag==0){'+wcb+'.wc_switch_mouse_flag=1;'+wcb+'.DisplayWCSwitch();}" onmouseout="if('+wcb+'.wc_switch_mouse_flag==1){'+wcb+'.wc_switch_mouse_flag=0;'+wcb+'.DisplayWCSwitch();}" onclick="'+wcb+'.wc_switch_mouse_flag=0;'+wcb+'.onWCSwitchButton();"></div>';
	s += sep1 + '4px' + sep2;
	s += vert;

	// slider
	s += '<div style="float:left;margin-top:4px;width:16px;height:16px;background-image:url(/pic/slider.gif);background-repeat:no-repeat;" onclick="'+wcb+'.onProgressClick(this,'+wcb+'.T-8,event);" id="wcbp4'+id+'"></div>';

	// holder
	//s = '<div style="width:'+width+'px;height:'+H+';background-image:url(/pic/bg.bmp);">' + s + '</div>';

	document.getElementById('wcBar'+id).innerHTML = s;

	this.id = id;
	this.videoid = videoid;
	this.wc = wc;
	this.width = width;
	this.ytplayer = null;
	this.bPlay = document.getElementById('wcbPlay'+id);
	this.bp0 = document.getElementById('wcbp0'+id);
	this.bp1 = document.getElementById('wcbp1'+id);
	this.bp2 = document.getElementById('wcbp2'+id);
	this.bp3 = document.getElementById('wcbp3'+id);
	this.bp4 = document.getElementById('wcbp4'+id);

	this.bt0 = null;
	this.bt1 = document.getElementById('wcbtime1'+id);
	this.bt2 = document.getElementById('wcbtime2'+id);
	this.bt3 = document.getElementById('wcbtime3'+id);
	this.bt4 = null;
	this.bt5 = document.getElementById('wcbtime5'+id);
	this.bt6 = document.getElementById('wcbtime6'+id);
	this.bt7 = document.getElementById('wcbtime7'+id);

	this.bVol = document.getElementById('wcbvol'+id);

	this.bWCSwitch = document.getElementById('wcbWCSwitch'+id);

	this.SetTimeMax(0);
	this.SetVolume();

	var d, preloaded_images = new Array('bg.bmp','bg0.bmp','bg1.bmp','bg2.bmp','slider.gif','play.gif','pause.gif','playRed.gif','pauseRed.gif','slider.gif',
	                                    'vol0.gif','volSlider.gif','volBar.gif','volBg.gif','volSlider0.gif','sl.gif','sc.gif','arrow.gif', 'wc_icon0.gif', 'wc_icon1.gif', 'volSlider.bmp');
	for (d=0; d<10; d++) preloaded_images[preloaded_images.length] = d + '.gif';
	this.preloaded_images = new Array();
	for (d=0; d<preloaded_images.length; d++)
	{
		this.preloaded_images[d] = new Image();
		this.preloaded_images[d].src = '/pic/' + preloaded_images[d];
	}

	

	this.play_flag = 1;
	this.play_mouse_flag = 0;

	this.wc_switch_flag = (wc.w) ? 1 : 0;
	this.wc_switch_mouse_flag = 0;

	this.DisplayPlay();
	this.SetProgress(0, 0, 0, 1);
	this.DisplayWCSwitch();

	UpdateTranscriptHeight(id);
}




function WC(id, num, times, captions)
{
	this.Show = function Show()
	{
		if (this.w) Hide();

		// determine height
		var width = parseInt(document.getElementById("wcCaptions"+this.id).offsetWidth);
		var tr_tmp = this.tr;
		this.tr = null;
		var h_max = 0;
		var w_parent = document.getElementById('wcCaptionsTmp'+this.id);
		var style0 = 'font-size:120%; float:left; background-color:black; text-align:center; color:white; overflow:hidden;';
		var style1 = 'padding:5px 12px 7px 12px;';
		w_parent.innerHTML = '<div style="'+style0+';width:'+width+'px;visibility:visible;"><div style="'+style1+'"></div></div>';
		w_parent.style.display = '';
		var w0 = w_parent.firstChild;
		this.w = w0.firstChild;
		var i;
		for (i=0; i<this.num; i++)
		{
			//this.SetCurrent(2*i);
			this.w.innerHTML = this.captions[i];

			var h = parseInt(w0.offsetHeight);
			if (h_max < h) h_max = h;
		}
		w_parent.innerHTML = ''; w_parent.style.display = 'none';
		this.tr = tr_tmp;
		// done

		w_parent = document.getElementById('wcCaptions'+this.id);
		w_parent.innerHTML = '<div style="'+style0+'width:'+width+'px;height:'+h_max+'px;"><div style="'+style1+'"></div></div>';
		w_parent.style.height = h_max + 'px';
		this.w = w_parent.firstChild.firstChild;

		this.SetCurrent(-1);
		UpdateTranscriptHeight(this.id);

	}
	this.Hide = function Hide()
	{
		w_parent = document.getElementById('wcCaptions'+this.id);
		w_parent.style.height = '2px';
		w_parent.innerHTML = '';
		this.w = null;
		UpdateTranscriptHeight(this.id);
	}
	this.SetTime = function SetTime(t)
	{
		var c = this.current;
		if (c < 0) c = 0;
		else if (c > 2*this.num-1) c = 2*this.num-1;
		do
		{
			if (this.times[c] <= t)
			{
				while (c+1 < 2*this.num && this.times[c+1] <= t) c++;
				break;
			}
			c --;
		} while (c >= 0);
		this.SetCurrentAndScroll(c);
	}
	this.SetCurrent = function SetCurrent(c)
	{
		if (this.current != c)
		{
			this.current = c;
			c = c/2;
			if (this.w)
			{
				if (c==Math.floor(c) && c>=0 && c<this.num)
				{
					var s = this.captions[c];
					this.w.innerHTML = s;
					var h0 = parseInt(this.w.offsetHeight);
					this.w.innerHTML = s.replace(/\n/,' <br /> ');
					if (parseInt(this.w.offsetHeight) > h0) this.w.innerHTML = s;
				}
				else this.w.innerHTML = '';
			}
			if (this.tr) this.tr.Select(this.current);
		}
	}
	this.SetCurrentAndScroll = function SetCurrentAndScroll(c)
	{
		if (this.current != c)
		{
			this.current = c;
			c = c/2;
			if (c==Math.floor(c) && c>=0 && c<this.num)
			{
				if (this.w)
				{
					var s = this.captions[c];
					this.w.innerHTML = s;
					var h0 = parseInt(this.w.offsetHeight);
					this.w.innerHTML = s.replace(/\n/,' <br />');
					if (parseInt(this.w.offsetHeight) > h0) this.w.innerHTML = s;
				}
				if (this.tr) { this.tr.Select(this.current); if (2*c == this.current) this.tr.UpdateScroll(c); }
			}
			else
			{
				if (this.w) this.w.innerHTML = '';
				if (this.tr) this.tr.Select(this.current);
			}
		}
	}

	this.w = null;
	this.tr = null;
	this.id = id;
	this.num = num;
	this.times = times;
	this.captions = captions;

	this.current = -1;
}


function UpdateWCVideoSize(id)
{
	if (WC_EMBEDDING) return;
	var WC_WIDTH = parseInt(document.getElementById('wcVideo'+id).parentNode.offsetWidth)-20;

	var W_min = 400, W_max = 800, WT_min = 360, WT_max = 500;
	var H_min = 300, H_max = 420, ratio = 0.75;
	if (WC_WIDTH <= W_min + WT_min) { W = W_min; WT = WT_min; }
	else if (WC_WIDTH >= W_max + WT_max) { W = W_max; WT = WT_max; }
	else
	{
		var a = W_max-W_min, b = WT_max-WT_min, c = WC_WIDTH - W_min - WT_min;
		W = Math.round(W_min + c*a/(a+b));
		WT = WC_WIDTH - W;
	}
	H = parseInt(W*ratio);
	if (H < H_min) H = H_min;
	if (H > H_max) H = H_max;

	document.getElementById('ytapiplayerParent'+id).style.width = W + 'px';
	document.getElementById('wcCaptions'+id).style.width = W + 'px';
	document.getElementById('wcCaptionsTmp'+id).style.width = W + 'px';
	document.getElementById('wcBar'+id).style.width = W + 'px';

	document.getElementById('ytapiplayerParent'+id).style.height = H + 'px';

	document.getElementById('wcTranscript'+id).style.width = (WT-55) + 'px';
	document.getElementById('wcTranscript'+id).parentNode.style.width = WT + 'px';
}

function UpdateTranscriptHeight(id)
{
	var p = document.getElementById('ytapiplayerParent'+id);
	var H = parseInt(p.parentNode.offsetHeight);
	if (p.VIDEO_HEIGHT > 0 && p.VIDEO_HEIGHT != H)
	{
		p.style.height = (parseInt(p.offsetHeight) + p.VIDEO_HEIGHT - H) + 'px';
		H = p.VIDEO_HEIGHT;
	}

	var w = document.getElementById('wcTranscript'+id).parentNode;

//	w.style.height = 'auto';
//	if (parseInt(w.offsetHeight) <= H) return;

	w.style.height = H + 'px';
	var delta = parseInt(w.offsetHeight) - H;
	w.style.height = (H - delta) + 'px';
	w.style.overflow = 'auto';
}

function WCTranscript(wc)
{
	this.getShift = function getShift(a)
	{
		return parseInt(a.offsetTop) - parseInt(this.links[0].offsetTop) + this.shift0;
	}

	this.Select = function Select(i)
	{
		if (this.current == i) return;
		var j = Math.floor(i/2);
		if (this.selection == 1)
		{
			if (this.current >= 0) this.links[Math.floor(this.current/2)].style.color = 'black';
			if (i >= 0 && 2*j==i && j<this.links.length) this.links[j].style.color = 'red';
		}
		else if (this.selection == 2)
		{
			if (i >= 0 && i<2*this.links.length-1)
			{
				this.arrow.style.visibility='visible';
				this.arrow.style.marginTop = (this.getShift(this.links[j]) + 10) + 'px';
			}
			else this.arrow.style.visibility='hidden';
		}
		this.current = i;
	}
	this.UpdateScroll = function UpdateScroll(i)
	{
		if (this.selection == 0) return;
		var w = this.w.parentNode;
		var h0 = parseInt(w.scrollTop);
		var h1 = this.getShift(this.links[i]);
		var h2 = this.getShift(this.links[(i<this.wc.num-1)?(i+1):i]);
		var h3 = parseInt(w.offsetHeight);
		if (h2 > h0 + h3 - 20 || h1 < h0) w.scrollTop = h1-20;
	}
	this.Show = function Show()
	{
		this.arrow.style.display = '';
		this.w.style.display = '';
		if (WC_EMBEDDING) document.getElementById('wcTrOptions'+this.wc.id).getElementsByTagName('a')[3].firstChild.style.visibility = 'hidden';
		else document.getElementById('wcTranscriptToggle'+this.wc.id).innerHTML = 'hide';
		this.is_active = true;
	}
	this.Hide = function Hide()
	{
		this.arrow.style.display = 'none';
		this.w.style.display = 'none';
		if (WC_EMBEDDING)
		{
			var a = document.getElementById('wcTrOptions'+this.wc.id).getElementsByTagName('a');
			for (var i=0; i<4; i++) a[i].firstChild.style.visibility = (i == 3) ? 'visible' : 'hidden';
		}
		else document.getElementById('wcTranscriptToggle'+this.wc.id).innerHTML = 'show';
		this.is_active = false;
	}
	this.Toggle = function Toggle()
	{
		if (this.is_active) { this.Hide(); return '0'; }
		else { this.Show(); return '1'; }
	}
	this.UpdateSelection = function UpdateSelection(s)
	{
		if (!this.is_active) this.Show();
		var a = document.getElementById('wcTrOptions'+this.wc.id).getElementsByTagName('a');
		for (var i=0; i<a.length; i++) a[i].firstChild.style.visibility = (i == s) ? 'visible' : 'hidden';
		var c = this.current;
		this.Select(-1);
		this.selection = s;
		this.Select(c);
	}

	var i;

	this.wc = wc;
	wc.tr = this;
	this.arrow = document.getElementById('wcArrow'+wc.id);
	this.w = document.getElementById('wcTranscript'+wc.id);
	this.current = -1;
	this.is_active = true;
	this.arrow.style.visibility = 'hidden';

	var a0 = document.createElement('a');
	a0.innerHTML = '&nbsp;';
	if (this.w.firstChild) a0 = this.w.insertBefore(a0, this.w.firstChild);
	else a0 = this.w.appendChild(a0);
	this.shift0 = -parseInt(a0.offsetTop);
	this.w.removeChild(a0);

	this.links = this.w.getElementsByTagName('a');
	for (i=0; i<wc.num; i++)
	{
		this.links[i].i = i;
		this.links[i].wc_id = wc.id;
		var t = Math.round(wc.times[2*i]);
		if (t<0) t = 0;
		var t0 = Math.floor(t/60);
		t -= 60*t0;
		if (t < 10) t = '0' + t;
		this.links[i].title = 'play from ' + t0 + ':' + t;
		this.links[i].style.color = 'black';
	}
	if (wc.num > 0) this.shift0 += parseInt(this.links[0].offsetTop);

	var d = GetWCCookie('wc_tracking');
	if (d == '0') d = 0;
	else if (d == '2') d = 2;
	else d = 1;
	this.UpdateSelection(d);

	var d = GetWCCookie('wc_transcript');
	if (d == '0') this.Hide();
	else this.Show();

	this.Select(wc.current);
}

WC_CACHE = [];
function UpdateWCEditLink(id)
{
	if (WC_EMBEDDING) return;
	var url = document.getElementById('wcVideo'+id).wcb.currentCaptionsUrl;
	var a = document.getElementById('wcEditLink'+id);
	if (!a && wgAction=="view")
	{
		var edit = document.createElement('span');
		var embed_url = '/wiki/Special:EmbedVideo?src=' +  escape(url) + '&w1=450&w2=400&h=400';
		edit.innerHTML = '<span class="editsection" style="font-size:80%;">[<a href="'+embed_url+'">embed video</a>] [<a id="wcEditLink'+id+'" title="Edit captions">edit captions</a>]</span>';
		var w = document.getElementById('firstHeading');
		w.insertBefore(edit, w.firstChild);
		a = document.getElementById('wcEditLink'+id);
	}
	if (!a) return;
	var m = /^:(.*?):(.*)$/.exec(url);
	if (m) url = "http://" + m[1] + ".wikicaptions.org/wiki/" + m[2] + "?action=edit"; //&editcaptions=1
	a.href = url;
}

function StoreWCCaptions(id)
{
	var i, wcb = document.getElementById('wcVideo'+id).wcb, wc = wcb.wc, url = wcb.currentCaptionsUrl;
	for (i=0; i<WC_CACHE.length; i++)
	{
		if (WC_CACHE[i].url == url && WC_CACHE[i].id == id) return;
	}
	WC_CACHE[WC_CACHE.length] = {url:url, id:id, num:wc.num, times:wc.times, captions:wc.captions, transcript:document.getElementById('wcTranscript'+id).innerHTML};
}

function UpdateWCCaptions(id, url)
{
	var i, wcb = document.getElementById('wcVideo' + id).wcb;
	if (wcb.currentCaptionsUrl == url) return;
	for (i=0; i<WC_CACHE.length; i++)
	{
		var c = WC_CACHE[i];
		if (c.url == url && c.id == id)
		{
			StoreWCCaptions(id);
			if (wcb.currentCaptionsUrl != '#') wcb.lastCaptionsUrl = wcb.currentCaptionsUrl;
			var wc_show = wcb.wc.w;
			var tr_show = wcb.wc.tr.is_active;
			var tr_selection = wcb.wc.tr.selection;
			wcb.wc = new WC(id, c.num, c.times, c.captions);
			if (wc_show) wcb.wc.Show();
			else wcb.wc.Hide();
			document.getElementById('wcTranscript'+id).innerHTML = c.transcript;
			var tr = new WCTranscript(wcb.wc);
			if (tr_show || url=='#') tr.Show();
			else tr.Hide();
			tr.UpdateSelection(tr_selection);
			wcb.currentCaptionsUrl = url;
			UpdateWCEditLink(id);
			UpdateTranscriptHeight(id);
			return;
		}
	}
	if (WC_EMBEDDING)
	{
		var ajax_url = '/w?action=ajax&rs=wfAjaxQueryCaptions&rsargs[]=Youtube:'+wcb.videoid+'&rsargs[]='+escape(url)+'&rsargs[]='+id;
		var xmlhttp;
		if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest();
		else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttp.onreadystatechange=function() { if(xmlhttp.readyState==4) { processAjaxCaptions(xmlhttp); } }
		xmlhttp.open("GET",ajax_url,true);
		xmlhttp.send(null);
	}
	else sajax_do_call( "wfAjaxQueryCaptions", ['Youtube:'+wcb.videoid, url, id], processAjaxCaptions );
}

function processAjaxCaptions(request)
{
	var x = request.responseText.split('|');
	var url = x[0];
	var id = x[1];
	var num = parseInt(x[2]);
	var captions = x[3].replace(/\#1/g, '|').replace(/\#0/g, '#');
	var times = x[4];
	var transcript = x[5].replace(/\#1/g, '|').replace(/\#0/g, '#');

	eval('WC_CACHE[WC_CACHE.length] = {url:url, id:id, num:num, times:new Array('+times+'), captions:new Array('+captions+'), transcript:transcript};');
	UpdateWCCaptions(id, url);
}


function onCaptionsNewGo(w, id)
{
	var wcb = document.getElementById('wcVideo'+id).wcb, inputs = w.parentNode.getElementsByTagName('input');
	var lan = inputs[0].value.toLowerCase(), n = inputs[1].value, err = document.getElementById('new-error-msg');
	if (!lan) { err.innerHTML = 'Please enter language code'; return; }
	var LAN_CODES = document.getElementById('LAN_CODES').value.split(',');
	for (var i in LAN_CODES) { if (LAN_CODES[i] == lan) { LAN_CODES = null; break; } }
	if (LAN_CODES) { err.innerHTML = 'Language <b>' + lan + '</b> is not supported yet. To request it, please leave a comment on the main page.'; return; }
	var url = 'http://'+lan+'.wikicaptions.org/wiki/Youtube:'+wcb.videoid;
	if (n) url += ':' + escape(n);
	url += '?action=edit'; //&editcaptions=1';
	if (inputs[2].checked) url += '&copycaptions=' + escape(wcb.lastCaptionsUrl);
	window.open(url, '_self');
}




