﻿     var stars = new Array();
     var ObjectId ;
     
        function ObjectVoteSel(ObjectId)
        {
        document.getElementById('starsLoading').style.display = 'block';

	        ObjectId = ObjectId;
            var url = '/Votes/Votes.ashx?photoVoteSel&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 = ' Foto ya votada!'; 
        }
        
        
        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?photoVoteIns&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';
				                                                    }				                                                    
															    }
															        

																			
                        } });

        }