var playerWidth;
var playerHeight;
var screenWidth;
var screenHeight;
var sourceID;
var sourceURL;
var allowFullScreen;
var autoStart;
var currentPosition;
var currentRemaining;
var playerStatus;
var showNavigation;

var currentPlayerWidth;
var currentPlayerHeight;
var currentScreenWidth;
var currentScreenHeight;

var stream_type;
var scope;
var content_group;
var current_guid;
var central_id;
var current_bitrate = "";
var trueworld_token;
var referer = "truemusic";
var ext;
var clientip;
var theme_path;
var guidtype;
var channel;
var jwplayer;
var playerSize;

var siamza_url = "http://truemusic.truelife.com/home/player/theme_tv/siamza/";

// ---------------------------------------------
// Playlist Variable
// ---------------------------------------------

var myMessage 




function initFPlayer(){
switch (playerSize)
	{
	case "tl":
			currentPlayerWidth=fpSize_w_tl.m;
			currentPlayerHeight=fpSize_h_tl.m;
			currentScreenWidth=fpSize_w_tl.m;
			currentScreenHeight=fpSize_h_tl.m;
			allowFullScreen=true;
			break;
	case "mini":
			currentPlayerWidth=fpSize_w_mini.m;
			currentPlayerHeight=fpSize_h_mini.m;
			currentScreenWidth=fpSize_w_mini.m;
			currentScreenHeight=0;
			allowFullScreen=false;
			break;
	default:
			currentPlayerWidth=fpSize_w.m;
			currentPlayerHeight=fpSize_h.m;
			currentScreenWidth=fpSize_w.m;
			currentScreenHeight=fpSize_h.m;
			allowFullScreen=true;
			break;
	}
	
	showNavigation=true;
	autoStart=true;
	prepareFPlayer();
	writeFPlayer();
	showBitrate(defaultTxtBitrate);
}

function prepareFPlayer(){
	//get_trueworld_token()
	if (current_bitrate == ""){current_bitrate = vdo_quality.hi;}
	//sourceURL = stream_url.replace("~streamtype~", stream_type).replace("~scope~", scope).replace("~contentgroup~", content_group).replace("~guid~", current_guid).replace("~bitrate~", current_bitrate).replace("~trueworldtoken~", trueworld_token).replace("~referer~", referer).replace("~channel~", channel).replace("~ext~", ext).replace("~clientip~", clientip).replace("~jwplayer~", jwplayer);
	sourceURL = stream_url.replace("~streamtype~", stream_type).replace("~scope~", scope).replace("~contentgroup~", content_group).replace("~guid~", current_guid).replace("~bitrate~", current_bitrate).replace("~trueworldtoken~", trueworld_token).replace("~referer~", referer).replace("~channel~", channel).replace("~ext~", ext).replace("~clientip~", clientip).replace("~jwplayer~", jwplayer);
	//alert("url = " + sourceURL);
}


function writeFPlayer()
{
  var so = new SWFObject('/home/player/flash/flvplayer.swf',swf,currentPlayerWidth,currentPlayerHeight,'9');
  so.addParam('allowscriptaccess','always');
  so.addParam('allowfullscreen',allowFullScreen);
  so.addParam('shownavigation',showNavigation);  
  so.addVariable('javascriptid','fpObj');
  so.addVariable('enablejs','true');
  so.addVariable('usefullscreen','true');
  so.addVariable('width',currentPlayerWidth);
  so.addVariable('height',currentPlayerHeight); 
  so.addVariable('displaywidth',currentScreenWidth);
  so.addVariable('displayheight',currentScreenHeight);
  so.addVariable('autostart',autoStart);
  so.addVariable('controlbar',"bottom");
  //so.addVariable('file','http://truemusic.truelife.com/home/player/streammanager.xml');
  so.addVariable('file',sourceURL);
  so.addParam("wmode","transparent");
  so.addParam("allowScriptAccess", "always");
  so.write('showFlashPlayer');
}

function set_hd_quality()
{
	f_stop(swf);
}

function set_hi_quality()
{
	current_guid = stream_guid.hi;
	current_bitrate = vdo_quality.hi;
	prepareFPlayer();
	writeFPlayer();
	showBitrate("hi");
}

function set_low_quality()
{
	current_guid = stream_guid.low;
	current_bitrate = vdo_quality.low;
	
	prepareFPlayer();
	writeFPlayer();
	showBitrate("low");
}


function showBitrate(quality)
{
	try
		{
			if(isBitrate){
				
				document.getElementById("showBitrate").style.visibility = "visible";

				if (quality=="" || quality == null)	{
					quality = defaultTxtBitrate;
				}
				
				switch (quality)
				{
				/*case "hd":
					document.getElementById("div_low_quality").innerHTML = '<A HREF="#" onclick="set_low_quality();" class="lnk_bitrate">256</a>';
					document.getElementById("div_hi_quality").innerHTML = '<A HREF="#" onclick="set_hi_quality();" class="lnk_bitrate">512</a>';
					document.getElementById("div_hd_quality").innerHTML = '<font class="b_white">HD</font>';
					break;*/
				case "hi":
					document.getElementById("div_low_quality").innerHTML = '<A HREF="#" onclick="set_low_quality();" class="lnk_bitrate">low</a>';
					document.getElementById("div_hi_quality").innerHTML = '<font class="b_white">high</font>';
					//document.getElementById("div_hd_quality").innerHTML = '<A HREF="#" onclick="set_hd_quality();" class="lnk_bitrate">HD</a>';
					break;
				case "low":
					document.getElementById("div_low_quality").innerHTML = '<font class="b_white">low</font>';
					document.getElementById("div_hi_quality").innerHTML = '<A HREF="#" onclick="set_hi_quality();" class="lnk_bitrate">high</a>';
					//document.getElementById("div_hd_quality").innerHTML = '<A HREF="#" onclick="set_hd_quality();" class="lnk_bitrate">HD</a>';
					break;
				}
			}
			else {
				document.getElementById("showBitrate").style.visibility = "hidden";
			}
		}
		catch (e)
		{
		}
	
}

function set_zoom_in()
{
	currentPlayerWidth=fpSize_w.s;
	currentPlayerHeight=fpSize_h.s;
	currentScreenWidth=fpSize_w.s;
	currentScreenHeight=fpSize_h.s;
	allowFullScreen=true;
	showNavigation=true;
	autoStart=true;
	prepareFPlayer();
	writeFPlayer();
	$("zoom_title_size").innerHTML = ''+fpSize.s+'%';
}

function set_zoom_out()
{
	currentPlayerWidth=fpSize_w.m;
	currentPlayerHeight=fpSize_h.m;
	currentScreenWidth=fpSize_w.m;
	currentScreenHeight=fpSize_h.m;
	allowFullScreen=true;
	showNavigation=true;
	autoStart=true;
	prepareFPlayer();
	writeFPlayer();
	$("zoom_title_size").innerHTML = ''+fpSize.m+'%';
}

function set_zoom_in_tl()
{
	currentPlayerWidth=fpSize_w_tl.s;
	currentPlayerHeight=fpSize_h_tl.s;
	currentScreenWidth=fpSize_w_tl.s;
	currentScreenHeight=fpSize_h_tl.s;
	allowFullScreen=true;
	showNavigation=true;
	autoStart=true;
	prepareFPlayer();
	writeFPlayer();
	$("zoom_title_size").innerHTML = ''+fpSize.s+'%';
}

function set_zoom_out_tl()
{
	currentPlayerWidth=fpSize_w_tl.m;
	currentPlayerHeight=fpSize_h_tl.m;
	currentScreenWidth=fpSize_w_tl.m;
	currentScreenHeight=fpSize_h_tl.m;
	allowFullScreen=true;
	showNavigation=true;
	autoStart=true;
	prepareFPlayer();
	writeFPlayer();
	$("zoom_title_size").innerHTML = ''+fpSize.m+'%';
}



function addToFavorite()
{
	var strTitle = document.title;
	strTitle = strTitle.substring(0,100);
	window.external.AddFavorite(location.href, strTitle); 
}


function sendToFriends()
{
	var path = window.location;
	var windowprops = "directories=no,scrollbars=no,width=480,height=350,left=200,top=50,resizable=1";
	window.open(domain_trueworld+"newsletter/send2fr/index.jsp?requestpath=" + escape(path),"",windowprops)
}

function get_trueworld_token()
{
	var URL = "http://truemusic.truelife.com/home/player/inc/inc_genTrueworldToken2.php";
		param = "r=" + Math.floor(Math.random()*100);
		URL = URL+"?"+param;
	var xmlr = new clsAjaxMini(); 
	var xChild = xmlr.LoadSync("GET", URL,"",true);
		trueworld_token = xChild;

}

function get_livechat_meta(conId)
{
	var URL = "http://truemusic.truelife.com/home/player/inc/inc_call_meta_livechat.php";
		param = "conId="+conId+"&r=" + Math.floor(Math.random()*100);
		URL = URL+"?"+param;
		
	var xmlr = new clsAjaxMini(); 
	var xChild = xmlr.LoadSync("GET", URL,"",true);
	//	trueworld_token = escape(xChild);
		$("livechat_meta").innerHTML = xChild;
}

/* Start: DEBUG =====*/
function log(txt)
{
	document.getElementById("log").innerText += "\n" + txt;
}



// Start: Tong Function ---- //

//---------------------------------------------
// Song Information Function
//---------------------------------------------
/*function $(name) {
	return document.getElementById(name);
}*/

function gid(name) {
	return document.getElementById(name);
}

function loadInformation(JONMessage)
{
    myMessage=eval('(' + JONMessage + ')');        
}

//---------------------------------------------
// Event Function
//---------------------------------------------
function thisMovie(movieName) {
	return document.getElementById(movieName);
};

function sendEvent(swf,typ,prm) { 
	thisMovie(swf).sendEvent(typ,prm); 
};

function getUpdate(typ,pr1,pr2,swf) {
	//log('type:'+typ+' ,pr1:'+pr1+' ,pr2:'+pr2+' ,swf:'+swf);
	if(typ == "state"){	
        chargeStatus(pr1);
    }else if(typ == "item") { 
        chargeItem(swf,pr1);
    }
    else if(typ == "time") { 
        chargeRemain(swf,pr1,pr2);
    }
	

};


// --------------------------------------------------------
// Charge Status
// --------------------------------------------------------

function chargeStatus(pr1)
{
	    if (pr1 ==0)
	    {
		    gid('showState').innerHTML = "ready/paused";
		    playerstatus = 0;
	    }
	    else if (pr1 ==1)
	    {
		    gid('showState').innerHTML = "loading";
		    playerstatus = 1;
	    }
	    else if (pr1 ==2)
	    {
		    gid('showState').innerHTML = "playing";	 
			//alert("playing");
			//alert(thisMovie(swf));
//			alert('Current Media:'+thisMovie(swf).currentMedia.sourceURL);
			//log('Current Media:'+thisMovie(swf).currentMedia.sourceURL);
		    playerstatus = 2;
	    }
}

function chargeRemain(swf,pr1,pr2)
{

    if (parseInt(parseInt(pr2)/60) < 10)
    {
        mm = '0'+ parseInt(parseInt(pr2)/60)
    }
    else
    {
        mm = parseInt(parseInt(pr2)/60)
    }
        
    if (parseInt(parseInt(pr2)%60) < 10)
    {
        ss = "0" + parseInt(parseInt(pr2)%60)
    }
    else
    {
        ss = parseInt(pr2)%60
    }

   gid("timeshow").innerHTML= mm + ":" + ss;

}

function w_play_tv_item(url, newwin)
{
	if (url == domain_name+"home/player/player_livetv.php?guid=1")
	{
		url = livetv_ch3_url;
		newwin = "playerwin";
	}
	if (url == domain_name+"home/player/player_livetv.php?guid=3")
	{
		url = livetv_ch3_url;
		newwin = "playerwin";
	}
	if (newwin != null && newwin != "undefined")
			window.open(url);
	else
	{
		window.location.href = url;
	}
}

/*function w_play_tv_partner_player(url, newwin)
{
	if (url == domain_name+"home/player/theme_tv/siamza/index.php?guid=1")
	{
		url = livetv_ch3_url;
		newwin = "playerwin";
	}
	if (url == domain_name+"home/player/theme_tv/siamza/index.php?guid=3")
	{
		url = livetv_ch3_url;
		newwin = "playerwin";
	}
	if (newwin != null && newwin != "undefined")
			window.open(url);
	else
	{
		window.location.href = url;
	}
}*/


function w_play_tv_partner_player(ch, newwin)
{
	if (ch == "" || ch == null || ch == "undefined")
	{
		alert("กรุณากำหนดช่องรายการ");
		return false;
	}
	else
	{
		var url = siamza_url+"?guid="+ch;
		}	
		if (newwin != null && newwin != "undefined")
			window.open(url);
		else
			window.location.href = url;
	
}
function f_stop(swf)
{
        document.getElementById(swf).sendEvent('stop','');
}
