﻿ //PAra esse script funcionar a tela precisa ter as seguintes variaveis
//$("hCodFilme")
 
 var tplPlayer = {}; 
 var strVideoInicial = "";
 var CodUsuario = -1;

  // $load(init2);
 $load(initTelaPlayer);

  // var initPlayerOK = false;
    function initTelaPlayer ()
    {
        tplPlayer = new Template("shl");
        if(Request.browser.type.isIe && Request.browser.version < 6){
             //Versoes nao suportadas do IE
            Page.setTitle('Erro!');
            $("conteudoGeral").innerHTML = tplPlayer.erroNavegador.getHtml();
        }
        else
        {
            var CodMedia;
            if(Request.queryString('CodMedia')!=null)
            {
                CodMedia = Request.queryString('CodMedia'); //Player.PegaCodFilme();
            }
            else
            {
                var resCodMedia = Player.PegaDadosFilme2();
                //var resCodMedia = Player.PegaCodMedia(); 
                if(resCodMedia.error==null)
                {
                    //CodMedia = resCodMedia.value;

                    DTVideoCarregado = resCodMedia.value;
                    CodMedia = DTVideoCarregado.rows[0]["CodMedia"];
                }
                else
                    alert("Erro ao pegar p CodMedia:" + resCodMedia.error);
             }

            $("hCodFilme").value = CodMedia;
            
            Player.CarregaPaginaPlayerInicial(NumeroVideosDestaques,CodMedia,CarregaPagina);

        }
    }
    
    
    function CarregaPagina(res)
    {
        if(res.error==null)
        {
             DSItem = res.value;
             
             var DTUsuario = DSItem.tables.Usuario;
             CodUsuario = DTUsuario.rows[0]["CodUsuario"];
//alert(CodUsuario );
    		//initPlayerOK = true;
             CarregaUltimosVideosDT(DSItem.tables.UltimosVideos);
             
         }
         else
         {
            alert("Erro ao carregar os dados iniciais do player. Erro:" + res.error.message);
         }
    }
    
/*function VerificaVelocidade()
{
//TESTEEMERSON
    Velocidade = "28k"; // session nao funciona Request.Session("Velocidade");
    if(Velocidade==null)
    {
        //CALCULAR e colocar na Velocidade
    }
    
    return Velocidade;
}*/

function CarregaDadosVideo(res)
{
    var DTDadosFilme;
    if(res.error==null)
    {
        DTDadosFilme = res.value;
    
        if(DTDadosFilme.rows.length>0)
        {
            var CodMedia = DTDadosFilme.rows[0]["CodMedia"];
            //var CodTipo = 2;
            //var CodTipo = DTDadosFilme.rows[0]["CodTipo"];
            
            if(DTDadosFilme.rows[0]["CodFormato"]==4)
            {
                CodTipo=2;
            }
            else if(DTDadosFilme.rows[0]["CodFormato"]==1)
            {
                CodTipo=1;
            }
                
            var Titulo =  DTDadosFilme.rows[0]["Titulo"];
            var Descricao =  DTDadosFilme.rows[0]["Descricao"];
            var Duracao =  DTDadosFilme.rows[0]["Duracao"];  
            var TimeCodeInicio =  DTDadosFilme.rows[0]["TimeCodeInicio"]/1000;   
            var TimeCodeFim =  DTDadosFilme.rows[0]["TimeCodeFim"]/1000;    
            //var VideoRestrito =  DTDadosFilme.rows[0]["Restrito"]; 
            var Largura = 640;
            var Altura =480;
            $("spanTitulo").innerHTML = Titulo;
            $("divSinopse").innerHTML = Descricao;
            
            if(DTDadosFilme.rows[0]["Altura"]!=null)
            {
                Altura = DTDadosFilme.rows[0]["Altura"];
            }

            if(DTDadosFilme.rows[0]["Largura"]!=null)
            {
                Altura = DTDadosFilme.rows[0]["Largura"];
            }
                
                
           
            
        }

    }
}


//Lembrando que na base nova, CodMedia = CodFilme da base velha
function CarregaVideo(res)
{
    if(res.error==null)
    {    
        DTVideoCarregado = res.value;
        CarregaVideoDT(DTVideoCarregado);
    }   
}

//Lembrando que na base nova, CodMedia = CodFilme da base velha
function CarregaVideoDT(DTDadosFilme)
{
    //var DTDadosFilme;
    //if(res.error==null)
    //{
       // DTDadosFilme = res.value;
    
        if(DTDadosFilme.rows.length>0)
        {
            var CodMedia = DTDadosFilme.rows[0]["CodMedia"];
            var CodTipo = 2;
            //var CodTipo = DTDadosFilme.rows[0]["CodTipo"];
            if(DTDadosFilme.rows[0]["CodFormato"]==4)
            {
                CodTipo=2;
            }
            else if(DTDadosFilme.rows[0]["CodFormato"]==1)
            {
                CodTipo=1;
            }
            var Titulo =  DTDadosFilme.rows[0]["Titulo"];
            var Descricao =  DTDadosFilme.rows[0]["Descricao"];
            var Duracao =  DTDadosFilme.rows[0]["Duracao"];  
            var TimeCodeInicio =  DTDadosFilme.rows[0]["TimeCodeInicio"]/1000;   
            var TimeCodeFim =  DTDadosFilme.rows[0]["TimeCodeFim"]/1000;     
            var Largura = 640;
            var Altura =480;
            var VideoRestrito =  DTDadosFilme.rows[0]["Restrito"];
            
            $("spanTitulo").innerHTML = Titulo;
            $("divSinopse").innerHTML = Descricao;
            document.title = NomeDoSite + " - " + Titulo;
            //alert(CodUsuario);
            if(!VideoRestrito ||  (VideoRestrito &&  (CodUsuario>0 ||  RecemLogado)  ))
            {
            
                var URL  = ""; //DTDadosFilme.rows[0]["Url"];
               

                if(DTDadosFilme.rows[0]["Altura"]!=null)
                {
                    Altura = DTDadosFilme.rows[0]["Altura"];
                }

                if(DTDadosFilme.rows[0]["Largura"]!=null)
                {
                    Altura = DTDadosFilme.rows[0]["Largura"];
                }
                    
                var ThumbPlayerUrl1 = "imagens/tb_ndispo_med_play.jpg";
                if(DTDadosFilme.rows[0]["Thumb"]!=null)
                {
                     ThumbPlayerUrl1 =  "Arquivos/Thumbs/b_" + DTDadosFilme.rows[0]["Thumb"] ;
                }                
                   
                
                if(TimeCodeFim<=0)
                {
                    _voxelPlayer.AddMediaInPlaylist(CodMedia,CodTipo,URL,ThumbPlayerUrl1,true);
                }
                else
                {
                    _voxelPlayer.AddMediaWithCut(CodMedia,CodTipo, TimeCodeInicio,TimeCodeFim,URL,ThumbPlayerUrl1,true);
                }
            }
            else
            {
                alert("Video Restrito. Faça o Login para visualizar esse video." + CodUsuario);
            }
        }

    //}
    
}

    //function init2()
   // {
       // alert("sss");
        //alert(strVideoInicial);
       //    eval(strVideoInicial);
  //  }
    
    //function CodFilmeVerificaVelocidade()
    //{
        //TESTEEMERSON
    //    return "28k";
   // }
function CarregaListaAtores(res)
{
    if(res.error==null)
    {
          var DTAtores = res.value;
          var sbListaAtores = new StringBuilder();
          if(DTAtores.rows.length>0)
          {
              for(var j = 0; j < DTAtores.rows.length; j++)
               {
                    tplPlayer.player.ListaItemAtores.Nome =  DTAtores.rows[j]["Nome"];
                    tplPlayer.player.ListaItemAtores.Foto =  "atores/" + DTAtores.rows[j]["FotoPequenaPrincipal"];
                    tplPlayer.player.ListaItemAtores.Link = NomePaginaAtores + DTAtores.rows[j]["CodAtor"] + "_" + DTAtores.rows[j]["Nome"] +".html";  //"HomeAtriz.aspx?CodAtor=" + DTAtores.rows[j]["CodAtor"];
                    tplPlayer.player.ListaItemAtores.NumeroFilmes = ""; // dtCategoria.rows[j]["NumeroFilmes"];
                    tplPlayer.player.ListaItemAtores.NumeroFotos = ""; //  dtCategoria.rows[j]["NumeroFotos"];
                    sbListaAtores.append(tplPlayer.player.ListaItemAtores.getHtml());
               }
               
               tplPlayer.player.ListaAtores.ListaItemAtores = sbListaAtores.toString();
                               
               $("divAtores").innerHTML =  tplPlayer.player.ListaAtores.getHtml();
           }
           else
           {
                $("divAtores").innerHTML =  "";
           }
    }
    else
    {
        return "";
    }
}


function CarregaVideosRelacionados(res)
{
    if(res.error==null)
    {
          var DTVideos = res.value;
          var sbListaVideos = new StringBuilder();
          if(DTVideos.rows.length>0)
          {
              for(var j = 0; j < DTVideos.rows.length; j++)
               {
                   var urlvideo = ""; //DTVideos.rows[j]["VideoUrl"];                 
                   //if( urlvideo==null)
                   //{
                   //     urlvideo = Player.PegaUrlVideoVelho(DTVideos.rows[j]["CodMedia"],"28k").value;
                   //}
                   
                                     
                   var CodMedia1 =  DTVideos.rows[j]["CodMedia"];
                   var CodTipo = 2;
                   if(DTDadosFilme.rows[0]["CodFormato"]==4)
                   {
                        CodTipo=2;
                   }
                    else if(DTDadosFilme.rows[0]["CodFormato"]==1)
                    {
                        CodTipo=1;
                    }
                   var TimeCodeInicio =  DTVideos.rows[j]["TimeCodeInicio"]/1000;   
                   var TimeCodeFim =  DTVideos.rows[j]["TimeCodeFim"]/1000;     

                   var VideoUrl1 = "'" + urlvideo + "'";
                   
                   var ThumbPlayerUrl1 = "'imagens/tb_ndispo_med_play.jpg'";
                    if(DTVideos.rows[j]["ThumbPlayerUrl"]!=null)
                    {
                         ThumbPlayerUrl1 =  "'Arquivos/Thumbs/b_" + DTVideos.rows[j]["ThumbPlayerUrl"] + "'";
                    }                
                    else if(DTVideos.rows[j]["ThumbPlayerUrlAlt"]!=null)
                    {
                         ThumbPlayerUrl1 =  "'fotos/" + DTVideos.rows[j]["ThumbPlayerUrlAlt"] + "'";
                    }      
                        
                    if(TimeCodeFim>0)
                    {
                        tplPlayer.player.ListaItemVideosRelacionados.Link = "_voxelPlayer.AddMediaWithCut(" + CodMedia1 + "," +CodTipo + "," +TimeCodeInicio + "," +TimeCodeFim + "," +VideoUrl1 + "," +ThumbPlayerUrl1 + ",true);";
                    }
                    else
                    {
                        tplPlayer.player.ListaItemVideosRelacionados.Link = "_voxelPlayer.AddMediaInPlaylist(" + CodMedia1 + "," +CodTipo + "," +VideoUrl1 + "," +ThumbPlayerUrl1 + ",true);";
     		    }
                   // alert(tplPlayer.player.ListaItemVideosRelacionados.Link);


                     //tplPlayer.player.ListaItemVideosRelacionados.Width1 =  DTVideos.rows[j]["Width"];
                    //tplPlayer.player.ListaItemVideosRelacionados.Width2 =  DTVideos.rows[j]["Width"]; 
                  // tplPlayer.player.ListaItemVideosRelacionados.Height1 =  DTVideos.rows[j]["Height"];
                    //tplPlayer.player.ListaItemVideosRelacionados.Height2 =  DTVideos.rows[j]["Height"];
                    tplPlayer.player.ListaItemVideosRelacionados.Categoria =  DTVideos.rows[j]["Categoria"];
                    
                          
                     
                    if(DTVideos.rows[j]["ImageUrl"]!=null)
                    {
                        tplPlayer.player.ListaItemVideosRelacionados.ImageUrl = "'Arquivos/Thumbs/m_" + DTVideos.rows[j]["ImageUrl"] + "'";
                    }
                    else if(DTVideos.rows[j]["ImageUrlAlt"]!=null)
                    {
                    	tplPlayer.player.ListaItemVideosRelacionados.ImageUrl = "'fotos/" + DTVideos.rows[j]["ImageUrlAlt"] + "'";
                    }
                    else
                    {
                        tplPlayer.player.ListaItemVideosRelacionados.ImageUrl = "'/imagens/ImageUrlDefault.jpg'";
                    }

                    //alert(tplPlayer.player.ListaItemVideosRelacionados.getHtml());
                    sbListaVideos.append(tplPlayer.player.ListaItemVideosRelacionados.getHtml());
               }
               
               tplPlayer.player.ListaVideosRelacionados.Itens = sbListaVideos.toString();
                               
               $("divCenasDesteFilme").innerHTML  = tplPlayer.player.ListaVideosRelacionados.getHtml();
           }
           else
           {
               $("divCenasDesteFilme").innerHTML  = "";
           }
    }
  
}


function PegaURLVideoVelho(CodFilme,CodVelocidade)
{
    var res = Player.PegaUrlVideoVelho(Quantidade);
}


function CarregaUltimosVideos(res)
{
    if(res.error==null)
    {
          var DTVideos = res.value;
          var sbListaVideos = new StringBuilder();
        
          for(var j = 0; j < DTVideos.rows.length; j++)
           {
                                    
               var CodMedia1 =  DTVideos.rows[j]["CodMedia"];
               var CodTipo = 2;
               if(DTVideos.rows[0]["CodFormato"]==4)
               {
                    CodTipo=2;
               }
               else if(DTVideos.rows[0]["CodFormato"]==1)
               {
                    CodTipo=1;
               }
               var TimeCodeInicio =  DTVideos.rows[j]["TimeCodeInicio"]/1000;   
               var TimeCodeFim =  DTVideos.rows[j]["TimeCodeFim"]/1000;     

               var VideoUrl1 = "''";
               
               var ThumbPlayerUrl1 ="'images/tb_ndispo_med_play.jpg'";
                if(DTVideos.rows[j]["ThumbPlayerUrl"]!=null)
                {
                     ThumbPlayerUrl1 =  "'Arquivos/Thumbs/b_" + DTVideos.rows[j]["ThumbPlayerUrl"] + "'";
                }                
                else if(DTVideos.rows[j]["ThumbPlayerUrlAlt"]!=null)
                {
                     ThumbPlayerUrl1 =  "'fotos/" + DTVideos.rows[j]["ThumbPlayerUrlAlt"] + "'";
                }      
                    
                if(TimeCodeFim>0) 
                {
                    tplPlayer.player.ListaItemUltimosVideos.Link = "_voxelPlayer.AddMediaWithCut(" + CodMedia1 + "," +CodTipo + "," +TimeCodeInicio + "," +TimeCodeFim + "," +VideoUrl1 + "," +ThumbPlayerUrl1 + ",true);";
                }
                else
                {
                    tplPlayer.player.ListaItemUltimosVideos.Link = "_voxelPlayer.AddMediaInPlaylist(" + CodMedia1 + "," +CodTipo + "," +VideoUrl1 + "," +ThumbPlayerUrl1 + ",true);";
 		}

                //alert(tplPlayer.player.ListaItemUltimosVideos.Link);
                                    
                //tplPlayer.player.ListaItemUltimosVideos.CodMedia1 =  DTVideos.rows[j]["CodMedia"];
               // tplPlayer.player.ListaItemUltimosVideos.type1 =  DTVideos.rows[j]["type"];
               // tplPlayer.player.ListaItemUltimosVideos.VideoUrl1 = "''"; //"'" +   DTVideos.rows[j]["VideoUrl"] + "'";
                //tplPlayer.player.ListaItemUltimosVideos.Width1 =  DTVideos.rows[j]["Width"];
                //tplPlayer.player.ListaItemUltimosVideos.Height1 =  DTVideos.rows[j]["Height"];
                //if(DTVideos.rows[j]["ThumbPlayerUrl"]!=null)
                //    tplPlayer.player.ListaItemUltimosVideos.ThumbPlayerUrl1 = "'/fotos/" +   DTVideos.rows[j]["ThumbPlayerUrl"] + "'";
                //else if(DTVideos.rows[j]["ThumbPlayerUrlAlt"]!=null)
                //    tplPlayer.player.ListaItemUltimosVideos.ThumbPlayerUrl1 = "'/fotos/" +   DTVideos.rows[j]["ThumbPlayerUrLAlt"] + "'";
               // else
               //     tplPlayer.player.ListaItemUltimosVideos.ThumbPlayerUrl1 = "'/imagens/ThumbPlayerUrlDefault.jpg'";

                if(DTVideos.rows[j]["ImageUrl"]!=null)
                {
                    tplPlayer.player.ListaItemUltimosVideos.ImageUrl =  "Arquivos/Thumbs/m_" + DTVideos.rows[j]["ImageUrl"] ;
                }
                else if(DTVideos.rows[j]["ImageUrlAlt"]!=null)
                {
                    tplPlayer.player.ListaItemUltimosVideos.ImageUrl =  "fotos/" + DTVideos.rows[j]["ImageUrlAlt"] ;
                }
                else
                {
                    tplPlayer.player.ListaItemUltimosVideos.ImageUrl =  "images/tb_ndispo_grd.jpg" ;
		}

                sbListaVideos.append(tplPlayer.player.ListaItemUltimosVideos.getHtml());
           }
           
           tplPlayer.player.ListaUltimosVideos.Itens = sbListaVideos.toString();
           
                           
           $("divOutrosFilmes").innerHTML =  tplPlayer.player.ListaUltimosVideos.getHtml();
    }
}


function CarregaUltimosVideosDT(DTVideos)
{
   // if(res.error==null)
   // {
       //   var DTVideos = res.value;
          var sbListaVideos = new StringBuilder();
        
          for(var j = 0; j < DTVideos.rows.length; j++)
           {
                                    
               var CodMedia1 =  DTVideos.rows[j]["CodMedia"];
               var CodTipo = 2;
               if(DTVideos.rows[0]["CodFormato"]==4)
               {
                    CodTipo=2;
               }
               else if(DTVideos.rows[0]["CodFormato"]==1)
               {
                    CodTipo=1;
               }
               var TimeCodeInicio =  DTVideos.rows[j]["TimeCodeInicio"]/1000;   
               var TimeCodeFim =  DTVideos.rows[j]["TimeCodeFim"]/1000;     

               var VideoUrl1 = "''";
               
               var ThumbPlayerUrl1 ="'images/tb_ndispo_med_play.jpg'";
                if(DTVideos.rows[j]["ThumbPlayerUrl"]!=null)
                {
                     ThumbPlayerUrl1 =  "'Arquivos/Thumbs/b_" + DTVideos.rows[j]["ThumbPlayerUrl"] + "'";
                }                
                else if(DTVideos.rows[j]["ThumbPlayerUrlAlt"]!=null)
                {
                     ThumbPlayerUrl1 =  "'fotos/" + DTVideos.rows[j]["ThumbPlayerUrlAlt"] + "'";
                }      
                    
                if(TimeCodeFim>0)
                {
                    tplPlayer.player.ListaItemUltimosVideos.Link = "_voxelPlayer.AddMediaWithCut(" + CodMedia1 + "," +CodTipo + "," +TimeCodeInicio + "," +TimeCodeFim + "," +VideoUrl1 + "," +ThumbPlayerUrl1 + ",true);";
                }
                else
                {
                    tplPlayer.player.ListaItemUltimosVideos.Link = "_voxelPlayer.AddMediaInPlaylist(" + CodMedia1 + "," +CodTipo + "," +VideoUrl1 + "," +ThumbPlayerUrl1 + ",true);";
 		}

                //alert(tplPlayer.player.ListaItemUltimosVideos.Link);
                                    
                //tplPlayer.player.ListaItemUltimosVideos.CodMedia1 =  DTVideos.rows[j]["CodMedia"];
               // tplPlayer.player.ListaItemUltimosVideos.type1 =  DTVideos.rows[j]["type"];
               // tplPlayer.player.ListaItemUltimosVideos.VideoUrl1 = "''"; //"'" +   DTVideos.rows[j]["VideoUrl"] + "'";
                //tplPlayer.player.ListaItemUltimosVideos.Width1 =  DTVideos.rows[j]["Width"];
                //tplPlayer.player.ListaItemUltimosVideos.Height1 =  DTVideos.rows[j]["Height"];
                //if(DTVideos.rows[j]["ThumbPlayerUrl"]!=null)
                //    tplPlayer.player.ListaItemUltimosVideos.ThumbPlayerUrl1 = "'/fotos/" +   DTVideos.rows[j]["ThumbPlayerUrl"] + "'";
                //else if(DTVideos.rows[j]["ThumbPlayerUrlAlt"]!=null)
                //    tplPlayer.player.ListaItemUltimosVideos.ThumbPlayerUrl1 = "'/fotos/" +   DTVideos.rows[j]["ThumbPlayerUrLAlt"] + "'";
               // else
               //     tplPlayer.player.ListaItemUltimosVideos.ThumbPlayerUrl1 = "'/imagens/ThumbPlayerUrlDefault.jpg'";

                if(DTVideos.rows[j]["ImageUrl"]!=null)
                {
                    tplPlayer.player.ListaItemUltimosVideos.ImageUrl =  "Arquivos/Thumbs/m_" + DTVideos.rows[j]["ImageUrl"] ;
                }
                else if(DTVideos.rows[j]["ImageUrlAlt"]!=null)
                {
                    tplPlayer.player.ListaItemUltimosVideos.ImageUrl =  "fotos/" + DTVideos.rows[j]["ImageUrlAlt"] ;
                }
                else
                {
                    tplPlayer.player.ListaItemUltimosVideos.ImageUrl =  "images/tb_ndispo_grd.jpg" ;
   		}

                sbListaVideos.append(tplPlayer.player.ListaItemUltimosVideos.getHtml());
           }
           
           tplPlayer.player.ListaUltimosVideos.Itens = sbListaVideos.toString();
           
                           
           $("divOutrosFilmes").innerHTML =  tplPlayer.player.ListaUltimosVideos.getHtml();
    //}
}



function CarregaCategorias(res)
{
    if(res.error==null)
    {
          var DTCategorias = res.value;
          var sbCategorias = new StringBuilder();
        
        if(DTCategorias.rows.length>0)
        {
          for(var j = 0; j < DTCategorias.rows.length; j++)
           {
                tplPlayer.player.ListaItemCategorias.Link = "Busca.aspx?CodCategoria=" + DTCategorias.rows[j]["CodCategoria"];
                tplPlayer.player.ListaItemCategorias.Nome =  DTCategorias.rows[j]["Nome"];

                sbCategorias.append(tplPlayer.player.ListaItemCategorias.getHtml());
           }
           
           tplPlayer.player.ListaCategorias.Itens = sbCategorias.toString();
                           
           $("divCategoriasFilmes").innerHTML = tplPlayer.player.ListaCategorias.getHtml();
        }
        else
        {
           $("divCategoriasFilmes").innerHTML = "";
        }
    }
    else
    {
        return "";
    }
}


function CarregaPlaylist(res)
{
    if(res.error==null)
    {
          var DTVideos = res.value;
          var sbListaVideos = new StringBuilder();
        
          for(var j = 0; j < DTVideos.rows.length; j++)
           {
               var urlvideo = ""; //DTVideos.rows[j]["VideoUrl"];                 
               
               var CodMedia1 =  DTVideos.rows[j]["CodMedia"];
               var type1 =  DTVideos.rows[j]["type"];
               var TimeCodeInicio =  DTVideos.rows[j]["TimeCodeInicio"]/1000;   
               var TimeCodeFim =  DTVideos.rows[j]["TimeCodeFim"]/1000;     

               var VideoUrl1 = "'" + urlvideo + "'";
               
               var ThumbPlayerUrl1 = "'imagens/tb_ndispo_med_play.jpg'";
                if(DTVideos.rows[j]["ThumbPlayerUrl"]!=null)
                {
                     ThumbPlayerUrl1 =  "'fotos/" + DTVideos.rows[j]["ThumbPlayerUrl"] + "'";
                }                
                else if(DTVideos.rows[j]["ThumbPlayerUrlAlt"]!=null)
                {
                     ThumbPlayerUrl1 =  "'fotos/" + DTVideos.rows[j]["ThumbPlayerUrlAlt"] + "'";
                }      
                    
                if(TimeCodeFim>0)
                {
                    _voxelPlayer.AddMediaWithCut(CodMedia1,type1,TimeCodeInicio,TimeCodeFim ,VideoUrl1,ThumbPlayerUrl1,false);
                }
                else
                {
                    _voxelPlayer.AddMediaInPlaylist(CodMedia1,type1,VideoUrl1,ThumbPlayerUrl1,false);
 		}
            }
    }
  
}



//FUNCOES DO PLAYR r************************************************************
var DTVideoCarregado;
function OnLoadProtectedMediaPlayer(CodMedia) //Funcao chamada toda vez que nao tiver uma url, chamar a cdn daqui e retornar a url com o ticket
{
    var OK = false;
    
    if (CodUsuario>0 ||  RecemLogado)
    {
        OK = true;
    }
    else
    {
         if(DTVideoCarregado==null)
         {

             if(hCacheTodosDados.containsKey(CodMedia))
             {
	         //alert("AChou o cache:" + CodMedia);
                 DTVideoCarregado  = hCacheTodosDados.getValue(CodMedia).DTDadosFilme;
             }
             else
             {
	         //alert("Med:" + CodMedia);
                  var res = Player.PegaDadosFilme(CodMedia);
                 //res = Player.IsVideoFechado(CodMedia);
                 if(res.error==null)
                 {
                    //alert("Carreg DT 2");
                      DTVideoCarregado = res.value;
                  }
                  else
                  {
		      alert("Erro ao carregar a media:" +CodMedia );
		  }
              }
         }
           // alert("Restrito?:" + DTVideoCarregado.rows[0].Restrito + "Tit:" + DTVideoCarregado.rows[0].Titulo );

         if(!DTVideoCarregado.rows[0].Restrito)
         {
             OK = true;
         }
         else
         {
            alert("Video Restrito. Faça o Login para visualizar esse video." + CodUsuario);
         }
    }
   // alert(CodUsuario);
   // alert(OK);
    
    if(OK)
    {
         var res = Player.PegaPathVideoCDNFromCodMedia(CodMedia);
         if(res.error==null)
         {
             if(res.value!="Permissão Negada")
             {
                  URL = res.value;
                  //alert(htmlEmbed(URL));
                  return URL;
              }
              else
              {
                 alert("Permissão Negada. Servidor não tem acesso aos videos.");
              }
         }
         else
         {
            alert("Erro ao carregar o video:" + res.error.message);
         }
     }
     return "";
     
}


function OnLoadPlayer()
    {
	setTimeout(carregaonLoadPlayer,1000);
    }


function carregaonLoadPlayer()
{
//alert("onload");
        try
        {
		//while(!initPlayerOK)
		//{
		//	var x=1;
		//}
            if($("hCodFilme").value!=null && $("hCodFilme").value!="")
            {
                if(DTVideoCarregado==null)
                {
                    Player.PegaDadosFilme($("hCodFilme").value,CarregaVideo);  //CarregaVideo($("hCodFilme").value);
                }
                else
                {
                    CarregaVideoDT(DTVideoCarregado);
                }
            }
        }
        catch(e)
        {
            alert("Erro ao carregar o video:" + e.message);
         }
}
    
    function OnInitPlayer(CodMedia)
    {
	//alert("oninit");
        //Player.PegaDadosFilme(CodMedia,CarregaDadosVideo);
       // Player.PegaListaAtores(CodMedia,CarregaListaAtores); //$("divAtores").innerHTML = CarregaListaAtores(CodMedia);
       // Player.PegaVideosRelacionados(CodMedia,CarregaVideosRelacionados); //$("divCenasDesteFilme").innerHTML = CarregaVideosRelacionados(CodMedia);
       // Player.PegaCategoriasFilmes(CodMedia,CarregaCategorias); //$("divCategoriasFilmes").innerHTML = CarregaCategorias(CodMedia);
    

        if(hCacheTodosDados.containsKey(CodMedia))
        {
	//alert("AChou o cache:" + CodMedia);
            CarregaTodosDadosEListasDoFilmeDS(hCacheTodosDados.getValue(CodMedia));
        }
        else
        {
            Player.PegaTodosDadosEListasDosFilmes(CodMedia,CarregaTodosDadosEListasDoFilme,CodMedia);
         }

    }
    
    var hCacheTodosDados = new Hashtable();
    function CarregaTodosDadosEListasDoFilme(res)
    {
        if(res.error==null)
        {
             DSItem = res.value;
//alert(res.params);
             hCacheTodosDados.add(res.params,DSItem);
             CarregaTodosDadosEListasDoFilmeDS(DSItem);
        }
        else
        {
            alert("Erro ao carregar dados e lista do filme: Erro:" + res.error.message);
            return "";
        }
        
    
    }
    
    function CarregaTodosDadosEListasDoFilmeDS(DSItem)
    {
    
        //CARREGA DADOS DOS FILMES
             DTDadosFilme = DSItem.tables.DadosFilmes;
    
            if(DTDadosFilme.rows.length>0)
            {
		//alert("Carreg DT 1");
                DTVideoCarregado = DTDadosFilme;
                var CodMedia = DTDadosFilme.rows[0]["CodMedia"];
                //var CodTipo = 2;
                //var CodTipo = DTDadosFilme.rows[0]["CodTipo"];
                
                if(DTDadosFilme.rows[0]["CodFormato"]==4)
                {
                    CodTipo=2;
                }
                else if(DTDadosFilme.rows[0]["CodFormato"]==1)
                {
                    CodTipo=1;
                }
                    
                var Titulo =  DTDadosFilme.rows[0]["Titulo"];
                var Descricao =  DTDadosFilme.rows[0]["Descricao"];
                var Duracao =  DTDadosFilme.rows[0]["Duracao"];  
                var TimeCodeInicio =  DTDadosFilme.rows[0]["TimeCodeInicio"]/1000;   
                var TimeCodeFim =  DTDadosFilme.rows[0]["TimeCodeFim"]/1000;    
                //var VideoRestrito =  DTDadosFilme.rows[0]["Restrito"]; 
                var Largura = 640;
                var Altura =480;
                $("spanTitulo").innerHTML = Titulo;
                $("divSinopse").innerHTML = Descricao;
                
                if(DTDadosFilme.rows[0]["Altura"]!=null)
                {
                    Altura = DTDadosFilme.rows[0]["Altura"];
                }

                if(DTDadosFilme.rows[0]["Largura"]!=null)
                {
                    Altura = DTDadosFilme.rows[0]["Largura"];
                }
                    
                document.title = NomeDoSite + " - " + Titulo;


             
            }
            //FIM DO CARREGA DADOS DOS  FILMES

             //LISTA DE CATEGORIAS
             var DTCategorias = DSItem.tables.CategoriasFilmes;
             var sbCategorias = new StringBuilder();
            
            if(DTCategorias.rows.length>0)
            {
              for(var j = 0; j < DTCategorias.rows.length; j++)
               {
                    tplPlayer.player.ListaItemCategorias.Link = "Busca.aspx?CodCategoria=" + DTCategorias.rows[j]["CodCategoria"];
                    tplPlayer.player.ListaItemCategorias.Nome =  DTCategorias.rows[j]["Nome"];

                    sbCategorias.append(tplPlayer.player.ListaItemCategorias.getHtml());
               }
               
               tplPlayer.player.ListaCategorias.Itens = sbCategorias.toString();
                               
               $("divCategoriasFilmes").innerHTML = tplPlayer.player.ListaCategorias.getHtml();
            }
            else
            {
               $("divCategoriasFilmes").innerHTML = "";
            }
            //FIM DE LISTA DE CATEGORIAS
            
            
            
            //LISTA DE VIDEOS RELACIONADOS
            var DTVideos = DSItem.tables.VideosRelacionados;
          var sbListaVideos = new StringBuilder();
          if(DTVideos.rows.length>0)
          {
              for(var j = 0; j < DTVideos.rows.length; j++)
               {
                   var urlvideo = ""; //DTVideos.rows[j]["VideoUrl"];                 
                   //if( urlvideo==null)
                   //{
                   //     urlvideo = Player.PegaUrlVideoVelho(DTVideos.rows[j]["CodMedia"],"28k").value;
                   //}
                   
                                     
                   var CodMedia1 =  DTVideos.rows[j]["CodMedia"];
                   var CodTipo = 2;
                   if(DTDadosFilme.rows[0]["CodFormato"]==4)
                   {
                        CodTipo=2;
                    }
                    else if(DTDadosFilme.rows[0]["CodFormato"]==1)
                    {
                        CodTipo=1;
                    }
                   var TimeCodeInicio =  DTVideos.rows[j]["TimeCodeInicio"]/1000;   
                   var TimeCodeFim =  DTVideos.rows[j]["TimeCodeFim"]/1000;     

                   var VideoUrl1 = "'" + urlvideo + "'";
                   
                   var ThumbPlayerUrl1 = "'imagens/tb_ndispo_med_play.jpg'";
                    if(DTVideos.rows[j]["ThumbPlayerUrl"]!=null)
                    {
                         ThumbPlayerUrl1 =  "'Arquivos/Thumbs/b_" + DTVideos.rows[j]["ThumbPlayerUrl"] + "'";
                    }                
                    else if(DTVideos.rows[j]["ThumbPlayerUrlAlt"]!=null)
                    {
                         ThumbPlayerUrl1 =  "'fotos/" + DTVideos.rows[j]["ThumbPlayerUrlAlt"] + "'";
                    }      
                        
                    if(TimeCodeFim>0) 
                    {
                        tplPlayer.player.ListaItemVideosRelacionados.Link = "_voxelPlayer.AddMediaWithCut(" + CodMedia1 + "," +CodTipo + "," +TimeCodeInicio + "," +TimeCodeFim + "," +VideoUrl1 + "," +ThumbPlayerUrl1 + ",true);";
                    }
                    else
                    {
                        tplPlayer.player.ListaItemVideosRelacionados.Link = "_voxelPlayer.AddMediaInPlaylist(" + CodMedia1 + "," +CodTipo + "," +VideoUrl1 + "," +ThumbPlayerUrl1 + ",true);";
     		    }
                   // alert(tplPlayer.player.ListaItemVideosRelacionados.Link);


                     //tplPlayer.player.ListaItemVideosRelacionados.Width1 =  DTVideos.rows[j]["Width"];
                    //tplPlayer.player.ListaItemVideosRelacionados.Width2 =  DTVideos.rows[j]["Width"]; 
                  // tplPlayer.player.ListaItemVideosRelacionados.Height1 =  DTVideos.rows[j]["Height"];
                    //tplPlayer.player.ListaItemVideosRelacionados.Height2 =  DTVideos.rows[j]["Height"];
                    tplPlayer.player.ListaItemVideosRelacionados.Categoria =  DTVideos.rows[j]["Categoria"];
                    
                          
                     
                    if(DTVideos.rows[j]["ImageUrl"]!=null)
                    {
                        tplPlayer.player.ListaItemVideosRelacionados.ImageUrl = "'Arquivos/Thumbs/m_" + DTVideos.rows[j]["ImageUrl"] + "'";
                    }
                    else if(DTVideos.rows[j]["ImageUrlAlt"]!=null)
                    {
                    	tplPlayer.player.ListaItemVideosRelacionados.ImageUrl = "'fotos/" + DTVideos.rows[j]["ImageUrlAlt"] + "'";
                    }
                    else
                    {
                        tplPlayer.player.ListaItemVideosRelacionados.ImageUrl = "'/imagens/ImageUrlDefault.jpg'";
                    }

                    //alert(tplPlayer.player.ListaItemVideosRelacionados.getHtml());
                    sbListaVideos.append(tplPlayer.player.ListaItemVideosRelacionados.getHtml());
               }
               
               tplPlayer.player.ListaVideosRelacionados.Itens = sbListaVideos.toString();
                               
               $("divCenasDesteFilme").innerHTML  = tplPlayer.player.ListaVideosRelacionados.getHtml();
           }
           else
           {
               $("divCenasDesteFilme").innerHTML  = "";
           }
           //FIM DOS VIDEOS RELACIONADOS
            
            
           //LISTA ATORES
          var DTAtores = DSItem.tables.ListaAtores;
          var sbListaAtores = new StringBuilder();
          if(DTAtores.rows.length>0)
          {
              for(var j = 0; j < DTAtores.rows.length; j++)
               {
                    tplPlayer.player.ListaItemAtores.Nome =  DTAtores.rows[j]["Nome"];
                    tplPlayer.player.ListaItemAtores.Foto =  "atores/" + DTAtores.rows[j]["FotoPequenaPrincipal"];
                    tplPlayer.player.ListaItemAtores.Link = NomePaginaAtores +  DTAtores.rows[j]["CodAtor"] + "_" + DTAtores.rows[j]["Nome"] +".html";  //"HomeAtriz.aspx?CodAtor=" + DTAtores.rows[j]["CodAtor"];
                    tplPlayer.player.ListaItemAtores.NumeroFilmes = ""; // dtCategoria.rows[j]["NumeroFilmes"];
                    tplPlayer.player.ListaItemAtores.NumeroFotos = ""; //  dtCategoria.rows[j]["NumeroFotos"];
                    sbListaAtores.append(tplPlayer.player.ListaItemAtores.getHtml());
               }
               
               tplPlayer.player.ListaAtores.ListaItemAtores = sbListaAtores.toString();
                               
               $("divAtores").innerHTML =  tplPlayer.player.ListaAtores.getHtml();
           }
           else
           {
                $("divAtores").innerHTML =  "";
           }
           //FIM DA LISTA DE ATORES

           DTVideoCarregado = null;
    
    }
    
      //==================================================================    
    //Grava a Playlist de um  usuario
    //******************************************************************    
    
   function SavePlaylist(pNomePlayList,pStrCodMedia)
   {               
         //Calcula a duração total dos videos                                   
         var vlDuracao =  0;//Player.GetDuracaoPlayList().res.value;

        var res = CascaPrincipalSH.RetornaCodUsuario();
        //Verifica se esta logado
        if(res.error ==null && res.value>0)
        {                         
            //Validação dos parametros
            if(pNomePlayList.trim()=="")
            {
                alert('O campo "Nome Playlist" deve ser preenchido');
                return;
            }                                            
            
            Player.GravarPlaylist(pNomePlayList, 
                                    pStrCodMedia, 
                                    res.value, 
                                    vlDuracao, 
                                    mGravarPlaylist_Callback);
        }
        else
        {
            alert("Você precisa estar logado para salvar a playlist");
        }    
    }
    function mGravarPlaylist_Callback(res)
        {        
            if(res.error == null)
            {                
                alert('Playlist gravada com sucesso!');   
                //Cache.remove(MediaCenter.PlayList.cacheName);
                //MediaCenter.PlayList.init();                              
            }
            else
            {                
                //MediaCenter.logError(res.error,"PlayList.mGravarPlaylist");
                alert('Erro. Tente novamente!');
            }
        }
         //==================================================================
    //Abre uma playlist salva pelo usuário
    //******************************************************************    
    
    function OpenPlaylists(pCodPlayList)
    {        
        //_voxelPlayer.TotalClearPlaylist();
        Player.GetVideosPlaylist(pCodPlayList, mOpenPlaylist_Callback);
    }
    //Callbacks Abre playLista cadastradas
    function mOpenPlaylist_Callback(res)
    {
        if(res.error == null)
        {                
            mMostrarOpenPlayList(res.value);
        }
        else
        {
            alert("Erro ao abrir a playlist:" + res.error.message);
            //MediaCenter.logError(res.error,"Player.mMostrarOpenPlayList");
        }
    }
    //==================================================================
    //Método para popular o player com uma playlist salva
    //******************************************************************    
    function mMostrarOpenPlayList(res)
    {   
        //Armazena o data table de retorno
        var dt = res;
        
        //Carrega o Player caso exista algum video salvo na playlistr
        if (dt.rows.length > 0)
        {
            //Popula o player com os vídeos salvos na PlayList
            for(var i = 0; i < dt.rows.length; i++)
            {                       
                //MediaCenter.Media.mCarregarMedia(idTest, true, false,'add');
                Player.PegaDadosFilme(dt.rows[i]["CodMedia"],CarregaPlaylist);

                
               /* 
               var CodMedia1 =  DTVideos.rows[j]["CodMedia"];
               var type1 =  DTVideos.rows[j]["type"];
               var TimeCodeInicio =  DTVideos.rows[j]["TimeCodeInicio"]/1000;   
               var TimeCodeFim =  DTVideos.rows[j]["TimeCodeFim"]/1000;     

               var VideoUrl1 = "''";
               
               var ThumbPlayerUrl1 ="'images/tb_ndispo_med_play.jpg'";
                if(DTVideos.rows[j]["ThumbPlayerUrl"]!=null)
                {
                     ThumbPlayerUrl1 =  "'fotos/" + DTVideos.rows[j]["ThumbPlayerUrl"] + "'";
                }                
                else if(DTVideos.rows[j]["ThumbPlayerUrlAlt"]!=null)
                {
                     ThumbPlayerUrl1 =  "'fotos/" + DTVideos.rows[j]["ThumbPlayerUrlAlt"] + "'";
                }      
                    
                if(TimeCodeFim>0) 
                {
                    _voxelPlayer.AddMediaWithCut(CodMedia1,type1,TimeCodeInicio,TimeCodeFim,VideoUrl1,ThumbPlayerUrl1,false);
                }
                else
                {
                    _voxelPlayer.AddMediaInPlaylist(CodMedia1,type1,VideoUrl1,ThumbPlayerUrl1,false);
 		} */
                
            }
        }            
    }
    //==================================================================
    //Método para popular o player com uma playlist da Query String
    //******************************************************************    
    function mOpenPlaylistQueryString(pStringPlayList)
    {   
        alert('string: ' + pStringPlayList);
        //Armazena o data table de retorno
        var ArrayCodMedia = pStringPlayList.split(',');        
        //Carrega o Player caso exista algum video salvo na playlistr
        if (ArrayCodMedia.length > 0)
        {  
            //Popula o player com os vídeos salvos na PlayList
            for(var i = 0; i < ArrayCodMedia.length; i++)
            {  
                Player.PegaDadosFilme(ArrayCodMedia[i],CarregaPlaylist);
                //MediaCenter.Media.mCarregarMedia(ArrayCodMedia[i], true, false,'add');
            }
        }            
        //Ancora para o topo da tela
        $(tpl.geral.idMenuTopo).scrollIntoView();        
    }
        
        
         //==================================================================
    //Carrega "Encaminhar Playlist"
    //******************************************************************
    function onForwardPlaylist(pStrCodMedia)
    {
        //Seta o parametro da StrCodMedia como Global
        var strCodMedia = pStrCodMedia;
        
        $('divEncaminharPlaylist').display = '';
        $('txtUrlEnc').value = 'http://www.sexyhard.com.br/Player.aspx?PlayList=' + strCodMedia;

    }
    
    function EnviarEmailPlaylist()
    {
        var res = Player.EnviaEmailPlaylist();
        if(res.error==null)
        {
            alert("Email enviado com sucesso!");
            $('divEncaminharPlaylist').display = 'none';
        }
        else
        {
            alert("Erro ao enviar email");
        }
    }