function updateLikes(id,l,d) {
	var like=Number(l);
	var dislike=Number(d);

	 var total=like+dislike;
	
	 if(total > 0) {
	 	$("[id^='p_like_"+id+"']").html(""+Math.round(like/total*100));
	 	 $("[id^='p_dislike_"+id+"']").html(""+Math.round(dislike/total*100)); 
	 	} 	
	 $("[id^='c_like_"+id+"']").html(""+like); 
	 $("[id^='c_dislike_"+id+"']").html(""+dislike); 
	 $("[id^='c_total_"+id+"']").html(""+total); 
	
}
$(document).ready(function() {	
	 
	 $("[id^='ci_like']").each(function(){
		 var id=$(this).attr('id').replace(/ci_([^_]*)_(.*)/,'$2');
	 		
			if($.cookie("cl_"+id)=="true") {
				$("div#feedbackstatus").html("<h3>Thanks for the feedback.</h3><p><label for='feedbackcomment'>Stay in touch with <a href=\"javascript:window.open('http://service.govdelivery.com/service/multi_subscribe.html?code=USHHSHC','Popup','width=780,height=440,toolbar=no,scrollbars=yes,resizable=yes'); void('');\" onClick=\"window.status='Sign Up'; return true\" onMouseOver=\"window.status='Sign Up'; return true\" onMouseOut=\"window.status=''; return true\">e-mail updates</a></label></p>")
				//$("div#feedbackstatus").html("<p>Thank you for your participation to make the site even better.</p>");
				//$(this).hide();
				//$("[id='ci_dislike_"+id+"']").hide();
			}
			
			
			/******** page rating effects ***********/
		$("[id^='ci_like']").each(function(){
			$(this).mouseover(function() {
				$(this).children("img").attr("src",rxs_navbase + "/images/sys_images/comment-yes-checked.gif");
			}).mouseout(function (){
				$(this).children("img").attr("src",rxs_navbase + "/images/sys_images/comment-yes-unchecked.gif");
			});
		});
		
		$("[id^='ci_dislike']").each(function(){
			$(this).mouseover(function() {
				$(this).children("img").attr("src",rxs_navbase + "/images/sys_images/comment-no-checked.gif");
			}).mouseout(function (){
				$(this).children("img").attr("src",rxs_navbase + "/images/sys_images/comment-no-unchecked.gif");
			});
		});
			

});

 $("[id^='ci_']").click(function () { 
		 var counter=$(this).attr('id').replace(/ci_([^_]*).*/,'$1');
		 var id=$(this).attr('id').replace(/ci_([^_]*)_(.*)/,'$2');
		// Work around for spanish translation site 		 
		var prefix = "";
		if (window.location.href.indexOf("www.cuidadodesalud.gov") > 0 ) {
			prefix = "/enes";
		}
		$.ajax({
  			url: prefix + "/delivery-counters/services/count/"+counter+"_"+id + "?var" + Math.floor(Math.random()*11),
		    complete: function(jqXHR, textStatus){
   			
   			var data = 1;
 			$.cookie("cl_"+id, "true")
			
			if (counter=="like") {
					like = 1;
					dislike = $("[id^='c_dislike_"+id+"']").html();
					
					$("div#feedbackstatus").html("<h3>Great!</h3><p><label for='feedbackcomment'>Could we make it even better?</label></p>")
			} else {
					like = $("[id^='c_like_"+id+"']").html();
					dislike=1;
					$("#evaluate_option").attr("value", "No");
					$("div#feedbackstatus").html("<h3>We're listening</h3><p><label for='feedbackcomment'>How can we make it better?</label></p>")
			}
			
			$("[id='ci_dislike_"+id+"']").hide();
			$("[id='ci_like_"+id+"']").hide();
			
			$("div#feedbackblock").slideDown("slow");				
			if (data > 0) {
				//$("div#feedbackblock").slideDown("slow");
				//updateLikes(id,like,dislike);				
			}
			return false;
 			
			},	
			type: "POST",
			mimeType: "application/json",
			dataType: "text json",
			error: function(jqXHR, textStatus, errorThrown){
				console.log(jqXHR);
			}
		})
		return false;
		});
				
	
 var options = { 
 				//url: "/perc-comments-services/comment",
		url: $('#feedback_form').attr("action"),
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse  // post-submit callback 
    }; 
 
    // bind to the form's submit event 
    $('#feedback_form').submit(function() {         
       	 //Get the current path name so we can pass this on to the comments service
		 var pathArray = window.location.pathname.split("/");
		 var basePath = "";
		 
		 for(i=0;i<pathArray.length-1;i++)
		 {
		 	basePath += "/";
		 	basePath += pathArray[i];
		 }
       	 
       	 $("#URL").val(window.location.href);
		 $("#pagepath").val(basePath);
       
        $(this).ajaxSubmit(options); 
  
  		$("div#feedbackstatus").html("<h3>Thanks for the feedback.</h3><p><label for='feedbackcomment'>Stay in touch with <a href=\"javascript:window.open('http://service.govdelivery.com/service/multi_subscribe.html?code=USHHSHC','Popup','width=780,height=440,toolbar=no,scrollbars=yes,resizable=yes'); void('');\" onClick=\"window.status='Sign Up'; return true\" onMouseOver=\"window.status='Sign Up'; return true\" onMouseOut=\"window.status=''; return true\">e-mail updates</a></label></p>")
		$("div#feedbackblock").hide();
        return false; 
    }); 
	
	
});

// pre-submit callback 
function showRequest(formData, jqForm, options) {     
    var queryString = $.param(formData); 

    return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText, xhr, $form)  {     
//		if(statusText == "success"){			
//			$("div#feedbackstatus").html("<h3>Thanks for the feedback.</h3><p><label for='feedbackcomment'>Stay in touch with <a href=\"javascript:window.open('http://service.govdelivery.com/service/multi_subscribe.html?code=USHHSHC','Popup','width=780,height=440,toolbar=no,scrollbars=yes,resizable=yes'); void('');\" onClick=\"window.status='Sign Up'; return true\" onMouseOver=\"window.status='Sign Up'; return true\" onMouseOut=\"window.status=''; return true\">e-mail updates</a></label></p>")
//			$("div#feedbackblock").hide();
//		}  
} 
