/********************************************************************************
This is a part of the ieHIP Software Code.
Copyright (C) 2003-2006 A5tek Corporation.
All rights reserved.
The contents of this file are subject to the ieHIP Software License Agreement
Version 1.0 (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.iehip.com/legal/sw-license/1.0/
*********************************************************************************/
var DUniversalTVX;
function CTV( oTV, oMedia,i_myJSONObject )
{
	DUniversalTVX=this.TV = oTV;
	this.Media = oMedia;
	
	
	
/*	
	this.dev_i = null; // device index
	this.source_i = null;  // source index
	this.MaxVideoSouce=null;
	this.CurrentVideoSouce=1;
	this.currChannel = null;
	this.PlayerState = null;
	
	// surfing object
	this.Surfing = new Object();
	this.Surfing.Channel = new Array();
	this.Surfing.Image = new Array();
	this.Surfing.Count = 0;
	this.Surfing.Done = false;
	
	this.AudioLines =	new Array('WAV','AUX','LIN','MIC','CD ','CD-');
	this.AudioLineNames = new Array('Wave','Aux In','Line In','Mic In','CD In','CD In');
	this.AudioOutLine = null;
	this.timerId = -1;
	this.scanId = -1;
	this.SignalName = new Array('Antenna', 'Cable');
	
//POwen 	this.Country = new parent.CCountry();


	CTV.prototype.SetProperty = TV_SetProperty;
	CTV.prototype.Broadcast = TV_Broadcast;	
	CTV.prototype.Play = TV_Play;
	CTV.prototype.Stop = TV_Stop;
	CTV.prototype.Pause = TV_Pause;
	CTV.prototype.LastChannel = TV_LastChannel;
	CTV.prototype.CaptureImage = TV_CaptureImage;
	CTV.prototype.ChannelSurf = TV_ChannelSurf;
	CTV.prototype.MenuItem = TV_MenuItem;
	CTV.prototype.GetContentPath = TV_GetContentPath;
	CTV.prototype.InitContentPath = TV_InitContentPath;
	CTV.prototype.SortableDateTime = TV_SortableDateTime;
	CTV.prototype.addZero = TV_addZero;
	CTV.prototype.Executable = TV_Executable;
	CTV.prototype.getImgFilename = TV_getImgFilename;
	CTV.prototype.getVideoFilename = TV_getVideoFilename;
	CTV.prototype.AvailableAudioDevice = TV_AvailableAudioDevice;
	CTV.prototype.SetAudioDevice = TV_SetAudioDevice;
	CTV.prototype.AvailableAudioSource = TV_AvailableAudioSource;
	CTV.prototype.SetAudioSource = TV_SetAudioSource;
	CTV.prototype.AutoScan = TV_AutoScan;

	
	
	CTV.prototype.AddChannel = TV_AddChannel;
	CTV.prototype.DeleteChannel = TV_DeleteChannel;
	CTV.prototype.SetSignalSource = TV_SetSignalSource;
	CTV.prototype.SetCountry = TV_SetCountry;
	CTV.prototype.SetVideoQuality = TV_SetVideoQuality;
	CTV.prototype.CB = TV_CB;
	CTV.prototype.PlayTV = TV_PlayTV;
	CTV.prototype.Record = TV_Record;
	CTV.prototype.WSpace2UScore = TV_WSpace2UScore;
	CTV.prototype.ShowMessage = TV_ShowMessage;
	CTV.prototype.LineMute = TV_LineMute;
//	CTV.prototype.SetChannel = TV_SetChannel;
	CTV.prototype.onDoubleClick = TV_onDoubleClick;
	//CTV.prototype.GotFocus = TV_GotFocus; //ge;-, native event not working as of 7Apr2006	
	

	CTV.prototype.onMouseMove=TV_onMouseMove;
	CTV.prototype.onfocus=TV_onfocus;
	
	CTV.prototype.SetMMK=TV_SetTVMMK;
	
	this.PATH_MYCAPTURED = null;
	this.PATH_MYRECORDED = null;
	this.ESC_CONTENTPATH = null;
	this.DIR_MYCAPTURED = 'My Captured Images\\';
	this.DIR_MYRECORDED = 'My Recorded Programs\\';
	this.IMG_PREFIX = 'IMAGE_';
	this.IMG_EXTENSION = '.JPG';
	this.VID_PREFIX = 'VIDEO_';
	
	////////////////////////
	this.c_channel=0;
	////////////////////////
	this.InitContentPath();
	
	*/
	
	
	
	CTV.prototype.Next = TV_Next;
	CTV.prototype.Previous = TV_Previous;
	CTV.prototype.Switch = TV_Switch;
//	CTV.prototype.Setup = TV_Setup;
	CTV.prototype.CH = TV_CH;
		
	
	this.LayerDIVName=i_myJSONObject["divname"];	
	CTV.prototype.Duration = TV_Duration;
	CTV.prototype.TimeOut = TV_TimeOut;			
	CTV.prototype.Inital =  TV_Inital;
	CTV.prototype.Stop	 =TV_Stop;
	// Inital
	


	

	


	try
	{		
	 this.Inital(i_myJSONObject);	
	// this.Next();
	}catch(err){}
		
}


function TV_Inital(i_myJSONObject)
{

	var t_currentcontent=i_myJSONObject["currentcontent"];	
	////////////////////////////////////////
	// for Duration time
			


	
	
 	// var t_UniplayDivName=this.LayerDIVName+"CTV"; //this.Player;			
   	var t_objUniplayDivName= this.TV;  //JSFUN_getElementByIdCompatible(t_UniplayDivName);
	
			
	this.TV.style.height = "100%";
	this.TV.style.width  = "100%"; 




	// document.all.DUniversalTVX=this.TV;
	g_GlobeValue["SET_TV_CurrentVideoDevice"]=o_SET_TV_CurrentVideoDevice=this.TV.getIniData("UniversalTV.ini","UniversalHDTV","IDC_SET_TV_CurrentVideoDevice","NOVIDEODEVICE");


    g_GlobeValue["TVCurrentVideoDeviceName"]=o_TVVideoDeviceName=g_TVCurrentVideoDeviceName=this.TV.getIniData("UniversalTV.ini","UniversalHDTV","IDC_SET_TVVideo_Device"+o_SET_TV_CurrentVideoDevice,"NOVIDEODEVICE");     
    g_GlobeValue["TVAudioDeviceName"]=t_TVAudioDeviceName=this.TV.getIniData("UniversalTV.ini",g_TVCurrentVideoDeviceName,"IDC_SET_TVAUDIO_DEVICENAME","NOVIDEODEVICE");					
     
			                  
							  
							  
        ///////// Get the Audio Name and write to ini File.
     this.TV.BuildGraph(o_TVVideoDeviceName,t_TVAudioDeviceName,1);	   	
	 
	 
	// 	   {'player':'CTV',"url":"DSContent/samsung.wmv","style": "width:100%;height:100%;","parameter":
	//	   {"duration":10000,"tag":"AirPort","website":"http://www.looptek.com","Chnnel":12,"Major":-1,"Minor":-1}},	
	
	try{	      
	 t_Chnnel=i_myJSONObject["jsondata"][t_currentcontent]["parameter"]["Chnnel"];	
	 t_Marjor=i_myJSONObject["jsondata"][t_currentcontent]["parameter"]["Major"];	
	 t_Minor=i_myJSONObject["jsondata"][t_currentcontent]["parameter"]["Minor"];	

	 if (t_Chnnel!=null)
	 {
	 FunSetChannel(t_Chnnel,t_Marjor,t_Minor);
	 }
	 }catch(err){}
	 
	 
	 var t_TVAction=i_myJSONObject["jsondata"][t_currentcontent]["parameter"]["TVAction"];
	if (t_TVAction=="Broadcast")
	{
		t_Preview=i_myJSONObject["jsondata"][t_currentcontent]["parameter"]["Preview"];	
		if (t_Preview==null)
		{ 
			t_Preview=false; 
		}		
		FunTVMyWay_Broadcast(t_Preview);
	}else if (t_TVAction=="Live")
	{
		
	}
	 

	 
	 
	 
	 
//	 ChannelUp();
//	 ChannelUp();
//     g_GlobeValue["AudioDControls"]=t_AudioDControls=document.all.DUniversalTVX.XMLCommand("GETAUDIOCONTROL","<AudioDeviceName>"+t_TVAudioDeviceName+"</AudioDeviceName>");  			 
//    podebug(g_GlobeValue.toJSONString());

/*
				
	a1a1=this.TV.getIniData("UniversalTV.ini","UniversalHDTV","IDC_SET_TV_CurrentVideoDevice","NOVIDEODEVICE");	
	alert(a1a1);	
	alert("1");	
*/
//   	g_GlobeValue["SET_TV_CurrentVideoDevice"]=o_SET_TV_CurrentVideoDevice=this.TV.getIniData("UniversalTV.ini","UniversalHDTV","IDC_SET_TV_CurrentVideoDevice","NOVIDEODEVICE");
	
	
	/*
    g_GlobeValue["TVCurrentVideoDeviceName"]=o_TVVideoDeviceName=g_TVCurrentVideoDeviceName=this.TV.getIniData("UniversalTV.ini","UniversalHDTV","IDC_SET_TVVideo_Device"+o_SET_TV_CurrentVideoDevice,"NOVIDEODEVICE");     
    g_GlobeValue["TVAudioDeviceName"]=t_TVAudioDeviceName=this.TV.getIniData("UniversalTV.ini",g_TVCurrentVideoDeviceName,"IDC_SET_TVVIDEO_DEVICENAME","NOVIDEODEVICE");					

                          
        ///////// Get the Audio Name and write to ini File.
     this.TV.BuildGraph(o_TVVideoDeviceName,t_TVAudioDeviceName,1);	   	
     g_GlobeValue["AudioDControls"]=t_AudioDControls=this.TV.XMLCommand("GETAUDIOCONTROL","<AudioDeviceName>"+t_TVAudioDeviceName+"</AudioDeviceName>");  	
    podebug(g_GlobeValue.toJSONString());

   //  updateTVControls();	
*/
		



	
	////////////////////////////////////////
	// for Duration time
			

    var t_UniplayDivName=this.TV.id; //this.LayerDIVName+"CTV"; //this.Player;	
	
	var  EventArray = {"action":"event","actiondetail":"PlayStateChange","player":"CTV","data":0};		
             EventArray["UniplayDivName"]=t_UniplayDivName;			 
             EventArray["jsondata"]=i_myJSONObject;		
    var t_duration=0;
	
	
	try
    {
		    t_duration=i_myJSONObject["jsondata"][t_currentcontent]["parameter"]["duration"];					
			if (t_duration!=-1 && t_duration!=null)
			{
				this.Duration(EventArray.toJSONString(),t_duration);
			}
	}catch(err)
	{

	}			

		
			
}

function TV_TimeOut(i_EventArray)
{
	
	var myObject = i_EventArray.parseJSON();
	TV_Stop();
	// YAHOO.log(myObject);   
	JS_JSONStringProcess(myObject);
}

function TV_Duration(i_EventArrayString,t_duration)
{
	var i_EventArray = i_EventArrayString.parseJSON();	
	
	var t_divname=i_EventArray["jsondata"]["divname"];	
	i_EventArray["TimeIDField"]= t_divname;
	// var timerID=0; 	
	this.EventArray=i_EventArray;
	var t_EventArrayString=i_EventArray.toJSONString();
	var t_CallBackFunction="TV_TimeOut('"+t_EventArrayString+"')";	
	var timerID=setTimeout(t_CallBackFunction,t_duration);	
	return timerID;
}



///////////////////////////////////////////////////////
function TV_CB(obj)
{
	if(this.cbFunction == null ) return;
	
	this.cbFunction(obj);
}


function TV_Pause()
{
	var TV = this.TV;
//	var t_RecordingPath=document.all.DUniversalTVX.getIniData("UniversalTV.ini","UniversalHDTV","IDC_SET_TVVideo_Device"+o_SET_TV_CurrentVideoDevice,"NOVIDEODEVICE");     		


	switch(this.PlayerState) {
		case 'play':
			this.LineMute(this.AudioLineNames[2], true); // default mute is Line In
			this.LineMute(TV.getAudioControlLineName(TV.getCurrentAudioControlLine()), true); // user settings mute 
			TV.Pause();
			this.PlayerState = 'pause'; // timeshift
			var o = new Object();
					o.s = 'TimeShifting: On';
					o.suspend = true;
			this.ShowMessage(o);
			break;
		case 'playfile':
			TV.Pause();
			this.PlayerState = 'pausefile';
			break;
	}
}

function TV_Record()
{
	UNI_JS_CallParent(2,0);	
	var TV = this.TV;
//LoopTeK\\Univergence

//var temp2=TV.getCRegValue("SOFTWARE\\LoopTeK\\UniversalHDTV","ContentPath");
//alert(temp2);
	var strPath =this.PATH_MYRECORDED + this.getVideoFilename();
	//alert(strPath);
	TV.Record(strPath);
	var o = new Object();
					o.s ='Recording: On';
					o.suspend = true;
	this.ShowMessage(o);
	this.PlayerState = 'recording';
}
/*
function TV_Stop()
{
	UNI_JS_CallParent(2,0);	
	var TV = this.TV;
	var o = new Object();
		
	switch(this.PlayerState) {
		case 'pause': //timeshift
		case 'playfile':
		case 'pausefile':
			this.LineMute(this.AudioLineNames[2], false); // release default mute
			TV.Stop();//1);
			this.PlayerState = 'play'; // back to live
			o.s = 'TimeShifting: Off';
			break;		
		case 'autoscan':
			TV.Stop();//1);
			clearTimeout(this.scanId);
			this.PlayerState = 'play'; // back to live
			o.s = 'Auto Scan: Off';
			break;	
		case 'recording':
			TV.Stop();//1);
			this.PlayerState = 'play'; // back to live
			o.s = 'Recording: Off';
			this.ShowMessage(o);
			break;	
		case 'channelsurf':
			TV.Stop();//1);
			this.PlayerState = 'play'; // back to live
			o.s = 'Channel Surf: Off';
			this.ShowMessage(o);
			break;	
		case  'broadcast':
			TV.Stop();//1);
			//TV.StopDevice();
			FunPlayLive();
			this.PlayerState = 'stop';
			o.s = 'broadcast: Off';
			break;						
		case 'play':
			TV.Stop();
			o.s = 'Stop';
			break;	
		default:
			TV.Stop();//1);
			//TV.StopDevice();
			this.PlayerState = 'stop';
			break;	
	}
	
	this.SetMMK();
	this.ShowMessage(o.s);
	return true;
}
*/
function TV_LastChannel()
{
	TV.Recall();
	if(TV.currentChannel() != -1) {
		var o = new Object();
				o.s = 'Ch: ' + TV.currentChannel();
		this.ShowMessage(o);
	}
}

function TV_ChannelSurf()
{
	var TV = this.TV;
	
	TV.ChannelSurf();
	var o = new Object();
			o.s = 'Channel Surf: On';
			o.suspend = true;
	this.ShowMessage(o);
	this.PlayerState = 'channelsurf';
}

function TV_GetContentPath()
{
	var TV = this.TV;
	var path = TV.getCRegValue("SOFTWARE\\LoopTeK\\UniversalHDTV","ContentPath");

	
	var re = /(\\)/gi;
	this.ESC_CONTENTPATH = path.replace(re, '\\\\');
	return path
}

function TV_InitContentPath()
{
	this.PATH_MYCAPTURED = this.GetContentPath() + this.DIR_MYCAPTURED;
	this.PATH_MYRECORDED = this.GetContentPath() + this.DIR_MYRECORDED;
}

function TV_CaptureImage() 
{
	var TV = this.TV;
	
	var strPath = this.PATH_MYCAPTURED + this.getImgFilename();
	
	TV.Capture(strPath);
	var o = new Object();
			o.s = 'Image Captured';
	this.ShowMessage(o);
}


/***********************************************************/
function TV_MenuItem(obj) 
{
	var o = new Object();
			o.ioMenu = new parent.IOReturn();
			o.Menu = new Array();
			
	var TV = this.TV
	var item;
	
	var ActionID = obj.ActionID; 	
	
	switch(ActionID) {
		case 1803: // Last Channel
			this.LastChannel();
			break;
		case 1806: // Capture Still Image
			this.CaptureImage();
			break;
		case 1807: // Channel Surf
			this.ChannelSurf();
			//this.Sleep(this);
			break;
		case 1816: // Audio Device
			if(this.Executable(obj)) {
				this.SetAudioDevice(obj.URL);
			}
			else {
				item = this.AvailableAudioDevice();
				for(var i=0; i<=item.length-1; i++) {
					o.Menu[i] = new parent.MenuItem();
					/*
					with(o.Menu[i]) {
						ActionID = 1816;
						Title = item[i].Name;
						URL = String(item[i].Value);
						Icon = this.ESC_CONTENTPATH + 'Icons\\TV\\audio_device.JPG';	
					}
					*/
					o.ioMenu.Menu.item[i] = o.Menu[i];
				}
				o.ioMenu.Menu.Title = 'Audio Device';
				var ob = new Object();
						ob.s = 'Current Audio Device: ' + TV.getAudioDeviceName(TV.getCurrentAudioDevice());
				this.ShowMessage(ob);
			}
			break;
		case 1822: // Auto Scan
			this.AutoScan();
			break;
		case 1823: // Audio Source
			if(this.Executable(obj)) {
				this.SetAudioSource(obj.URL);
			}
			else {
				item = this.AvailableAudioSource();
				for(var i=0; i<=item.length-1; i++) {
					o.Menu[i] = new parent.MenuItem();
					/*
					with(o.Menu[i]) {
						ActionID = 1823;
						Title = item[i].Name;
						URL = String(item[i].Value);
						Icon = this.ESC_CONTENTPATH + 'Icons\\TV\\' + this.WSpace2UScore(item[i].Name) + '.JPG';	
					}
					*/
					o.ioMenu.Menu.item[i] = o.Menu[i];
				}
				o.ioMenu.Menu.Title = 'Audio Source';
				var ob = new Object();
						ob.s = o.s = 'Current Audio Source: ' +  TV.getAudioControlLineName(TV.getCurrentAudioControlLine());
				this.ShowMessage(ob);
			}
			break;
		case 1825: // Add Channel
			this.AddChannel();
			break;
		case 1826: // Delete Channel
			this.DeleteChannel();
			break;
		case 1829: // Signal Source
			this.SetSignalSource(obj.URL);
			break;
		case 1830: // Country
			this.SetCountry(obj.URL);
			break;
		case 1831: // Video Quality
			this.SetVideoQuality(obj.URL);
			break;
		case 1834: // Video Quality Menu
			var ob = new Object();
					ob.s = 'Current Video Quality: ' + TV.GetVideoQualityName(TV.GetVideoQuality());
			this.ShowMessage(ob);
			break;
		case 1835: // Signal Source Menu
			var ob = new Object();
					ob.s = 'Current Signal Source: ' + this.SignalName[parseInt(TV.GetInputType(),10)];
			this.ShowMessage(ob);
			break;
		case 1836: // Country Menu
			var ob = new Object();
					ob.s = 'Current Country: ' + this.Country.getCountryName(TV.getCountryCode());
			this.ShowMessage(ob);
	}
	
	o.ioMenu.Menu.MenuType = 2;
	o.ioMenu.CInfo.Source = 'TV';
	
	return o;
}

function TV_SetSignalSource( iSource )
{
	var TV = this.TV;
	
	iSource = parseInt(iSource,10);
	
	TV.SetInputType(iSource);
	var o = new Object();
			o.s = 'Signal Source set to ' + this.SignalName[parseInt(TV.GetInputType(),10)];
	this.ShowMessage(o);
}

function TV_SetCountry( iCode )
{
	var TV = this.TV;
	iCode = parseInt(iCode,10);
	
	if(TV.setCountryCode(iCode) != -1) {
		var o = new Object();
			o.s = 'Country set to ' + this.Country.getCountryName(TV.getCountryCode());
		this.ShowMessage(o);
	}
	else {
		var o = new Object();
			o.s = 'Country Not Supported';
		this.ShowMessage(o);
	}
}

function TV_SetVideoQuality( iQuality )
{
	var TV = this.TV;
	iQuality = parseInt(iQuality,10);
	
	TV.SetVideoQuality(iQuality);
	var o = new Object();
			o.s = 'Video Quality set to ' + TV.GetVideoQualityName(TV.GetVideoQuality());
	this.ShowMessage(o);
}

function TV_AddChannel()
{
	var TV = this.TV;
	
	TV.AddChannel(TV.currentChannel());
	if(TV.currentChannel() != -1) {
		var o = new Object();
				o.s = 'Channel ' + TV.currentChannel() + ' Added';
		this.ShowMessage(o);
	}
}

function TV_DeleteChannel()
{
	var TV = this.TV;
	
	TV.RemoveChannel(TV.currentChannel());
	if(TV.currentChannel() != -1) {
		var o = new Object();
				o.s = 'Channel ' + TV.currentChannel() + ' Removed';
		this.ShowMessage(o);
	}
}

function TV_AutoScan()
{
	var TV = this.TV;
	
	TV.ScanChannel();
	this.PlayerState = 'autoscan';		
}

function TV_SetAudioDevice( iDevice )
{
	var TV = this.TV;
		
	iDevice = parseInt(iDevice,10);
	
	TV.setAudioDevice(iDevice);
	var o = new Object();
			o.s = 'Audio Device set to ' + TV.getAudioDeviceName(TV.getCurrentAudioDevice());
	this.ShowMessage(o);
}

function TV_AvailableAudioDevice()
{
	var TV = this.TV;
	var AudioDev = new Array();
	
	for(var i=0; i<=TV.AudioDeviceCount-1; i++) {
		AudioDev[i] = new Object();	
		AudioDev[i].Name = TV.getAudioDeviceName(i+1);
		AudioDev[i].Value = i+1;
	}
	
	return AudioDev;
}

function TV_SetAudioSource( iSource )
{
	var TV = this.TV;

	iSource = parseInt(iSource,10);
	
	TV.setAudioControlLine(iSource);
	var o = new Object();
			o.s = 'Audio Source set to ' +  TV.getAudioControlLineName(TV.getCurrentAudioControlLine());
	this.ShowMessage(o);
}

function TV_AvailableAudioSource()
{
	var TV = this.TV;
	var AudioSrc = new Array();
	var linecount = 0;
	
	for(var i=0; i<=this.AudioLines.length-1; i++) {
		for(var j=0; j<=TV.AudioControlLineCount-1 ; j++) {
			var linename = TV.getAudioControlLineName(j);
			if(linename.substr(0,3).toUpperCase() == this.AudioLines[i]) {
				AudioSrc[linecount] = new Object();	
				AudioSrc[linecount].Name = this.AudioLineNames[i];
				AudioSrc[linecount].Value = j;
				linecount++;
			}
		}
	}
	return AudioSrc;
}



function TV_Previous()
{
	var TV = this.TV;	
	var t_ch2=TV.ChannelDown();
	
    var t_ch=TV.currentChannel();
	if( t_ch != -1) {
	/*
		var o = new Object();
				o.s = 'Ch: ' + TV.currentChannel();
		this.ShowMessage(o);
	*/	
	
		UNI_Message('Ch: ' + t_ch); //TV.currentChannel());
		var t_channel = document.getElementById('channel');
		if (t_channel!=null)
		{
			t_channel.value=t_ch;
		}		
	}
	
    UNI_Message(t_ch);	
	var t_channel = document.getElementById('channel');
	if (t_channel!=null)
	{
		t_channel.value=t_ch;
	} 
}

function TV_LineMute( sOSDLineName, bMute )
{
	var TV = this.TV;
	try {
		if((sOSDLineName==undefined) && (bMute==undefined)) {
			sOSDLineName = TV.getAudioControlLineName(TV.getCurrentAudioControlLine());					
		}
	
		var hipAudio = new parent.iehipAudio(); // see iehipAudio.js
		var lines = hipAudio.lines;
		
		for(var i=0; i <= lines.count() - 1; i++)
		{
			if(sOSDLineName == lines.item(i).name) {
				if(bMute==undefined) { 
					bMute = !lines.item(i).isMute();
				}
				lines.item(i).mute(bMute);
				return true;
			}
		}
	}
	catch(er)
	{
		return false;
	}
}

function TV_SetChannel( iChannel )
{
	var TV = this.TV;	
	if (TV!=null)
	{
//		TV.ChannelUp();
//	}
	
	
	 TV.SetChannel(iChannel,-1,-1);
	 TV.SetIniData("UniversalTV.ini",g_TVCurrentVideoDeviceName,"IDC_SET_TVVIDEO_CHANNELINPUT",iChannel+",-1,-1");	 
	}

	
    UNI_Message("Channel:"+iChannel);	
	
	/*
	var iChan = parseInt(iChannel,10); 	
	
	if(!isNaN(iChan)) {	
		TV.SetChannel(parseInt(iChannel,10));
		
		if(TV.currentChannel() != -1) 
		{

		}
	}
	else {

	}
	*/
}

// Event
function TV_onSurfing(oUni, sparam, lparam)
{
	var oTV = oUni.CurrPlayer;
	
	oTV.Surfing.Channel[oTV.Surfing.Count] = lparam;
	oTV.Surfing.Image[oTV.Surfing.Count] = sparam;
	oTV.Surfing.Count++;
	
	/*
	var mnu = new parent.MenuItem();
			mnu.Title = 'Test';
	
	parent.doDispPerItem(mnu, 0);
	*/
}

function TV_onSurfDone(oUni)
{
	var oTV = oUni.CurrPlayer;
	oTV.Surfing.SurfDone = true;
}

function TV_onChannelScanning(oUni, iChannel, iSignal)
{
	var oTV = oUni.CurrPlayer;
	var signalCap = new Array('No', 'Yes');
	
	var o = new Object();
			o.s  = 'Auto Scan: On<br>';
			o.s += 'Ch: ' + iChannel + '<br>';
			o.s += 'Signal: ' + signalCap[parseInt(iSignal,10)];
			//o.suspend = true;
	oTV.ShowMessage(o); 
	
	clearTimeout(oTV.scanId);
	oTV.scanId = setTimeout(function() {
		TV_SetScanningDone(oTV, true);	
	}, 5000);
}
// end Event

/******************************************************************/
// Helper
function TV_SetScanningDone(o, bDone)
{
	if(bDone)
		o.PlayerState = 'play';
}

function TV_Executable(o)
{
	if(o.URL != '')
		return true;
	else
		return false;
}


function TV_SortableDateTime() // yyyyMMddHHmmss
{
	var today = new Date();
	
	var yyyy = today.getFullYear();
	var MM   = today.getMonth(); // 0-11
	var dd   = today.getDate(); //0-31

	var HH	 = today.getHours(); //0-23
	var mm	 = today.getMinutes();
	var ss	 = today.getSeconds(); //0-59
	
	MM++; // add 1 since it starts at 0
	ss++;
	
	return yyyy.toString() + this.addZero(MM) + this.addZero(dd) + this.addZero(HH) + this.addZero(mm) + this.addZero(ss);
}

function TV_addZero(num)
{
	if(num < 10)
		return "0" + num.toString();
	else
		return num.toString();
}

function TV_getImgFilename()
{
	return this.IMG_PREFIX + this.SortableDateTime() + this.IMG_EXTENSION;
}

function TV_getVideoFilename()
{
	var TV = this.TV;
	
	if(TV.currentChannel() != -1)
		var sprefix = "Ch" + TV.currentChannel() + "_";
	else
		var sprefix = this.VID_PREFIX+"_"; 
		
	return sprefix +  this.SortableDateTime();
}

function TV_WSpace2UScore(str)
{
	var uscore = '_';
	var wspace = ' ';
	var re = new RegExp(wspace, 'ig');
	
	return str.replace(re, uscore);
}



function TV_onDoubleClick() {
	UNI_toggleScreenDisplay();
}

function TV_GotFocus() {
	//parent.objInputFocus.throwFocus();
	//alert("TV_GotFocus");
}

function TV_onfocus()
{
	try{
			parent.toggleDMMonClick();
		}	
		catch(er) 
		{
		/*if(parent.DEBUG_MODE) 
		    {
			this.Error.Occur = true;
			this.Error.Id = fnid;
			this.Error.Code = er.number;
			this.Error.Message = er.description;
			throw er;
			}*/
		}

	
	TV_GotFocus();	
}

///////////////////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////////////////
function TV_onMouseMove(event)
{
   try{
	       parent.showDMM(event);
	   }	
	   catch(er) 
	   {
		  /*if(parent.DEBUG_MODE) 
	      {
		  	this.Error.Occur = true;
			this.Error.Id = fnid;
			this.Error.Code = er.number;
			this.Error.Message = er.description;
			throw er;
		  }*/
	   }
}
	
	

///////////////////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////////////////
function TV_Play()
{
	var TV = this.TV;
	
	switch(this.PlayerState) {
		case 'pause': // timeshift
			DUniversalTVX.Resume();
			this.PlayerState = 'playfile';
			break;
		case 'pausefile':
			DUniversalTVX.Resume();
			this.PlayerState = 'play';
			break;
		//case null:
		default:
		    if (this.Media.URL==""){ return;}
			var str =this.Media.URL.split('[IDC_SET_TV_DeviceCount]');			
			if (str.length==0)
			{ 
				str =this.Media.URL.split('[File]');			
				if (str.length==0){ return;}
			}
			
			

			var sURL ="";// str[1].split('[$$$]');
			this.dev_i =str[1]; //parseInt(sURL[0],10);
			this.source_i =1; // parseInt(sURL[1],10);
			
			var o = new Object();
					o.imgFile = 'connecting.gif';
					o.on = true;
			this.CB(o);
			
			//playSource(this.dev_i,1);
			
			this.PlayerState = 'play';
			
			JSTVStart();			
			this.SetMMK();
			
			
					
	}
}

///////////////////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////////////////
function TV_PlayTV(ob)
{
	var CTV = ob.CTV;
	var TV = ob.TV;
	
	if(ob.Media.isDigital) {
		TV.DTV(ob.dev_i);
	}
	else {
		TV.setCaptureDevice(ob.dev_i);
		if(!isNaN(ob.source_i))
			TV.setVideoSource(ob.source_i);
	}
	
	if(TV.currentChannel() != -1) {
		var o = new Object();
				o.s = 'Ch: ' + TV.currentChannel();
		CTV.ShowMessage(o);
	}
	
	CTV.PlayerState = 'play';
	
	var o = new Object();
			o.imgFile = '';
			o.on = false;
	CTV.CB(o);
	
	parent.iehip_fs[parent.hfID.PM].setPlayer(25);
}	
	

///////////////////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////////////////	
function TV_SetTVMMK()
{	


		
		var t_channel = document.getElementById('channel');		
		if (t_channel!=null)
		{
			t_channel.value="";
		}				


        if (this.PlayerState=='broadcast')		
        {

            var t_MMK_BROADCAST=document.getElementById("MMK_BROADCAST"); 	
			if (t_MMK_BROADCAST!=null)	{	t_MMK_BROADCAST.style.background="url('../../img/style001/UniversalHDTV-TopBar-MMK-Br.gif')"; 	}
        }
        else
        {

    //    var t_MMK_Stop=document.getElementById("MMK_Stop"); 
    //    var t_MMK_Play=document.getElementById("MMK_Play"); 
        var t_MMK_Pause=document.getElementById("MMK_Pause"); 
        var t_MMK_FB=document.getElementById("MMK_FB"); 
        var t_MMK_FF=document.getElementById("MMK_FF"); 
            var t_MMK_BROADCAST=document.getElementById("MMK_BROADCAST"); 	
     //   var t_MMK_REC=document.getElementById("MMK_REC"); 
	//	if (t_MMK_Stop!=null)	{	t_MMK_Stop.style.background="url('../../img/style001/UniversalHDTV-TopBar-MMK-null-1.gif')"; 	}
	//	if (t_MMK_Play!=null)	{	t_MMK_Play.style.background="url('../../img/style001/UniversalHDTV-TopBar-MMK-null-1.gif')"; 	}
	//	if (t_MMK_Pause!=null)	{	t_MMK_Pause.style.background="url('../../img/style001/UniversalHDTV-TopBar-MMK-null-1.gif')"; 	}
		if (t_MMK_FB!=null)	{	t_MMK_FB.style.visibility = "hidden"; //background="url('../../img/style001/UniversalHDTV-TopBar-MMK-null-1.gif')"; 	
							}
		if (t_MMK_FF!=null)	{	t_MMK_FF.style.visibility = "hidden"; 
		//background="url('../../img/style001/UniversalHDTV-TopBar-MMK-null-1.gif')"; 	
		}
	//	if (t_MMK_REC!=null)	{	t_MMK_REC.style.background="url('../../img/style001/UniversalHDTV-TopBar-MMK-null-1.gif')"; 	}

		if (g_id!="UniversalHDTV")
		{
			if (t_MMK_BROADCAST!=null)	{	
			//t_MMK_BROADCAST.style.visibility = "hidden"; 
			t_MMK_BROADCAST.style.background="url('../../img/style001/UniversalHDTV-TopBar-MMK-Broadcasting.gif')"; 	
			}	
		}
	    
			t_MMK_BROADCAST.style.background="url('../../img/style001/UniversalHDTV-TopBar-MMK-Broadcasting.gif')"; 
		}
		

}

///////////////////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////////////////
function TV_Switch()
{
	var TV = this.TV;	
     //alert("TV_Switch");	

    //JSTVStart_SUB(2);
    //this.CurrentVideoSouce=
    this.MaxVideoSouce=t_DeviceCount=document.all.DUniversalTVX.CaptureDeviceCount;
//	alert(t_DeviceCount); 
//	alert(this.MaxVideoSouce);
//	alert(this.CurrentVideoSouce);
			  	
    if (this.MaxVideoSouce!=null)
    {
	    JSUnload();	
	   this.CurrentVideoSouce=this.CurrentVideoSouce+1;
       if (this.MaxVideoSouce<this.CurrentVideoSouce)
       {
		   this.CurrentVideoSouce=1;
       }
		   JSTVStart_SUB(this.CurrentVideoSouce);      	
	}
    
    
    /*
	TV.ChannelDown();
	if(TV.currentChannel() != -1) {
		var o = new Object();
				o.s = 'Ch: ' + TV.currentChannel();
		this.ShowMessage(o);
	}
	*/
	
	
	
}
/*
function TV_Setup()
{
	gotoURL("UniversalHDTV/Setup.html");	
	// alert("TV_Setup");
}
*/
function TV_Broadcast()
{	
	var TV = this.TV;
	var t_FileName="";
	var t_port=7887;
	var t_PublishingURL="127.0.0.1";



	//	var t_return=TV.FunRecordOrASFStreamGraph("TS","c:\\1",1,1,7887,"192.168.1.105");

	var t_return=TV.FunRecordOrASFStreamGraph("TS",t_FileName,1,1,t_port,t_PublishingURL); 

//	var t_return=TV.FunRecordOrASFStreamGraph("TS","c:\\1.asf",1,1,7887,"192.168.1.105");

//	var t_FileName="c:\\VidCap";
//	var t_return=TV.FunRecordOrASFStreamGraph("ts",t_FileName,false,0,8080,"http://127.0.0.1"); 
//	FunTVMyWay_Broadcast
//	var t_meg='Broadcast TV/Webcam......mms://'+t_PublishingURL+':'+t_port;
//	alert(t_meg);
//	
 //   UNI_Message(t_meg);
	
//	var o = new Object();
//					o.s ='Broadcast TV/Webcam......mms://'+t_PublishingURL+':'+t_port;
//					o.suspend = true;


	if (t_return==1)
	{
		UNI_JS_CallParent(2,1);	//Set PHP DB is broading.
		this.ShowMessage("broadcasting..");
		this.PlayerState = 'broadcast';
	}else
	{
		UNI_JS_CallParent(2,0);	//Set PHP DB is broading.
		this.PlayerState = 'broadcast failed';	
	}
		this.SetMMK();
	
	
}

///////////////////////////////////////////////////
var iTimerID;
function TV_CH()
{
	var TV = this.TV;	
 
		var t_channel = document.getElementById('channel');
		if (t_channel!=null)
		{
			var t_channelNO=this.c_channel=t_channel.value;
																
			clearTimeout(iTimerID);
			iTimerID=setTimeout(function() 
			{
					if (TV!=null)
					{
						//TV.ChannelUp();						
				
							TV.SetChannel(t_channelNO,-1,-1);//(t_channel,-1,-1);
							var o_SET_TV_CurrentVideoDevice=document.all.DUniversalTVX.getIniData("UniversalTV.ini","UniversalHDTV","IDC_SET_TV_CurrentVideoDevice","NOVIDEODEVICE");																					
							TV.SetIniData("UniversalTV.ini",g_TVCurrentVideoDeviceName,"IDC_SET_TVVIDEO_CHANNELINPUT",t_channelNO+",-1,-1");	
						
						
					}	
			}, 100);
	
		}
		/*
		<div id="MMK_CH" onKeydown="UNI_CH()" style="Z-INDEX: 113"  >			
			<input type="text" name="channel" ID="channel" size="4">
			</div>	
			
		*/	
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function TV_Next()
{
	var TV = this.TV;	
	
		
	var t_ch2=TV.ChannelUp();
    var t_ch=TV.currentChannel();

	if(t_ch != -1) {
		UNI_Message('Ch: ' + t_ch);
		var t_channel = document.getElementById('channel');
		if (t_channel!=null)
		{
			t_channel.value=t_ch;
		}
	}	

    UNI_Message('Ch:'+t_ch);	
	var t_channel = document.getElementById('channel');
	if (t_channel!=null)
	{
		t_channel.value=t_ch;
	}    
}
///////////////////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////////////////
/*
function TV_SetProperty(i_Para1,i_Para2,i_Para3)
{	alert("WMP_SetProperty");
}
*/
///////////////////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////////////////
function TV_SetProperty(i_Para1,i_Para2,i_Para3)
{	



}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function TV_ShowMessage(o)
{
	//if(parent.iehip_fs.oUni != null) {
	var t_Msg;
	if (o.s!=null)
	{
	t_Msg=o.s;
	}else
	{
	t_Msg=o;
	}
	UNI_Message(t_Msg);
	UNI_JS_CallParent(1,t_Msg);

}

