﻿var lastplayerdivname = '';
var lastPlaylistName = '';
var lastPlayerWidth = 0;
var lastPlayerHeight = 0;
var LastIsPlaylist = false;
var playingtemas = false;
var DebeMostrarSugeridos = false;
var esSugerido = false;

function MostrarSugeridos(index) {
    //Si hay un div de Sugeridos
    if ($('div.Sugeridos').length > 0) {
        //Si hay que mostrarlo se lo hace visible si hay una lista para mostrar
        if (DebeMostrarSugeridos) {
            $('.Sugeridos').addClass('hidden');
            $('.PaginaSugeridos').addClass('hidden');
            $('.PaginaSugeridosVideos').addClass('hidden');
            if (playingtemas) {
                if ($('#SugeridosTemasPL_' + index)[0].innerHTML != "") {
                    $('.Sugeridos').removeClass('hidden');
                    $('#SugeridosTemas_' + index).removeClass('hidden');
                }
            }
            else {
                if ($('#SugeridosVideosPL_' + index)[0].innerHTML != "") {
                    $('.Sugeridos').removeClass('hidden');
                    $('#SugeridosVideos_' + index).removeClass('hidden');
                }
            }
        }
//        sino se lo oculta
        else {
            if (!esSugerido)
                $('.Sugeridos').addClass('hidden');
        }
    }
}

function playDestacados(filename, imagename, divname, playWidth, playHeight, index, doTemas,pldiv,mSugeridos, esSug)
{	
    var listaTemas = null;
    playingtemas = doTemas;
    DebeMostrarSugeridos = mSugeridos;
    esSugerido = esSug;
    listaTemas = eval($(pldiv).html());
   
    if(lastplayerdivname != divname && lastplayerdivname != '' && lastPlayerWidth == playWidth && lastPlayerHeight == playHeight)
    {
        if (jwplayer(lastplayerdivname).getState() != undefined) {
            jwplayer(lastplayerdivname).stop();           
        }   
    }

    if (jwplayer(divname).getState() != undefined && LastIsPlaylist && lastPlaylistName == pldiv) {
		if(jwplayer(divname).getPlaylist() && jwplayer(divname).getPlaylistItem(0)['file'] == listaTemas[0]['file'])
		{
			jwplayer(divname).playlistItem(index);
		}
		else
		{
			jwplayer(divname).load(listaTemas);
			jwplayer(divname).playlistItem(index);
		}
    } else {
    if (lastplayerdivname != divname && lastplayerdivname != '') {
        if (jwplayer(lastplayerdivname).getState() != undefined) {
            jwplayer(lastplayerdivname).stop();
            jwplayer(lastplayerdivname).remove();
        }
    }
    jwplayer(divname).setup({
        flashplayer: '/js/player.swf',
        playlist: listaTemas,
        provider: 'http',
        item: index,
        'playlist.position': 'none',
        image: imagename,
        autostart: 'true',
        stretching: 'fill',
        screencolor: '000000',
        backcolor: '000000',
        frontcolor: '009999',
        lightcolor: '006666',
        height: playHeight,
        width: playWidth,
        allowfullscreen: 'true',
        allowscriptaccess: 'always',
        wmode: 'transparent',
        bgcolor: '#ffffff',
        controlbar: 'bottom',
        repeat: 'always',
        events:
            {
                onPlaylistItem: function (event) {
                    MostrarSugeridos(event.index);
                    $('#CurrentTrackName').html(jwplayer(divname).getPlaylistItem(event.index)['Title']);
                    if (jwplayer(divname).getPlaylistItem(event.index)['mediaid'] != null && jwplayer(divname).getPlaylistItem(event.index)['mediaid'] != undefined) {
                        AuditarAccion('Ver', jwplayer(divname).getPlaylistItem(event.index)['description'], jwplayer(divname).getPlaylistItem(event.index)['mediaid']);
                    }
                }
            }
    });
    }
    lastPlayerWidth == playWidth;
    lastPlayerHeight == playHeight;
    lastplayerdivname = divname;
    lastPlaylistName = pldiv;
    LastIsPlaylist = true;
}
var plDiscoActual = 0;
var working = false; //para evitar clicks sucesivos 
function playDisco(idDisco,index,imagename, divname, playWidth, playHeight)
{
    var listaTemas = null;
    if(idDisco != 0)
    {	
       var plid = '#DiscoPlaylist_' + Number(idDisco).toString();
       listaTemas = eval($(plid).html());    
    }
   if (lastplayerdivname != divname && lastplayerdivname != '') {
       if (jwplayer(lastplayerdivname).getState() != undefined) {
           jwplayer(lastplayerdivname).stop();
           jwplayer(lastplayerdivname).remove();
       }
   }
    
    if (jwplayer(divname).getState() != undefined && LastIsPlaylist) 
    {
		if(plDiscoActual != 0 && (plDiscoActual == idDisco || idDisco == 0) && jwplayer(divname).getPlaylist())
		{
		    jwplayer(divname).playlistItem(index);			
		}
		else if(idDisco != 0 && listaTemas != null)
		{
		    plDiscoActual = idDisco;
			jwplayer(divname).load(listaTemas);
			jwplayer(divname).playlistItem(index);
		}
    } 
    else if(idDisco != 0) {
        if (lastplayerdivname != divname && lastplayerdivname != '') {
            if (jwplayer(lastplayerdivname).getState() != undefined) {
                jwplayer(lastplayerdivname).stop();
                jwplayer(lastplayerdivname).remove();
            }
        }
       plDiscoActual = idDisco;
       jwplayer(divname).setup({
           flashplayer: '/js/player.swf',
           playlist: listaTemas,
           'playlist.position': 'none',
           image: imagename,
           autostart: 'true',
           stretching: 'fill',
           screencolor: '000000',
           backcolor: '000000',
           frontcolor: '009999',
           lightcolor: '006666',
           height: playHeight,
           width: playWidth,
           allowfullscreen: 'true',
           allowscriptaccess: 'always',
           wmode: 'transparent',
           bgcolor: '#ffffff',
           controlbar: 'bottom',
           repeat: 'always',
           events:
            {
                onPlaylistItem: function (event) {
                    $('#CurrentTrackName').html(jwplayer(divname).getPlaylistItem(event.index)['Title']);
                    AuditarAccion('Ver', 'Temas', jwplayer(divname).getPlaylistItem(event.index)['description']);                    
                }
            }
       });
    }
    LastIsPlaylist = true;
    lastplayerdivname = divname;
    working = false;
    return 0;
}

function play(filename, imagename, divname, playWidth, playHeight, pduration)
{	
    if(lastplayerdivname != divname && lastplayerdivname != '')
    {
        if(jwplayer(lastplayerdivname).getState() != undefined)
            jwplayer(lastplayerdivname).stop();    
    }

    if (pduration == undefined || pduration == null)
        pduration = 0;

    if (jwplayer(divname).getState() != undefined && !LastIsPlaylist) 
    {
        if (jwplayer(divname).getPlaylist()[0]['file'] == filename) 
        {
			jwplayer(divname).seek(0);
        }
        else 
        {
            jwplayer(divname).load({ file: imagename });
            if (pduration > 0) {
                jwplayer(divname).load({ file: filename, image: imagename, duration: pduration });
            }
            else {
                jwplayer(divname).load({ file: filename, image: imagename});
            }   
		}
    } else {
        if (lastplayerdivname != divname && lastplayerdivname != '') {
            if (jwplayer(lastplayerdivname).getState() != undefined) {
                jwplayer(lastplayerdivname).stop();
                jwplayer(lastplayerdivname).remove();
            }
        }
        if (pduration > 0) {
            jwplayer(divname).setup({
                flashplayer: '/js/player.swf',
                file: filename,
                image: imagename,
                autostart: 'true',
                stretching: 'fill',
                screencolor: '000000',
                backcolor: '000000',
                frontcolor: '009999',
                lightcolor: '006666',
                height: playHeight,
                width: playWidth,
                allowfullscreen: 'true',
                allowscriptaccess: 'always',
                wmode: 'transparent',
                bgcolor: '#ffffff',
                controlbar: 'bottom',
                duration: pduration
            });
        }
        else {
            jwplayer(divname).setup({
                flashplayer: '/js/player.swf',
                file: filename,
                image: imagename,
                autostart: 'true',
                stretching: 'fill',
                screencolor: '000000',
                backcolor: '000000',
                frontcolor: '009999',
                lightcolor: '006666',
                height: playHeight,
                width: playWidth,
                allowfullscreen: 'true',
                allowscriptaccess: 'always',
                wmode: 'transparent',
                bgcolor: '#ffffff',
                controlbar: 'bottom'
            });
        }
        //setTimeout(function () { jwplayer(divname).load({ file: imagename, image: imagename }); if (pduration > 0) { jwplayer(divname).load({ file: filename, image: imagename, duration: pduration }); } else { jwplayer(divname).load({ file: filename, image: imagename }); } }, 500);
    }
    LastIsPlaylist = false;
    lastplayerdivname = divname;
}

function playStream(filename, imagename, divname, playWidth, playHeight, pduration)
{	
    if(lastplayerdivname != divname && lastplayerdivname != '')
    {
        if(jwplayer(lastplayerdivname).getState() != undefined)
            jwplayer(lastplayerdivname).stop();
    }
    if (pduration == undefined || pduration == null)
        pduration = 0;
    if (jwplayer(divname).getState() != undefined && !LastIsPlaylist) {
         if(jwplayer(divname).getPlaylist()[0]['file'] == filename) {
			jwplayer(divname).seek(0);
		} else {
            jwplayer(divname).load({ provider: 'http', file: filename, image: imagename, duration: pduration });
		}
    } else {
        if (lastplayerdivname != divname && lastplayerdivname != '') {
            if (jwplayer(lastplayerdivname).getState() != undefined) {
                jwplayer(lastplayerdivname).stop();
                jwplayer(lastplayerdivname).remove();
            }
        }
       jwplayer(divname).setup({
            flashplayer:        '/js/player.swf',
            provider:           'http',
            file:               filename,
            image:				imagename,
            autostart:  		'true',
            stretching:			'fill',
            screencolor:		'000000',
            backcolor:			'000000',
            frontcolor:			'009999',
            lightcolor:			'006666',
            height:             playHeight,
            width:              playWidth,
            allowfullscreen:    'true',
            allowscriptaccess:  'always',
            wmode:				'transparent',
            bgcolor:        	'#ffffff',
            controlbar:			'bottom',
            duration: pduration
        });
        setTimeout(function () { jwplayer(divname).load({ provider: 'http', file: filename, image: imagename, duration: pduration }); }, 500);
    }
    LastIsPlaylist = false;
    lastplayerdivname = divname;
}


function play2(filename, imagename, divname, playWidth, playHeight)
{	
	play(filename, imagename, divname, playWidth, playHeight);
}

 function replay(divname)
{
    if(jwplayer(divname).getState() != undefined)
            jwplayer(divname).stop();  

    playFirstBand();
 }
 
