
function setCookie(cookieName,cookieValue,expires,path,domain,secure) {
	document.cookie=
	escape(cookieName)+'='+escape(cookieValue)
	+(expires?'; EXPIRES='+expires.toGMTString():'')
	+(path?'; PATH='+path:'')
	+(domain?'; DOMAIN='+domain:'')
	+(secure?'; SECURE':'');
}

function getCookie(cookieName) {
	var cookieValue=null;
	var posName=document.cookie.indexOf(escape(cookieName)+'=');
	if (posName!=-1) {
		var posValue=posName+(escape(cookieName)+'=').length;
		var endPos=document.cookie.indexOf(';',posValue);
		if (endPos!=-1) cookieValue=unescape(document.cookie.substring(posValue,endPos));
		else cookieValue=unescape(document.cookie.substring(posValue));
	}
	return cookieValue;
} 

function testCookie(cookieName) {
	if (getCookie(cookieName) == 1){
		return 1;
	}
}

function loadRating(id){
	
	var type = $('#rate_type').attr('value'); 
	
	$.ajax({
        type: 'GET',
        url: '/rate/loadRate/'+ type + '/' +id, 
        success: function(resp) {                
            $('.stars').each(function() {
                var $list = $('<div></div>');
                // loop over every radio button in each container
                $(this).find('input:radio').each(function() {

                    var rating = $(this).parent().text();
                    var item = $('<a href="#"></a>').attr('title', rating);
                    
                    if(item.attr('title') <= resp) {
                    	// add class rating to a
                    	$('a[title="'+rating+'"]').addClass('rating');           
                    }
                });
            });
        }
	}); 
}


$(document).ready(function(){
	// @todo RAUL get & load current rating
	starRating.create('.stars');
});

// The widget
var starRating = {
  create: function(selector) {
    // loop over every element matching the selector
    $(selector).each(function() {
      var $list = $('<div></div>');
      // loop over every radio button in each container
      $(this)
        .find('input:radio')
        .each(function(i) {
          var rating = $(this).parent().text();
          var $item = $('<a href="#"></a>')
            .attr('title', rating)
            .addClass(i % 2 == 1 ? 'rating-right' : '')
            .text(rating);
          
          starRating.addHandlers($item);
          $list.append($item);
          
          if($(this).is(':checked')) {
            $item.prevAll().andSelf().addClass('rating');
          }
        });
        // Hide the original radio buttons
        $(this).append($list).find('label').hide();
    });
  },
  addHandlers: function(item) {
    $(item).click(function(e) {
    	
      if (! VISITED){ 	
	      // Handle Star click
	      var $star = $(this);
	      var $allLinks = $(this).parent();
	      
	      // Set the radio button value
	      $allLinks
	        .parent()
	        .find('input:radio[value=' + $star.text() + ']')
	        .attr('checked', true);
	        
	      // Set the ratings
	      $allLinks.children().removeClass('rating');
	      $star.prevAll().andSelf().addClass('rating');
	      //alert($star.html());
	      
	      var vote = $star.text();
	      var noticia_id = $('.stars').attr('id').split('noticia_');
          var noticia_id = noticia_id[1];
          
          var type = $('#rate_type').attr('value');          
                    
          $.ajax({
        	  type: 'GET',
        	  url: '/rate/rate/'+ type + '/'+noticia_id+'/'+vote,
                success: function(resp) {                		
                        // voto realizado
                        setConcreteCookie();
                        location.reload(true); 
                } 
          });

      // prevent default link click
      e.preventDefault();

      }
          
    }).hover(function() {
        if (! VISITED){ 
		      // Handle star mouse over
		      $(this).prevAll().andSelf().addClass('rating-over rating');
        }
    }, function() {
        if (! VISITED){ 
		      // Handle star mouse out
		      $(this).siblings().andSelf().removeClass('rating-over rating')
        }
    });    
  }
  
}
