/*
This file is part of iptv-stb-gui.

Copyright (C) 2010

iptv-stb-gui is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

iptv-stb-gui is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with iptv-stb-gui.  If not, see <http://www.gnu.org/licenses/>.

http://code.google.com/p/iptv-stb-gui/

*/

// Notes:
// Browser AntFresco 4.x : XMLHttpRequest not supported, JS 1.3, CSS 1.0, DOM level 0 
// Body height: 576px (HD resolutions also).
// Main overlay frame consist of two table rows: Upper 0-400px, Lower 400-576px.

// channelInfo array (PHP): 
//[0=index(number), 1=name, 2=number, 3=ip, 4=port, 5=group, 6=language 7=epg, 8=locked] 

var gaPlaylist = new Array();	// array of channelInfo arrays 
var gaPlaylistFiltered = new Array();	 // array of filtered channelInfo arrays
var gaGroups = new Array();		// array of unique groups array (0=index,1=group name) 
var gaCurrentEpg = new Array();	// epg data for all channels 

var gnCurrentIndex = 0; // index of a current playing channel
var gnCurrentGroup = -1;	// current group index (playing)
var gnCurrentItemIndex = 0;	// index of a selected item in a channel table
var gnCurrentItemGroup = gnCurrentGroup;	// selected group in channel table

var gnPreviousIndex = 0;  // index of a previous playing item
var gnPreviousGroup= -1;	// index of a previous group

// visibility of elements
var gbGroupBoxVisible = false;
var gbNumInputVisible = false;
var gbPinInputVisible = false;
var gbOsdBannerVisible = false;

// timers
var gnTmGroupBoxHide = 0;
var gnTmOsdBannerHide = 0;
var gnTmNumpad = 0;
var gnCheckForSubsTimer = 0;
var gnCheckForSubsInterval= 4000 ; // 4 seconds
var gnTmDelayedSetChannel = 0;

// input mode
var cnImNone = 0;
var cnImChannel = 1;
var cnImPin = 2;
var gnInputMode = cnImNone;


// Denis Pavliha - variables for audio&subtitles
var currAudio = 0;
var currSubtitles = 0;

var gsStreamSubtitleLanguageList = ''; 

var gbLoading = true;
var gbDelayedSetChannel = false;

gnEpgDownloadInterval = 5 * 60 * 1000; // Download epg on every 5 minutes.

function init()
{
	sagemSetDateTime(TIME_OFFSET);
	sagemSetLoadingPictTime();
	sagemSetDimming();
	sagemKillMedia();
	maxLetters();
	loadPlaylistHandler(la);
	downloadCurrentEpg();
	setInterval(downloadCurrentEpg,gnEpgDownloadInterval);
}

// LoadPlaylist handler
function loadPlaylistHandler(ar)
{
	gaPlaylist = ar;
	getUniqueGroups();
		
	// get current channel and group
	sTemp = sagemCurrentChannelGet();
	if (sTemp.indexOf(";") != -1){
		gnCurrentIndex = parseInt(sTemp.split(";")[0]);
		gnCurrentGroup = parseInt(sTemp.split(";")[1]);
	}
	if (gaGroups.length -1 < gnCurrentGroup)
		gnCurrentGroup=-1;
	filterByGroup(gnCurrentGroup);	
	if (gaPlaylistFiltered.length-1 < gnCurrentIndex)
		gnCurrentIndex=0;
	setChannel(gnCurrentIndex, gnCurrentGroup, true);
}

// Calculate maximum number of letters that would fit into channel selection table and osd banner, based on font size and table width. 
function maxLetters()
{
	var nBodyWidth = 720-(2*gnMarginLeft); //actual body width
	var nGBWidth = (gnGroupBoxWidth/100 * nBodyWidth) - gn_GrpBoxRow0Width; // right column width of the channel selection table
	gnGrpBoxMaxLetters = Math.round( nGBWidth / (gnGrpBoxFontFactor * gnGroupBoxFontSize /100) ); 

	var nOBDescWidth =  (gnOsdBannerWidth/100) * (nBodyWidth) ; // osd banner width
	gnOsdBanEpgMaxLetters = Math.round( nOBDescWidth / (gnOsdBanFontFactor * gnOsdBannerFontSize/100) ); 
}

// Create an array of unique groups.
function getUniqueGroups()
{
	nPlSize = gaPlaylist.length;
	for (var i=0; i<nPlSize; i++){
		var nSize = gaGroups.length;
		var bExists = false;

		for (var j=0; j<nSize; j++){  
			if (gaPlaylist[i][5]==gaGroups[j][1]) {
				bExists=true;
				break;
			}
		}
		if (!bExists)
		gaGroups.push([i,gaPlaylist[i][5]]);
	}
	gaGroups.sort(compareGroups); // sort by name (ASCII only)	
}

// Populate an array of channelInfo arrays filtered by group name.
function filterByGroup(nGroupIndex)
{
	gnCurrentItemGroup = nGroupIndex; // Update group index in the channel selection table.
	
	if (nGroupIndex == -1){
		// Don't filter, just point to gaPlaylist array.
		gaPlaylistFiltered = [] ; // Clear array
		gaPlaylistFiltered = gaPlaylist; // gaPlaylist holds all channels.
		return;
	}
	
	var sGroupName = '' ;
	var nSize = gaPlaylist.length;
	sGroupName = gaGroups[nGroupIndex][1];
	gaPlaylistFiltered = [];
	for (var i=0; i<nSize; i++){
		if (gaPlaylist[i][5]==sGroupName)
			gaPlaylistFiltered.push(new cloneObject(gaPlaylist[i]));
	}
}

// Display or hide channel selection table.
// If bRefresh is false, main frame is not rendered.
function displayGroupBox(bDisplay, bRefresh)
{
	if (bRefresh === undefined)
		bRefresh=true;

	if(bDisplay){
		stopTimer(gnTmGroupBoxHide); 
		gbGroupBoxVisible=true; 
		gnTmGroupBoxHide = setTimeout("displayGroupBox(false)",gnTmGroupBoxHideInterval); 
	}
	else{
		stopTimer(gnTmGroupBoxHide);
		gbGroupBoxVisible=false;
		gnCurrentItemIndex=gnCurrentIndex; // Revert to the currently playing index
		if(gnCurrentItemGroup != gnCurrentGroup) // Revert to the group from which the playing channel was selected.
			filterByGroup(gnCurrentGroup);
	}
	if (bRefresh)
		drawMainFrame();
}

// Display or hide osd banner.
// If bRefresh is false, main frame is not rendered.
function displayOsdBanner(bDisplay, bRefresh)
{
	if (bRefresh === undefined)
		bRefresh=true;

	if(bDisplay){
		stopTimer(gnTmOsdBannerHide);
		gbOsdBannerVisible=true;
		gnTmOsdBannerHide = setTimeout("displayOsdBanner(false)",gnTmOsdBannerHideInterval);
	}
	else{
		stopTimer(gnTmOsdBannerHide);
		gbOsdBannerVisible=false;
	}	
	if (bRefresh)
		drawMainFrame();
}

// Display or hide numeric input.
function displayNumpadInput(b)
{
	if(b)
		gbNumInputVisible=true;
	else
		gbNumInputVisible=false;

	drawMainFrame();
}

// Display or hide pin input.
function displayPinInput(b)
{
	if(b)
		gbPinInputVisible=true;
	else
		gbPinInputVisible=false;

	drawMainFrame();
}


// Clear OSD.
function clearMainFrame()
{
	if (gbGroupBoxVisible)
		displayGroupBox(false);
	if (gbOsdBannerVisible)
		displayOsdBanner(false);
	if (gbNumInputVisible)
		displayNumpadInput(false);
	if (gbPinInputVisible)
		displayPinInput(false);
}


// Refresh screen
function drawMainFrame()
{
	var sHtmlUp = ''
	var sTempFrame = window.top.mainFrame;
	var sHtmlDown = '';

    //Upper:
	sHtmlUp += gsMainFrameHeader;
	sHtmlUp += '<table width=\"100%\" cellspacing=\"0\" ><tr><td valign=\"top\" height=400px>';
	if (gbNumInputVisible)
		sHtmlUp += drawNumInput() 
	else if (gbPinInputVisible)
		sHtmlUp += drawPinInput()
	else if (gbGroupBoxVisible)
		sHtmlUp +=  drawChanSelTable();

	//Lower:
	sHtmlDown += '</td></tr><tr><td valign=\"top\" >';
	if (gbOsdBannerVisible){
		getCurrentEpg(); 
		sHtmlDown += drawOsdBanner() ;
	}
	sHtmlDown += '</td></tr></table>';
	sHtmlDown += gsMainFrameFooter;

	sTempFrame.document.open();
	sTempFrame.document.write(sHtmlUp+sHtmlDown);
	sTempFrame.document.close();
}

function getCurrentEpg()
{
	var sEpgId = gaPlaylistFiltered[gnCurrentItemIndex][7];
	var bFound=false;
	var i=0;
	var nSize = gaCurrentEpg.length;
	var nTimeNow;
	var nLength;
   
	// Reset previous values.
	gsCurrentShow='';
	gsCurrentShowDesc = '';
	gsCurrentProgress ='0';
	gsNextShow = '';
	gsNextShowDesc = '' ;
   
	if (sEpgId!=''){
		var dt = getRTC();
		for (; i<nSize; i++)
		{
			if (gaCurrentEpg[i][0]==sEpgId){
				nLength = gaCurrentEpg[i][5]*60000;
				nTimeNow = dt.getTime();
				if (nTimeNow < gaCurrentEpg[i][4] + nLength ){
					bFound=true;
					break;
				}
			}
		}
	}
 
	if (bFound){
		var dtStart = new Date();
		dtStart.setTime(gaCurrentEpg[i][4]);
		gsCurrentShow= formatTime(dtStart) + ' ' +  gaCurrentEpg[i][6] ;
		gsCurrentShowDesc = gaCurrentEpg[i][7] ;
		gsCurrentProgress=Math.max(0,Math.min(100,Math.round((nTimeNow-gaCurrentEpg[i][4])*100/(nLength))))+'' ;

		// Check if next show is listed.
		if (i+1 < nSize){
			if (gaCurrentEpg[i+1][0]==sEpgId){
				dtStart.setTime(gaCurrentEpg[i+1][4]);
				gsNextShow = formatTime(dtStart) + ' ' + gaCurrentEpg[i+1][6];
				gsNextShowDesc = gaCurrentEpg[i+1][7];
		   }
	   }
   }
}

function downloadCurrentEpgHandler(ar)
{
    gaCurrentEpg = [];
    gaCurrentEpg= ar;
}

// Get an array of a current and next epg data for every channel.
function downloadCurrentEpg()
{
	var sTempFrame = window.top.epgFrame;
	var sHtml = '<html><META http-equiv=\"PRAGMA\" content=\"NO-CACHE\"><META name=\"cache-control\" content=\"NO-CACHE\"></head>';
	//sHtml += '<script src=\"'+EPG_SERVER+'?t='+getRTC().getTime()+'\" type=\"text/javascript\"><\/script>';
	sHtml += '<script src=\"'+EPG_SERVER+'?t='+getRTC().getTime()+'\" type=\"text/javascript\"><\/script>';
	sHtml += '<script language=\"JavaScript\">';
	sHtml += 'function init(){if(typeof(la) != \"undefined\" && la!=null ){window.top.downloadCurrentEpgHandler(la);} if (window.top.gbLoading){window.top.gbLoading=false; window.top.drawMainFrame();}}';
	sHtml += '<\/script>';	
	sHtml += '<body onload=\"init();\" onKeypress=\"javascript:return window.top.keyAction(event);\" ></body></html>';
	sTempFrame.document.open();
	sTempFrame.document.write(sHtml);
	sTempFrame.document.close();
}

// Select next/prev channel from GroupBox.
function chSelChange(bDirection)
{
	if (gbGroupBoxVisible)
	{
		var nSize = gaPlaylistFiltered.length;
		if (bDirection)
		{
			gnCurrentItemIndex++;
			if (gnCurrentItemIndex >= nSize)
				gnCurrentItemIndex=0;
		}
		else
		{
			gnCurrentItemIndex--;
			if (gnCurrentItemIndex < 0)
				gnCurrentItemIndex=nSize-1;
		}
	}
		
	displayGroupBox(true,false);
	displayOsdBanner(true);
}

// Select next/prev group.
function groupChange(bDirection)
{
	if (gbGroupBoxVisible){
		var nSize = gaGroups.length;
		gnCurrentItemIndex=0;
		if (bDirection){
			gnCurrentItemGroup++;
			if (gnCurrentItemGroup >= nSize)
				gnCurrentItemGroup = -1;
		}
		else{
			gnCurrentItemGroup--;
			if (gnCurrentItemGroup < -1)
				gnCurrentItemGroup = nSize -1;
		}
		filterByGroup(gnCurrentItemGroup);
	}

	displayGroupBox(true,false);
	displayOsdBanner(true);
}

function chCanChange(nIndex)
{
	var IsAdultChannel = false;
	IsAdultChannel = (gaPlaylistFiltered[nIndex][8] == "true");

	return ((!IsAdultChannel)) || ((IsAdultChannel) && (gsPinpad == gnPIN))
}

// Set next/prev channel.
function chChange(bDirection)
{
	var nIndex = gnCurrentIndex;
	var nSize = gaPlaylistFiltered.length;

	while (true) {
		if (bDirection){
			nIndex++;
			if (nIndex >= nSize)
				nIndex=0;
		}
		else{
			nIndex--;
			if (nIndex < 0)
				nIndex=nSize-1;
		}

		if (chCanChange(nIndex))
			break;
	}

	gbDelayedSetChannel = true;
	setChannel(nIndex, gnCurrentGroup, true);
}

function InternalSetChannel(nIndex, bRefresh)
{
	sagemJoinMulticast(gaPlaylistFiltered[nIndex][3], gaPlaylistFiltered[nIndex][4]);
	gnCheckForSubsTimer = setTimeout("setSubtitles(0)", gnCheckForSubsInterval); // Auto select first available subtitle.
}

function DelayedSetChannel(nIndex, bRefresh)
{
	stopTimer(gnTmDelayedSetChannel);
	gbDelayedSetChannel = false;

	InternalSetChannel(nIndex, bRefresh);
}

function setChannel(nIndex, nGroup, bRefresh)
{
	if (chCanChange(nIndex)) {
		stopTimer(gnTmDelayedSetChannel);
		
		gnPreviousIndex = gnCurrentIndex; // Update previous index.
		gnPreviousGroup = gnCurrentGroup; //Update previous group.
		gnCurrentIndex = nIndex; // Set current index.
		gnCurrentItemIndex = nIndex; //Set index in the channel table.
		gnCurrentGroup = nGroup; // Set current playing group.
		gnCurrentItemGroup = nGroup; // Set group in channel table.
		gsStreamSubtitleLanguageList = ''; //Clear Subtitle List

		if (gbPinInputVisible) {
			gnInputMode = cnImNone;
			gbPinInputVisible = false;
		}

		sagemSetDisplay(gaPlaylistFiltered[nIndex][2]); // change display to current channel
		sagemCurrentChannelSet(nIndex, nGroup);
		displayOsdBanner(true, bRefresh);	//Show epg for selected channel.

		if (gbDelayedSetChannel) {
			gnTmDelayedSetChannel = setTimeout("DelayedSetChannel(" + nIndex + "," + bRefresh + ")", gnTmDelayedSetChannelInterval);
		}
		else {
			InternalSetChannel(nIndex, bRefresh);
		}
	}
}

function setSubtitles(index)
{
	gsStreamSubtitleLanguageList = sagemGetSubtitleList(); // Update available subtitles
	if (gsStreamSubtitleLanguageList.length>index){
			sagemSetSubtitle(index);
	}
}

// Switch to previous channel
function previousChannel()
{
    if (gnCurrentGroup != gnPreviousGroup)
		filterByGroup(gnPreviousGroup); // refilter
	setChannel(gnPreviousIndex, gnPreviousGroup, true);
}

// Key input handler.
//V tej funkciji bi bilo potrebno popraviti tudi situacijo ko je gsNumpad.length enaka 3, 
//takrat bi se moral ustaviti samo timer in nato takoj klicati chSetNum();
function keyInput(n)
{   
	if (gnInputMode == cnImNone)
		gnInputMode = cnImChannel;

	if (gnInputMode == cnImChannel) {
			 stopTimer(gnTmNumpad);

		if (gsNumpad.length<3)
					gsNumpad+=n.toString(); // nadaljuj z sestavljanjem številke
			else
					gsNumpad = n.toString(); // začni z sestavljanjem številke

					displayNumpadInput(true);

			if (gsNumpad.length<3)  // Čakaj na morebitno naslednjo številko
					gnTmNumpad=setTimeout("chSetNum()", gnTmNumpadInterval);
			else
					chSetNum(); // Vtipkane 3 številke - takoj spremeni kanal
	}
	else {
		// reset
		if (gsPinpad.length == 3)
			gsPinpad = '';
		if (gsPinpad.length < 3)
			gsPinpad+=n.toString();
		displayPinInput(true);

		if (gsPinpad.length == 3) {
			gnInputMode = cnImNone;
			displayPinInput(false);
		}
	}
}


function chSetNum()
{
	var nChNum = Number(gsNumpad);
	gsNumpad='';
	
    displayNumpadInput(false);
	filterByGroup(-1);
	
    // find channel    
	var nSize = gaPlaylistFiltered.length;
	for (var i=0; i<nSize; i++)
	{
		if (gaPlaylistFiltered[i][2]==nChNum){
			setChannel(i, -1, true);
			break;
		}
	}
	stopTimer(gnTmNumpad);	// stop timer
}

function stopTimer(id)
{
	if (id!=0)
		clearTimeout(id);
}

// Remote controll =================================
function keyAction(e)
{  
	k=typeof(e.which)=='undefined'?e.keyCode:e.which; 
	switch (k)
	{
	    case KEY_0:
		case 48:
		    keyInput(0);
		    return 0;
		case KEY_1:
		case 49:
			keyInput(1);
			return 0;
		case KEY_2:
		case 50:
		    keyInput(2);
			return 0;
		case KEY_3:
		case 51:
			keyInput(3);
			return 0;
		case KEY_4:
		case 52:
			keyInput(4);
			return 0;
		case KEY_5:
		case 53:
			keyInput(5);
			return 0;
		case KEY_6:
		case 54:
			keyInput(6);
			return 0;
		case KEY_7:
		case 55:
			keyInput(7);
			return 0;
		case KEY_8:
		case 56:
			keyInput(8);
			return 0;
		case KEY_9:
		case 57:
			keyInput(9);
			return 0;			
		case KEY_MENU:
			return(0);
		case KEY_UP:
		case 119:
			chSelChange(false);	// Open group box / select previous chan
			return(0);
		case KEY_DOWN:
		case 115:
			chSelChange(true);	// Open group box / select next chan.
			return(0);
		case KEY_LEFT:
		case 97:
			groupChange(false);	// Open group box / select previous group.
			return(0);
		case KEY_RIGHT:
		case 100:
			groupChange(true);	// Open group box / select next group.
			return(0);
		case KEY_REW:
			return(0);
		case KEY_FWD:
			return(0);
		case KEY_CHUP:
			chChange(true);	// Set next channel.
			return(0);
		case KEY_CHDOWN:
			chChange(false);	// Set previous channel.
			return(0);
		case KEY_RED:
			clearMainFrame();	// Clear osd.
			return(0);
		case KEY_GREEN:
			if (gbOsdBannerVisible)	// Toggle osd banner
				displayOsdBanner(false);
			else
				displayOsdBanner(true);		
			return(0);
		case KEY_VOLUP:
		case KEY_VOLDOWN:
			return(1);
		case KEY_MUTE:
			return(1);
		case KEY_OK:
		case 113: //Q
			if (gbGroupBoxVisible) {	// Set selected channel.
				setChannel(gnCurrentItemIndex,gnCurrentItemGroup,false);	
				displayGroupBox(false,true);	// Close channel selection table.
			}
			else	 
				displayGroupBox(true);	// Show only group box - no osd banner.
				return(0);
		case KEY_TV:
			if (!gbPinInputVisible) {
				gsPinpad = 'XXX';
				gnInputMode = cnImPin;
				displayPinInput(true);
			} else {
				gnInputMode = cnImNone;
				displayPinInput(false);
			}
			return(0);
		case KEY_IDENT:
			return(0);
		case KEY_RETOUR:
		case 114: //R
			previousChannel();
			return(0);
		case KEY_YELLOW:
			SwitchAudio();
			return(0);			
		case 116: //T  TODO: Display available subtitles
			setSubtitles(0);
			return(0);
		case KEY_BLUE:
			//sagemSetSubtitle(-1); //Disable subtitles
			SwitchSubtitles();
			return (0);
		case KEY_POWER:
			sagemPowerOff();
			return(0);
		case KEY_QUIT:
			location.reload();				
			return(0);
		default:
			return(1);
    }
}

function getRTC() {
	return new Date();
}

function formatTime(dt)
{
	var lsMin = dt.getMinutes().toString();
	if (lsMin.length == 1)
		lsMin = '0' + lsMin;
	return dt.getHours().toString() + ':' + lsMin;
}

//==================================================
// Helper functions ================================

function compareGroups(a,b)
{
	if (a[1]<b[1])
		return -1;
	if (a[1]>b[1])
		return  1;
	if (a[1]==b[1])
		return 0;
}

function cloneObject(source)
 {
	for (i in source){
		if (typeof source[i] == 'source')
			this[i] = new cloneObject(source[i]);
		else
			this[i] = source[i];
	}
}

// TODO: Try this function for string concentration
function StringBuffer() 
{ 
	this.buffer = []; 
} 

StringBuffer.prototype.append = function append(string)
{ 
	this.buffer.push(string); 
	return this; 
}; 

StringBuffer.prototype.toString = function toString()
{ 
	return this.buffer.join(""); 
}; 

String.prototype.trunc = function(n)
 {
	return this.substr(0,n-1)+(this.length>n?'...':'');
 };

// -------------------------------------------------------------
// Denis Pavliha - AUDIO & SUBTITLE CHANGER [BEGIN]

function SwitchAudio()
{
	var sHtmlUp = ''
	var sTempFrame = window.top.mainFrame;
	var sHtml = '';
	var sLang = ChangeAudio();

	//to-do: assign and setup own timer for hiding the audio-switching OSD
	
	sHtml += '<html><head><title></title></head><body onKeypress=\"javascript:return window.top.keyAction(event);\" >';
	sHtml += '<table width=\"100%\" height=100px border = \"0\" cellspacing=\"0\"><tr>';
	sHtml += '<td width=\"80%\"></td>';
	sHtml += '<td valign=\"middle\" align=\"center\" width=\"20%\" bgcolor=\"' + gsGropBoxBackgroundColor + '\">';
	sHtml += '<u>Audio:</u></td></tr>';
	sHtml += '<td width=\"80%\"></td>';
	sHtml += '<td valign=\"middle\" align=\"center\" width=\"20%\" bgcolor=\"' + gsGropBoxBackgroundColor + '\">';
	sHtml += sLang + '</td></tr>';
	sHtml += '</table></body></html>';

	sTempFrame.document.open();
	sTempFrame.document.write(sHtml);
	sTempFrame.document.close();
}

function SwitchSubtitles()
{

	var sHtmlUp = ''
	var sTempFrame = window.top.mainFrame;
	var sHtml = '';
	var sLang = ChangeSubtitles();
	
	//to-do: assign and setup own timer for hiding the subtitle-switching OSD

	sHtml += '<html><head><title></title></head><body onKeypress=\"javascript:return window.top.keyAction(event);\" >';
	sHtml += '<table width=\"100%\" height=100px border = \"0\" cellspacing=\"0\"><tr>';
	sHtml += '<td width=\"80%\"></td>';
	sHtml += '<td valign=\"middle\" align=\"center\" width=\"20%\" bgcolor=\"' + gsGropBoxBackgroundColor + '\">';
	sHtml += '<u>Podnapisi:</u></td></tr>';
	sHtml += '<td width=\"80%\"></td>';
	sHtml += '<td valign=\"middle\" align=\"center\" width=\"20%\" bgcolor=\"' + gsGropBoxBackgroundColor + '\">';
	sHtml += sLang + '</td></tr>';
	sHtml += '</table></body></html>';

	sTempFrame.document.open();
	sTempFrame.document.write(sHtml);
	sTempFrame.document.close();
}

function ChangeAudio()
{
	var audLanguages = new Array();	
	var audFiltered = new Array();
	var sAudList = "";
	
	// Retrieve available audio languages
	audLanguages = VideoStreamGetAllAudioLanguagesArray();
	
	// Use only the retrieved tracks that have descriptors, transcribe into new array
	var j = 0;
	for (i = 0; i < audLanguages.length; i++)
	{
		if (audLanguages[i] != null) audFiltered[j++] = audLanguages[i];
	}

	// Change audio
	if (++currAudio > audFiltered.length - 1) currAudio = 0;		
	VideoStreamSetCurrentAudioLanguage(audFiltered[currAudio]);	
	
	// Prepare visualization
	if (audFiltered.length < 1)
	{
		sAudList += "<i>Audio ni na voljo!</i>";
	}
	else
	{
		for (i = 0; i < audFiltered.length; i++)
		{
			if (i == currAudio) sAudList += "<strong>";
			sAudList += ExplainLanguage(audFiltered[i]) + "<br />";
			if (i == currAudio) sAudList += "</strong>";
		}
	}
	
	return sAudList;
}

function ChangeSubtitles()
{
	var subLanguages = new Array();	
	var sSubList = "";
	var indexSearch = 0;
	var indexWrite = 0;
	var cCurrent;
	
	// Retrieve available subtitles
	sSubList = sagemGetSubtitleList(); 
	subLanguages[0] = "";
	
	// Parse and transform into array
	while (indexSearch < sSubList.length)
	{
		
		cCurrent = sSubList.charAt(indexSearch);
		
		if (cCurrent == ' ')
		{			
			subLanguages[++indexWrite] = "";
		}
		else
		{
			subLanguages[indexWrite] += cCurrent;
		}		
		indexSearch++;
	}
	
	// Change subtitles
	if (++currSubtitles > subLanguages.length - 1) 
	{
		currSubtitles = -1;			
		sagemSetSubtitle(currSubtitles);	
	}
	else
	{
		sagemSetSubtitle(subLanguages[currSubtitles]);	
	}
	
	// Prepare visualization
	sSubList = "";
	if (subLanguages.length < 1)
	{
		sSubList += "<i>Ni podnapisov!</i>";
	}
	else
	{
		for (i = -1; i < subLanguages.length; i++)
		{
			if (i == currSubtitles) sSubList += "<strong>";
			if (i == -1)
			{
				sSubList += "<i>(off)</i> <br />";
			}
			else
			{
				sSubList += ExplainLanguage(subLanguages[i]) + "<br />";
			}
			if (i == currSubtitles) sSubList += "</strong>";
		}
	}
	
	return sSubList;
}

function ExplainLanguage(strLabel)
{
	switch (strLabel)
	{
		case "eng":	
			return "English";
			break;
		case "slv":	
			return "Slovenian";
			break;
		case "hrv":	
			return "Croatian";
			break;
		case "ger":	
			return "German";
			break;
		case "pol":	
			return "Polish";
			break;
		case "tur":	
			return "Turkish";
			break;
		case "rus":	
			return "Russian";
			break;
		case "hun":	
			return "Hungarian";
			break;
		case "cze":	
			return "Czech";
			break;
		case "fre":	
			return "French";
			break;
		case "ita":	
			return "Italian";
			break;
		case "spa":	
			return "Spanish";
			break;
		case "heb":	
			return "Hebrew";
			break;
		case "rum":
			return "Romanian";
			break;
		case "gre":
			return "Greek";
			break;
		case "swe":
			return "Swedish";
			break;	
		case "nor":
			return "Norwegian";
			break;	
		case "dan":
			return "Danish";
			break;	
		case "dut":
			return "Dutch";
			break;	
		case "fin":
			return "Finnish";
			break;	
		case "ara":
			return "Arabic";
			break;	
		case "por":
			return "Portuguese";
			break;	
		case "alb":
			return "Albanian";
			break;	
		case "srp":
			return "Serbian";
			break;	
		case "scr":
			return "Serbo-Croatian";
			break;	
		case "NAT":	// myZen.tv
			return "<i>(nature)</i>"; 
			break;
		case "MUS": // myZen.tv	
			return "<i>(music)</i>";
			break;
		default:
			return strLabel;
	}
}

function sagemGetSubtitleList()
{
	if (typeof(sagem) != 'undefined'){
		return SAGEM_JS_Get("SUBTITLE_LIST");	
	}
	else
		return '';
}

function sagemSetSubtitle(index)
{
	if (typeof(sagem) != 'undefined'){
		if (index==-1){
			SAGEM_JS_Set("SUBTITLE_ACTIVATION", "off");
		}
		else
		{
			SAGEM_JS_Set("SUBTITLE_LANGUAGE", "slv");
			SAGEM_JS_Set("SUBTITLE_ACTIVATION", "on");			
		}
	}
}

function VideoStreamGetAllAudioLanguagesArray()
{
  var returnedValue = SAGEM_GetAudioList() ;

  if (returnedValue.indexOf("session") == -1)
  {
    var reReg = new RegExp("[A-Z]{3}","ig");
    //var audioList = "00" ;
	if (returnedValue.length > 0)
    {
	  var audioList = new Array(returnedValue.length);
	  
      var tab = returnedValue.match(reReg);
      if (tab != null && tab.length > 0)
      {
        /*if (tab.length < 10)
          audioList = "0" + tab.length ;		  
        else
          audioList = "" + tab.length ;	*/	  
        for ( var i=0 ; i<tab.length ; i++)
        {
          //audioList = audioList + "," + tab[i] ;
		  audioList[i] = tab[i];
        }
        return audioList;
      }
      else
        return -1;
    }
    else
      return -1;
  }
  else
    return -1;
}

function VideoStreamGetCurrentAudioLanguage()
{
  var lsResult = SAGEM_JS_Get("Audio_Lang");
  if (lsResult == "" || lsResult == "-2")
  {
    var lsResultDefault = SAGEM_JS_Get("AUDIO_LANG_DEFAULT");
    var aSplit = String(lsResultDefault).split(',');
    var liResult = aSplit[0] *1;
    if (liResult == 0)
      return aSplit[1];
    else
      return -1;
  }
  else
    return lsResult;
}

function VideoStreamSetCurrentAudioLanguage(language)
{
  if (language != '' && language != null)
  {
    var lLanguage = language.toLowerCase();
    var lsResult = SAGEM_JS_Set("Audio_Lang", lLanguage);
    if (lsResult.indexOf(lLanguage) != -1 || lsResult == 0)
      return 0;
    else
      return -1;
  }
  else
    return -1;
}

// Denis Pavliha - AUDIO & SUBTITLE CHANGER [END]
// -------------------------------------------------------------

// keys 
KEY_0=536870922;
KEY_1=536870923;
KEY_2=536870924;
KEY_3=536870925;
KEY_4=536870926;
KEY_5=536870927;
KEY_6=536870928;
KEY_7=536870929;
KEY_8=536870930;
KEY_9=536870931;

KEY_CHUP=0x40000013;
KEY_CHDOWN=0x40000012;
KEY_OK=0x40000004;
KEY_RED=0x40000084;
KEY_GREEN=0x40000085;
KEY_YELLOW=0x40000086;
KEY_BLUE=0x40000087;
KEY_UP=0x4000000E;
KEY_DOWN=0x4000000F;
KEY_LEFT=0x40000010;
KEY_RIGHT=0x40000011;
KEY_MENU=0x4000000A;
KEY_REFRESH=0x4000008A;
KEY_RETOUR=0x4000008A;
KEY_PLAY=0x40000100;
KEY_IDENT=0x40000083;
KEY_VOLUP=0x30000001;
KEY_VOLDOWN=0x30000002;
KEY_REW=0x40000105;
KEY_FWD=0x40000104;
KEY_MUTE=0x30000003;
KEY_POWER=0x30000000;
KEY_QUIT=0x40000082;
KEY_TV=0x40000097;


