$(document).ready(function(){ 
    //add code to check if the "#wp_logged_in" hidden input exists...
    /////
    /*
    $(".prod-favorite").live("hover", function() {
	console.log("hover");
	jQuery(".prod-favorite").tooltip();
    });*/
    $(".prod-favorite").live("click", function() {
	if($("#wp_logged_in").val() !== "logged_in") {
	    alert("Click Sign In above to begin adding favorites");
	}
	else {
	    the_post_id = $(this).attr("rel");
	    button = $(this);
	
	    if(!button.hasClass("favorited")) {
		$(button).addClass("favorited");
		$(button).attr("title", "Click to remove from favorites");
		
		jQuery.post(
		    // see tip #1 for how we declare global javascript variables
		    //MyAjax.ajaxurl,
		    "../../../wp-admin/admin-ajax.php",
		    {
			// here we declare the parameters to send along with the request
			// this means the following action hooks will be fired:
			// wp_ajax_nopriv_myajax-submit and wp_ajax_myajax-submit
			action : 'pf-post-favorited',
		 
			// other parameters can be added along with "action"
			post_id: the_post_id
		    },
		      function( response ) {
			//console.log(response);
			jQuery(".prod-favorite").tooltip();
		    }
		);
	    }
	    else {
		removeFavorite(the_post_id, button);
	    }
	}
    });
       
    $(".remove-favorite").live("click", function() {
	the_post_id = $(this).attr("rel");
	button = $(this);
	
	removeFavorite(the_post_id, button);
    });
});


function removeFavorite(post_id, button) {
    the_post_id = post_id;
    
    if($("#wp_logged_in").val() !== "logged_in") {
	    alert("Click Sign In above to begin adding favorites");
	}
	else {
	     $(button).removeClass("favorited");
	    $(button).attr("title", "Click to add to favorites");
    
	jQuery.post(
	    // see tip #1 for how we declare global javascript variables
	    //MyAjax.ajaxurl,
	    "../../../wp-admin/admin-ajax.php",
	    {
		// here we declare the parameters to send along with the request
		// this means the following action hooks will be fired:
		// wp_ajax_nopriv_myajax-submit and wp_ajax_myajax-submit
		action : 'pf-remove-favorite',
	 
		// other parameters can be added along with "action"
		post_id: the_post_id
	    },
	     function( response ) {
		//console.log(response);
		if(!$(button).hasClass("prod-favorite")) {
		    $(button).parent().hide();
		}
		else {	    
		    jQuery(".prod-favorite").tooltip();
		}
	    }
	);
	}
}
