// Code for CSS Tab Menu
/*sfHover = function() {
	var sfEls = document.getElementById("tabs").getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);*/

function popitup(url, width, height) {
	var left = (screen.width/2)-(width/2);
	var top = (screen.height/2)-(height/2);
	
	newwindow=window.open(url,'Castlebeck','height='+height+',width='+width+',top='+top+',left='+left);
	if (window.focus) {newwindow.focus()}
	return false;
}

function togglePriceFields(elem)
{
	if(elem.value == 2) {
		// disable and hide the min price field
		document.getElementById('min').disabled=true;
		document.getElementById('min').style.display='none';
		// disable and hide the max price field
		document.getElementById('max').disabled=true;
		document.getElementById('max').style.display='none';
		
		// enable and show the min price pcm field
		document.getElementById('min_pcm').disabled=false;
		document.getElementById('min_pcm').style.display='inline';
		// enable and show the max price pcm field
		document.getElementById('max_pcm').disabled=false;
		document.getElementById('max_pcm').style.display='inline';
	} else {
		// disable and hide the min price pcm field
		document.getElementById('min_pcm').disabled=true;
		document.getElementById('min_pcm').style.display='none';
		// disable and hide the max price pcm field
		document.getElementById('max_pcm').disabled=true;
		document.getElementById('max_pcm').style.display='none';
		
		// enable and show the min price field
		document.getElementById('min').disabled=false;
		document.getElementById('min').style.display='inline';
		// enable and show the max price field
		document.getElementById('max').disabled=false;
		document.getElementById('max').style.display='inline';
	}
}

function changeClass(elem)
{
	(elem.className == "txtfld") ? elem.className= "current_txtfld" : elem.className = "txtfld";
}

$(document).ready(function(){ 

	$("a.colorbox").colorbox();
	
	$(".link_viewmap").colorbox({width:"95%", height:"95%", iframe:true});
	$(".link_viewmaplettings").colorbox({width:"95%", height:"95%", iframe:true});
	
	$(".vt").colorbox({width:"700px", height:"575px", iframe:true});
	
	/*
		Change the input field style on focus.  Revert back on blur
	------------------------------------------------------------------------------------------------------*/
	$('.txtfld').focus(function() {
		$(this).addClass("current_txtfld");
	});
	$('.txtfld').blur(function() {
		$(this).removeClass("current_txtfld");
	});
	
	
	/*
		Function to save a property to my white walls
	------------------------------------------------------------------------------------------------------*/
	$('.save_property').click(function(e) {
		var id = $(this).attr('id');
		$.ajax({  
			type: "POST",  
			url: "/ajax.php",  
			dataType: "text",
			data: ({
				'action':'save_property',
				'id': id
			}),  
			success: function(result)
				{ 
					if(result=='1') {
						$('#'+id).attr('src', '/i/btn_saved.png');
					} else {
						alert(result);
					}
				}  
		});
	});
	
	/*
		Function to remove a property from my white walls saved properties
	------------------------------------------------------------------------------------------------------*/
	$('.delete_saved_property').click(function(e) {
		e.preventDefault();
		var title = $(this).attr('title');
		if (confirm('Are you sure you want '+title+'?')) {
			var id = $(this).attr('name');
			$.ajax({  
				type: "POST",  
				url: "/ajax.php",  
				dataType: "text",
				data: ({
					'action':'delete_saved_property',
					'id': id
				}),  
				success: function(result)
					{ 
						if(result=='1') {
							$('#'+id).fadeOut(300, function() {
									$(this).remove();
								}
							);
						} else {
							alert(result);
						}
					}  
			});
		}
		return false;
	});
	
	/*
		Function to delete a search from my white walls saved searches
	------------------------------------------------------------------------------------------------------*/
	$('.delete_saved_search').click(function(e) {
		e.preventDefault();
		if (confirm('Are you sure you want delete this saved search?')) {
			var id = $(this).attr('name');
			$.ajax({  
				type: "POST",  
				url: "/ajax.php",  
				dataType: "text",
				data: ({
					'action':'delete_saved_search',
					'id': id
				}),  
				success: function(result)
					{ 
						if(result=='1') {
							$(e.target).closest('tr').fadeOut(300, function() {
								$(this).remove();
								}
							);
						} else {
							alert(result);
						}
					}  
			});
		}
		return false;
	});
	
	
	/*
		Disable alert preference fields when getting all alerts
	------------------------------------------------------------------------------------------------------*/
	$('#all_alerts_checkbox').click(function() {
		// see bottom of page
		toggleAlertFields(this);
	});
	
	
	
	/*
		Change the price options for property alerts when member change sale type
	------------------------------------------------------------------------------------------------------*/
	$("select#alert_sale_type_id").change(function(){
		var type = $(this).val();
		$.ajax({  
			type: "POST",  
			url: "/ajax.php",  
			dataType: "text",
			data: ({
				'action':'get_alert_price_options',
				'sale_type': type
			}),  
			success: function(result)
				{ 
					if(result!='0') {
						//var res = result.split('#');
						//$("select#min").html(res[0]);
						$("select#max").html(result);
					} else {
						alert('Sorry, but an error occured.');
					}
				}  
		});
	});
	
});

function toggleAlertFields(e)
	{
		if ($(e).is(':checked')) {
			//$("#txtAge").show();
			$('.alert_field').attr("disabled", "disabled");
		} else {
			//$("#txtAge").hide();
			$('.alert_field').removeAttr("disabled");
		}
	}
