﻿

function $_cambiarVideo(pos, tipo)
{
    var A=0;
    var F=0;
    var V=0;
    
    if($('#' + videoInicioPagePrefix + 'optRanking_0').is(":checked"))
        V=1;
        
    if($('#' + videoInicioPagePrefix + 'optRanking_1').is(":checked"))
        A=1;
        
    if($('#' + videoInicioPagePrefix + 'optRanking_2').is(":checked"))
	    F=1;
	    
    $.ajax({
            url: "Handler.ashx",
            type: "POST",
            data: "action=devuelveVideo&pos=" + pos + "&tipo=" + tipo + "&A=" + A + "&F=" + F + "&V=" + V,
            dataType: "json",
            success: function(jsonObj)
                                    {
                                        if(jsonObj.status==="OK")
                                        {                                                                            
                                            $('#body').ScrollTo(1500);
                                            $('#' + videoInicioPagePrefix + 'lnkSiguiente').unbind().removeAttr("onclick");
                                            $('#' + videoInicioPagePrefix + 'lnkAnterior').unbind().removeAttr("onclick");
                                                                                        
                                            var fnSiguiente = function(){$_cambiarVideo((Number(pos) + 1), tipo);};
                                            var fnAnterior = function(){$_cambiarVideo((Number(pos) - 1), tipo);};
                                            
                                            $('#' + videoInicioPagePrefix + 'lnkSiguiente').click(fnSiguiente);
                                            $('#' + videoInicioPagePrefix + 'lnkAnterior').click(fnAnterior);

                                            $('#' + videoInicioPagePrefix + 'divPos').empty();
                                            $('#' + videoInicioPagePrefix + 'divPos').html(jsonObj.divPos);
                                            
                                            $('#' + videoInicioPagePrefix + 'lblTitulo').html(jsonObj.lblTitulo);
                                            $('#' + videoInicioPagePrefix + 'lblDescripcion').html(jsonObj.lblDescripcion);
                                            $('#' + videoInicioPagePrefix + 'lblTags').html(jsonObj.lblTags);
                                            $('#' + videoInicioPagePrefix + 'lblVisitas').html(jsonObj.lblVisitas);
                                         
                                            $('#' + videoInicioPagePrefix + 'txtLink').val(jsonObj.txtLink);
                                         
                                            $('#videoEmbed').empty();
                                            $('#videoEmbed').html(jsonObj.videoEmbed);
                                            
                                            $('#' + videoInicioPagePrefix + 'divComentarios').empty();
                                            $('#' + videoInicioPagePrefix + 'divComentarios').html(jsonObj.comentarios);
                                            
                                            $('#' + videoInicioPagePrefix + 'identidicadorMedia').val(jsonObj.identidicadorMedia);
                                            $('#' + videoInicioPagePrefix + 'contenidoTipo').val(jsonObj.contenidoTipo);
                                           
                                            $('#' + videoInicioPagePrefix + 'divRanking').empty();
                                            $('#' + videoInicioPagePrefix + 'divRanking').html(jsonObj.divRnk);
                                        }
                                        else
                                        {
                                            alert(jsonObj.msg);
                                        }
                                    }
            });
}

function $_mostrarDedicalo()
{
var paramData = 'action=validaSesion';

$.ajax({
          type: "POST",
          url: "Handler.ashx",
          data: paramData,
          dataType: "json",
          success: function(jsonObj){
                                    switch(jsonObj.status)
                                        {
                                        case 'OK':
                                            $.blockUI({ message: $('#divDedicalo'), css: { width: '300px' } }); 
                                            $_centrarDiv('.blockPage');
                                            break;
                                        case 'ERR':
                                            location.href="Registrate.aspx";
                                            break;
                                        }
                                    }
        });
}

function $_dedicar()
{
	var paramData = 'action=dedicar' + 
	                '&de=' + encodeURIComponent($('#' + videoInicioPagePrefix + 'txtDe').val()) + 
	                '&para=' + encodeURIComponent($('#' + videoInicioPagePrefix + 'txtPara').val()) + 
	                '&mensaje=' + encodeURIComponent($('#' + videoInicioPagePrefix + 'txtMensaje').val())+
	                '&idMedia=' + $('#' + videoInicioPagePrefix + 'identidicadorMedia').val() + 
	                '&contenidoTipo=' + $('#' + videoInicioPagePrefix + 'contenidoTipo').val();
    
    $.ajax({
            type: 'POST',
            url: "Handler.ashx",
            data: paramData,
			dataType: 'json',
            success: function(jsonObj)
                                {
	                                switch(jsonObj.status)
                                        {
                                        case 'OK':
                                            $('#' + videoInicioPagePrefix + 'txtDe').val('');
	                                        $('#' + videoInicioPagePrefix + 'txtPara').val('');
	                                        $('#' + videoInicioPagePrefix + 'txtMensaje').val('');

									        $_showMessage('OK', 'OK', jsonObj.msg);
									        $.unblockUI();
                                            break;
                                       
                                        case 'ERR':
                                            $_showMessage('ERR', 'ERROR', jsonObj.msg);
									        $.unblockUI();
                                            break;
                                        }
                                }
            });

return true;    
}

function $_validaFormDedicar()
{
    var err = false;
    
    if($('#' + videoInicioPagePrefix + 'txtDe').val() === '')
    {
        err = true;
        $('#' + videoInicioPagePrefix + 'txtDe').effect("highlight", {color:'#FF0000'}, 1000);
    }

    if(!$_mailValido($('#' + videoInicioPagePrefix + 'txtPara').val()))
    {
        err = true;
        $('#' + videoInicioPagePrefix + 'txtPara').effect("highlight", {color:'#FF0000'}, 1000);
    }

    if($('#' + videoInicioPagePrefix + 'txtMensaje').val() === '')
    {
        err = true;
        $('#' + videoInicioPagePrefix + 'txtMensaje').effect("highlight", {color:'#FF0000'}, 1000);
    }
    
    if(err){
        $_showMessage('ERR', 'ERROR', 'Faltan ingresar campos obligatorios');
    }
    else{
        $_dedicar();
    }

return err;
}

function $_cambiarRanking(elem)
{
    var A=0;
    var F=0;
    var V=0;
    
    var tipo= $('#' + videoInicioPagePrefix + 'rnkTipo').val();
    
    if($('#' + videoInicioPagePrefix + 'optRanking_0').is(":checked"))
        V=1;
        
    if($('#' + videoInicioPagePrefix + 'optRanking_1').is(":checked"))
        A=1;
        
    if($('#' + videoInicioPagePrefix + 'optRanking_2').is(":checked"))
	    F=1;
	
	if(A=="0" && F=="0" && V=="0")
	{
	    $(elem).attr("checked","checked");
	    return;
	}
	 
	$_cargarRanking(tipo, A, F, V);    
}

function $_enviarComentario()
{
	var paramData = 'action=comentar' + 
	                '&idMedia=' + $('#' + videoInicioPagePrefix + 'identidicadorMedia').val() + 
	                '&contenidoTipo=' + $('#' + videoInicioPagePrefix + 'contenidoTipo').val() + 
	                '&comentario=' + encodeURIComponent($('#' + videoInicioPagePrefix + 'txtComentar').val());

    $.ajax({
            type: 'POST',
            url: "Handler.ashx",
            data: paramData,
			dataType: 'json',
            success: function(jsonObj)
                                {
                                    switch(jsonObj.status)
                                            {
                                            case 'OK':
                                                $('#' + videoInicioPagePrefix + 'txtComentar').val('');
                                                $('#' + videoInicioPagePrefix + 'divComentarios').html(jsonObj.tblComentarios);
                                                $_showMessage('OK', 'OK', jsonObj.msg);
                                                break;
                                            case 'ERR':
                                                $_showMessage('ERR', 'ERROR', jsonObj.msg);
                                                     break;
                                            case 'ALERT':
                                                location.href="Registrate.aspx";
                                                break;
                                            }
                                    }
            });

return true;    
}

function $_agregarFavorito()
{
    var paramData = 'action=agregarFavorito' + 
	                '&id=' + encodeURIComponent($('#' + videoInicioPagePrefix + 'identidicadorMedia').val()) +
                    '&tipo=' + encodeURIComponent($('#' + videoInicioPagePrefix + 'contenidoTipo').val());
    $.ajax({
              type: "POST",
              url: "Handler.ashx",
              data: paramData,
              dataType: "json",
              success: function(jsonObj){
                                        switch(jsonObj.status)
                                            {
                                            case 'OK':
                                                $_showMessage('OK', 'OK', jsonObj.msg);
                                                break;
                                            case 'ERR':
                                                $_showMessage('ERR', 'ERROR', jsonObj.msg);
                                                     break;
                                            case 'ALERT':
                                                location.href="Registrate.aspx";
                                                break;
                                            }
                                        }
            });
}

function $_cargarRanking(tipo, A, F, V)
{
    $.ajax({
            type: "POST",
            url: "Handler.ashx",
            data: "action=cargarRanking&tipo=" + tipo + "&A=" + A + "&F=" + F + "&V=" + V,
            dataType: "json",
            success: function(jsonObj)
                                    {
                                        if(jsonObj.status==="OK")
                                        {                                                                            
                                            $('#' + videoInicioPagePrefix + 'divRanking').empty();
                                            $('#' + videoInicioPagePrefix + 'divRanking').html(jsonObj.divRnk);
                                         
                                            //$_cambiarMedia(0, tipo, A, F, V);
                                        }
                                        else
                                        {
                                            alert(jsonObj.msg);
                                        }
                                    }
            });
}

function $_cambiarMedia(idMedia, idMediaTipo, idRankingTipo)
{
    var A=0;
    var F=0;
    var V=0;
    
    if($('#' + videoInicioPagePrefix + 'optRanking_0').is(":checked"))
        V=1;
        
    if($('#' + videoInicioPagePrefix + 'optRanking_1').is(":checked"))
        A=1;
        
    if($('#' + videoInicioPagePrefix + 'optRanking_2').is(":checked"))
	    F=1;
	    
    $.ajax({
            url: "Handler.ashx",
            type: "POST",
            data: "action=devuelveMedia&idMedia=" + idMedia + "&idMediaTipo=" + idMediaTipo + "&idRankingTipo=" + idRankingTipo + "&A=" + A + "&F=" + F + "&V=" + V,
            dataType: "json",
            success: function(jsonObj)
                                    {
                                        if(jsonObj.status==="OK")
                                        {   
                                            $('#body').ScrollTo(1500);                                                                    
                                            $('#' + videoInicioPagePrefix + 'lnkSiguiente').unbind().removeAttr("onclick");
                                            $('#' + videoInicioPagePrefix + 'lnkAnterior').unbind().removeAttr("onclick");
                                                                                        
                                            var fnSiguiente = function(){$_cambiarVideo((Number(jsonObj.pos) + 1), tipo);};
                                            var fnAnterior = function(){$_cambiarVideo((Number(jsonObj.pos) - 1), tipo);};
                                            
                                            $('#' + videoInicioPagePrefix + 'lnkSiguiente').click(fnSiguiente);
                                            $('#' + videoInicioPagePrefix + 'lnkAnterior').click(fnAnterior);

                                            $('#' + videoInicioPagePrefix + 'divPos').empty();
                                            $('#' + videoInicioPagePrefix + 'divPos').html(jsonObj.divPos);

                                            $('#' + videoInicioPagePrefix + 'lblTitulo').html(jsonObj.lblTitulo);
                                            $('#' + videoInicioPagePrefix + 'lblDescripcion').html(jsonObj.lblDescripcion);
                                            $('#' + videoInicioPagePrefix + 'lblTags').html(jsonObj.lblTags);
                                            $('#' + videoInicioPagePrefix + 'lblVisitas').html(jsonObj.lblVisitas);
                                         
                                            $('#' + videoInicioPagePrefix + 'txtLink').val(jsonObj.txtLink);
                                         
                                            $('#videoEmbed').empty();
                                            $('#videoEmbed').html(jsonObj.videoEmbed);
                                            
                                            $('#' + videoInicioPagePrefix + 'divComentarios').empty();
                                            $('#' + videoInicioPagePrefix + 'divComentarios').html(jsonObj.comentarios);
                                            
                                            $('#' + videoInicioPagePrefix + 'identidicadorMedia').val(jsonObj.identidicadorMedia);
                                            $('#' + videoInicioPagePrefix + 'contenidoTipo').val(jsonObj.contenidoTipo);
                                           
                                        
                                            $('#' + videoInicioPagePrefix + 'divRanking').empty();
                                            $('#' + videoInicioPagePrefix + 'divRanking').html(jsonObj.divRnk);
                                        }
                                        else
                                        {
                                            alert(jsonObj.msg);
                                        }
                                    }
            });
}

function $_mostrarMsje()
{
$.blockUI({ message: $('#divMsje'), css: { width: '500px' } }); 
$_centrarDiv('.blockPage');

}