var tcast_ver_4ie = '3.8';
var tcast_ver_4ff = '';
var tcast_f_v = '1';
var tcast_cab_v = '3.0.0.8';
var buffertimer;
var buffertimes=0;
var _starttm=new Date().getTime();
var tcast_playednum = 0;	
var tcast_isplay = false;
var tcast_version;  //1老版本 2新版本


var slist=[
'PHIAAEg8hIf0j+qG/VJpp4gGuWlz0F4YbvaSlcgPeVb5',
'aHIAAKfXcBAdeEjv2W3b4zRfGj+7u8iZTHTz+rZLO8UZ',
'XUwAAK7yMHrHf4HiO3FR+eXoiorahn/nAGdpH7weIGoEYA==',
'vhkAAFM3bGGcQkNgh1oYuJNzEntyRRynCJ58C9NFMro6uQ==',
'zx8AAJe4BuWs2yfBGoq4oBXpLmLL1c0qyMYvf7FwfyNOCw==',
'iFwAAPH7S+PwK1ZbaPHIcm1BHLtzuhqFRAQt1VBhp3XP',
'sUEAAEDUFzNt2PPvN5P9UD3C6iM7yPoBKmn6fWjoB+xblw==',
'p3cAAPQ7jeI5pNJA0UHqRAckNOU8UrBghp4bbaWzxc8=',
'EgAAAOvVZj9/66xD14bBJp48orygwrwQxTlEaEHEzBE=',
'ejAAAOThFw7JbmZ7o/Qg6WwGZPEdZtpcHFmNoVvkk54=',
'dREAAE5YIQzgLRUQDajRQyOnVIxtZZgkt3tDZiJYPVw=',
'YGoAAG5lFdYeTlgb4C+6XOKsf6cj51smYUrl7nQIs4g=',
'FDgAAI7jPJ218iXFDIc7f2rgexj4hKkgIHkrZKAETaAn',
'LjwAALxTQBa+1BbtU/94BDXCOV7eExL9Utp/QrOHsA7o',
'aQAAAMRPGTG0o+1gM79/e8m2ncnyFvyHgoAIu/gCAq4I',
'kzkAAFv0SB2g8LyrHShSdB005AW8MCaw/LxsUqRI2/zY',
'MnwAAPUz8B7jWa8YSNBRhtkbXthH4s1v3WmuDQMaVPbT'
];


var llist=
[
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1
];


function TcastReportCount()
{
	var img=document.getElementById('countimg');
	img.src='http://xtrack.tomonline-inc.com/?ID=-.-TCASTAD-.-&ver=1';
}
function reportCount()
{
	var img=document.getElementById('countimg');
	img.src='http://xtrack.tomonline-inc.com/?ID=-.-TCASTAD-.-&ver=1&type=01';
}
function TcastGetUrl()
{
	if(slist.length==0) return false;
	var sl=new Array();
	var ll=0;
	sl[0]=0;
	for(i=0;i<llist.length;++i)
	{
		ll+=llist[i];
		sl[i+1]=ll;
	}
	var rd=Math.random()*ll;
	for(i=sl.length-1;i>=0;--i)
	{
		if(rd>=sl[i]) break;
	}
	return slist[i];
}

var buffertimer;
var buffertimes=0;
var _starttm=new Date().getTime();

function GetTcast() { return document.getElementById('tomcast'); }

function TcastCheckBufferTimeout()
{
	function $(i){return document.getElementById(i);}
	
	buffertimer=0;
	var ht=$("helptext");
	var hl=$("helplink");
	
	if(!ht || !hl)return;
	
	ht.innerHTML="出现长时间缓冲状态？尝试停止后再点击播放键";
	hl.href="http://nba.tom.com/help06/index.html";
	ht.parentNode.style.display="";
}

function Tcast_OnStateChanged(s)
{
	function $(i){return document.getElementById(i);}
	if(s==2)reportCount();
	var ht=$("helptext");
	var hl=$("helplink");
	
	if(!ht || !hl)return;
	
	if(s==4)
	{
		ht.innerHTML="可能是您的UDP端口受限";
		hl.href="http://nba.tom.com/help04/index.html";
		ht.parentNode.style.display="";
	}
	else if(s==7)
	{
		ht.innerHTML="为什么“频道已关闭”？请再次点击播放键";
		hl.href="http://nba.tom.com/help05/index.html";
		ht.parentNode.style.display="";
	}
	else if(s==101)
	{
		var tc = GetTcast();
		if(tc&&tc.BufferingProgress&&buffertimer)
		{
			window.clearTimeout(buffertimer);buffertimer=0;
		}
	}
	else if(s==102)
	{
		++buffertimes;
		if(buffertimes>10&&buffertimes*60000/(new Date().getTime()-_starttm)>1)
		{
			ht.innerHTML="为什么播放断断续续的，不流畅？";
			hl.href="http://nba.tom.com/help07/index.html";
			ht.parentNode.style.display="";
		}
		if(!buffertimer)
		{
			buffertimer=window.setTimeout("TcastCheckBufferTimeout()",20000);
		}
	}
}

function Tcast_Active() //控件已经加载
{
	GetTcast().Events.Attach('StateChanged', Tcast_OnStateChanged);
	document.getElementById('tcastheader').style.display = 'block';
}

function Tcast_CreateTcast(elem)
{

	do
	{
		var videosrc = TcastGetUrl();
		var result = 0;
		var targ="5gkAAAECFdLHB9CZsM/opQnbI3vRn67xoKZRtUk+cCdzbVyr7Cb+8j+3woj3RgbhfFVmnI7YOJ2sawc4IkbAdrwoCtPiw+kQRGB2Cm2NO3REJ9OA44S7SLyKbFgssLEX0Vswowoq95mkLiR7AdXQ8Elk2HfOK7oxxNPbQiDDpnUBpcxB3dknZw1sEp9U8NpR46VzNrcXtg9gIJX5ALFNgmVqRGtj48VLna9be1ZT3g+PWHAt6tcLT/pi3mqmPAYDr+0JUanbxJY2WUSov1S7cn7b3J7kmHe43IuLVdJl8e3vV29IZ8t0kKwnTgjS8SndUQ==";
	
		var ua = navigator.userAgent;
		var ss = new Array();
		
		_ds='<center><div style="width:520px;height:405px;background:#000;overflow:hidden"><div class="upload"><p>如果您无法正常安装，请下载控件手动安装！</p>'+
			'<br/><br/><a href="http://nba.tom.com/video/TOM_NBA_Setup.zip" target="_blank">'+
			'<img border="0" src="images/uploadbtn.jpg" /></a></div></div></center>';
		ss.push('<img id="countimg" src="about:blank" style="display:none"/>');
		ss.push('<center><div id="tcastheader" style="display:none;width:440px;height:22px;background-color:#1f2a47">');
			ss.push('<div style="margin-top:3px;margin-left:10px;float:left">');
				ss.push('<IMG src="images/Fullscreen01.gif" ');
				ss.push('onmouseout="this.src=\'images/Fullscreen01.gif\'" ');
				ss.push('onmouseover="this.src=\'images/Fullscreen02.gif\'" ');
				ss.push('onmousedown="this.src=\'images/Fullscreen03.gif\'" ');
				ss.push('onmouseup="this.onmouseout();GetTcast().FullScreen=true;">');
			ss.push('</div>');
			ss.push('<div style="margin-top:3px;margin-left:10px;float:left;">');
				ss.push('<IMG src="images/View03.gif" ');
				ss.push('onmousedown="var tc=GetTcast();tc.StretchToFit=!tc.StretchToFit;');
				ss.push('if(tc.StretchToFit)this.src=\'images/View03.gif\';else this.src=\'images/View01.gif\';" >');
			ss.push('</div>');
		ss.push('</div></center>');
		
		if( ua.indexOf('MSIE') != -1 )
		{
			try
			{
				var x = new ActiveXObject("XPPIE.XPPIECtrl");
				result = 1;
			}catch(e){}
			
			ss.push('<object id="tomcast" classid="clsid:5AB1EF72-6CC6-4090-9030-8E0ACF7E6D3E" codebase="http://nba.tom.com/video/xppie.cab#version=1.0.0.1" width="440" height="395">');
			ss.push('<param name="Target" value="');
			ss.push(targ);
			ss.push('">');
			ss.push('<param name="url" value="');
			ss.push(videosrc);
			ss.push('">');
			ss.push('<param name="PluginLoaded" value="Tcast_Active()">');
			if(!result)	ss.push(_ds); //插件没有安装
			ss.push('</object>');
		}
		else if( (ua.indexOf('Firefox/3') != -1 || ua.indexOf('Opera/9') != -1) || ua.indexOf('Chrome') != -1 )
		{
			if(navigator.mimeTypes)
			{
				for (var i=0; i < navigator.mimeTypes.length; i++)
				{
					if(navigator.mimeTypes[i].type == 'application/x-tom-xpp')
					{
						result = 1;
						break;
					}
				}
			}
			
			if(!result) ss.push('<div style="display:none">');
			ss.push('<embed id="tomcast" ');
			ss.push('type="application/x-tom-xpp" pluginspage="http://nba.tom.com/video/xppff.xpi" width="440" height="395" ');
			ss.push('Target="');
			ss.push(targ);
			ss.push('" url="');
			ss.push(videosrc);
			ss.push('" PluginLoaded="Tcast_Active()">');
			if(!result) ss.push('</div>');
			if(!result) ss.push(_ds);
		}
		else
		{
			break;
		}
		
		if(elem)
			elem.innerHTML = ss.join('');
		else
			document.write(ss.join(''));
	
		return result;
		
	} while(false);
	
	var d = document;
	d.write('<div class="tallpic">');
		d.write('<div class="upload bg01">您的WEB浏览器不支持TOM直接软件</div>');
		d.write('<p style="color:#fff;text-align:center;margin:5px; padding:5px;font-size:14px;">');
			d.write('请使用Internet Explorer 6.0、Firefox 3.0、Opera 9.0、Google Chome。(或以上版本)');
	d.write('</p></div>');

	return 2;
}

function CheckXPPInstalled() //判断插件是否安装,0没安装,1已经安装,2浏览器不支持
{
	var ua = navigator.userAgent;
	if( ua.indexOf('MSIE') != -1 )
	{
		try
		{
			var x = new ActiveXObject("XPPIE.XPPIECtrl");
			return 1;
		}catch(e){}
	}
	else if( (ua.indexOf('Firefox/3') != -1 || ua.indexOf('Opera/9') != -1) || ua.indexOf('Chrome') != -1 )
	{
		if(navigator.mimeTypes)
		{
			for (var i=0; i < navigator.mimeTypes.length; i++)
			{
				if(navigator.mimeTypes[i].type == 'application/x-tom-xpp')
					return 1;
			}
		}
	}
	else
		return 2; //浏览器不支持

	return 0; //插件没有安装
}

function CheckTcastInstalled()
{
	try
	{
		var x = new ActiveXObject('TCAST.Player.' + tcast_ver_4ie);
	}
		catch(e){
	return false;
	}
	return true;
} 

function createTcast()
{
	

	function $(i){return document.getElementById(i);}
	var isie = false;


  tcast_html = $('tcast_Player');
	if (navigator.userAgent.indexOf('MSIE') == -1) //not ie
	{
		if(navigator.userAgent.indexOf('Firefox/3') == -1 ) // not firefox 3
		{
			if(navigator.userAgent.indexOf('Firefox') == -1) //not firefox, browser not support
			{
				//show message to user
				tcast_html.innerHTML ='<div class="tallpic"><div class="upload bg01">TOMLive控件不支持您的WEB浏览器</div>'+
					'<p style="color:#fff;text-align:center;margin:5px; padding:5px;font-size:14px;">'+
            				'请使用<a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=9ae91ebe-3385-447c-8a30-081805b2f90b&DisplayLang=zh-cn"'+
            				'target="_blank">Internet Explorer 6.0</a>以上版本或<a href="http://download.mozilla.org/?product=firefox-3.0.3&os=win&lang=zh-CN"'+
            				'target="_blank">Firefox3.0</a>及以上版本浏览器</p></div>';
				return false;
			}
			else //prompt user to update firfox
			{
				//show message to user
				tcast_html.innerHTML ='<div class="tallpic"><div class="upload bg02">请升级浏览器到Firefox 3.0</div>'+
					'<p style="color:#fff;text-align:center;margin:5px; padding:5px;font-size:14px;">'+
					'点击下载<a href="http://download.mozilla.org/?product=firefox-3.0.3&os=win&lang=zh-CN" target="_blank">Firefox 3.0</a></p></div>';
				return false;
			}
		}
		else //firefox 3
		{
			if (navigator.mimeTypes && navigator.mimeTypes.length > 0)
			{
				for (var i=0; i < navigator.mimeTypes.length; i++ )
				{
					if(navigator.mimeTypes[i].type == 'application/x-tcast-v1' && navigator.mimeTypes[i].description == ('TCAST NP' + tcast_ver_4ff) )
					{
						outputTcast(false);
						return true;
					}
				}
			}
		}
	}
	else //ie
	{
		try
		{
			var x = new ActiveXObject('TCAST.Player.' + tcast_ver_4ie);

			outputTcast(true);
			return true;
		}
		catch(e){}
		isie = true;
	}
	outputTcast(isie, true);
	return false;
}


function tcast_getList()
{
  var sl=new Array();
	var ll=0;
	sl[0]=0;
	for(i=0;i<llist.length;++i)
	{
		ll+=llist[i];
		sl[i+1]=ll;
	}
	var rd=Math.random()*ll;
	for(i=sl.length-1;i>=0;--i)
	{
		if(rd>=sl[i]) break;
	}    
	return i;
}

function outputTcast(ie, download)
{
	if (tcast_isplay) {
		return;
  	} 

  	tcast_isplay = true;
	if(slist.length==0) return;
	function $(i){return document.getElementById(i);}
	//tcast_html = $('tcast_Player');
	i = tcast_getList();
	
	var w=440;
	var h=395;
	var _ds='';
	var _os='';
	var tcast_obj ="";
	if(download)
	{
		_os='style="display:none" ';
		_ds='<div class="tallpic"><div class="upload"><p>如果您无法正常安装，请下载控件手动安装！</p>'+
			'<br/><br/><a href="http://nba.tom.com/video/tomliveV'+tcast_f_v+'.msi" target="_blank">'+
			'<img border="0" src="images/uploadbtn.jpg" /></a></div></div>';
	}
	else
	{
		_os='style="margin-top:22px" ';
	}

	if(ie)
	{

		tcast_obj = '<div style="width:440px;height:22px;background-color:#1f2a47">'+
		'<div style="margin-top:3px;margin-left:10px;float:left">'+
		'<IMG src="images/Fullscreen01.gif" onmouseout="this.src=\'images/Fullscreen01.gif\'" '+
			'onmouseenter="this.src=\'images/Fullscreen02.gif\'" '+
			'onmousedown="if(event.button==1)this.src=\'images/Fullscreen03.gif\'" '+
			'onmouseup="if(event.button==1){this.onmouseenter();tc.FullScreen=true;}"></div>'+
		'<div style="margin-top:3px;margin-left:10px;float:left;">'+
		'<IMG src="images/View03.gif" '+
			'onmousedown="if(event.button==1){tc.StretchToFit=!tc.StretchToFit;'+
			'if(tc.StretchToFit)this.src=\'images/View03.gif\';else this.src=\'images/View01.gif\';}" >'+
		'</div></div>'+
		'<object id="tc" style="margin-top:1px" classid="clsid:9CA74596-B5BB-4634-971C-F0224115A15F" '+
		'codebase="http://nba.tom.com/video/tcastV'+tcast_f_v+'.cab#version='+tcast_cab_v+'" width="'+w+'" height="'+h+'">'+
		'<param name="url" value="' + slist[i] + '">'+
		'<img id="countimg" src="about:blank" style="display:none"/>';
		//tcast_html.innerHTML = "";

		document.write(tcast_obj);
		tc.attachEvent("StateChanged", OnStateChanged);
	}
	else
	{
		tcast_obj = '<embed type="application/x-tcast-v1"'+_os+
		'pluginspage="http://nba.tom.com/video/tcastV'+tcast_f_v+'.xpi" width="'+w+'" height="'+h+'" '+
		'url="' + slist[i] + '" directurl="'+hlist[i] + '">'+_ds;
		tcast_obj += '<img src="http://analytics.tom.com/live.png" style="display:none"/>';
		//tcast_html.innerHTML = "";

		document.write(tcast_obj);
	}
}

function Tcast_CheckTcast()
{ 
	var nCheck = CheckXPPInstalled();
  	if( nCheck == 0 && CheckTcastInstalled () )
  	{	
		if(Math.random()*10 > 5)
  			{
				tcast_version = 1;

				createTcast();
				return true;
  			}
  	}
  	else if( nCheck  == 1 )
 		{
  		tcast_version  = 2;

  		Tcast_CreateTcast();
   		return true;
  	}
  	tcast_version = 0;
  	Tcast_CreateTcast ();
	return false;
}         
  
function Tcast_Select()
{
	if(tcast_version == 1)
	{
		createTcast();
	}
	else {
		Tcast_CreateTcast();
	}
}



Tcast_CheckTcast();

