

$(function(){
       /**
     * This call is triggered from the vehicle list page.
     * it gets the id number from the id attribute of the link
     * The calls the mylist_manager.php file to add the vehcile id to the session.
     * On success it updates the button text and adds a link to view the saved list
     */
       $(".add_to_my_list").live("click", function(){
           
      var  id = $(this).attr('ID').replace("my_list_action_", ""); // strip the text from the id so we just have a number
       $("#my_list_link").attr("href", "/my-list.html");
      // call the php file via ajax telling it we want to add the id number to the saved list
        $.get("mylist_manager.php", {action: "add", vehicle_id: id},
            function(count){ // once done
                if(count != "failed"){ // everything worked
                    // update the banner count
                    
                    $("#rightcar_my_list_banner_link").html(count+" items");
                   
                    // update the list item button
                    $("#my_list_action_"+id).val("Added");
                     
                 //   $("#my_list_action_"+id).html("Added"); // removed as it causes errors in ie8
            
       
                    $("#my_list_action_"+id).addClass("added");
                    $("#my_list_action_"+id).removeClass("add_to_my_list");
                    $("#my_list_action_"+id).addClass("remove_from_my_list");
                    $("#rightcar_my_list_link_"+id).remove(); // in case this is already there from the previous click, remove it
                    $("#my_list_action_"+id).after('<a href = "/my-list.html" id = "rightcar_my_list_link_'+id+'">See my List</a>');
                }else{
                    alert("Failed to add your vehicle");
                }
            }
        );
            return false;
    });
     /**
     * This call is triggered from the vehicle list page.
     * it gets the id number from the id attribute of the link
     * The calls the mylist_manager.php file to remove the vehcile id from the session.
     * On success it updates the button text and removes the link to view the saved list
     */
    $(".remove_from_my_list").live("click", function(){
      var  id = $(this).attr('ID').replace("my_list_action_", "");// strip the text from the id so we just have a number
      // call the php file via ajax telling it we want to remove the id number from the saved list
        $.get("mylist_manager.php", {action: "remove", vehicle_id: id},
            function(count){ // once done
                if(count != "failed"){
                    // update the banner count
                    $("#rightcar_my_list_banner_link").html(count+" items");
                    // update the list item button
                    $("#my_list_action_"+id).val("Add to my list");
                    //$("#my_list_action_"+id).html("Add to my list"); //// removed as it causes errors in ie8
                    $("#my_list_action_"+id).removeClass("added");
                    $("#my_list_action_"+id).removeClass("remove");
                    $("#my_list_action_"+id).removeClass("remove_from_my_list");
                    $("#my_list_action_"+id).addClass("add_to_my_list");
                    // remove the link under the button
                    $("#rightcar_my_list_link_"+id).remove();
                }else{
                    alert("Failed to remove your vehicle");
                }
            }
        );
            return false;
    });
});

