$(document).ready(function(){
    initEvents();
});

function initEvents(){

$('.good').unbind('click');
$('.good').click(function(){
    var id = $(this).attr('id');
    id = id.replace("good_", "");
		
    $.ajax({
      url: "/ajax/set_rate.php",
      type: "GET",
      cache: false,
      data: ({ID : id, good : 'Y'}),
      success: function(msg){
         if(msg == "Y"){
             eraseCookie("h"+id);
             createCookie("h"+id,"Y",500);

             $.ajax({
                 url: "/ajax/update_rate.php",
                 type: "GET",
                 cache: false,
                 data: ({ID : id}),
                 success: function(msg){
                     $('.toggle-text').html(msg);
                     initEvents();
                 }
             });
         }
      }
    });

    return false;
});

$('.bad').unbind('click');
$('.bad').click(function(){
    var id = $(this).attr('id');
    id = id.replace("bad_", "");

    $.ajax({
      url: "/ajax/set_rate.php",
      type: "GET",
      cache: false,
      data: ({ID : id, bad: 'Y'}),
      success: function(msg){
         if(msg == "Y"){
             eraseCookie("h"+id);
             createCookie("h"+id,"N",500);

             $.ajax({
                 url: "/ajax/update_rate.php",
                 type: "GET",
                 cache: false,
                 data: ({ID : id}),
                 success: function(msg){
                     $('.toggle-text').html(msg);
                     initEvents();
                 }
             });
         }
      }
    });

    return false;
});

}

function createCookie(name,value,days){
    if (days){
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function eraseCookie(name){
    createCookie(name,"",-1);
}

