function getVLC( name )
{
    if (window.document[name]) 
    {
        return window.document[name];
    }
    if (navigator.appName.indexOf("Microsoft Internet")==-1)
    {
        if (document.embeds && document.embeds[name])
            return document.embeds[name];
    }
    else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
    {
        return document.getElementById(name);
    }
}
var vlc = getVLC("ply-vlc");


var vlc_play_time;
function vlc_get_stream( stream_id )
{
	try
	{
		change_old_volume = vlc.audio.volume;
		change_old_mute = vlc.audio.mute;
	}
	catch(err){};
	
	vlc_stop();
	vlc_preview();
	if ( vlc_play_time !== undefined ) clearTimeout( vlc_play_time );
	$.post(
		'sources/acts.php',
		{
			act: 'vlc_get_stream',
			stream_id: stream_id
		} ,
		function ( data )
		{
			if ( data.success )
			{
				vlc_player();
				vlc_play_time = setTimeout ("vlc_play( '"+data.server_url+"' )", 500);

				$('#dbg_str_name').html( data.server_url );
				
				// change epg
				$('#epg_list').html( data.epg_list );
				// change current ems
				$('#program_curent').html( data.program_selectat );
				if ( data.emisiune_curenta )
				{
					$('#emisiune_curenta').html( data.emisiune_curenta_trunc );
					$('#emisiune_curenta').attr( 'title', data.emisiune_curenta );
				}
				else
				{
					$('#emisiune_curenta').html( '...' );
				}
				// change fav if needed
				$('a.list-channel').removeClass('nowplaying');
				$('a.list-channel').find('em').hide();
				if ( !$('#favorites_list').hasClass('vocalnet') )
				{
					if ( $('#fav_' + data.chn_id ).find('a.list-channel').hasClass('list-channel') ) 
					{
						$('#fav_' + data.chn_id ).find('a.list-channel').addClass('nowplaying');
						$('#fav_' + data.chn_id ).find('a.list-channel').find('em').show();
					}
				}
				else
				{
					if ( $('#fav_' + data.stream_id ).find('a.list-channel').hasClass('list-channel') ) 
					{
						$('#fav_' + data.stream_id ).find('a.list-channel').addClass('nowplaying');
						$('#fav_' + data.stream_id ).find('a.list-channel').find('em').show();
					}
				}
				
				$('.fullscreen').show();
				$('.popup').show();
				$('.small-play').hide();
				$('.small-stop').show();
				
				// set keepalive timeout
				player_ready();
				if ( playInt2 !== undefined ) clearTimeout(playInt2);
				playInt2 = setTimeout( 'vlc_keep_alive()', keep_alive_time );
				
				$.post(
					'sources/acts.php',
					{
						act: 'start_history',
						player: 'VLC'
					} ,
					function ( data )
					{
						
					},
					'json'
				);
			}
			else
			{
				// afiseaza o eroare ..
				vlc_stop();
				vlc_preview();
				$('#alerta-eroare').html( data.message );
				$('#alerta-eroare').slideDown( 1000 );
			}
		},
		'json'
	);
}

function vlc_preview()
{
	$('#vlc-player').hide();
	$('#vlc-preview').show();
}
function vlc_player()
{
    $('#vlc-preview').hide();
    $('#vlc-player').show();
}

var options;
var itemId;

function vlc_play( url )
{
	if ( (typeof(window['vlc_cache_buffer']) == "undefined") )
	{
		vlc_cache_buffer = 1000;
	}

	vers = '0.9.8a';
	if ( vlc.VersionInfo )
	{
		vers = vlc.VersionInfo.substring(0, 6);
		//alert( vers );
	}
	// check if cookie exists
	if ( vers != '0.9.8a' && !readCookie ( 'vlc_version' ) )
	{
		$('#alerta-eroare-vlc').slideDown( 1000 );
		createCookie( 'vlc_version', '1', 30 );
	}
	vlc_stop();
	vlc_player();
	if ( vlc.playlist !== undefined ) try { vlc.playlist.items.clear() } catch(err) {};
	//options = new Array( ( ":http-caching=" + vlc_cache_buffer ) );
	
	var options = new Array()
	options.push(":vout-filter=deinterlace");
	options.push(":deinterlace-mode=linear");
	options.push(":http-caching=" + vlc_cache_buffer);
	options.push(":audio-track=<n>");
	options.push(":dvb-sub=auto");
	options.push(":audio-language=EN");
	options.push(":sub-language=RO");
	options.push(":dvb-sub-language=RO");
	options.push(":sub-type=auto");
		

	//setTimeout('vlc.video.subtitle = 1;', 1000);
	
	try { 
		itemId = vlc.playlist.add(url, null, options);
		vlc.playlist.playItem(itemId);
		
		if ( change_old_volume !== undefined && change_old_mute !== undefined ) 
		{
			vlc.audio.volume = change_old_volume;
			if ( change_old_mute ) 
			{
				vlc.audio.mute = change_old_mute;
			}
		}
	}
	catch(err)
	{
		vlc_play( url );
	}
	//vlc.video.subtitle = 1;
}

function vlc_play_audio( url, name )
{
	if ( (typeof(window['vlc_cache_buffer']) == "undefined") )
	{
		vlc_cache_buffer = 1000;
	}

	vers = '0.9.8a';
	if ( vlc.VersionInfo )
	{
		vers = vlc.VersionInfo.substring(0, 6);
		//alert( vers );
	}
	// check if cookie exists
	if ( vers != '0.9.8a' && !readCookie ( 'vlc_version' ) )
	{
		$('#alerta-eroare-vlc').slideDown( 1000 );
		createCookie( 'vlc_version', '1', 30 );
	}
	vlc_stop();
	vlc_player();
	if ( vlc.playlist !== undefined ) try { vlc.playlist.items.clear() } catch(err) {};
	//options = new Array( ( ":http-caching=" + vlc_cache_buffer ) );
	
	var options = new Array()
	options.push(":http-caching=" + vlc_cache_buffer);
	options.push(":audio-track=<n>");
	options.push(":audio-language=EN");
	
	$.post(
		'sources/acts.php',
		{
			act: 'play_audio',
			url: url,
			timeout: 2000
		},
		function( data )
		{
			
		},
		'json'
	);
	
	setTimeout( "vlc_play_audio_final('" + url + "','"+ name+"','" + options+"')", 1000 );
}

function vlc_play_audio_final(url, name, options)
{
	try { 
		itemId = vlc.playlist.add(url, name, options);
		vlc.playlist.playItem(itemId);
		
		if ( change_old_volume !== undefined && change_old_mute !== undefined ) 
		{
			vlc.audio.volume = change_old_volume;
			if ( change_old_mute ) 
			{
				vlc.audio.mute = change_old_mute;
			}
		}
		clearTimeout( playInt2 );
		
		$('a.list-channel').removeClass('nowplaying');
		$('a.list-channel').find('em').hide();
		$('#program_curent').html( name );
		$('#emisiune_curenta').html( '' );
		$('.fullscreen').hide();
		$('.popup').hide();
		
	}
	catch(err)
	{
//		vlc_play_audio( url, name );
	}
}

function vlc_stop()
{
	if ( vlc.playlist !== undefined ) try { vlc.playlist.stop() } catch(err) {};
	return false;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function vlc_play_last()
{
	$.post(
		'sources/acts.php',
		{
			act : 'last_stream_id'
		},
		function ( data )
		{
			if( data.success )
			{
				if( data.video )
				{
					vlc_get_stream( data.stream_id );
				}
				else
				{
					if( data.audio ) 
					{
						try
						{
							vlc_play_audio( data.url, data.nume );
						}
						catch(err){}
					}
				}
				$('.small-play').hide();
				$('.small-stop').show(); 
			}
		},
		'json'
	);
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}