﻿function countChars(e)
{
    var element = document.getElementById('line').value
    n = element.length;
    t = 164;
    if (n > t)
    {
        document.getElementById('line').value = document.getElementById('line').value.substring(0, t);
        document.getElementById('cont').innerHTML = 'Te quedan ' + num + ' caracteres';
    }
    else
    {
        num = t - n;
        document.getElementById('cont').innerHTML = 'Te quedan ' + num + ' caracteres';
    }
    if(e)
    { 
        e = e;
        characterCode = e.which ;
    }
    else
    {
        e = event;
        characterCode = e.keyCode;
    }
    
    /*if (n >= 39 || characterCode == 13)
    {
        document.getElementById('line').style.height = '120px';
    }
    else
    {
        document.getElementById('line').style.height = '23px';
    } */

} 

function refresh()
{
 if (document.getElementById('line').value == 'Hace click para comentar')
    document.getElementById('line').value = '';
}

function checkEmpty()
{
    var element = document.getElementById('line').value;
    if(element == '')
    {
        document.getElementById('line').value = 'Hace click para comentar';
    }
}          


function commentVideo(ObjectId)
{
    var element = document.getElementById('line').value;
    
    if(element != '' && element != 'Hace click para comentar' )
    {
        AddComment(ObjectId, element);
        document.getElementById('commentMessage').innerHTML = element;
        document.getElementById('showSendComment').style.display = 'block';
        document.getElementById('sendComment').style.display = 'none';
    }
    else
    {
        alert('No puede dejar un comentario vacio');
    }
}

function deleteVideo(Id)
{
    if (confirm('¿Estás seguro que deseas eliminar el video?'))
        Delete(Id);
    else
        return;
}

function deleteComment(commentId, ObjectId)
{
    if(confirm('¿Está seguro que desea eliminar el comentario?'))
    {
        document.getElementById('comment_' + commentId).style.display = 'none';
	var url = '/Comment/Comment.ashx?DelInVideo&Format=json';												
					new Ajax.Request(url, {method: 'post', parameters: { ObjectId: ObjectId, Id: commentId },												
									 onSuccess: function(transport) {     
                					 var rsp = transport.responseText.evalJSON().rsp;
								if(rsp.stat != 'fail')
									alert(rsp.err.msg);
                        } });
						document.getElementById('Line_' + commentId).style.display = 'none';
    }
    else
    {
        return;
    }        
}

/*function compartirVideo(param)
{
    var elem = document.createElement('a');
    var attr = document.createAttribute('rel');
    attr.value = 'shadowbox;width=373;height=432';
    elem.setAttributeNode(attr);

    var attr3 = document.createAttribute('href');
    attr3.value = '/LayerShare.aspx?idVideo='+param;
    elem.setAttributeNode(attr3);

    Shadowbox.open(elem);
}*/
 
function reportarVideo(param, paramId)
{
    var elem = document.createElement('a');
    var attr = document.createAttribute('rel');
    attr.value = 'shadowbox;width=370;height=340';	            
    elem.setAttributeNode(attr);

    var attr3 = document.createAttribute('href');
    attr3.value = '/LayerReport.aspx?strObject='+param+'&strObjectId='+paramId;
    elem.setAttributeNode(attr3);

    Shadowbox.open(elem);
}	     
 
function FavoriteAdd(Name,ObjectTypeId,ObjectId,OwnerId,Status)
{
    VideoFavoriteIns(Name,ObjectId, OwnerId);
    alert('Se agregó el video \"' + Name + '\" a tus Favoritos.');
}
 
function compartirVideo(paramId)
{
    var elem = document.createElement('a');
    var attr = document.createAttribute('rel');
    
    if (document.getElementById('UserLogInn').value =='')
    {
        attr.value = 'shadowbox;width=366;height=445';//376
    }
    
    if (document.getElementById('UserLogInn').value !='')
    {
        attr.value = 'shadowbox;width=366;height=445';
    }
    
    elem.setAttributeNode(attr);

    var attr3 = document.createAttribute('href');
    attr3.value = '/LayerShareVideo.aspx?idVideo='+paramId;
    elem.setAttributeNode(attr3);

    Shadowbox.open(elem);
}
      
function reportarVideo(param, paramId)
{
    var elem = document.createElement('a');
    var attr = document.createAttribute('rel');
    attr.value = 'shadowbox;width=368;height=340';
    elem.setAttributeNode(attr);

    var attr3 = document.createAttribute('href');
    attr3.value = '/LayerReport.aspx?strObject='+param+'&strObjectId='+paramId;
    elem.setAttributeNode(attr3);

    Shadowbox.open(elem);
}	     

function ActiveEditTitulo(id)
{
    document.getElementById('control_edit_tit_'+id).style.display='none';
    document.getElementById('div_control_edit_tit_'+id).style.display='none';
    
    document.getElementById('edit_tit_'+id).style.display='block';   //muestro edicion
    document.getElementById('control_edit_des_'+id).style.display='block'; 
    //document.getElementById('edit_des_'+id).style.display='none'; 
    //document.getElementById('lahora').style.display='none';  
}
    
function GuardarTit(id)
{
    document.getElementById('control_edit_tit_'+id).innerText =  document.getElementById('txtTitle_'+id).value ;  //seteo
    document.getElementById('txtTitle_'+id).value = document.getElementById('control_edit_tit_'+id).innerText;

    document.getElementById('edit_tit_'+id).style.display='none';  //oculto la edicion
    //document.getElementById('lahora').style.display='block';  

    document.getElementById('div_control_edit_tit_'+id).style.display='block'; 

    if(document.getElementById('txtTitle_'+id).value  =='' || document.getElementById('txtTitle_'+id).value  =='Agregar titulo')
    {
        document.getElementById('edit_tit_'+id).style.display='block';  //obligo a que no sea null
        document.getElementById('control_edit_tit_'+id).style.display='none';
    }
    else
    {
        document.getElementById('edit_tit_'+id).style.display='none';
        document.getElementById('control_edit_tit_'+id).style.display='block';  // muestro el nuevo
        document.getElementById('div_control_edit_tit_'+id).style.display='block';
    }

    //Community_TitleUpd
    if(document.getElementById('txtTitle_'+id).value !='')
    {
        var control_edit_tit = document.getElementById('control_edit_tit_'+id); //label titulo
        var txtTitle = document.getElementById('txtTitle_'+id).value; // guardo el valor del input
        
        control_edit_tit.innerHTML = document.getElementById('txtTitle_'+id).value;  // seteo valor del input
        var url = '/Video/Video.ashx?TitleUpd&Format=json';
        
        new Ajax.Request(url, {method: 'post', parameters: {Id: id, Title: txtTitle}, onSuccess: function(transport) 
            {
                    var rsp = transport.responseText.evalJSON().rsp;
	                    if(rsp.stat == 'fail')
		                    alert(rsp.err.msg);
                    }
                }
            );
     }
}

function CancelarTit(id,title)
{
    document.getElementById('control_edit_tit_'+id).style.display='block'; 
    document.getElementById('div_control_edit_tit_'+id).style.display='block'; 
    document.getElementById('edit_tit_'+id).style.display='none';     
    document.getElementById('control_edit_tit_'+id).innerText =document.getElementById('control_edit_tit_'+id).innerText;
    //document.getElementById('lahora').style.display='block';  
    
    if(document.getElementById('txtTitle_'+id).value =='' || document.getElementById('txtTitle_'+id).value =='Agregar titulo' )
    {
        document.getElementById('control_edit_tit_'+id).innerHTML = 'Agregar titulo';
    }
}

function ActiveEditDescription(id)
{
    document.getElementById('control_edit_des_'+id).style.display='none'; 
    document.getElementById('divDelLabel').style.display='none';
    document.getElementById('edit_des_'+id).style.display='block';   //muestro edicion

    document.getElementById('control_edit_tit_'+id).style.display='block'; 
    document.getElementById('div_control_edit_tit_'+id).style.display='block'; 
    document.getElementById('edit_tit_'+id).style.display='none';

    document.getElementById('txtDescription_'+id).value =  document.getElementById('control_edit_des_'+id).innerHTML;
}
    
function GuardarDes(id)
{

    document.getElementById('control_edit_des_'+id).innerText = document.getElementById('txtDescription_'+id).value ;  //seteo
    document.getElementById('txtDescription_'+id).value = document.getElementById('control_edit_des_'+id).innerText ; //para evitar que al Cancelar recargue el anterior
    document.getElementById('edit_des_'+id).style.display='none';  //oculto la edicion
    
    if(document.getElementById('txtDescription_'+id).value  =='')
    {
         document.getElementById('edit_des_'+id).style.display='block';  //obligo a que no sea null
         document.getElementById('control_edit_des_'+id).style.display='none';  
    }
    else
    {
         document.getElementById('edit_des_'+id).style.display='none';  
         document.getElementById('control_edit_des_'+id).style.display='block';  // muestro el nuevo
         
         document.getElementById('divDelLabel').style.display='block';
     
    }
    
    //Community_DescriptionUpd
    if(document.getElementById('txtDescription_'+id).value !='')
    {
        var control_edit_des = document.getElementById('control_edit_des_'+id); //label titulo
        var txtDesc = document.getElementById('txtDescription_'+id).value; // guardo el valor del input
        
        control_edit_des.innerHTML = document.getElementById('txtDescription_'+id).value  // seteo valor del input
        
        var url = '/Video/Video.ashx?DescriptionUpd&Format=json';												
            new Ajax.Request(url, {method: 'post', parameters: {Id: id, Description: txtDesc}, onSuccess: function(transport) 
                {
                        var rsp = transport.responseText.evalJSON().rsp;
			                if(rsp.stat == 'fail')
				                alert(rsp.err.msg);
                        }
                    }
                );
    }
}
        
function CancelarDes(id,title)
{
    document.getElementById('control_edit_des_'+id).style.display='block'; 
    document.getElementById('edit_des_'+id).style.display='none';     
    document.getElementById('control_edit_des_'+id).innerText =document.getElementById('control_edit_des_'+id).innerText;
             
    if(document.getElementById('txtDescription_'+id).value =='' || document.getElementById('txtDescription_'+id).value == 'Agregar descripcion ' )
    {
        document.getElementById('control_edit_des_'+id).innerHTML = 'Agregar descripcion';
    }
    
    document.getElementById('divDelLabel').style.display='block';
}

function publicAllComment(objectId)
{
    var countOfComment = document.getElementById('totalPendComment').getElementsByTagName('input');
    var commentIdCollection = '';

    for(x=0; x < countOfComment.length; x++)
    {
        //alert('gg');
        if(countOfComment.item(x).checked == true && countOfComment.item(x).style.display != 'none')
        {
            var collectionId = countOfComment.item(x).value;

            commentIdCollection = commentIdCollection + collectionId + ', ';
            publicMassiveComment(collectionId);
        }
    }	        

    commentIdCollection = commentIdCollection.substring(0,commentIdCollection.length-2);
    VideoApproval(commentIdCollection, objectId);
}
 
function rechazarAllComment(objectId)
{
    var countOfComment = document.getElementById('totalPendComment').getElementsByTagName('input');
    var commentIdCollection = '';
    
    for(x=0; x < countOfComment.length; x++)
    {
        //alert('gg');
        if(countOfComment.item(x).checked == true && countOfComment.item(x).style.display != 'none')
        {
            var collectionId = countOfComment.item(x).value;
            
            commentIdCollection = commentIdCollection + collectionId + ', ';
            
            rechazarMassiveComment(collectionId);
            
        }
    }       
    
    commentIdCollection = commentIdCollection.substring(0,commentIdCollection.length-2);
    VideoDesapproval(commentIdCollection, objectId);
}

function selectAllComment()
{
    var countOfComment = document.getElementById('totalPendComment').getElementsByTagName('input');
    
    for(x=0; x < countOfComment.length; x++)
    {
        countOfComment.item(x).checked = true;
    }
}
 
function unchekedAllComment()
 {
    var countOfComment = document.getElementById('totalPendComment').getElementsByTagName('input');
    
    for(x=0; x < countOfComment.length; x++)
    {
        countOfComment.item(x).checked = false;
    }
 }	     
 
function publicOneComment(commentToHidden, commentId, objectId)
{
    VideoApproval(commentId, objectId);
    
    var commentStatusInput = document.getElementById('commentStatus_' + commentId).getElementsByTagName('input').item(0);
    var countOfCommentToAprobal = parseInt(document.getElementById('countOfAprobalComment').value);
    var nuevaCantidad = document.getElementById('countOfAprobalComment').value = countOfCommentToAprobal - 1;
    
    document.getElementById('cantidadDeCommenPend').innerHTML = '(' + nuevaCantidad + ')';
    
    var commentStatus = document.getElementById('commentStatus_' + commentId);
    
    commentStatus.removeChild(commentStatusInput);
    document.getElementById('acctionPendComment_' + commentId).innerHTML = '<a class=\"btnPublicado\">Publicado</a>';
}

function publicMassiveComment(commentId)
{
    var commentStatusInput = document.getElementById('commentStatus_' + commentId).getElementsByTagName('input').item(0);
    var countOfCommentToAprobal = parseInt(document.getElementById('countOfAprobalComment').value);
    var nuevaCantidad = document.getElementById('countOfAprobalComment').value = countOfCommentToAprobal - 1;
    
    document.getElementById('cantidadDeCommenPend').innerHTML = '(' + nuevaCantidad + ')';
    
    var commentStatus = document.getElementById('commentStatus_' + commentId);
 
    commentStatusInput.style.display = 'none';
    document.getElementById('acctionPendComment_' + commentId).innerHTML = '<a class=\"btnPublicado\">Publicado</a>';    
}
 
function rechazarMassiveComment(commentId)
{
    var commentStatusInput = document.getElementById('commentStatus_' + commentId).getElementsByTagName('input').item(0);
    var countOfCommentToAprobal = parseInt(document.getElementById('countOfAprobalComment').value);
    var nuevaCantidad = document.getElementById('countOfAprobalComment').value = countOfCommentToAprobal - 1;
    
    document.getElementById('cantidadDeCommenPend').innerHTML = '(' + nuevaCantidad + ')';
    
    var commentStatus = document.getElementById('commentStatus_' + commentId);

    commentStatusInput.style.display = 'none';
    document.getElementById('acctionPendComment_' + commentId).innerHTML = '<a class=\"btnRechazado\">Rechazado</a>';
}	     	     
 
function rechazarOneComment(commentToHidden, commentId, objectId)
{
    VideoDesapproval(commentId, objectId);
    
    var commentStatusInput = document.getElementById('commentStatus_' + commentId).getElementsByTagName('input').item(0);
    var countOfCommentToAprobal = parseInt(document.getElementById('countOfAprobalComment').value);
    var nuevaCantidad = document.getElementById('countOfAprobalComment').value = countOfCommentToAprobal - 1;
    
    document.getElementById('cantidadDeCommenPend').innerHTML = '(' + nuevaCantidad + ')';
    
    var commentStatus = document.getElementById('commentStatus_' + commentId);
    
    commentStatus.removeChild(commentStatusInput);
    document.getElementById('acctionPendComment_' + commentId).innerHTML = '<a class=\"btnRechazado\">Rechazado</a>';
}     
 
function blockOneUser(commentToHidden, commentId, objectId)
{
    VideoBloqUser(commentId, objectId);
    
    var commentStatusInput = document.getElementById('commentStatus_' + commentId).getElementsByTagName('input').item(0);
    var countOfCommentToAprobal = parseInt(document.getElementById('countOfAprobalComment').value);
    var nuevaCantidad = document.getElementById('countOfAprobalComment').value = countOfCommentToAprobal - 1;
    
    document.getElementById('cantidadDeCommenPend').innerHTML = '(' + nuevaCantidad + ')';
    
    var commentStatus = document.getElementById('commentStatus_' + commentId);
    
    commentStatus.removeChild(commentStatusInput);
    document.getElementById('acctionPendComment_' + commentId).innerHTML = '<a class=\"btnBloqueado\">Bloqueado</a>';
}
	     
var stars = new Array();
var ObjectId ;

function ObjectVoteSel(ObjectId)
{
    document.getElementById('starsLoading').style.display = 'block';

    ObjectId = ObjectId;
    
    var url = '/Votes/Votes.ashx?videoVoteSel&Format=json';
    
    new Ajax.Request(url, {   parameters: { ObjectId: ObjectId},onSuccess: function(transport) {     
                                   var rsp = transport.responseText.evalJSON().rsp;
			                       
                                    if(rsp.stat == 'fail')
		                {
		                   alert('Ha ocurrido un error, por favor vuelva a intentar en unos minutos');
		                }
		                else
		                {
                            StarCount = parseInt(rsp.data.StarCount);
                            VotesCount = parseInt(rsp.data.VotesCount);

                            IsVoted = rsp.data.IsVoted;
                            IsLogin = rsp.data.IsLogin;
                            document.getElementById('RateItBox').style.display = 'block';
                            document.getElementById('starsLoading').style.display = 'none';
                            document.getElementById('belowStars').innerHTML = VotesCount + ' votos';
                            
                            if(IsLogin==1)
                            {
                                if(IsVoted == 1)
                                {

	                                document.getElementById('aboveStars').innerHTML = 'Puntaje:';

                                    for(var i = 1; i <= StarCount; i++)
                                    {
	                                    stars[i] = document.getElementById('star' + i);
	                                    stars[i].className = 'starHover';
	                                    stars[i].starNum = i;
                                    }
                                    
                                    for(var i = 1; i <= 5; i++)
                                    {
	                                    stars[i] = document.getElementById('star' + i);
	                                    stars[i].onclick = function(){isVoted()};
	                                    
                                    }		                                    
                                }
                                else
                                {
                                    document.getElementById('aboveStars').innerHTML = 'Puntaje:';
                                    document.getElementById('RateItBox').onmouseout = resetStars;
                                    for(var i = 1; i <= 5; i++)
                                    {
	                                    stars[i] = document.getElementById('star' + i);
	                                    stars[i].onmouseover = mouseOverStar;
	                                    stars[i].className = 'star';
	                                    stars[i].starNum = i;
	                                   
                                    }
                                    
                                    for(var i = 1; i <= StarCount; i++)
                                    {
	                                    stars[i].className = 'starHover';
	                                   
                                    }		                                    
                                    
                                }
                                
                            }
                            else
                            {       //mostrar el panelsito para que largue el login
                                    document.getElementById('aboveStars').innerHTML = 'Puntaje:';
                                    document.getElementById('RateItBox').onmouseout = resetStars;
                                    for(var i = 1; i <= 5; i++)
                                    {
	                                    stars[i] = document.getElementById('star' + i);
	                                    stars[i].className = 'star';
	                                    stars[i].onmouseover = mouseOverStar;
	                                    stars[i].onclick = function(){starClicklogoff()};
	                                    stars[i].starNum = i;
                                    }
                                    
                                    for(var i = 1; i <= StarCount; i++)
                                    {
	                                    stars[i].className = 'starHover';
	                                   
                                    }		                                    

                            }                     
		                }
                                } }); 
		
}

function mouseOverStar()
{
    for(var i = 1; i <= this.starNum; i++)
    {
        stars[i].className = 'starHover';
    }
    
    for(var i = this.starNum + 1; i <= 5; i++)
    {
        stars[i].className = 'star';
    }
}

function isVoted()
{
    document.getElementById('belowStars').innerHTML = ' Video ya votado!'; 
}


function resetStars()
{
    if(StarCount == 0)
    {
        for(var i = 1; i <= 5; i++)
        {
	        stars[i].className = 'star';
        }            
    }
    else
    {
        for(var i = 1; i <= 5; i++)
        {
	        stars[i].className = 'star';
        }             
        for(var i = 1; i <= StarCount; i++)
        {
	        stars[i].className = 'starHover';
        }            
    }
}

function starClicklogoff()
{
    //document.getElementById('VoteNotLogin').style.display = 'block';  // definir si lanzo shadown
    //alert('Necesita estar logueado para poder votar');
    var urlparam = window.location.href;
    var elem = document.createElement('a');
    var attr = document.createAttribute('rel');

    attr.value = 'shadowbox;width=330;height=340';
    elem.setAttributeNode(attr);

    var attr3 = document.createAttribute('href');
    
    attr3.value = '/layerLogin.aspx';
    elem.setAttributeNode(attr3);
    Shadowbox.open(elem);
}

function starClick(vote,ObjectId)
{
    var StarCount = vote;
    var url = '/Votes/Votes.ashx?videoVoteIns&Format=json';	
    											
	new Ajax.Request(url, {
	    method: 'post', 
	    parameters: {
	        ObjectId: ObjectId, 
	        StarCount: StarCount
	    },												
		onSuccess: 
		    function(transport) {     
			    var rsp = transport.responseText.evalJSON().rsp;
			    
				if(rsp.stat != 'fail')
				{
		            var nuevaCantidad = VotesCount + 1;
		            document.getElementById('belowStars').innerHTML = nuevaCantidad + ' votos';
		            document.getElementById('RateItBox').onmouseout = null;
    		        
		            for(var i = 1; i <= 5; i++)
                    {
                        stars[i] = document.getElementById('star' + i);
                        stars[i].onmouseover = null;
                        stars[i].onclick = null;
                    }
                    
		            for(var i = 1; i <= vote; i++)
                    {
                        stars[i] = document.getElementById('star' + i);
                        stars[i].className = 'starVoted';
                    }
				}
			}
        });
}