$(document).ready(function() {
	window['unique_js_ajax_count'] = 0;
	
	
	//Add to shortlist button on letting/buying Detail pages.
	$('.shortlist').click(function() {
		window['unique_js_ajax_count'] = window['unique_js_ajax_count'] + 1;
		window['p_id'] = $(this).attr('rel');
		$.post(
			'/shortlist-edit.php?rid=' + window['unique_js_ajax_count'], 
			{behave : "add", id : window['p_id'], type : $('#rent_buy').val() }, 
			function(data, textStatus, XMLHttpRequest) {
				$('#shortlisted').slideDown();
				var add_button = true;
				$('#buttons a.button').each(function() {
					if ($(this).attr('href').indexOf('/shortlist.php') != -1) {
						add_button = false;
					}
				});
				
				if (add_button == true) {
					$('#buttons').append("<a class='button new-shortlist-button' style='display:none' href='/shortlist.php'>My Property Shortlist</a><br><br><br>");
					$('#buttons .button.new-shortlist-button').fadeIn();
				}
			}
		);
		return false;
	});
	
	//Add to shortlist button on search result pages
	$('.add-to-shortlist').click(function() {
		window['unique_js_ajax_count'] 	= window['unique_js_ajax_count'] + 1;
		window['p_id'] 					= $(this).attr('rel');
		if ($(this).hasClass('rent')) {
			window['rent-buy'] 			= 'rent';
		} else {
			window['rent-buy'] 			= 'buy';
		}
		
		$.post(
			'/shortlist-edit.php?rid=' + window['unique_js_ajax_count'], 
			{behave : "add", id : window['p_id'], type : window['rent-buy'] }, 
			function(data, textStatus, XMLHttpRequest) {
				$('.add-to-shortlist').each(function() {
					if ($(this).attr('rel') == window['p_id']) {
						$(this).fadeOut(function() {
							$(this).html("Shortlisted").removeClass('.add-to-shortlist');
							$(this).fadeIn();
						});
					}
				});
				
				
				var add_button = true;
				$('#buttons a.button').each(function() {
					if ($(this).attr('href') == '/shortlist.php') {
						add_button = false;
					}
				});
				
				if (add_button == true) {
					$('#buttons').append("<a class='button new-shortlist-button' style='display:none' href='/shortlist.php'>My Property Shortlist</a><br><br><br>");
					$('#buttons .button.new-shortlist-button').fadeIn();
				}
			}
		);
		return false;
	});
	
	//remove shortlist link on detail pages
	$('.remove-shortlist').click(function() {
		window['unique_js_ajax_count'] = window['unique_js_ajax_count'] + 1;
		window['p_id'] = $(this).attr('rel');
		if ( $(this).hasClass('on-shortlisted-page') ) {
			window['on-shortlisted-page'] = 'yes';
			if ($(this).hasClass('rent')) {
				window['rent-buy'] = 'rent';
			} else {
				window['rent-buy'] = 'buy';
			}
		} else {
			window['on-shortlisted-page'] = 'no';
			window['rent-buy'] = $('#rent_buy').val();
		}
		
		$.post(
			'/shortlist-edit.php?rid=' + window['unique_js_ajax_count'], 
			{behave : "remove", id : window['p_id'], type : window['rent-buy'] }, 
			function(data, textStatus, XMLHttpRequest) {
				if (window['on-shortlisted-page'] != 'yes') {
					$('#shortlisted').slideUp();
				} else {
					$(this).parent().parent().slideUp();
				}
				
				var text = XMLHttpRequest.responseText;
				text = text.split(',');
				if (text[1] == '0') {
					$('#buttons a.button').each(function() {
						if ($(this).attr('href') == '/shortlist.php') {
							$(this).fadeOut(function() {
								$(this).next().remove();
								$(this).next().remove();
								$(this).next().remove();
								$(this).remove();
							});
						}
					});
				}
			}
		);
		return false;
	});

	//remove from shortlist button on shortlist.php
	$('.shortlist-remove-this').click(function() {
		window['unique_js_ajax_count'] = window['unique_js_ajax_count'] + 1;
		window['p_id'] = $(this).attr('rel');
		
		if ($(this).hasClass('rent')) {
			window['rent-buy'] = 'rent';
		} else {
			window['rent-buy'] = 'buy';
		}
		
		// NEED THESE VALUES TO BE SET BY HERE
		//
		// window['rent-buy']
		// window['p_id']
		
		$.post(
			'/shortlist-edit.php?rid=' + window['unique_js_ajax_count'], 
			{behave : "remove", id : window['p_id'], type : window['rent-buy'] }, 
			function(data, textStatus, XMLHttpRequest) {
				
				$('.shortlist-remove-this').each(function() {
					if ($(this).attr('rel') == window['p_id']) {
						$(this).parent().parent().slideUp();
					}
				});
			}
		);
		return false;
	});
	
	
});
