$(document).ready(function(){

  var return_url = function(question_or_ampersand) {
    question_or_ampersand = question_or_ampersand || '&';
    return question_or_ampersand+'r='+encodeURIComponent(window.location.pathname+window.location.search);
  };
	
	// Adding favorites
	$('a.add_to_favorites').live('click', function(e){
		e.preventDefault();
		
		if(wm_user == false)
		{
			var redirect = confirm('You are not currently logged in. Would you like to login in order to save this property?');
			
			if(redirect == true)
			{
				document.location = 'login.html?fav='+$(this).attr('rel')+return_url();
			}
		}
		else{
		
			var wm = $(this);
			
			wm.before('<img class="loading-gif" style="height: 16px; width:16px; border-width:0;" src="common/img/ajax-loader.gif" title="loading" alt="loading" />');
			
			$.getJSON('my-favorites.html?ajax=true&add='+wm.attr('rel'), function(data){
				if(data['response'] == 'success')
				{
					wm.prev('img.loading-gif').remove();
					wm.text('Remove From Favorites').addClass('remove_from_favorites').removeClass('add_to_favorites').attr('href', 'my-favorites.html?remove='+wm.attr('rel'));
					
					wm.before('<div class="growl success">This property was successfully saved as one of your favorites.</div>').fadeIn('slow').delay(3000).prev('div.growl').fadeOut('slow').remove();
				}
				else
				{
					wm.before('<div class="growl failure">Sorry, but there was an error contacting the server. Please try again later.</div>').fadeIn('slow').delay(3000).prev('div.growl').fadeOut('slow').remove();
				}
			});
		}
	});
	
	
	// Removing favorites
	$('a.remove_from_favorites').live('click', function(e){
		e.preventDefault();
		
		var redirect = confirm('Are you sure you would like to remove this property from your favorites?');
		
		if(redirect == true)
		{
			var wm = $(this);
			
			wm.before('<img class="loading-gif" style="height: 16px; width:16px; border-width:0;" src="common/img/ajax-loader.gif" title="loading" alt="loading" />');
			
			$.getJSON('my-favorites.html?ajax=true&remove='+wm.attr('rel'), function(data){
			if(data['response'] == 'success')
			{
				wm.prev('img.loading-gif').remove();
				wm.text('Add To Favorites').addClass('add_to_favorites').removeClass('remove_from_favorites').attr('href', 'my-favorites.html?add='+wm.attr('rel'));

        if(wm.hasClass('remove_from_dom'))
        {
          wm.closest('.propertyResult').remove();
        }
			}
		});
		}
	});



	// Toggling latest Search
	$('a.save_latest_search').live('click', function(e){
		e.preventDefault();
		
		if(wm_user == false)
		{
			var redirect = confirm('You are not currently logged in. Would you like to login in order to save this search?');
			
			if(redirect == true)
			{
				document.location = 'login.html?save=1&toggle=true'+return_url();
			}
		}
		else{
		
			var myPrompt = prompt('Please give this search a name.', '');
			
			if(myPrompt != null && myPrompt != '')
			{
				$.getJSON('saved-searches.html?ajax=true&type=toggle&name='+myPrompt, function(data){
					if(data['response'] == 'success')
					{
						alert(myPrompt+' was successfully saved.');
					}
					else
					{
						alert(data['response']);
					}
				});
			}
		}
	});
	
	// Deleting saved searches
	$('a.delete_saved_search').live('click', function(e){
		e.preventDefault();
		
		var redirect = confirm('Are you sure you would like to delete this custom search? This cannot be undone.');
		
		if(redirect == true)
		{
			window.location = $(this).attr('href');
		}
	});
});

