$(document).ready(function(){
    $(".jqMyActions_button_lg").mouseover(function () { $(this).attr("src", $(this).attr("src").replace("off", "on"))});
    $(".jqMyActions_button_lg").mouseout(function () { $(this).attr("src", $(this).attr("src").replace("on", "off"))});
    $(".jqMyActions_button_mini").mouseover(function () { $(this).attr("src", $(this).attr("src").replace("off", "on"))});
    $(".jqMyActions_button_mini").mouseout(function () { $(this).attr("src", $(this).attr("src").replace("on", "off"))});
        $("form.jqMyActions").submit(function () { 
         var currentForm = this;
         event_id = $(this).find("input[name='eventid']").val();
         form_action_uri = $(this).attr("action");
         my_action = $(this).find("input[name='action']").val();
         var actionIcon =  $(currentForm).find("input.myicon");
         actionIconSrc = $(actionIcon).attr("src").replace("on", "off");
         if(actionIconSrc.match("mini")) {
             $(actionIcon).attr("src", "/resources/images/ajax-loader.gif");
             
         }
         else {
             $(actionIcon).attr("src", "/resources/images/ajax-loader-lg.gif");
             
         }
         $.post(form_action_uri, { action: my_action, eventid:event_id, output: "json" }, function(data){
           switch(my_action)
           {
           case "add_recommend":
             $(currentForm).find("input[name='action']").val("remove_recommend");
             $(actionIcon).fadeOut("fast", function(){$(this).attr("src", actionIconSrc.replace("_unsaved", "_saved"))}).fadeIn();
             $("span.stats_recommend").fadeOut("fast", function(){$(this).text(parseInt($(this).text())+1)}).fadeIn();
             if($(currentForm).next().attr("class") == "seentext") {
                 numSeenOrRec = parseInt($(currentForm).next().text().replace("[", "").replace("]", ""))+1;
                 $(currentForm).next().fadeOut("fast", function(){$(this).text("["+numSeenOrRec+"]")}).fadeIn();
             }
             break;
           case "remove_recommend":
             $(currentForm).find("input[name='action']").val("add_recommend");
             $(actionIcon).fadeOut("fast", function(){$(this).attr("src", actionIconSrc.replace("_saved", "_unsaved"))}).fadeIn();
             $("span.stats_recommend").fadeOut("fast", function(){$(this).text(parseInt($(this).text())-1)}).fadeIn();
             if($(currentForm).next().attr("class") == "seentext") {
                 numSeenOrRec = parseInt($(currentForm).next().text().replace("[", "").replace("]", ""))-1;
                 $(currentForm).next().fadeOut("fast", function(){$(this).text("["+numSeenOrRec+"]")}).fadeIn();
             }
             break;
           case "add_mytab":
             $(currentForm).find("input[name='action']").val("remove_mytab");
             $(actionIcon).fadeOut("fast", function(){$(this).attr("src", actionIconSrc.replace("_unsaved", "_saved"))}).fadeIn();
             $("span.stats_wanttosee").fadeOut("fast", function(){$(this).text(parseInt($(this).text())+1)}).fadeIn();
             if($(currentForm).next().attr("class") == "seentext") {
                 numSeenOrRec = parseInt($(currentForm).next().text().replace("[", "").replace("]", ""))+1;
                 $(currentForm).next().fadeOut("fast", function(){$(this).text("["+numSeenOrRec+"]")}).fadeIn();
             }
             break;
           case "remove_mytab":
             $(currentForm).find("input[name='action']").val("add_mytab");
             $(actionIcon).fadeOut("fast", function(){$(this).attr("src", actionIconSrc.replace("_saved", "_unsaved"))}).fadeIn();
             $("span.stats_wanttosee").fadeOut("fast", function(){$(this).text(parseInt($(this).text())-1)}).fadeIn();
             if($(currentForm).next().attr("class") == "seentext") {
                 numSeenOrRec = parseInt($(currentForm).next().text().replace("[", "").replace("]", ""))-1;
                 $(currentForm).next().fadeOut("fast", function(){$(this).text("["+numSeenOrRec+"]")}).fadeIn();
             }
             break;
           default:
             return true;
             //in case of failure, bypass javascript and do the page-reload thing
           }
         } );
         
          return false;
        });
});