
var Listing = {

	lastLinkClicked: null,
	
	saveFav: function(ln, mlsid, link) {
		Ajax.post('/webservices/saveFavorite', { ln : ln, mlsid : mlsid }, Listing.saveFav_response, link);
	
	},

	saveFav_response: function(r) {
		if(r.failure || !r.json) {
			alert('There was a problem completing your request. Please try again later.');
			return;
		}
		
		if(r.json.save == 'failed') {
			$('login-form').style.display = '';
			Listing.lastLinkClicked = this;
			window.location.href = '#';
			return;
		}
		
		alert('This listing has been added to your favorites');
	}

}

var MapView = {
	saveFavorite: function(mlsnum) {
		Ajax.post('/webservices/saveFavorite', { ln : mlsnum }, MapView.saveFavorite_response);
	
	},
	
	saveFavorite_response: function(r) {
		
	}

}

var Search = {

	getName: function() {
		$('search-name').style.display = '';
		$('search-buttons').style.display = 'none';
		
	},

	save: function(sname) {
		if(sname == '') {
			alert('The search name can not be blank');
			return;
		}
		var query = window.location.search.substring(1);
		Ajax.post('/webservices/saveSearch?' + query + '&sname=' + sname, {}, Search.save_response);
		$('search-name').style.display = 'none';
		$('search-buttons').style.display = '';
	},
	
	save_response: function(r) {
		if(r.failure || !r.json) {
			alert('There was a problem completing your request. Please try again later.');
			return;
		}

		alert("Your search has been saved. \nTo view or modify your search go to \"My Searches\" under myAsset on the main menu.");
	}
}

var Favorite = {

	save: function(mlsnum, mls_id, img) {
		var url = '/webservices/saveFavorite';

		if(img.src.match(/_on\.gif$/) != null) {
			url = '/webservices/deleteFavorite';
			img.src = '/graphics/favorites_off.gif';
		} else {
			img.src = '/graphics/favorites_on.gif';
		}

		Ajax.post(url, { ln : mlsnum, mlsid : mls_id }, Favorite.save_response);
	},
	
	save_response: function(r) {
		if(r.failure || !r.json) {
			alert('There was a problem completing your request. Please try again later.');
			return;
		}

	}

}
