jQuery.noConflict();
jQuery(document).ready(function() {
	jQuery("a#donate").bind("click", function() {
		jQuery("#donate_form").submit()
	});

	jQuery("a#single_1").fancybox();
		
	jQuery("a#single_2").fancybox({
		'zoomOpacity'			: true,
		'overlayShow'			: false,
		'zoomSpeedIn'			: 500,
		'zoomSpeedOut'			: 500
	});
	
	jQuery("a#single_3").fancybox({
		'overlayShow'			: false,
		'zoomSpeedIn'			: 600,
		'zoomSpeedOut'			: 500,
		'easingIn'				: 'easeOutBack',
		'easingOut'				: 'easeInBack'
	});
	
	jQuery("a.group").fancybox({
		'hideOnContentClick': false
	});

	jQuery("a.popup_emailfriend").fancybox({
		'hideOnContentClick': false,
		'frameHeight': 370,
		'frameWidth':  560
	});

	jQuery("a.popup_save").fancybox({
		'hideOnContentClick': false,
		'frameHeight': 250,
		'frameWidth':  400,
		'scrollable': false
	});

	jQuery('#top-s').val('Property ID Search').focus(function(){ jQuery(this).val(''); }).blur(function(){ if(jQuery(this).val() == '') jQuery(this).val('Property ID Search'); });

		
	jQuery(".menu ul").hover(
		function(){jQuery(this).parent().children('a:first').addClass('dropdown');},
		function(){jQuery(this).parent().children('a:first').removeClass('dropdown');}
	);

	jQuery(".rent", "#qs_region").css("display", "none");
	jQuery(".rent_price", "#qs_price").css("display", "none");

	jQuery("#slider").easySlider({
		controlsBefore:	'<p id="controls">',
		controlsAfter:	'</p>',
		auto: true, 
		continuous: true
	});

	jQuery("input#qs_st_buy").change(function(){
		jQuery(".buy", "#qs_region").css("display", "block");
		jQuery(".rent", "#qs_region").css("display", "none");
		action = jQuery("#quicksearch").attr("action");
		action = action.replace("renting/rental-listings.php", "buying/listings.php");
		jQuery("#quicksearch").attr("action", action);
		link = jQuery("#adv_search").attr("href");
		link = link.replace("renting/rental-search.php", "buying/search.php");
		jQuery("#adv_search").attr("href", link);
		jQuery(".rent_price", "#qs_price").css("display", "none");
		jQuery(".buy_price", "#qs_price").css("display", "block");
	});

	jQuery("input#qs_st_rent").change(function(){
		jQuery(".rent", "#qs_region").css("display", "block");
		jQuery(".buy", "#qs_region").css("display", "none");
		action = jQuery("#quicksearch").attr("action");
		action = action.replace("buying/listings.php", "renting/rental-listings.php");
		jQuery("#quicksearch").attr("action", action);
		link = jQuery("#adv_search").attr("href");
		link = link.replace("buying/search.php", "renting/rental-search.php");
		jQuery("#adv_search").attr("href", link);
		jQuery(".rent_price", "#qs_price").css("display", "block");
		jQuery(".buy_price", "#qs_price").css("display", "none");
	});
	
	jQuery("input[id='checkbox'][name^='regions']", "#regions").click(function() {	
		regions_filter();
	});

	/*
	jQuery("input[id='checkbox'][name^='catg']", "#propertytype").click(function() {
		if (jQuery(this).val() == 4 || jQuery(this).val() == 46) {
			jQuery("input[id='checkbox'][name^='catg'][value='46']", "#propertytype").attr('checked', jQuery(this).attr('checked'));
			jQuery("input[id='checkbox'][name^='catg'][value='4']", "#propertytype").attr('checked', jQuery(this).attr('checked'));
		}
	});

	jQuery("input[id='checkbox'][name^='catg_filter']", "#filter_type").click(function() {
		if (jQuery(this).val() == 4 || jQuery(this).val() == 46) {
			jQuery("input[id='checkbox'][name^='catg_filter'][value='46']", "#filter_type").attr('checked', jQuery(this).attr('checked'));
			jQuery("input[id='checkbox'][name^='catg_filter'][value='4']", "#filter_type").attr('checked', jQuery(this).attr('checked'));
		}
	});
	*/

	jQuery("input[id='checkbox'][name^='reg_filter']", "#filter_region").click(function() {	
		reg_filter_filter();
	});

	jQuery("input[id='checkbox'][name^='country']", "#country").click(function() {
		country_filter();
	});

	jQuery("select[id$='min_price']", "dd").click(function() {
		min_price = jQuery('option:selected', this).val();
		max_selected = 0;
		max_price = jQuery("select[id$='max_price'] option:selected", "dd").val();
		if(parseInt(max_price) < parseInt(min_price)) {
			max_selected = 1;
		}
		if(max_selected == 1) {
			jQuery("select[id$='max_price']", "dd").children('option:last').attr('selected', true);
		}
	});

	function regions_filter() {
		jQuery("div", "div#suburbs").css("display", "none");
		var show_all = 1;
		jQuery("input[id='checkbox'][name^='regions']", "#regions").each(function() {
			if(jQuery(this).val().match('sta_') == 'sta_') {
				reg_val = jQuery(this).val();
			} else {
				reg_val = "-reg_" + jQuery(this).val() + "-";
			}
			if (jQuery(this).attr('checked') === true && jQuery(this).parent('.multi').css("display") == "block") {
				jQuery("div[name*='"+reg_val+"']", "div#suburbs").css("display", "block");
				show_all = 0;
			}
		});
		
		if (show_all == 1) {
			//jQuery("div", "div#suburbs").css("display", "block");
			jQuery("input[id='checkbox'][name^='regions']", "#regions").each(function() {
				reg_val = "-reg_" + jQuery(this).val() + "-";
				if(jQuery(this).parent('div').css("display") == "block") {
					jQuery("div[name*='"+reg_val+"']", "div#suburbs").css("display", "block");
				}
			});
			//jQuery("input", "div#suburbs").attr("checked", false);
		}
	}

	function reg_filter_filter() {
		jQuery("div", "div#filter_suburb").css("display", "none");
		var show_all = 1;
		jQuery("input[id='checkbox'][name^='reg_filter']", "#filter_region").each(function() {
			if(jQuery(this).val() != '0') {
			
				if(jQuery(this).val().match('sta_') == 'sta_') {
					reg_val = jQuery(this).val();
				} else {
					reg_val = "-reg_" + jQuery(this).val() + "-";
				}
				if (jQuery(this).attr('checked') === true && jQuery(this).parent('.multi').css("display") == "block") {
					jQuery("div[name*='"+reg_val+"']", "div#filter_suburb").css("display", "block");
					show_all = 0;
				}
			}
		});
		
		if (show_all == 1) {
			//jQuery("div", "div#suburbs").css("display", "block");
			jQuery("input[id='checkbox'][name^='reg_filter']", "#filter_region").each(function() {
				if(jQuery(this).val() != '0') {
					if(jQuery(this).val().match('sta_') == 'sta_') {
						reg_val = jQuery(this).val();
					} else {
						reg_val = "-reg_" + jQuery(this).val() + "-";
					}
					if(jQuery(this).attr('checked') === true) {
						jQuery("div[name*='"+reg_val+"']", "div#filter_suburb").css("display", "block");
						show_all = 0;
					}
				}
			});
			if(show_all == 1) {
				jQuery("div#filter_suburb").children('div').css("display", "block");
			}
			//jQuery("input", "div#suburbs").attr("checked", false);
		}
	}

	function country_filter() {
		jQuery("div", "div#regions").css("display", "none");
		jQuery("div", "div#suburbs").css("display", "none"); //
		var show_all = 1;
		jQuery("input[id='checkbox'][name^='country']", "#country").each(function() {
			sta_id = jQuery(this).val();
			sta_val = "-cty_" + sta_id + "-";
			if (jQuery(this).attr('checked') === true) {
				jQuery("div[name*='"+sta_val+"']", "div#regions").css("display", "block");
				show_all = 0;

				jQuery("input", "div[name*='"+sta_val+"']", "div#regions").each(function() {
					reg_id = jQuery(this).val();
					reg_val = "-reg_" + reg_id + "-";
					//if (jQuery("input", "div[name*='"+sta_val+"']", "div#regions").attr('checked') === true) {
						jQuery("div[name*='"+reg_val+"']", "div#suburbs").css("display", "block");
					//}
				});
			}
		});
		
		if (show_all == 1) {
			jQuery("div", "div#regions").css("display", "block");
			//jQuery("input", "div#regions").attr("checked", false);
			//jQuery("div", "div#suburbs").css("display", "block");
			//jQuery("input", "div#suburbs").attr("checked", false);
		}
		regions_filter();
	}

	country_filter();
	regions_filter();

});
