$(function() {
  Rating();
});

var valueGlobal = 0;

function Rating()
{  
  $(".rating img").each(function() {
    if ($(this)[0].parentNode.getAttribute("disabled") == null || $(this)[0].parentNode.getAttribute("disabled") == false)
    {
      $(this).bind("mouseover",function () { $(this)[0].style.cursor = 'pointer'; } );
      $(this).bind("mouseover",function () { ShowOver($(this)[0].parentNode.id,$(this)[0].getAttribute("value")); } );
      $(this).bind("mouseout",function () { $(this)[0].style.cursor = 'default'; } );
      $(this).bind("mouseout",function () { ShowOut($(this)[0].parentNode.id); } );    
      $(this).bind("click",function () { SetImages($(this)[0]); } );  
    }
  });
  
  $(".rating span").each(function() {
    if ($(this)[0].parentNode.getAttribute("disabled") == null || $(this)[0].parentNode.getAttribute("disabled") == false)
    {
     $(this).bind("click",function () { DoRate($(this)[0]); } );  
    }
  });
  
}

function ShowOver(parentId,val)
{
  ClearRating(parentId);
  ShowRate(parentId,val); 
}

function ClearRating(parentId)
{
  for (i=1;i<=5;i++)
  {
    $("#"+parentId+" img")[i-1].src = '/images/icon_vote.gif';
  }  
}

function ShowOut(parentId)
{
  if ($("#"+parentId)[0].getAttribute("default") != null && $("#"+parentId)[0].getAttribute("default") != "")
  {
    ShowRate(parentId,parseInt($("#"+parentId)[0].getAttribute("default")));
  }
  else
  {
    ClearRating(parentId);
  }
}

function ShowRate(parentId,rate)
{
  for (i=1;i<=rate;i++)
  {
    $("#"+parentId+" img")[i-1].src = '/images/icon_vote_on.gif';
  }  
  for (i=rate+1;i<=5;i++)
  {
    $("#"+parentId+" img")[i-1].src = '/images/icon_vote.gif';    
  }    
}

function Unbind(parentId)
{
  $("#"+parentId+" img").each(function() {
    $(this).unbind("mouseover");
    $(this).unbind("mouseout");    
    $(this).unbind("click");  
  });    
}

function SetImages(obj)
{
  channelid = obj.parentNode.getAttribute("channelid");
  contentid = obj.parentNode.getAttribute("contentid");
  value = obj.getAttribute("value");
  Unbind(obj.parentNode.id); 
  
  valueGlobal = value;
     
  //$.ajax({url:"/AJAX/rating.aspx", 
    //type:"POST",
    //dataType:"text",
    //data:{request: "rate", channelid: channelid ,contentid: contentid, value: value},
    //success:function(result){	      
      //alert("Obrigado pelo seu voto");
    //},
    //error:function(req,error,ex){			        			    
    //}
  //});    
}

function DoRate(obj)
{
  channelid = obj.parentNode.getAttribute("channelid");
  contentid = obj.parentNode.getAttribute("contentid");
   var  indexRating = Checkrating(contentid);
   if(indexRating == true || indexRating == "true")
    {
        alert("O seu voto ja tinha sido contabilizado. Obrigado.");
        document.getElementById("divVotar").style.display = "none";
    }    
   else
   {
      value = valueGlobal;
      Unbind(obj.parentNode.id); 
      
      $.ajax({url:"/AJAX/rating.aspx", 
        type:"POST",
        dataType:"text",
        data:{request: "rate", channelid: channelid ,contentid: contentid, value: value},
        success:function(result){	      
          alert("Obrigado pelo seu voto.");
          document.getElementById("divVotar").style.display = "none";
        },
        error:function(req,error,ex){			        			    
        }
      });
  }
   
}

function Checkrating(contentid)
{
    var x = document.cookie;
    var temp = "";
    if(x.indexOf("votedCookieArticle") != -1)
    {
        if(x.indexOf(contentid) != -1)
        {
            temp = "true";
        }
        else
            temp = "false";
    }
    return temp;
}

