var Favorites = {
	
	init:function() {
		$('.add-favorite').each(function(i){
			// get reference to the actual div.add-favorite
			var fav_container = $(this);
			
			// assign click event handler on the link 
			$('.add-favorite a.favorite_this').click(function(e){
				// prevent default event (desactivate link)
				e.preventDefault();
				
				if (!confirm('Add this post to My Favorites List? \n\nYou can view your favorites on your profile page which can be accessed by clicking on your username anywhere it appears on the site.')) return false;
				
				// event is capturing so the actual target is the image
				$(e.target).html('<img src="/images/ajax-loader.gif" />').css('background','none').css('padding','0');
				// send ajax request to get the item favorited

				$.get($(e.target).attr('href'), 
						null, 
						function(data){
							// the string data is splited
							resp = data.split(':');
							if (resp[0]=='success') {
								// change the favorite link for the "favorite added" picture
								fav_container.html($('<a href="/style/members/profile/" class="meta_link favorited">favorited</a>'));
								//update the bookmark list in the sidebar
								$.get('/style/ajax/load_favorite_entry/'+fav_container.attr('entry_id'), 	
										null,
										function(data){
											// $('#favoritebox').append(data);
											// FavoritesBox.reInit();
											alert('This post has been added to your list of favorites. You can view and edit favorites on your profile page.');
										},
										'html');
							}
							else if (resp[0]=='error') {
								alert(resp[1]);
							}
						},
						'text'
					);
			});
		});
	}
}

$(document).ready(function () {
	Favorites.init();
});