(function($){
/*
 *  favorites() provides AJAX capability for a pair of anchors.  You must supply a 
 * 
 *      bindAdd:               Class of anchor to add a favorite.
 *      bindRem:              Class of anchor to remove a favorite.
 *      ajaxImgClass:     Class to put on inserted ajax image.
 *      ajaxImgSrc:          Source of ajax image.
 * 
 *  @name favorites
 *  @type jQuery
 *  @param options ([bindAdd], [bindRem], [ajaxImgClass], [ajaxImgSrc])
 *  @cat Specialized
 *  @return jQuery
 */ 
    $.fn.favorites = function(options){
        // Extend our default options with those provided.
        // Note that the first arg to extend is an empty object -
        // this is to keep from overriding our "defaults" object.
        var o = $.extend({}, $.fn.favorites.defaults, options);
        return this.each(function(){
          var container = $(this);
          //Write in custom ajax image in same area
          /*var html = "<img class=\"" + o.ajaxImgClass + "\" src=\""+o.ajaxImgSrc+"\" style=\"display:none;\" />";
          container.append(html); */
          container.children("." + o.bindAdd).addClass("favorite").bind("click", {
              add: true
          }, function(event){
            favUpdate(o,event);
          });
          container.children("." + o.bindRem).addClass("favorite").bind("click", {
              add: false
          }, function(event){
            favUpdate(o,event);
          });
        });
    };
    //private function, hidden from other functions
    function favUpdate(o,ev){
        ev.preventDefault(); //prevents anchor from doing any default action
        var tar = $(ev.target);
        var modelnum = tar.attr("rel");
        tar.addClass("ajaxImg");
        /*var $ajImg = tar.siblings("." + o.ajaxImgClass);
        $ajImg.show();*/
        $.ajax({
            url: "/cfc/AJAX.cfc",
            data: {
                method: "updateFavorites",
                model: modelnum,
                add: ev.data.add,
                type: o.type
            },
            error: function(){
                //$ajImg.hide();
                tar.removeClass("ajaxImg");
                alert("There was an error processing your request.  Please try again.");
            },
            success: function(data){
              if(!ev.data.add && o.remove){
                tar.parent().remove();
              }
                tar.addClass("hide");
                tar.removeClass("ajaxImg");
                //$ajImg.hide();
                var $down = tar.siblings(".favorite");
                $down.removeClass("hide");
            }
        });
    }
    //plugin defaults
    $.fn.favorites.defaults = {
        bindAdd: "addFavorite",
        bindRem: "remFavorite",
        ajaxImgClass: "favLoading",
        ajaxImgSrc: "/images/ajax-loader.gif",
        type: "af",
        remove: false //Remove the favorites visual when removed from favorites
    };
    
})(jQuery);
