$(function() {
	
	$('.outgoing').click(function() {
		var record = $(this).attr('rel');
		var url = $(this).attr('href');
		$.get('/listings/record_outgoing/' + record,
			null,
			function (data) { }
		);
	});
	$('.facebook-share').click(function() {
		var mls = $(this).attr('rel');
		var img_url = $('#img_' + mls).attr('src');
		var out_url = $('#outgoing_' + mls).attr('href');
		var caption = $('#info_' + mls).text().replace(/\n/g, '').replace(/  /g, '');
		var description = $('#remarks_' + mls).text();
		var attachment = {
			'name': caption,
			'href': out_url,
			'caption': 'MLS #' + mls,
			'description': description,
			'media': [ {
				'type': 'image',
				'src': img_url,
				'href': out_url
			} ]
		};
		var actionlink = [ {
			'text': 'Browse All Nashville MLS',
			'href': 'http://apps.facebook.com/nashville-homes/'
		} ];
		FB.Connect.streamPublish(
			'Check out this property I found! What do you think?',
			attachment,
			actionlink
		);
		return false;
	});

	$('.toggle').click(function() {
		$('#' + $(this).attr('rel')).toggle();
	});

	var captured_email = false;
	$('#UserSearchAddForm').submit(function() {
		if ($(this).find('input[name="data[UserSearch][search_name]"]').val() != '') {
				if ($('#has_email_permission').val() == '1') {
					// already have email
					return true;
				} else if (captured_email == false) {
					window.location.hash='top';
					FB.Connect.showPermissionDialog('email', function(data) {
						captured_email = true;
						$('#UserSearchAddForm').submit();
					});
					return false;
				} else {
					return true;
				}
		} else {
			// not saving
			return true;
		}
	});
	
	$('#UserSearchQuickSearch').focus(function() {
		if ($(this).val() == 'City, zip code, or MLS number') {
			$(this).val('');
			$(this).css('color', '#000000');
		}
	});
	$('#UserSearchQuickSearch').blur(function() {
		if ($(this).val() == '') {
			$(this).val('City, zip code, or MLS number');
			$(this).css('color', '#888');
		}
	});

});

