//////////////////////////////////////////////////////////////
//
//	Copyright (c) 2001-2007 Syabas Technology Inc.  All rights reserved.
//	Free for public non-commercial use and modification
//	as long as this header is kept intact and unmodified.
//	Please see http://www.sayatv.com for more information
//
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
//////////	 SayaTV Viewer 	 //////////////////////
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
var userAgent = navigator.userAgent;
var autoChannelAddr;
var autoChannelName;
var started = 0;
var autoTimeID = 0;
var initialized = 0;
var global_locale, global_id;
var SayaTVViewerVersion = '0,8,0,2';

function initScreen_callback(locale,id)
{
 	if (detectPlugin() && userAgent.indexOf('IE') > -1)
 	{
		try
		{
			document.getElementById('SayaTVViewer').SetChannelId(id);
			document.getElementById('SayaTVViewer').SetLanguage(locale); 
		}
		catch (e)
		{
			alert("You haven't installed SayaTV Viewer.");
		}
	}
}


function initScreen(locale,id)
{
	global_locale = locale;
	global_id = id;
	setTimeout("initScreen_callback(global_locale, global_id);", 200);
}



		
function CreateSayaTVViewerControlEmbeded(width,height, url)
{
	try
    {
		document.write ('<OBJECT ID="SayaTVViewer" width="'+width+'" height="'+height+'" name="SayaTVViewer" CLASSID=clsid:62BA437C-7712-48C6-9F0B-D251FA43192B CODEBASE="'+url+'#Version='+SayaTVViewerVersion+'"></OBJECT>'); 
    }
    catch (e)
    {	
    	alert("You haven't installed SayaTV Viewer, \nPlease use Internet Explorer to view this site.");
	}  
}

function CreateSayaTVViewerControl_callback(DivID,width,height, url)
{
	var d = document.getElementById(DivID);	
	try
    {
		//var version = '0,8,0,2';
		d.innerHTML = '<OBJECT ID="SayaTVViewer" width="'+width+'" height="'+height+'" name="SayaTVViewer" CLASSID=clsid:62BA437C-7712-48C6-9F0B-D251FA43192B CODEBASE="'+url+'#Version='+SayaTVViewerVersion+'"></OBJECT>'; 
    }
    catch (e)
    {	
    	alert("You haven't installed SayaTV Viewer, \nPlease use Internet Explorer to view this site.");
	}  
}

var global_DivID, global_width, global_height, global_url;

function CreateSayaTVViewerControl(DivID,width,height, url)
{
	global_DivID = DivID;
	global_width = width;
	global_height = height;
	global_url = url;
	setTimeout("CreateSayaTVViewerControl_callback(global_DivID, global_width, global_height, global_url);",100);
}

function InitPlayer()
{	
	if (detectPlugin() && userAgent.indexOf('IE') > -1) 
	{	
		initialized = 1;	
		document.getElementById('SayaTVViewer').InitPlayer();
	}	
}
		
function OnPlay(channelUrl, channelName)
{
	if (detectPlugin() && userAgent.indexOf('IE') > -1) 
	{
		if(document.getElementById('SayaTVViewer').GetState(1) == 0) 
		{
			alert("SayaTV Viewer hasn't been initialized!");
			return;
		}	
		document.getElementById('SayaTVViewer').SetChannelName(channelName);	
		document.getElementById('SayaTVViewer').SetdvbiAddress(channelUrl);
		document.getElementById('SayaTVViewer').Play();
	}
	else
	{
		alert("Please install the SayaTV Viewer!");
	}
}

function StopPlayer()
{
	if (detectPlugin() && userAgent.indexOf('IE') > -1) 
		document.getElementById('SayaTVViewer').Stop();
}	

function AutoPlay_callback(channelAddress, channelName)
{
	if (detectPlugin() && userAgent.indexOf('IE') > -1) 
	{
		started = 0;
		autoTimeID = setInterval("StartPlayer();", 1000);
	}
}

function AutoPlay(channelAddress, channelName)
{
	autoChannelAddr = channelAddress;
	autoChannelName = channelName;
	setTimeout("AutoPlay_callback(autoChannelAddr, autoChannelName);",400);
} 	

var global_channelAddress, global_channelName;

function InitChannel_callback(channelAddress, channelName)
{   
   if (detectPlugin() && userAgent.indexOf('IE') > -1) {
		if(initialized == 0)
			InitPlayer();	
		document.getElementById('SayaTVViewer').SetChannelName(channelName);	
		document.getElementById('SayaTVViewer').SetdvbiAddress(channelAddress);
	}
}

function InitChannel(channelAddress, channelName)
{
	global_channelAddress = channelAddress;
	global_channelName = channelName;
	setTimeout("InitChannel_callback(global_channelAddress, global_channelName);",300);
}

function StartPlayer()
{
	if(initialized == 0)
		InitPlayer();

	if(started == 0)
	{
		if(document.getElementById('SayaTVViewer').GetState(1) == 1)
		{
			started = 1;
			OnPlay(autoChannelAddr, autoChannelName);
		}		
	}
	else
	{
		if(document.getElementById('SayaTVViewer').GetState(13) == -109)	// No buffer (Cannot retrieve data)
		{
			started = 0;
		}		
	}
}

var noinstallerr=0;
function detectPlugin()
{
    var pVersion;
    try
    {
        pVersion = document.getElementById('SayaTVViewer').GetVersion();  
		return true;
    }
    catch (e)
    {
		if (noinstallerr==0) 
		{
		  //	alert("You haven't installed SayaTV Viewer, \nPlease use Internet Explorer to view this site.");
		}
		noinstallerr=1;
        return false;
    }
}

function refresh_player()
{
	if(detectPlugin() && userAgent.indexOf('IE') > -1)
	{
		if(initialized == 0)
		{
			InitPlayer();
			AutoPlay('dvbi://broker1.sopcast.com:3912/16499', 'Supreme Master TV'); 
		}	
		document.getElementById('SayaTVViewer').Stop();
		document.getElementById('SayaTVViewer').Play();		
	}
}
	
